Skip to main content

fastmcp.experimental.client.code_mode

Client-side CodeMode: discovery and sandboxed execution over remote tools. CodeModeClient wraps a Client or ClientGroup and presents the same discovery + execute meta-tools as the server-side CodeMode transform. The sandbox runs locally; each call_tool inside it is an ordinary tools/call on the wrapped client, so servers never see the generated code and need no CodeMode support of their own. This module is experimental and may change without deprecation.

Classes

CodeModeClient

Present a client’s tools through CodeMode discovery and execute tools. list_tools() returns only the meta-tools (by default search, get_schema, and execute). call_tool() runs them locally: discovery reads the wrapped client’s live tool list, and execute runs Python in a sandbox where call_tool(name, params) calls the wrapped client. Wrapping a ClientGroup lets one execute block chain tools from several servers, using the group’s namespaced tool names. Args:
  • client: The connected (or connectable) Client or ClientGroup whose tools the meta-tools expose.
  • sandbox_provider: Executes generated code. Defaults to MontySandboxProvider, which requires fastmcp[code-mode].
  • discovery_tools: Discovery tool factories, as for CodeMode. Defaults to Search and GetSchemas.
  • execute_tool_name: Name of the execute tool.
  • execute_description: Description of the execute tool.
  • max_tool_calls: Maximum call_tool invocations per execute; None removes the limit.
Methods:

client

The wrapped client or group.

list_tools

List the meta-tools in place of the wrapped client’s tools.

call_tool_mcp

Run a meta-tool and return its MCP result. As with Client.call_tool_mcp, a failing tool produces a result with is_error set rather than raising.

call_tool

Run a meta-tool and parse its result as Client.call_tool does. Raises:
  • ToolError: If the tool fails and raise_on_error is true.