loader
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
Classes¶
Functions¶
load_mcp_tools_from_config
¶
load_mcp_tools_from_config(mcp_cfg: Any, *, allowed_names: Optional[set[str]] = None) -> tuple[list['BaseTool'], list['MCPClient']]
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.
Source code in src/openjarvis/mcp/loader.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | |