View the source code for this module on GitHub: https://github.com/mcp-use/mcp-use/blob/main/libraries/python/mcp_use/client/client.py
MCPClient
method init
Initialize a new MCP client.ParametersSignatureEither a dict containing configuration or a path to a JSON config file.Server name or configurationWhether to use sandboxed execution mode for running MCP servers.Optional sandbox configuration options.Optional sampling callback function.Callback functionParameter valueCallback functionMiddleware instanceWhether to enable code execution mode for tools.Boolean flag
method close_all_sessions
Close all active sessions.This method ensures all sessions are closed even if some fail.Signaturemethod create_session
Create a session for the specified server.ParametersReturnsThe name of the server to create a session for.Whether to automatically initialize the session.
SignatureThe created MCPSession.
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:ReturnsPython code to execute with tool access.Execution timeout in seconds.
Signature
method search_tools
Search available MCP tools across all active sessions.Example:ReturnsSearch query to filter tools by name or description.Level of detail to return:
SignatureLevel of detail to return: - “names”: Only tool names and server - “descriptions”: Names, server, and descriptions - “full”: Complete tool information including schemas