Claude Agent SDK: How to Add an MCP Server
Updated 2026-12-01
The Claude Agent SDK loads MCP tools when you pass mcp_servers (Python) or mcpServers (TypeScript) into query(), then allow each tool with allowed_tools / allowedTools using the mcp__<server>__<tool> naming pattern. This guide wires a local stdio filesystem server and a project .mcp.json file. If tools never appear, check Fix: MCP server connection closed before changing SDK options. Last tested 2026-12-01.
Prerequisites
- Python 3.10+ or Node.js 22+
- Claude Code CLI available to the SDK (
claude-agent-sdkbundles against it) - Node.js installed if you use
npxto start stdio MCP servers
Docs used:
Steps
1. Install the Claude Agent SDK (Python)
python -m venv .venv && source .venv/bin/activate
pip install claude-agent-sdk
On Debian/Ubuntu/Homebrew Python, install into a venv (system pip may refuse with externally-managed-environment).
2. Add a stdio MCP server in code
Replace /path/to/projects with a directory the model may list:
# sdk_mcp.py
import asyncio
from claude_agent_sdk import ClaudeAgentOptions, ResultMessage, query
async def main() -> None:
options = ClaudeAgentOptions(
mcp_servers={
"filesystem": {
"command": "npx",
"args": [
"-y",
"@modelcontextprotocol/server-filesystem",
"/path/to/projects",
],
}
},
allowed_tools=["mcp__filesystem__*"],
)
async for message in query(
prompt="List the top-level files in my project folder",
options=options,
):
if isinstance(message, ResultMessage) and message.subtype == "success":
print(message.result)
asyncio.run(main())
MCP tools are named mcp__<server_key>__<tool_name>. Dots and spaces in the server key become underscores (MCP tool naming).
3. Or load servers from .mcp.json
Create .mcp.json at the project root:
{
"mcpServers": {
"filesystem": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-filesystem", "/path/to/projects"]
}
}
}
Ensure setting_sources includes "project" when you override defaults so the file loads (Agent SDK MCP — config file).
4. Pre-approve specific tools instead of wildcards
allowed_tools=[
"mcp__filesystem__read_file",
"mcp__filesystem__list_directory",
]
Without explicit permission, the model sees tools but cannot call them (allow MCP tools).
5. Add an in-process SDK MCP server (optional)
For tools that live inside your app (no subprocess):
from claude_agent_sdk import ClaudeAgentOptions, create_sdk_mcp_server, query, tool
@tool("add", "Add two numbers", {"a": float, "b": float})
async def add(args):
return {"content": [{"type": "text", "text": f"Sum: {args['a'] + args['b']}"}]}
calc = create_sdk_mcp_server(name="calc", tools=[add])
options = ClaudeAgentOptions(
mcp_servers={"calc": calc},
allowed_tools=["mcp__calc__add"],
)
6. Remote HTTP/SSE MCP servers
Pass type, url, and optional headers under the server key. First-turn connection timing depends on server type and cached tool lists (connection timing). For hosted HTTP examples, compare GitHub MCP server and How to deploy an MCP server to Vercel.
Common errors
Tools listed but never called. allowed_tools is missing or names do not match mcp__<server>__<tool>. Log the exact tool list from the SDK event stream.
First turn times out. Stdio servers wait up to MCP_TIMEOUT (default 30s). Fix the server command with Fix: MCP server not connecting.
spawn npx ENOENT. GUI and SDK hosts may not inherit your shell PATH. Use the absolute path from which npx in command.
Connection closed immediately. The MCP child process exited. See Fix: MCP server connection closed.
FAQ
How do I add an MCP server to the Claude Agent SDK?
Pass a mcp_servers map to ClaudeAgentOptions, then set allowed_tools to the MCP tool names (or mcp__server__* wildcards).
What is the MCP tool naming convention?
mcp__<server_key>__<tool_name> with punctuation in the server key normalized to underscores.
Can I reuse the same servers as Claude Code?
Yes. A project .mcp.json matches Claude Code when setting_sources includes "project". CLI install scopes are separate from per-query() servers (MCP scopes note).
How is this different from a Python MCP client loop?
Connect an AI agent to an MCP server uses the MCP Python SDK directly. The Agent SDK embeds Claude Code’s MCP client and permission model.