Path: redb-route/redb.Route.Llm.Mcp/Extensions/McpServiceCollectionExtensions.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/Extensions/McpServiceCollectionExtensions.cs ---- using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; using redb.Route.Extensions; using redb.Route.Llm.Abstractions.Tools; using redb.Route.Llm.Mcp.Transport; namespace redb.Route.Llm.Mcp; /// /// DI registration helpers for redb.Route.Llm.Mcp. /// public static class McpServiceCollectionExtensions { /// /// Registers the MCP component (mcp:// scheme), the client registry, /// and the discovery hosted service. Call for each /// MCP server you want spawned at host startup. /// /// /// /// services.AddRedbRoute(...); /// services.AddRedbRouteLlm(...); /// services.AddRedbRouteMcp(); /// services.AddMcpServer("serena", McpTransport.Stdio("uvx", /// ["--from", "git+https://github.com/oraios/serena", "serena", "start-mcp-server", /// "--context", "ide", "--project", "C:/path/to/project"])); /// /// public static IServiceCollection AddRedbRouteMcp(this IServiceCollection services) { ArgumentNullException.ThrowIfNull(services); services.TryAddSingleton(); // IRouteContextConfigurator is applied by RouteHostedService at startup -- // the correct registration hook (a lazy marker singleton never fires). services.AddRouteComponent(); // Tool descriptor registry is owned by redb.Route.Llm; don't add a duplicate here. // If the consumer hasn't called AddRedbRouteLlm() the discovery service will // fail-fast at construction time which is the right diagnostic. services.AddHostedService(); return services; } /// /// Registers a single MCP server entry. The discovery hosted service spawns /// the server at host startup, runs initialize + tools/list, and /// projects the discovered tools into . /// /// DI container. /// Logical server name — must be unique and matches the registry key. /// Transport configuration (use or ). /// Optional callback to set DiscoveryTimeout, RestartPolicy, or SafetyOverrides. public static IServiceCollection AddMcpServer( this IServiceCollection services, string name, McpTransport transport, Action? configure = null) { ArgumentNullException.ThrowIfNull(services); ArgumentException.ThrowIfNullOrWhiteSpace(name); ArgumentNullException.ThrowIfNull(transport); var builder = new McpServerOptionsBuilder { Name = name, Transport = transport }; configure?.Invoke(builder); var options = builder.Build(); services.AddSingleton(options); return services; } } /// Mutable builder for . public sealed class McpServerOptionsBuilder { /// Logical server name. public required string Name { get; set; } /// Transport configuration. public required McpTransport Transport { get; set; } /// Discovery timeout (default 30s). public TimeSpan DiscoveryTimeout { get; set; } = TimeSpan.FromSeconds(30); /// Restart policy (default 3 attempts at 1/3/10s). public McpRestartPolicy RestartPolicy { get; set; } = McpRestartPolicy.Default; /// Per-tool safety overrides. public List SafetyOverrides { get; set; } = []; /// /// Default safety attached when no override matches: external side effects and an approval /// requirement, because discovery cannot know what a third-party tool does. /// /// With the shipped this flag produces an audit row /// (ApprovalId = "auto"), not a blocked call — replace the gate to make approval a control /// point. /// /// public LlmToolSafety DefaultSafety { get; set; } = new() { SideEffect = ToolSideEffect.External, Cost = ToolCostClass.Cheap, RequiresApproval = true, }; internal McpServerOptions Build() { // A caching policy on a tool that is not read-only is refused here, at configuration time. The // tool registry refuses it as well, but that happens during discovery — where the failure reads // as "the server is silently missing from the tool set" instead of "this override is wrong". ValidateSafety("DefaultSafety", DefaultSafety); foreach (var ovr in SafetyOverrides) ValidateSafety($"safety override '{ovr.ToolNamePattern}'", ovr.Safety); return new McpServerOptions { Name = Name, Transport = Transport, DiscoveryTimeout = DiscoveryTimeout, RestartPolicy = RestartPolicy, SafetyOverrides = SafetyOverrides, DefaultSafety = DefaultSafety, }; } private static void ValidateSafety(string what, LlmToolSafety safety) { if (safety.Caching != ToolCachingPolicy.None && safety.SideEffect != ToolSideEffect.ReadOnly) throw new InvalidOperationException( $"MCP {what} declares Caching={safety.Caching} with SideEffect={safety.SideEffect}. " + "Only read-only tools may be cached — a cache hit would suppress the side effect."); } }