Skip to content

trading_agent.tools.call_mcp_tool_tool

The call_mcp_tool tool: runs one tool on one MCP server.

Typical usage example:

tool = CallMcpToolTool(manager)

CallMcpToolArguments

Bases: BaseModel

The arguments of call_mcp_tool.

Attributes:

Name Type Description
server str

The MCP server's name, as tool_search gives it.

tool str

The tool's name on that server.

arguments dict[str, Any]

The tool's arguments, matching the schema tool_search gave.

CallMcpToolTool

CallMcpToolTool(manager)

Bases: Tool

Runs an MCP tool and returns its output.

Creates the tool.

Parameters:

Name Type Description Default
manager McpClientManager

The gateway's MCP connections.

required
Source code in src/trading_agent/tools/call_mcp_tool_tool.py
def __init__(self, manager: mcp_client_manager.McpClientManager):
    """Creates the tool.

    Args:
        manager (mcp_client_manager.McpClientManager): The gateway's MCP connections.
    """
    self._manager = manager

run async

run(arguments)

Calls the tool.

Parameters:

Name Type Description Default
arguments CallMcpToolArguments

The server, the tool and its arguments.

required

Returns:

Type Description
ToolResult

The tool's output, marked as an error when the server reported one or the call failed.

Source code in src/trading_agent/tools/call_mcp_tool_tool.py
async def run(self, arguments: CallMcpToolArguments) -> tool.ToolResult:
    """Calls the tool.

    Args:
        arguments (CallMcpToolArguments): The server, the tool and its arguments.

    Returns:
        tool.ToolResult: The tool's output, marked as an error when the server reported one or the call failed.
    """
    try:
        result = await self._manager.call_tool(
            arguments.server,
            arguments.tool,
            arguments.arguments,
            _CALL_TIMEOUT_SECONDS,
        )
    except mcp_client_manager.McpToolError as error:
        return tool.ToolResult.error(str(error))
    text = result.text or '(the tool returned no text)'
    image = None
    if result.image_png is not None:
        image = messages.MessageImage('image/png', result.image_png)
    return tool.ToolResult(
        text=untrusted_text.UntrustedText().wrap(
            f'the MCP tool {arguments.server}/{arguments.tool}',
            text,
        ),
        is_error=result.is_error,
        image=image,
    )