Complete mcp_use API Documentation
MCPClient
is the core class for interacting with MCP servers. It handles connection management, session creation, and communication with MCP servers.
Parameter | Type | Required | Description |
---|---|---|---|
config_path | str | Yes | Path to the JSON configuration file |
Parameter | Type | Required | Description |
---|---|---|---|
config | dict | Yes | Dictionary containing MCP server configuration |
from_config_file
and from_dict
methods support the options
parameter for configuring client features, including sandboxed execution:
SandboxOptions
type supports the following options:
Option | Type | Required | Default | Description |
---|---|---|---|---|
api_key | str | Yes | None | E2B API key. Required - can be provided directly or via E2B_API_KEY environment variable |
sandbox_template_id | str | No | ”base” | Template ID for the sandbox environment |
supergateway_command | str | No | ”npx -y supergateway” | Command to run supergateway |
Parameter | Type | Required | Default | Description |
---|---|---|---|---|
server_name | str | Yes | - | Name of the server as defined in config |
timeout | float | No | 30.0 | Connection timeout in seconds |
retry_count | int | No | 3 | Number of connection retry attempts |
timeout
for servers that take more time to initializeretry_count
in unstable network environmentsserver_name
when working with multiple servers in the same configParameter | Type | Required | Description |
---|---|---|---|
session_id | str | Yes | ID of the session to close |
Parameter | Type | Required | Description |
---|---|---|---|
name | str | Yes | Name of the server as defined in config |
MCPAgent
class combines an LLM with an MCPClient to create an intelligent agent capable of using MCP tools.
Parameter | Type | Required | Default | Description |
---|---|---|---|---|
llm | BaseLanguageModel | Yes | - | Any LangChain-compatible language model |
client | MCPClient | No | None | The MCPClient instance |
connectors | list[BaseConnector] | No | None | List of connectors if not using client |
server_name | str | No | None | Name of the server to use |
max_steps | int | No | 5 | Maximum number of steps the agent can take |
auto_initialize | bool | No | False | Whether to initialize automatically |
memory_enabled | bool | No | True | Whether to enable memory |
system_prompt | str | No | None | Custom system prompt |
system_prompt_template | str | No | None | Custom system prompt template |
additional_instructions | str | No | None | Additional instructions for the agent |
session_options | dict | No | Additional options for session creation | |
output_parser | OutputParser | No | None | Custom output parser for LLM responses |
use_server_manager | bool | No | False | If True , enables automatic selection of the appropriate server based on the chosen tool when multiple servers are configured via MCPClient . |
disallowed_tools | list[str] | No | None | List of tool names that should not be available to the agent |
True
when using an MCPClient
configured with multiple servers to enable efficient, automatic server selection per tool call. This can reduce agent confusion and minimize unnecessary server connections.False
(default) if using a single server or if you prefer to manually specify the target server using the server_name
parameter in agent.run()
or rely on the agent to handle tool availability across all connected servers.set_disallowed_tools()
Parameter | Type | Required | Default | Description |
---|---|---|---|---|
query | str | Yes | - | The query to run |
max_steps | int | No | None | Overrides the instance max_steps |
stop_on_first_result | bool | No | False | Whether to stop at first result |
server_name | str | No | None | Specific server to use |
callbacks | list | No | None | Callback functions for events |
Parameter | Type | Required | Description |
---|---|---|---|
disallowed_tools | list[str] | Yes | List of tool names that should not be available |
Field | Type | Required | Description |
---|---|---|---|
command | string | Yes | The command to start the MCP server |
args | array | No | Arguments to pass to the command |
env | object | No | Environment variables for the server |
timeout | number | No | Connection timeout in seconds |
retry | object | No | Retry configuration |
retry.max_attempts | number | No | Maximum retry attempts |
retry.backoff_factor | number | No | Backoff multiplier between retries |
Exception | Description | When It Occurs |
---|---|---|
MCPConnectionError | Connection to MCP server failed | Network issues, server not running |
MCPAuthenticationError | Authentication with server failed | Invalid credentials or tokens |
MCPTimeoutError | Operation timed out | Server takes too long to respond |
MCPServerError | Server returned an error | Internal server error |
MCPClientError | Client-side error | Invalid configuration or parameters |
MCPError | Generic MCP-related error | Any other MCP-related issue |
Exception | Description | When It Occurs |
---|---|---|
MCPConnectionError | Connection to MCP server failed | Network issues, server not running |
MCPAuthenticationError | Authentication with server failed | Invalid credentials or tokens |
MCPTimeoutError | Operation timed out | Server takes too long to respond |
MCPServerError | Server returned an error | Internal server error |
MCPClientError | Client-side error | Invalid configuration or parameters |
MCPError | Generic MCP-related error | Any other MCP-related issue |