Agent Configuration
This guide covers MCPAgent configuration options for customizing agent behavior and LLM integration. For client configuration, see the Client Configuration guide.
API Keys
Never hardcode API keys in your source code. Always use environment variables for security.
Create a Load it in Python:
.env
file in your project root:.env
This method keeps your keys organized and makes them available to your Python runtime.
Agent Parameters
When creating an MCPAgent, you can configure several parameters to customize its behavior: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)use_server_manager
: Enable dynamic server selection (default: False)
Tool Access Control
You can restrict which tools are available to the agent for security or to limit its capabilities. Here’s a complete example showing how to set up an agent with restricted tool access:- 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
Working with Adapters Directly
If you want more control over how tools are created, you can work with the adapters directly. TheBaseAdapter
class provides a unified interface for converting MCP tools to various framework formats, with LangChainAdapter
being the most commonly used implementation.
- Create tools directly from an MCPClient
- Filter or customize which tools are available
- Integrate with different agent frameworks
- Flexibility: More control over tool creation and management
- Custom Integration: Easier to integrate with existing LangChain workflows
- Advanced Filtering: Apply custom logic to tool selection and configuration
- Framework Agnostic: Potential for future adapters to other frameworks
Server Manager
The Server Manager is an agent-level feature that enables dynamic server selection for improved performance with multi-server setups.Enabling Server Manager
To improve efficiency and potentially reduce agent confusion when many tools are available, you can enable the Server Manager by settinguse_server_manager=True
when creating the MCPAgent
.
How It Works
When enabled, the agent will automatically select the appropriate server based on the tool chosen by the LLM for each step. This avoids connecting to unnecessary servers and can improve performance with large numbers of available servers.Benefits
- Performance: Only connects to servers when their tools are actually needed
- Reduced Confusion: Agents work better with focused tool sets rather than many tools at once
- Resource Efficiency: Saves memory and connection overhead
- Automatic Selection: No need to manually specify
server_name
for most use cases - Scalability: Better performance with large numbers of servers
When to Use
- Multi-server environments: Essential for setups with 3+ servers
- Resource-constrained environments: When memory or connection limits are a concern
- Complex workflows: When agents need to dynamically choose between different tool categories
- Production deployments: For better resource management and performance
Memory Configuration
MCPAgent supports conversation memory to maintain context across interactions:System Prompt Customization
You can customize the agent’s behavior through system prompts:Custom System Prompt
Additional Instructions
Add task-specific instructions without replacing the base system prompt:System Prompt Templates
For more advanced customization, you can provide a custom system prompt template:Performance Configuration
Configure agent performance characteristics:Debugging Configuration
Enable debugging features during development:Agent Initialization
Control when and how the agent initializes:Error Handling
Configure how the agent handles errors:Best Practices
- LLM Selection: Use models with tool calling capabilities
- Step Limits: Set reasonable
max_steps
to prevent runaway execution - Tool Restrictions: Use
disallowed_tools
for security - Memory Management: Disable memory for stateless use cases
- Server Manager: Enable for multi-server setups
- System Prompts: Customize for domain-specific tasks
- Error Handling: Implement proper timeout and retry logic
- Testing: Test agent configurations in development environments
Common Issues
- No Tools Available: Check client configuration and server connections
- Tool Execution Failures: Enable verbose logging and check tool arguments
- Memory Issues: Disable memory or limit concurrent servers
- Timeout Errors: Increase
max_steps
or agent timeout values