Path: redb-route/tests/redb.Route.Tests.Llm.Mcp/TestHelpers/McpAgentHost.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/tests/redb.Route.Tests.Llm.Mcp/TestHelpers/McpAgentHost.cs ---- using Microsoft.Extensions.DependencyInjection; using redb.Route.Llm.Engine.Governance; using redb.Route.Llm.Engine.Observability; using redb.Route.Llm.Mcp.Protocol; namespace redb.Route.Tests.Llm.Mcp.TestHelpers; /// /// Minimal LLM-host wiring for MCP integration tests. Mirrors the /// LiveLlmHost pattern from redb.Route.Tests.Llm but adds the /// + so the agent can /// dispatch mcp:// URIs. /// /// Tests inject pre-discovered instances (typically /// from ) and supply the matching /// list — descriptors are projected into /// the same way would. /// /// public sealed class McpAgentHost : IAsyncDisposable { /// The shared route context. public RouteContext Context { get; } /// The producer template — used to invoke direct: entrypoints. public IProducerTemplate ProducerTemplate { get; } /// The agent engine wired with the producer template + observer. public IAgentEngine Engine { get; } /// The tool registry into which MCP descriptors were projected. public IToolDescriptorRegistry ToolRegistry { get; } /// The MCP client registry the producer dispatches against. public IMcpRegistry McpRegistry { get; } private McpAgentHost( RouteContext ctx, IProducerTemplate pt, IAgentEngine engine, IToolDescriptorRegistry toolRegistry, IMcpRegistry mcpRegistry) { Context = ctx; ProducerTemplate = pt; Engine = engine; ToolRegistry = toolRegistry; McpRegistry = mcpRegistry; } /// Builds a host with the LLM + MCP components wired and the supplied client(s) registered. /// Logical MCP server name (matches the mcp://<serverName>/... URI). /// Pre-initialized MCP client (e.g. from ). /// Tool catalogue to project as instances. /// Optional agent observer — defaults to . public static McpAgentHost Build( string serverName, IMcpClient client, IReadOnlyList tools, IAgentObserver? observer = null) { RouteContext ctx = null!; var services = new ServiceCollection(); services.AddSingleton(_ => ctx); var sp = services.BuildServiceProvider(); ctx = new RouteContext(sp, contextId: $"mcp-test-{serverName}"); ctx.AddComponent(new LlmComponent()); var mcpRegistry = new McpRegistry(); mcpRegistry.Register(client); ctx.AddComponent(new McpComponent(mcpRegistry)); var toolRegistry = new ToolDescriptorRegistry(); foreach (var tool in tools) { if (string.IsNullOrWhiteSpace(tool.Name)) continue; var modelName = McpToolDescriptor.BuildModelFacingName(serverName, tool.Name); var capability = new LlmToolCapability { Name = modelName, Description = tool.Description ?? $"MCP tool '{tool.Name}' from server '{serverName}'.", InputSchema = McpToolDescriptor.BuildInputSchema(tool), Safety = new LlmToolSafety(), }; toolRegistry.Register(new McpToolDescriptor(serverName, tool.Name, capability)); } ctx.AddService(typeof(IToolDescriptorRegistry), toolRegistry); var pt = new ProducerTemplate(ctx); ctx.AddService(typeof(IProducerTemplate), pt); var engine = new AgentEngine( logger: null, producerTemplate: pt, observer: observer ?? new NoopAgentObserver(), budget: new NoopBudgetEnforcer(), approval: new AutoApproveGate(), redaction: new NoopRedactionFilter(), shadow: new NoopShadowRunner(), conversation: null, idempotency: null, approvalStore: null); ctx.AddService(typeof(IAgentEngine), engine); return new McpAgentHost(ctx, pt, engine, toolRegistry, mcpRegistry); } /// Registers a connection factory for use in routes. public McpAgentHost AddFactory(string name, LlmConnectionFactory factory) { factory.Name = name; Context.AddToRegistry(name, factory); return this; } /// Declares routes inline and starts the context + producer template. public async Task StartAsync(Action configure) { Context.AddRoutes(configure); await Context.Start().ConfigureAwait(false); ((ProducerTemplate)ProducerTemplate).Start(); return this; } /// Sends one message into and returns the resulting exchange. public async Task SendAsync(string endpointUri, string body, CancellationToken ct = default) { var endpoint = Context.GetEndpoint(endpointUri); var producer = endpoint.CreateProducer(); await producer.Start(ct).ConfigureAwait(false); var msg = new Message(body); var ex = Exchange.Create(msg, endpoint.ScopeFactory); await producer.Process(ex, ct).ConfigureAwait(false); return ex; } /// public async ValueTask DisposeAsync() { await Context.DisposeAsync().ConfigureAwait(false); if (ProducerTemplate is IDisposable d) d.Dispose(); } }