MCPClient
MCPClient API Documentation
MCPClient API Reference
The MCPClient
class is the core component for managing connections to MCP servers and orchestrating tool access.
Constructor
MCPClient(config_dict, debug=False)
Creates a new MCPClient instance from a configuration dictionary.
Parameters:
config_dict
(dict): Configuration dictionary containing server definitionsdebug
(bool, optional): Enable debug logging. Defaults to False.
Example:
Class Methods
from_config_file(config_file_path, debug=False)
Creates an MCPClient instance from a JSON configuration file.
Parameters:
config_file_path
(str): Path to the JSON configuration filedebug
(bool, optional): Enable debug logging. Defaults to False.
Returns:
MCPClient
: Configured client instance
Example:
from_dict(config_dict, debug=False)
Creates an MCPClient instance from a configuration dictionary.
Parameters:
config_dict
(dict): Configuration dictionarydebug
(bool, optional): Enable debug logging. Defaults to False.
Returns:
MCPClient
: Configured client instance
Example:
Instance Methods
add_server(name, server_config)
Adds a new server configuration to the client.
Parameters:
name
(str): Name for the serverserver_config
(dict): Server configuration dictionary
Returns:
None
Example:
remove_server(name)
Removes a server configuration from the client.
Parameters:
name
(str): Name of the server to remove
Returns:
None
Example:
get_server_names()
Gets the names of all configured servers.
Returns:
List[str]
: List of server names
Example:
save_config(filepath)
Saves the current client configuration to a JSON file.
Parameters:
filepath
(str): Path where to save the configuration file
Returns:
None
Example:
async create_session(server_name, auto_initialize=True)
Creates a new session with the specified server.
Parameters:
server_name
(str): Name of the server to create a session withauto_initialize
(bool, optional): Whether to automatically initialize the session. Defaults to True.
Returns:
MCPSession
: The created session object
Raises:
ValueError
: If server is not configuredConnectionError
: If session creation fails
Example:
async create_all_sessions(auto_initialize=True)
Creates sessions with all configured servers.
Parameters:
auto_initialize
(bool, optional): Whether to automatically initialize sessions. Defaults to True.
Returns:
None
Example:
get_session(server_name)
Gets an existing session for the specified server.
Parameters:
server_name
(str): Name of the server
Returns:
MCPSession
: The session object
Raises:
ValueError
: If session does not exist
Example:
get_all_active_sessions()
Gets all currently active sessions.
Returns:
Dict[str, MCPSession]
: Dictionary mapping server names to session objects
Example:
async close_session(server_name)
Closes the session with the specified server.
Parameters:
server_name
(str): Name of the server whose session to close
Returns:
None
Example:
async close_all_sessions()
Closes all active sessions.
Returns:
None
Example:
Properties
sessions
Type: Dict[str, MCPSession]
Dictionary of active sessions by server name.
Example:
config
Type: Dict
The configuration dictionary used to initialize the client.
Example:
sandbox
Type: bool
Whether sandbox mode is enabled for server execution.
Example:
sandbox_options
Type: SandboxOptions | None
Configuration options for sandbox execution.
Example:
Configuration Format
The MCPClient expects a configuration dictionary with the following structure:
Configuration Fields
- command (required): The executable command to run the server
- args (optional): List of command line arguments
- env (optional): Environment variables for the server process
- cwd (optional): Working directory for the server process
Error Handling
The MCPClient can raise several types of exceptions:
ConnectionError
Raised when session creation or server connection fails.
ValueError
Raised when trying to access a non-existent session or server.
TimeoutError
Raised when session operations exceed timeout limits.
Context Manager Usage
MCPClient supports async context manager protocol for automatic session cleanup:
Best Practices
Session Management
Error Handling
Performance Optimization
Examples
Basic Usage
Multi-Server Configuration
See Also
- MCPAgent API Reference - High-level agent interface
- Configuration Guide - Detailed configuration options
- Connection Types - Understanding different server types