MCP
Connection Registry
Manage one or more MCP server connections.
Anvia does not keep a global MCP registry. Your app should own connection storage and lifecycle.
App-Owned Registry
import type { McpServer } from "@anvia/core";
class McpRegistry {
private readonly servers = new Map<string, McpServer>();
add(server: McpServer) {
this.servers.set(server.name, server);
}
get(name: string): McpServer | undefined {
return this.servers.get(name);
}
values(): McpServer[] {
return [...this.servers.values()];
}
async closeAll() {
await Promise.all([...this.servers.values()].map((server) => server.close()));
this.servers.clear();
}
}Use a registry when multiple agents need the same MCP connections.
Startup Shape
const registry = new McpRegistry();
registry.add(
await connectMcp(
mcp.stdio({
name: "filesystem",
command: "npx",
args: ["-y", "@modelcontextprotocol/server-filesystem", "/tmp"],
}),
),
);
registry.add(
await connectMcp(
mcp.http({
name: "docs",
url: "https://mcp.example.com/mcp",
}),
),
);Use Registered Servers
const agent = new AgentBuilder("support", model)
.mcp(registry.values())
.defaultMaxTurns(4)
.build();If different agents need different MCP tools, filter the registry values before registering them.
const agent = new AgentBuilder("docs-agent", model)
.mcp([registry.get("docs")].filter((server): server is McpServer => server !== undefined))
.build();Shutdown
process.on("SIGTERM", async () => {
await registry.closeAll();
});Keep connection lifecycle explicit so reconnects, shutdown, and resource ownership are easy to reason about.
