MCPClient

View as MarkdownOpen in Claude

MCPClient manages a single MCP server subprocess. It handles starting the process, initializing the MCP session via JSON-RPC, calling tools, and stopping the process with cleanup.

MCPClient instances are created internally by MCPManager.create_client(). You should not instantiate them directly in most cases.

1from signalwire.mcp_gateway import MCPClient

Properties

process
subprocess.Popen

The underlying subprocess object. None before start() is called.

tools
list[dict[str, Any]]

List of tool definitions retrieved from the MCP server after initialization.


start

start() -> bool

Spawn the MCP server process, initialize the MCP session, and retrieve the list of available tools. Returns True on success, False if initialization fails. The process runs in a sandboxed environment according to the service’s sandbox_config.

Returns

boolTrue if the process started and initialized successfully.


stop

stop() -> None

Stop the MCP server process. Attempts a graceful JSON-RPC shutdown first, then falls back to SIGTERM, and finally SIGKILL if needed. Cleans up the sandbox directory after the process stops.


call_tool

call_tool(tool_name, arguments) -> dict[str, Any]

Call a tool on the MCP server and return the result.

Parameters

tool_name
strRequired

Name of the tool to invoke.

arguments
dict[str, Any]Required

Arguments to pass to the tool.

Returns

dict[str, Any] — The tool result from the MCP server.


call_method

call_method(method, params) -> Any

Send a JSON-RPC request to the MCP server and wait for the response. Raises TimeoutError after 30 seconds if no response is received. Raises RuntimeError if the client is shutting down.

Parameters

method
strRequired

The JSON-RPC method name (e.g., "tools/call", "tools/list").

params
dict[str, Any]Required

Parameters for the JSON-RPC request.

Returns

Any — The result field from the JSON-RPC response.


get_tools

get_tools() -> list[dict[str, Any]]

Return a copy of the tool definitions retrieved during initialization.

Returns

list[dict[str, Any]]


Example

1from signalwire.mcp_gateway import MCPManager
2
3config = {
4 "services": {
5 "todo": {"command": ["python3", "todo_mcp.py"], "description": "Todo list", "enabled": True}
6 }
7}
8
9manager = MCPManager(config)
10client = manager.create_client("todo")
11tools = client.get_tools()
12print(f"Tools: {[t['name'] for t in tools]}")
13result = client.call_tool("add_todo", {"text": "Buy groceries"})
14print(result)
15client.stop()
16manager.shutdown()