trading_agent.extensions.mcp_client_manager
¶
Connects to the MCP servers in mcp_servers.json and keeps the connections alive for every desk.
Each server gets one connection, shared by all desks. A connection runs in its own task for the life of the gateway: it starts the server (or opens its HTTP endpoint), lists its tools, and then waits. If the server dies or cannot be reached, the connection waits 30 seconds, then 1 minute, then 5 minutes between attempts, like the model router, while the other servers carry on.
The desks do not get one tool per MCP tool. They get tool_search, which describes matching MCP tools on demand, and call_mcp_tool, which runs one. That keeps dozens of MCP tools out of every prompt, which matters for Gemma's and Qwen's 32,768-token window, and sends every call through trading-agent's own tool executor, journal and loop guard.
Typical usage example:
manager = McpClientManager(configurations) task = asyncio.create_task(manager.run()) result = await manager.call_tool('everything', 'echo', {'message': 'hi'}, 60)
McpToolError
¶
Bases: RuntimeError
An MCP tool could not be called: the server is unknown, not connected, or the call failed.
McpTool
dataclass
¶
One tool offered by an MCP server.
Attributes:
| Name | Type | Description |
|---|---|---|
server |
str
|
The server's name. |
name |
str
|
The tool's name on that server. |
description |
str
|
What the tool does, as the server describes it. |
input_schema |
dict[str, Any]
|
The JSON schema of its arguments. |
McpCallResult
dataclass
¶
What one MCP tool call returned.
Attributes:
| Name | Type | Description |
|---|---|---|
text |
str
|
The text content, joined. |
is_error |
bool
|
Whether the server reported the call as failed. |
image_png |
bytes | None
|
The first PNG image the tool returned, or None. |
McpServerConnection
¶
The connection to one MCP server.
Attributes:
| Name | Type | Description |
|---|---|---|
configuration |
The server's settings. |
|
state |
"connecting", "ready" or "failed". |
|
error |
Why the last attempt failed, or an empty string. |
|
tools |
list[McpTool]
|
The server's tools, known once it is ready. |
Creates the connection without connecting.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
configuration
|
McpServerConfiguration
|
The server's settings. |
required |
Source code in src/trading_agent/extensions/mcp_client_manager.py
run
async
¶
Connects, serves until the connection breaks, and reconnects, until cancelled.
Source code in src/trading_agent/extensions/mcp_client_manager.py
call_tool
async
¶
Calls one of the server's tools.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool_name
|
str
|
The tool. |
required |
arguments
|
dict[str, Any]
|
Its arguments. |
required |
timeout_seconds
|
float
|
How long to wait for the answer. |
required |
Returns:
| Type | Description |
|---|---|
McpCallResult
|
What the tool returned. |
Raises:
| Type | Description |
|---|---|
McpToolError
|
The server is not ready, or the call failed; a broken connection is also marked for reconnection. |
Source code in src/trading_agent/extensions/mcp_client_manager.py
error_suffix
¶
The last error, for messages.
Returns:
| Type | Description |
|---|---|
str
|
": |
McpClientManager
¶
All MCP connections of the gateway.
Creates one connection per server, without connecting.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
configurations
|
Sequence[McpServerConfiguration]
|
The servers. |
required |
Source code in src/trading_agent/extensions/mcp_client_manager.py
run
async
¶
Runs every connection until cancelled.
Source code in src/trading_agent/extensions/mcp_client_manager.py
tools
¶
Every tool of every ready server.
Returns:
| Type | Description |
|---|---|
list[McpTool]
|
The tools, server by server. |
Source code in src/trading_agent/extensions/mcp_client_manager.py
find_tool
¶
Finds one tool of a ready server.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
server
|
str
|
The server's name. |
required |
tool_name
|
str
|
The tool's name. |
required |
Returns:
| Type | Description |
|---|---|
McpTool | None
|
The tool, or None when it is not available. |
Source code in src/trading_agent/extensions/mcp_client_manager.py
call_tool
async
¶
Calls one tool on one server.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
server
|
str
|
The server's name. |
required |
tool_name
|
str
|
The tool's name. |
required |
arguments
|
dict[str, Any]
|
Its arguments. |
required |
timeout_seconds
|
float
|
How long to wait. |
required |
Returns:
| Type | Description |
|---|---|
McpCallResult
|
What the tool returned. |
Raises:
| Type | Description |
|---|---|
McpToolError
|
The server or tool is unknown or unavailable, or the call failed. |
Source code in src/trading_agent/extensions/mcp_client_manager.py
index_text
¶
The section of the system prompt naming each server's tools.
Returns:
| Type | Description |
|---|---|
str
|
The section, or an empty string when no server is configured. |
Source code in src/trading_agent/extensions/mcp_client_manager.py
status_lines
¶
One line per server, for the command line.
Returns:
| Type | Description |
|---|---|
list[str]
|
Each server's state and tool count. |
Source code in src/trading_agent/extensions/mcp_client_manager.py
settled
¶
Says whether every server has finished its first attempt to connect.
Returns:
| Type | Description |
|---|---|
bool
|
True when no server is still connecting. |