ractogateway.mcp.client
RactoMCPClient — connect to an MCP server and consume its tools.
Requires the mcp package:
pip install ractogateway[mcp]
Usage (async context manager — recommended for long-lived connections):
from ractogateway.mcp import RactoMCPClient, MCPClientConfig
config = MCPClientConfig(
transport="stdio",
command="python",
args=["-m", "my_package.server"],
)
async with RactoMCPClient(config) as client:
tools = await client.list_tools()
result = await client.call_tool("add", {"a": 1, "b": 2})
registry = await client.to_registry() # use with any kit
Usage (sync, one-shot — for scripts / REPLs):
client = RactoMCPClient(config)
tools = client.list_tools_sync()
result = client.call_tool_sync("add", {"a": 1, "b": 2})
Note
The sync *_sync() helpers use asyncio.run() and cannot be
called from within a running event loop (e.g. inside an async def or
a Jupyter notebook with %autoawait). Use the async context manager
interface in those environments.
- class ractogateway.mcp.client.RactoMCPClient(config)[source]
Bases:
objectConnect to an MCP server and consume its tools as
ToolSchemaobjects.This is an async context manager. Keep it alive to reuse the underlying connection for multiple tool calls (O(1) per call after connection setup). For single calls from synchronous code, use the
*_sync()convenience methods.- Parameters:
config (
MCPClientConfig) – Connection configuration (transport, command / URL, env, …).(recommended) (Example — async)
------------------------------
:: –
- config = MCPClientConfig(transport=”stdio”, command=”python”,
args=[“-m”, “my_server”])
- async with RactoMCPClient(config) as client:
# Reuse this connection for all calls. tools = await client.list_tools() result = await client.call_tool(“search”, {“query”: “AI”}) registry = await client.to_registry()
one-shot (Example — sync)
-----------------------
:: – client = RactoMCPClient(config) tools = client.list_tools_sync()
- async list_tools()[source]
List all tools exposed by the MCP server.
- Return type:
- Returns:
list[ToolSchema] – Provider-agnostic tool schemas — ready to be registered in any
ToolRegistryor passed directly to a developer kit viaChatConfig(tools=…).- Raises:
RuntimeError – If called outside an
async withblock.
- async call_tool(name, arguments=None)[source]
Call a remote MCP tool.
- Parameters:
- Return type:
- Returns:
MCPToolResult –
contentcontains all text blocks joined by"\n".is_errorisTruewhen the server signals a tool error.- Raises:
RuntimeError – If called outside an
async withblock.
- async to_registry()[source]
Return a
ToolRegistrypopulated with all server tools.Each callable in the registry makes a fresh one-shot MCP connection when invoked. This keeps the returned registry self-contained and usable outside an
async withblock.For high-throughput usage, hold the
RactoMCPClientcontext manager alive and callcall_tool()directly.- Return type:
- Returns:
ToolRegistry – Registry compatible with all three developer kits via
ChatConfig(tools=registry).
- list_tools_sync()[source]
Synchronous wrapper: connect, list tools, disconnect.
- Return type:
- Returns:
list[ToolSchema] – All tool schemas exposed by the server.
- Raises:
RuntimeError – If called from within a running event loop.
- call_tool_sync(name, arguments=None)[source]
Synchronous wrapper: connect, call tool, disconnect.
- Parameters:
- Return type:
- Returns:
MCPToolResult – Tool output.
- Raises:
RuntimeError – If called from within a running event loop.