Skip to main content
Client for managing MCP servers and sessions. This module provides a high-level client that manages MCP servers, connectors, and sessions from configuration.

MCPClient

method init

Initialize a new MCP client.Parameters
str | dict[str, typing.Any] | None
default:"None"
Either a dict containing configuration or a path to a JSON config file.
list[str] | None
default:"None"
Server name or configuration
bool
default:"False"
Whether to use sandboxed execution mode for running MCP servers.
mcp_use.client.connectors.sandbox.SandboxOptions | None
default:"None"
Optional sandbox configuration options.
mcp.client.session.SamplingFnT | None
default:"None"
Optional sampling callback function.
mcp.client.session.ElicitationFnT | None
default:"None"
Callback function
mcp.client.session.MessageHandlerFnT | None
default:"None"
Parameter value
mcp.client.session.LoggingFnT | None
default:"None"
Callback function
list[mcp_use.client.middleware.middleware.Middleware] | None
default:"None"
Middleware instance
list[mcp.types.Root] | None
default:"None"
Optional list of Root objects to advertise to servers.
mcp.client.session.ListRootsFnT | None
default:"None"
Optional custom callback for roots/list requests.
bool
default:"False"
Whether to enable code execution mode for tools.
bool | None
default:"True"
Boolean flag
Signature

method add_middleware

Add a middleware.Parameters
mcp_use.client.middleware.middleware.Middleware
required
The middleware to add
Signature

method add_server

Add a server configuration.Parameters
str
required
The name to identify this server.
dict[str, Any]
required
The server configuration.
Signature

method close_all_sessions

Close all active sessions.This method ensures all sessions are closed even if some fail.Signature

method close_session

Close a session.Parameters
str
required
The name of the server to close the session for.
Signature

method create_all_sessions

Create sessions for all configured servers.Parameters
bool
default:"True"
Whether to automatically initialize the sessions.
Returns
dict[str, mcp_use.client.session.MCPSession]
Dictionary mapping server names to their MCPSession instances.
Signature

method create_session

Create a session for the specified server.Parameters
str
required
The name of the server to create a session for.
bool
default:"True"
Whether to automatically initialize the session.
Returns
mcp_use.client.session.MCPSession | None
The created MCPSession.
Signature

method execute_code

Execute Python code with access to MCP tools (code mode).This method allows agents to interact with MCP tools through Python code instead of direct tool calls, enabling more efficient context usage and data processing.Example:
Parameters
str
required
Python code to execute with tool access.
float
default:"30.0"
Execution timeout in seconds.
Returns
dict[str, Any]
Signature

method get_all_active_sessions

Get all active sessions.Returns
dict[str, mcp_use.client.session.MCPSession]
Dictionary mapping server names to their MCPSession instances.
Signature

method get_server_names

Get the list of configured server names.Returns
list[str]
List of server names (excludes internal code mode server).
Signature

method get_session

Get an existing session.Parameters
str
required
The name of the server to get the session for.
Returns
mcp_use.client.session.MCPSession
The MCPSession for the specified server.
Signature

method remove_server

Remove a server configuration.Parameters
str
required
The name of the server to remove.
Signature

method save_config

Save the current configuration to a file.Parameters
str
required
The path to save the configuration to.
Signature

method search_tools

Search available MCP tools across all active sessions.Example:
Parameters
str
default:""
Search query to filter tools by name or description.
str
default:"full"
Level of detail to return:
Returns
dict[str, Any]
Level of detail to return: - “names”: Only tool names and server - “descriptions”: Names, server, and descriptions - “full”: Complete tool information including schemas
Signature