await and assume they are running inside an async function or an async entry point such as asyncio.run().
Example 1: Small toolset — normal mode
When the total number of tools (built-in defaults plus MCP tools) stays belowtool_threshold, all tools are exposed to the model directly with no discovery step.
mcp.json for this example:
The filesystem MCP server requires Node.js and
npx. Install with npm install -g @modelcontextprotocol/server-filesystem or let npx -y install it on first run.Example 2: Large toolset — auto deferred mode
When the combined tool count reachestool_threshold, MCPAgent automatically switches to deferred mode. The model receives only tool_search_regex and must discover tools before calling them.
debug=True you will see log lines like:
Example 3: Force deferred mode for small catalogs
Setdeferred_tools=True to always use deferred loading regardless of tool count. Useful when you want controlled, incremental tool exposure as a policy.
Example 4: Lazy initialization on first chat
Skip the explicitinit_mcp_servers() call. The first chat() call triggers initialization automatically via _lazy_init_mcp().
Example 5: Session management for production
Named sessions isolate conversation history per user, tenant, or workflow. Stale sessions can be cleaned up on a schedule.Example 6: Custom MCP server
You can connect to any MCP-compatible server by declaring it inmcp.json. This example shows a Python-based custom server started via python.
mcp.json:
Example 7: Inline config (no mcp.json file)
Pass the server configuration as a dict to avoid a file on disk — useful in containerized or test environments.Troubleshooting
MCPAgent returns None
MCPAgent returns None
chat() returns None when max_iterations is reached without a final assistant message. This usually means the agent is stuck in a tool-call loop.Steps to diagnose:- Set
debug=Trueto see the full tool execution trace. - Increase
max_iterationstemporarily to confirm the agent eventually resolves. - Check that your MCP server tools are returning results in a format the model understands.
- If in deferred mode, verify tool descriptions are clear enough for regex discovery.
MCP server fails to connect
MCP server fails to connect
Common causes and fixes:
- Command not found: The
commandinmcp.jsonmust be onPATHor an absolute path. Usewhich npxorwhich pythonto verify.MCPClientManagercallsshutil.which()to resolve the path, but if the command is missing entirely it will fail. - Timeout (10 s): The server process takes too long to start. The manager logs
[MCP Client] Timeout connecting to <name>and moves on. Check that the command exits quickly during initialization. - Initialization error: Look for
[MCP Client] Connection error for <name>: ...in the output. Usually indicates a crash in the server process — check the server’s own logs.
debug=True on MCPAgent to see connection status lines:Tools not appearing in deferred mode
Tools not appearing in deferred mode
If the model cannot find tools via
tool_search_regex:- Confirm
_mcp_initializedisTrueby checkingagent.get_registry_stats(). - Verify that
total_registered > 0in the stats output. - Use
pattern=".*"in a manual search to list all registered tools:
- If the registry is empty,
init_mcp_servers()may have silently failed — check for exceptions withdebug=True.
Session does not exist error
Session does not exist error
ValueError: Session <id> does not exist is raised when create_if_missing=False and the session has not been created.Either:- Call
agent.create_session(session_id)beforechat(), or - Keep the default
create_if_missing=True.
API key errors with cloud providers
API key errors with cloud providers
When using
provider="openai", "groq", or "gemini", api_key is required. Missing or invalid keys produce authentication errors from the provider.