Skip to content

loader

diapason.mcp.loader

Shared helper for loading MCP server tools from a TOML config blob.

Used by cli/ask.py, cli/serve.py, system/builder.py and server/agent_manager_routes.py so each call site doesn't reimplement the server-config → transport → client → discovered-tools pipeline.

The returned tuple of (tools, clients) is load-bearing: the caller MUST hold a reference to clients for as long as the tools are used, otherwise the MCP transport sessions get garbage-collected and the underlying HTTP connections close mid-execution (see #461 adversarial review). The recommended pattern is to stash the client list on the agent so they share its lifetime:

tools, mcp_clients = load_mcp_tools_from_config(config.tools.mcp)
agent = AgentCls(tools=tools, ...)
agent._mcp_clients = mcp_clients   # keep transports alive

load_mcp_tools_from_config(mcp_cfg, *, allowed_names=None)

Load tools from every server in mcp_cfg.servers.

Returns (tools, clients). clients is the list of live MCPClient instances — keep a reference or the transports get GC'd. Failures in any single server are logged and that server is skipped; the rest are returned as a best-effort batch.

allowed_names is an outer filter applied after each server's own include/exclude filter. Pass the caller's --tools/enabled list to honour CLI scoping; pass None to take every tool.

Returns ([], []) when mcp is disabled or no servers are configured — no exception, no warning.