Path: redb-route/redb.Route.Llm.Mcp/McpComponent.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/McpComponent.cs
----
using redb.Route.Abstractions;
using redb.Route.Core;
namespace redb.Route.Llm.Mcp;
///
/// MCP component. Scheme: mcp. Endpoint URI: mcp://serverName/toolName.
/// Producer-only — invokes tools/call on the named MCP server.
///
[EndpointRole(EndpointRole.Producer, Hint = "MCP servers are invoked through tools/call from the agent loop.")]
public sealed class McpComponent : ComponentBase
{
private IMcpRegistry? _registry;
///
/// Creates the component over the MCP client registry its context holds, looked up when the first endpoint is
/// created: the one module code gave the context (ctx.AddService(typeof(IMcpRegistry), registry), as a Tsak
/// module does in InitRoute), else the host's AddRedbRouteMcp() singleton. This is the constructor a
/// package's <component type="…McpComponent"/> and the catalog use.
///
public McpComponent()
{
}
/// Creates the component over the given MCP client registry.
/// Registry of MCP clients keyed by server name.
public McpComponent(IMcpRegistry registry)
{
ArgumentNullException.ThrowIfNull(registry);
_registry = registry;
}
///
public override string Scheme => "mcp";
/// Resolved registry — exposed to .
internal IMcpRegistry Registry => _registry ??= ResolveRegistry();
///
public override IEndpoint CreateEndpoint(EndpointUri uri)
{
ArgumentNullException.ThrowIfNull(uri);
_ = Registry; // a context without a registry fails here, when the route is built, not on the first call
var (server, tool) = SplitPath(uri.Path);
var options = new McpEndpointOptions { ServerName = server, ToolName = tool };
options.BindFromUri(uri.RawParameters);
// Path values win over query params for the canonical fields.
options.ServerName = server;
options.ToolName = tool;
options.Validate();
return new McpEndpoint(uri, this, options);
}
/// The registry the context holds: the one module code added, else the host's DI singleton.
private IMcpRegistry ResolveRegistry()
=> Context?.GetService()
?? Context?.GetServiceProvider()?.GetService(typeof(IMcpRegistry)) as IMcpRegistry
?? throw new InvalidOperationException(
"mcp:// needs the registry of the MCP servers it calls (IMcpRegistry). In a hosted app call " +
"services.AddRedbRouteMcp() and services.AddMcpServer(name, transport). In a Tsak module build an " +
"McpRegistry in InitRoute from the module's configuration, initialize and register its clients, and " +
"give it to the context with ctx.AddService(typeof(IMcpRegistry), registry).");
private static (string Server, string Tool) SplitPath(string path)
{
if (string.IsNullOrEmpty(path))
throw new ArgumentException("mcp:// URI requires a path of the form 'serverName/toolName'.", nameof(path));
var slash = path.IndexOf('/');
if (slash <= 0 || slash == path.Length - 1)
throw new ArgumentException(
$"Invalid mcp:// URI path '{path}'. Expected 'serverName/toolName'.", nameof(path));
return (path[..slash], path[(slash + 1)..]);
}
}