Quickstart
This example connects to the Claude Code documentation MCP server using HTTP transport and usesallowedTools with a wildcard to permit all tools from the server.
Add an MCP server
You can configure MCP servers in code when callingquery(), or in a .mcp.json file loaded via settingSources.
In code
Pass MCP servers directly in themcpServers option:
From a config file
Create a.mcp.json file at your project root. The SDK does not load filesystem settings by default, so set settingSources: ["project"] (Python: setting_sources=["project"]) in your options for the file to be picked up:
Allow MCP tools
MCP tools require explicit permission before Claude can use them. Without permission, Claude will see that tools are available but won’t be able to call them.Tool naming convention
MCP tools follow the naming patternmcp__<server-name>__<tool-name>. For example, a GitHub server named "github" with a list_issues tool becomes mcp__github__list_issues.
Grant access with allowedTools
UseallowedTools to specify which MCP tools Claude can use:
*) let you allow all tools from a server without listing each one individually.
Prefer
allowedTools over permission modes for MCP access. permissionMode: "acceptEdits" does not auto-approve MCP tools (only file edits and filesystem Bash commands). permissionMode: "bypassPermissions" does auto-approve MCP tools but also disables all other safety prompts, which is broader than necessary. A wildcard in allowedTools grants exactly the MCP server you want and nothing more. See Permission modes for a full comparison.Discover available tools
To see what tools an MCP server provides, check the server’s documentation or connect to the server and inspect thesystem init message:
Transport types
MCP servers communicate with your agent using different transport protocols. Check the server’s documentation to see which transport it supports:- If the docs give you a command to run (like
npx @modelcontextprotocol/server-github), use stdio - If the docs give you a URL, use HTTP or SSE
- If you’re building your own tools in code, use an SDK MCP server
stdio servers
Local processes that communicate via stdin/stdout. Use this for MCP servers you run on the same machine:- In code
- .mcp.json
HTTP/SSE servers
Use HTTP or SSE for cloud-hosted MCP servers and remote APIs:- In code
- .mcp.json
"type": "http" instead.
SDK MCP servers
Define custom tools directly in your application code instead of running a separate server process. See the custom tools guide for implementation details.MCP tool search
When you have many MCP tools configured, tool definitions can consume a significant portion of your context window. Tool search solves this by withholding tool definitions from context and loading only the ones Claude needs for each turn. Tool search is enabled by default. See Tool search for configuration options and details. For more detail, including best practices and using tool search with custom SDK tools, see the tool search guide.Authentication
Most MCP servers require authentication to access external services. Pass credentials through environment variables in the server configuration.Pass credentials via environment variables
Use theenv field to pass API keys, tokens, and other credentials to the MCP server:
- In code
- .mcp.json
HTTP headers for remote servers
For HTTP and SSE servers, pass authentication headers directly in the server configuration:- In code
- .mcp.json
OAuth2 authentication
The MCP specification supports OAuth 2.1 for authorization. The SDK doesn’t handle OAuth flows automatically, but you can pass access tokens via headers after completing the OAuth flow in your application:Examples
List issues from a repository
This example connects to the GitHub MCP server to list recent issues. The example includes debug logging to verify the MCP connection and tool calls. Before running, create a GitHub personal access token withrepo scope and set it as an environment variable:
Query a database
This example uses the Postgres MCP server to query a database. The connection string is passed as an argument to the server. The agent automatically discovers the database schema, writes the SQL query, and returns the results:Error handling
MCP servers can fail to connect for various reasons: the server process might not be installed, credentials might be invalid, or a remote server might be unreachable. The SDK emits asystem message with subtype init at the start of each query. This message includes the connection status for each MCP server. Check the status field to detect connection failures before the agent starts working:
Troubleshooting
Server shows “failed” status
Check theinit message to see which servers failed to connect:
- Missing environment variables: Ensure required tokens and credentials are set. For stdio servers, check the
envfield matches what the server expects. - Server not installed: For
npxcommands, verify the package exists and Node.js is in your PATH. - Invalid connection string: For database servers, verify the connection string format and that the database is accessible.
- Network issues: For remote HTTP/SSE servers, check the URL is reachable and any firewalls allow the connection.
Tools not being called
If Claude sees tools but doesn’t use them, check that you’ve granted permission withallowedTools:
Connection timeouts
The MCP SDK has a default timeout of 60 seconds for server connections. If your server takes longer to start, the connection will fail. For servers that need more startup time, consider:- Using a lighter-weight server if available
- Pre-warming the server before starting your agent
- Checking server logs for slow initialization causes
Related resources
- Custom tools guide: Build your own MCP server that runs in-process with your SDK application
- Permissions: Control which MCP tools your agent can use with
allowedToolsanddisallowedTools - TypeScript SDK reference: Full API reference including MCP configuration options
- Python SDK reference: Full API reference including MCP configuration options
- MCP server directory: Browse available MCP servers for databases, APIs, and more