Path: redb-route/redb.Route.Llm.Mcp/McpServerOptions.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route.Llm.Mcp/McpServerOptions.cs
----
using System.Text.RegularExpressions;
using redb.Route.Llm.Abstractions.Tools;
using redb.Route.Llm.Mcp.Transport;
namespace redb.Route.Llm.Mcp;
///
/// Restart policy for an MCP stdio server.
///
public sealed class McpRestartPolicy
{
/// How many auto-restart attempts before giving up. Default 3.
public int MaxAttempts { get; init; } = 3;
/// Backoff schedule applied between restarts. Default: 1s, 3s, 10s.
public IReadOnlyList Backoff { get; init; } =
[
TimeSpan.FromSeconds(1),
TimeSpan.FromSeconds(3),
TimeSpan.FromSeconds(10),
];
/// Default policy: 3 attempts at 1s, 3s, 10s.
public static McpRestartPolicy Default { get; } = new();
/// Disable auto-restart entirely.
public static McpRestartPolicy None { get; } = new() { MaxAttempts = 0, Backoff = [] };
}
///
/// Per-tool safety override. The first matching wins.
///
public sealed class McpSafetyOverride
{
private string _toolNamePattern = string.Empty;
private Regex? _compiled;
///
/// Regex matched against the raw server-side tool name — the name the MCP server reports,
/// not the model-facing {server}__{tool} form the descriptor exposes
/// ( sanitises the server name to 24 chars and
/// the tool name to 36, replacing invalid characters with _). A pattern copied from the
/// model-facing name therefore matches nothing; the discovery service warns about overrides that
/// matched no discovered tool.
/// Example: for server serena and tool find_symbol the pattern is
/// ^find_symbol$, not ^serena__find_symbol$.
/// The pattern is compiled when the override is built, so a typo fails at configuration time
/// instead of surfacing later as "the server is silently missing from the tool set".
///
public required string ToolNamePattern
{
get => _toolNamePattern;
init
{
ArgumentException.ThrowIfNullOrWhiteSpace(value);
try
{
_compiled = new Regex(value, RegexOptions.Compiled);
}
catch (ArgumentException ex)
{
throw new ArgumentException(
$"MCP safety override pattern '{value}' is not a valid regex: {ex.Message}",
nameof(ToolNamePattern), ex);
}
_toolNamePattern = value;
}
}
/// Replacement to attach to the descriptor.
public required LlmToolSafety Safety { get; init; }
/// Whether this override accepts (a raw server-side name).
internal bool Matches(string toolName) => (_compiled ??= new Regex(ToolNamePattern, RegexOptions.Compiled)).IsMatch(toolName);
}
///
/// Configuration for a single MCP server entry. Built via
/// .
///
public sealed class McpServerOptions
{
/// Logical server name — used as the registry key and embedded into descriptor names.
public required string Name { get; init; }
/// Transport configuration (stdio or HTTP+SSE).
public required McpTransport Transport { get; init; }
/// Maximum time to wait for initialize + tools/list. Default 30s.
public TimeSpan DiscoveryTimeout { get; init; } = TimeSpan.FromSeconds(30);
/// Auto-restart policy for stdio transports.
public McpRestartPolicy RestartPolicy { get; init; } = McpRestartPolicy.Default;
/// Per-tool safety overrides (first match wins).
public IReadOnlyList SafetyOverrides { get; init; } = [];
///
/// Default safety attached when no override matches. Discovery cannot know what a third-party tool
/// does, so it assumes the worst: external side effects and an approval requirement.
///
/// Note what approval means with the shipped gate: approves
/// automatically, so this flag buys an audit row (ApprovalId = "auto" in
/// ApprovalProps), not a control point. A deployment that wants the call blocked until a
/// human answers must replace the gate — services.Replace(ServiceDescriptor.Singleton<IApprovalGate, DenyAllGate>())
/// or a custom .
///
///
public LlmToolSafety DefaultSafety { get; init; } = new()
{
SideEffect = ToolSideEffect.External,
Cost = ToolCostClass.Cheap,
RequiresApproval = true,
};
}