Files
mcp-use__mcp-use/docs/python/agent/agent-configuration.mdx
Pietro Zullo 8348c59a53 docs: reorganize, fix accuracy, and update for agent readability (#1502)
* docs: reorganize navigation and clean up content

- Remove 'Misc' group with misplaced blog posts
- Remove 'Libraries' redirect group from Home nav
- Simplify 'Dev Tools' group (flatten Inspector, CLI, Tunneling)
- Remove individual changelog versions from Python nav (keep summary)
- Remove changelog from TypeScript 'Get Started' group
- Remove tunneling duplicate from Python Development group
- Rewrite mcp101 as clean MCP concepts page (was pitch deck)
- Delete typescript/blog posts (not technical docs)
- Add redirects for deleted blog post URLs

* docs: fix content accuracy and remove sloppy language

- Fix broken python/server/mcp-apps link in home page
- Remove informal pitch-speak from python/server/index.mdx
- Fix grammar error in typescript/server/index.mdx
- Update typescript/client/logging.mdx: remove fake 'coming soon' API code
- Fix non-existent model name gpt-5.1 in typescript quickstart
- Fix mcp_use naming in development contributing guide

* docs: fix incorrect TypeScript server API calls in inspector docs

- Replace server.addTool() with server.tool() (correct mcp-use API)
- Add missing zod import to debugging-chatgpt-apps example

* docs: fix fake model names (gpt-5.1 → gpt-4o) in agent docs

* docs: update model names, fix APIs, and add agent visibility hints

- Replace all deprecated model names with latest: gpt-4.1, gpt-4.1-mini,
  claude-sonnet-4-6, claude-opus-4-7, gemini-2.5-flash across all docs
- Fix TypeScript server.tool() API in installation.mdx (old positional string form)
- Fix TypeScript agent.run() in quickstart.mdx (deprecated string form → object)
- Remove broken manufact.com/docs/llms.txt hidden hints from 5 pages
- Add <Visibility for="agents"> block to mcp101.mdx with install commands,
  key classes, and API signatures for agent-oriented navigation
- Add LangChain provider install note to Python quickstart (mcp-use agent
  requires a separate langchain-* provider package)
- Update model name cards in Python llm-integration.mdx

* docs: clean up remaining stale model names and improve accuracy

- Update supported model lists in Python and TypeScript llm-integration pages
- Fix cost optimization table to use current model IDs
- Update TypeScript agent overview supported providers list

* docs: add LangChain provider install to TypeScript quickstart agent section

The MCPAgent requires a separate @langchain/* provider package; this was
not mentioned before the code example that imports from @langchain/openai.

* docs: update to latest flagship model IDs (May 2026)

- OpenAI: gpt-4.1 → gpt-5.5, gpt-4.1-mini → gpt-5.5-mini
- Google: gemini-2.5-flash → gemini-3.1-flash, gemini-2.5-pro → gemini-3.1-pro
- Groq: llama-3.1-* → llama-4-scout-17b-16e-instruct
- Anthropic (claude-opus-4-7, claude-sonnet-4-6) already current, no change

* docs: replace all em dashes with hyphens

* docs: restore llms.txt hints using Visibility component

* docs: simplify TypeScript logging page to unsupported notice only

* docs: rewrite mcp101 to explain MCP protocol with mcp-use examples and doc links

* docs: remove goal.md

* docs: revert nav changes, keep only Misc group removal and blog redirects

* docs: reorder mcp101 to Server/App -> Client/Inspector -> Agent, restore title

* docs: add code examples for Resources and Prompts in mcp101

* docs: remove em dashes from mcp101

* docs: add MCPClient code examples to mcp101

* docs: add React client example to mcp101

* docs: add agent code examples to mcp101

* docs: expand MCP Apps section in mcp101 with example and links

* docs: restore Contacts, Socials, and Founders sections to mcp101

* docs: add context-setting intro paragraph to mcp101 for new readers

* docs: add MCP stores section to mcp101 intro with Claude, ChatGPT, Cursor

* docs: fix Claude store link

* docs: fix ChatGPT store link

* docs: fix Cursor store link

* docs: replace store Cards with linked image grid

* docs: add captions to store images

* docs: add Notifications to client primitives table in mcp101

* docs: add lead-in paragraph before resource cards at bottom of mcp101

* docs: move resource cards to end of page after Contacts/Socials/Founders

* docs: demote Contacts/Socials/Founders to bold text to remove from TOC

* docs: move Contacts/Socials/Founders to very end of mcp101

* docs: add Resources and Contacts section headers, link official MCP docs

* chore(docs): update site metadata, footer links, and rename mcp_use to mcp-use

* update docs metadata + images

* docs: enhance MCP section by highlighting the reach of MCP stores with 1 billion weekly active users

* api ref
2026-05-14 18:49:06 +02:00

612 lines
17 KiB
Plaintext

---
title: "Agent Configuration"
description: "Configure MCPAgent behavior and LLM integration"
icon: "cog"
---
<Info>
This guide covers MCPAgent configuration options for customizing agent behavior and LLM integration. For client configuration, see the [Client Configuration](/python/client/client-configuration) guide.
</Info>
## API Keys
<Warning>
Never hardcode API keys in your source code. Always use environment variables for security.
</Warning>
Since agents use LLM providers that require API keys, you need to configure them properly:
<Tabs>
<Tab title=".env File (Recommended)">
Create a `.env` file in your project root:
```bash .env
# OpenAI
OPENAI_API_KEY=your_api_key_here
# Anthropic
ANTHROPIC_API_KEY=your_api_key_here
# Groq
GROQ_API_KEY=your_api_key_here
# Google
GOOGLE_API_KEY=your_api_key_here
```
Load it in your application:
<CodeGroup>
```python Python
from dotenv import load_dotenv
load_dotenv()
```
</CodeGroup>
<Tip>
This method keeps your keys organized and makes them available to your Python runtime.
</Tip>
</Tab>
<Tab title="Environment Variables">
Set environment variables directly in your terminal:
```bash
export OPENAI_API_KEY="your_api_key_here"
export ANTHROPIC_API_KEY="your_api_key_here"
```
Access them in your application:
<CodeGroup>
```python Python
import os
api_key = os.getenv("OPENAI_API_KEY", "")
```
</CodeGroup>
</Tab>
<Tab title="System Configuration">
For production environments, consider using:
- Docker secrets
- Kubernetes secrets
- Cloud provider secret managers (AWS Secrets Manager, etc.)
- System environment configuration
</Tab>
</Tabs>
## Agent Parameters
When creating an MCPAgent, you can configure several parameters to customize its behavior:
<CodeGroup>
```python Python
from mcp_use import MCPAgent, MCPClient
from langchain_openai import ChatOpenAI
# Basic configuration
agent = MCPAgent(
llm=ChatOpenAI(model="gpt-5.5", temperature=0.7),
client=MCPClient.from_config_file("config.json"),
max_steps=30
)
# Advanced configuration
agent = MCPAgent(
llm=ChatOpenAI(model="gpt-5.5", temperature=0.7),
client=MCPClient.from_config_file("config.json"),
max_steps=30,
server_name=None,
auto_initialize=True,
memory_enabled=True,
system_prompt="Custom instructions for the agent",
additional_instructions="Additional guidelines for specific tasks",
disallowed_tools=["file_system", "network", "shell"] # Restrict potentially dangerous tools
)
```
</CodeGroup>
### 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)
## Sending Files with HumanMessage
Pass files directly inside a `HumanMessage` instead of using a separate parameter. The agent accepts either a plain string or a `HumanMessage`, so you can include both text and file content together:
<CodeGroup>
```python Python
import asyncio
import base64
from pathlib import Path
from langchain_core.messages import HumanMessage
from langchain_openai import ChatOpenAI
from mcp_use import MCPAgent, MCPClient
async def main():
client = MCPClient(config = {})
agent = MCPAgent(
llm=ChatOpenAI(model="gpt-5.5", temperature=0),
client=client,
)
image_bytes = base64.b64encode(
Path("static/logo-gh.jpg").read_bytes()
).decode("utf-8")
human_message = HumanMessage(
content=[
{"type": "text", "text": "What is in this image?"},
{
"type": "image",
"base64": image_bytes,
"mime_type": "image/jpeg",
},
]
)
async for step in agent.stream(human_message, max_steps=10):
print(step)
if __name__ == "__main__":
asyncio.run(main())
```
</CodeGroup>
<Note>
Remote agents currently accept text-only `HumanMessage` content. Use a local agent when you need to pass files.
</Note>
## 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:
<CodeGroup>
```python Python
import asyncio
import os
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI
from mcp_use import MCPAgent, MCPClient
async def main():
# Load environment variables
load_dotenv()
# Create configuration dictionary
config = {
"mcpServers": {
"playwright": {
"command": "npx",
"args": ["@playwright/mcp@latest"],
"env": {
"DISPLAY": ":1"
}
}
}
}
# Create MCPClient from configuration dictionary
client = MCPClient(config)
# Create LLM
llm = ChatOpenAI(model="gpt-5.5")
# Create agent with restricted tools
agent = MCPAgent(
llm=llm,
client=client,
max_steps=30,
disallowed_tools=["file_system", "network"] # Restrict potentially dangerous tools
)
# Run the query
result = await agent.run(
"Find the best restaurant in San Francisco USING GOOGLE SEARCH",
)
print(f"\nResult: {result}")
if __name__ == "__main__":
asyncio.run(main())
```
</CodeGroup>
You can also manage tool restrictions dynamically:
<CodeGroup>
```python Python
# Update restrictions after initialization
agent.set_disallowed_tools(["file_system", "network", "shell", "database"])
await agent.initialize() # Reinitialize to apply changes
# Check current restrictions
restricted_tools = agent.get_disallowed_tools()
print(f"Restricted tools: {restricted_tools}")
```
</CodeGroup>
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
## Working with Adapters Directly
If you want more control over how tools are created, you can work with the adapters directly. The `BaseAdapter` class provides a unified interface for converting MCP tools to various framework formats, with `LangChainAdapter` being the most commonly used implementation.
```python
import asyncio
from langchain.agents import create_agent
from langchain.chat_models import init_chat_model
from mcp_use.client import MCPClient
from mcp_use.adapters import LangChainAdapter
async def main():
# Initialize client
client = MCPClient.from_config_file("browser_mcp.json")
# Create an adapter instance
adapter = LangChainAdapter()
# Get tools directly from the client
tools = await adapter.create_tools(client)
# Use the tools with any LangChain agent
model = init_chat_model("gpt-5.5", temperature=0.5)
agent = create_agent(
model=model,
tools=tools,
system_prompt="You are a helpful assistant with access to powerful tools.",
)
result = await agent.ainvoke(
{"messages": [{"role": "user", "content": "Search for information about climate change"}]}
)
print(result)
if __name__ == "__main__":
asyncio.run(main())
```
The adapter pattern makes it easy to:
1. Create tools directly from an MCPClient
2. Filter or customize which tools are available
3. Integrate with different agent frameworks
**Benefits of Direct Adapter Usage:**
- **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 setting `use_server_manager=True` when creating the `MCPAgent`.
<CodeGroup>
```python Python
# Enable server manager for automatic server selection
agent = MCPAgent(
llm=llm,
client=client,
use_server_manager=True # Enable dynamic server selection
)
```
</CodeGroup>
### 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.
<CodeGroup>
```python Python
# Multi-server setup with server manager
client = MCPClient.from_config_file("multi_server_config.json")
agent = MCPAgent(
llm=llm,
client=client,
use_server_manager=True
)
# The agent automatically selects servers based on tool usage
result = await agent.run(
"Search for a place in Barcelona on Airbnb, then Google nearby restaurants."
)
```
</CodeGroup>
### 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
For more details on server manager implementation, see the [Server Manager](./server-manager) guide.
## Memory Configuration
MCPAgent supports conversation memory to maintain context across interactions:
<CodeGroup>
```python Python
# Enable memory (default)
agent = MCPAgent(
llm=llm,
client=client,
memory_enabled=True
)
# Disable memory for stateless interactions
agent = MCPAgent(
llm=llm,
client=client,
memory_enabled=False
)
```
</CodeGroup>
## System Prompt Customization
You can customize the agent's behavior through system prompts:
### Custom System Prompt
<CodeGroup>
```python Python
custom_prompt = """
You are a helpful assistant specialized in data analysis.
Always provide detailed explanations for your reasoning.
When working with data, prioritize accuracy over speed.
"""
agent = MCPAgent(
llm=llm,
client=client,
system_prompt=custom_prompt
)
```
</CodeGroup>
### Additional Instructions
Add task-specific instructions without replacing the base system prompt:
<CodeGroup>
```python Python
agent = MCPAgent(
llm=llm,
client=client,
additional_instructions="Focus on finding recent information from the last 6 months."
)
```
</CodeGroup>
### System Prompt Templates
For more advanced customization, you can provide a custom system prompt template:
```python
from langchain_core.prompts import ChatPromptTemplate
custom_template = ChatPromptTemplate.from_messages([
("system", "You are an expert {domain} assistant. {instructions}"),
("human", "{input}"),
# ... other message templates
])
agent = MCPAgent(
llm=llm,
client=client,
system_prompt_template=custom_template
)
```
## Performance Configuration
Configure agent performance characteristics:
```python
# Limit execution steps
agent = MCPAgent(
llm=llm,
client=client,
max_steps=10 # Prevent runaway execution
)
# Enable server manager for better performance with multiple servers
agent = MCPAgent(
llm=llm,
client=client,
use_server_manager=True # Only connects servers when needed
)
# Limit concurrent servers (if not using server manager)
agent = MCPAgent(
llm=llm,
client=client,
max_concurrent_servers=3
)
```
## Debugging Configuration
Enable debugging features during development:
```python
# Enable verbose logging
agent = MCPAgent(
llm=llm,
client=client,
verbose=True,
debug=True
)
# Set debug level programmatically
import mcp_use
mcp_use.set_debug(2) # Full verbose logging
```
## Agent Initialization
Control when and how the agent initializes:
```python
# Auto-initialize on creation
agent = MCPAgent(
llm=llm,
client=client,
auto_initialize=True
)
# Manual initialization for more control
agent = MCPAgent(
llm=llm,
client=client,
auto_initialize=False
)
# Initialize manually when ready
await agent.initialize()
```
## Error Handling
Configure how the agent handles errors:
```python
# Set timeout for agent operations
agent = MCPAgent(
llm=llm,
client=client,
timeout=60 # 60 seconds timeout
)
# Configure retry behavior (if supported by LLM)
llm = ChatOpenAI(
model="gpt-5.5",
max_retries=3,
retry_delay=2
)
agent = MCPAgent(llm=llm, client=client)
```
### Automatic Error Retry
The agent includes automatic error handling that catches tool failures and allows the LLM to retry with corrected input. This is controlled by the `retry_on_error` parameter (default: `True`).
```python
# Default: Error handling enabled
agent = MCPAgent(
llm=llm,
client=client,
retry_on_error=True # Default
)
# Disable automatic error handling (errors will halt execution)
agent = MCPAgent(
llm=llm,
client=client,
retry_on_error=False
)
```
**How it works:**
When `retry_on_error=True`, the agent uses middleware to intercept tool errors:
1. **Catches all tool errors** including validation errors, connection errors, and execution errors
2. **Formats the error** into a clear, actionable message
3. **Returns error to LLM** as a tool result (not an exception)
4. **LLM reads the error** and can retry with corrected input
5. **Execution continues** based on the LLM's decision
**Example Flow:**
```python
# First attempt - missing required field
# Tool call: create_board_column(board_id="123", body={"name": "Assignee"})
# Error returned: "Tool input validation failed. Please fix the following errors and retry:
# - body.metaData: Field required (type: missing)"
# Second attempt - LLM automatically retries with correction
# Tool call: create_board_column(board_id="123", body={"name": "Assignee", "metaData": {}})
# Success: Column created
```
<Note>
The automatic retry mechanism works for all error types and is handled by the middleware, ensuring errors are caught at the right level - including Pydantic validation errors that occur before tool execution.
</Note>
**Error Message Formatting:**
Different error types receive different formatting:
- **Validation Errors**: Field-by-field breakdown showing exactly what's wrong
```
Tool input validation failed. Please fix the following errors and retry:
- body.metaData: Field required (type: missing)
- timeout: Input should be a valid integer (type: int_type)
```
- **Other Errors**: Error type and description
```
ConnectionError: Failed to connect to MCP server
```
**When to disable `retry_on_error`:**
Set `retry_on_error=False` when:
- Debugging tool issues and you want immediate failure
- You want strict validation without retry attempts
- Running in environments where retries could cause issues
## Best Practices
1. **LLM Selection**: Use models with tool calling capabilities
2. **Step Limits**: Set reasonable `max_steps` to prevent runaway execution
3. **Tool Restrictions**: Use `disallowed_tools` for security
4. **Memory Management**: Disable memory for stateless use cases
5. **Server Manager**: Enable for multi-server setups
6. **System Prompts**: Customize for domain-specific tasks
7. **Error Handling**: Implement proper timeout and retry logic
8. **Testing**: Test agent configurations in development environments
## Common Issues
1. **No Tools Available**: Check client configuration and server connections
2. **Tool Execution Failures**: Enable verbose logging and check tool arguments
3. **Memory Issues**: Disable memory or limit concurrent servers
4. **Timeout Errors**: Increase `max_steps` or agent timeout values
For detailed troubleshooting, see the [Security](/python/development/security) guide.