Configuration
Configure your mcp_use environment
Configuration Guide
This guide covers all the configuration options available in mcp_use.
API Keys
Make sure to have the api key relative to the provider of your choice available in the environment you can either:
1 - Create .env
file with your keys as:
and load it in python using
this will make all the keys defibned in .env
available in yout python runtime, granted that you run from where the .env is located.
2 - Set it in your environment by running in your terminal the following command, e.g. for openai:
and then import it in your python code as:
or any other method you might prefer.
MCP Server Configuration
mcp_use supports any MCP server through a flexible configuration system. (For a list of awesome servers you can visit https://github.com/punkpeye/awesome-mcp-servers or https://github.com/appcypher/awesome-mcp-servers which have an amazing collection of them)
The configuration is defined in a JSON file with the following structure:
MCP servers can use different connection types (STDIO, HTTP). For details on these connection types and how to configure them, see the Connection Types guide.
Each server entry in the mcpServers
object has a server_name
and then specific options depending on how mcp-use
should connect to and/or manage the server.
server_name
: (Required) A unique string identifier for this MCP server configuration. This name is used to select the server, for example, inagent.run(..., server_name="your_server_name")
.
For STDIO-based servers (local):
These are servers that mcp-use
will start and manage as local child processes, communicating via their standard input/output streams.
command
: (Required) The executable command to start the server (e.g.,"npx"
,"python"
).args
: (Optional) An array of string arguments to pass to thecommand
(e.g.,["-y", "@playwright/mcp@latest"]
).env
: (Optional) An object defining environment variables to set for the server’s process (e.g.,{"DISPLAY": ":1"}
).
For HTTP/HTTPS-based servers (SSE and Streamable HTTP)
These are servers that are typically already running and accessible via an HTTP(S) endpoint. mcp-use
acts as an HTTP client to communicate with them.
url
: (Required) The full URL where the MCP server is listening (e.g.,"http://localhost:7777/mcp"
,"https://api.example.com/mcp"
).headers
: (Optional) An object containing custom HTTP headers to be sent with every request to this server (e.g., for authentication:{"Authorization": "Bearer your_api_token"}
).
Additional options might be available depending on the specific connection type or wrappers used. Always refer to the Connection Types documentation for the most detailed and up-to-date specifications for each type.
Example Configuration
Here’s a basic example of how to configure an MCP server:
Multiple Server Configuration
You can configure multiple MCP servers in a single configuration file, allowing you to use different servers for different tasks or combine their capabilities (e.g.):
For a complete example of using multiple servers, see the multi-server example in our repository.
Sandboxed Execution
mcp_use supports running MCP servers in a sandboxed cloud environment using E2B. This is useful when you want to run MCP servers without having to install their dependencies locally.
Installation
To use sandboxed execution, you need to install the E2B dependency:
You’ll also need an E2B API key. You can sign up at e2b.dev to get your API key.
Configuration Example
To enable sandboxed execution, use the sandbox parameter when creating the MCPClient:
Available Sandbox Options
The SandboxOptions
type provides the following configuration options:
Option | Description | Default |
---|---|---|
api_key | E2B API key. Required - can be provided directly or via E2B_API_KEY environment variable | None |
sandbox_template_id | Template ID for the sandbox environment | ”base” |
supergateway_command | Command to run supergateway | ”npx -y supergateway” |
E2B API Key
To use sandboxed execution, you need an E2B API key. You can provide it in two ways:
-
Directly in the sandbox options:
-
Through the environment variable:
For more details on connection types and sandbox configuration, see the Connection Types guide.
Agent Configuration
When creating an MCPAgent, you can configure several parameters:
Available Parameters
llm
: Any LangChain-compatible language model (required)client
: The MCPClient instance (optional if connectors are provided)connectors
: List of connectors if not using client (optional)server_name
: Name of the server to use (optional)max_steps
: Maximum number of steps the agent can take (default: 5)auto_initialize
: Whether to initialize automatically (default: False)memory_enabled
: Whether to enable memory (default: True)system_prompt
: Custom system prompt (optional)system_prompt_template
: Custom system prompt template (optional)additional_instructions
: Additional instructions for the agent (optional)disallowed_tools
: List of tool names that should not be available to the agent (optional)
Tool Access Control
You can restrict which tools are available to the agent for security or to limit its capabilities:
This feature is useful for:
- Restricting access to sensitive operations
- Limiting agent capabilities for specific tasks
- Preventing the agent from using potentially dangerous tools
- Focusing the agent on specific functionality
Error Handling
mcp_use provides several ways to handle errors:
- Connection Errors: Check your MCP server configuration and ensure the server is running
- Authentication Errors: Verify your API keys are correctly set in the environment
- Timeout Errors: Adjust the
max_steps
parameter if operations are timing out
Best Practices
- Always use environment variables for sensitive information
- Keep configuration files in version control (without sensitive data)
- Use appropriate timeouts for different types of operations
- Enable verbose logging during development
- Test configurations in a development environment before production