Learning resourcesReviewed · Anthropic

Claude Agent SDK: Connect to external tools with MCP

This Claude Agent SDK guide helps TypeScript and Python developers connect AI agents to external tools, but permissions, credentials and connection failures need explicit handling. It covers local processes, remote HTTP/SSE servers, in-process tools and troubleshooting.

Researched 14 Nov 2026

In short.

This Claude Agent SDK guide is for developers connecting AI agents to external tools, with the catch that an agent can keep answering even when an MCP connection has failed. It explains configuration, permissions and authentication for local, remote and in-process servers, with TypeScript and Python examples. Its most useful operational detail is how to distinguish available tools, pending connections and authentication failures rather than treating a completed answer as proof of a working integration. The SDK does not perform interactive OAuth sign-in. Costs, licensing terms and real-world reliability are not established, and none of the examples were executed.

Researched and written by an AI model and checked by a second AI model. How it works

Coverage is limited to the complete documentation page at [https://code.claude.com/docs/en/agent-sdk/mcp.md], including its embedded examples and notices about permissions, authentication, connection timing, versions, failures and output limits. Linked pages, repositories, package files and licensing documents are excluded. No examples were executed, endpoints checked or runtime behavior tested.

The overview

Identity

Connect to external tools with MCP is a documentation guide for the Claude Agent SDK at [https://code.claude.com/docs/en/agent-sdk/mcp.md]. MCP means Model Context Protocol, a standard interface for connecting AI agents to tools and data sources. The page teaches SDK integration rather than providing a standalone application or downloadable MCP server.

Its scope is application-level configuration. Configuring MCP servers to load across Claude Code CLI projects is addressed through a separate linked page.

Assessment

The guide is useful when a developer needs more than a minimal connection example. It combines working-shaped configuration examples with explanations of tool permissions, startup timing, credential failures, reconnection and oversized outputs. TypeScript and Python examples make the same integration patterns accessible in either language.

Its practical value is strongest as implementation guidance. A successful agent response alone does not establish that an intended MCP tool was used: the page warns that Claude can fall back to built-in tools when a server is unavailable. Runtime reliability, answer accuracy and security effectiveness were not assessed.

Users

The primary readers are developers building Claude Agent SDK applications that need documentation lookup, filesystem access, repository information, databases or other external services. Beginners receive concrete examples and explanations of transport choices; experienced developers receive details about permissions, deferred connections, status polling and version-dependent behavior.

Readers should be comfortable editing TypeScript or Python and managing configuration and environment variables. Someone seeking a ready-to-use integration without application code would need additional setup guidance or a different interface.

Capabilities

The page explains how to register MCP servers directly in query options or through a project .mcp.json file. It covers local command-based servers, remote HTTP and SSE endpoints, and custom tools running inside the SDK application.

It also covers tool discovery, automatic approval of named tools, environment-variable credentials, authentication headers and connection diagnostics. Examples include searching Claude Code documentation, listing project files, retrieving GitHub issues and querying Postgres through DBHub. Tool search is described as enabled by default, loading tool definitions as needed to reduce their presence in the model's context.

Using it

Requirements

The examples require the corresponding Agent SDK: TypeScript imports query from @anthropic-ai/claude-agent-sdk, while Python imports from claude_agent_sdk. Full SDK installation instructions and minimum Node.js or Python versions are not supplied. The npx-based local-server examples require Node.js on PATH and the referenced server package to be available.

Remote integrations require a reachable endpoint and any credentials that service expects. The GitHub example requires a personal access token with repository read access; the database example requires a database connection string and DBHub configuration. The SDK does not open a browser for OAuth: an application must obtain and pass a token, or use an applicable sign-in completed through an interactive Claude Code session on the same machine.

Mechanism

An MCP server exposes tools that the agent can discover and call. A stdio server runs as a local process and communicates through standard input and output. HTTP and SSE servers are reached through network URLs. An SDK MCP server defines tools inside the application process. Tool names follow mcp__<server-name>__<tool-name>, with characters such as dots and spaces in server names replaced by underscores.

Connection timing depends on the configuration source, transport, tool search and cached tool lists. A remote server with cached tool definitions can expose those definitions immediately and connect on its first tool call. Consequently, pending status can mean a background connection, deferred connection or an expired connection deadline. Later status polling is needed to distinguish these cases.

For code-configured stdio and uncached HTTP/SSE servers, the documented default first-turn connection limit is MCP_TIMEOUT, normally 30 seconds. CLAUDE_CODE_MCP_STARTUP_WAIT_MS can change several waits, but permission-prompt servers, in-process SDK servers and still-connecting named servers retain specified exceptions.

Setup

In TypeScript, configure servers through mcpServers and automatic tool approval through allowedTools; Python uses mcp_servers and allowed_tools. A project-root .mcp.json can supply server configuration when the project setting source is enabled. The page says default query options enable it; explicitly setting settingSources requires including project.

Allow individual tool names where practical. A server wildcard approves all tools from that server. The acceptEdits permission mode does not automatically approve MCP calls, while bypassPermissions disables a much broader set of permission prompts. The page recommends allowedTools for MCP access.

For local servers, pass required credentials through the server's env configuration. For remote servers, supply authentication headers as appropriate. JSON configuration supports runtime substitution such as ${API_TOKEN}. Streamable HTTP uses type http in SDK code; streamable-http is accepted as an alias in JSON configuration files.

Example

The quickstart registers claude-code-docs as an HTTP server at [https://code.claude.com/docs/mcp], allows mcp__claude-code-docs__* and asks the agent to explain Claude Code hooks using that server. Both language examples print the result when the SDK reports success. This illustrates the documented configuration pattern; endpoint availability and the resulting answer were not checked.

The database example asks how many users signed up last week, grouped by day. DBHub receives a dbhub.toml configuration with readonly = true for execute_sql. That server-side setting rejects writes and schema-changing statements; the page warns that execute_sql otherwise accepts whatever SQL the agent emits. DATABASE_URL is resolved from the environment.

Assessment & evidence

Limitations

Server registration does not guarantee availability or use. Authentication failures can leave the agent running without that server's tools, and connected remote servers can return to pending during reconnection. The page documents automatic retries and manual reconnection methods, but supplies no measured uptime or recovery performance.

Large outputs have special handling. A successful result without image content that exceeds the default 25,000-token limit is saved to a file, and the tool result is replaced with an error identifying the path. MAX_MCP_OUTPUT_TOKENS changes that token limit. A separate 50,000-character text threshold applies unless the tool declares anthropic/maxResultSizeChars.

In the TypeScript SDK, a custom tool whose input schema cannot be converted to JSON Schema can be omitted from the server's tool listing with a warning. Implementation details for custom tools, complete permission evaluation and several advanced settings are delegated to linked pages, whose contents are outside this report's scope.

Design tradeoffs

The guide's main architectural benefit is a common connection pattern for different external services. Developers can use a suitable MCP server for a service while retaining explicit control over which exposed tools the agent may call. An inference from the examples is that this reduces the need to build each service integration directly into the agent application, while leaving server configuration and credentials to manage.

Permission and data-access controls operate at different levels. allowedTools controls which tools can run automatically; DBHub's readonly configuration constrains what its SQL tool can do. The database example shows why a narrowly named tool can still require restrictions inside the server.

Alternatives

Within the documented options, stdio fits a server launched on the same machine, HTTP or SSE fits a remote service, and an SDK MCP server fits custom tools defined in application code. These are deployment choices rather than measured competitors.

For persistent Claude Code CLI configuration, the page points to separate MCP installation-scope documentation. For building custom in-process tools, it points to a separate custom-tools guide. Neither linked guide nor competing SDKs were assessed, so comparative performance, cost and feature completeness are unknown.

Evidence

The evidence is the complete page at [https://code.claude.com/docs/en/agent-sdk/mcp.md], captured at 2026-11-14T19:04:33.593Z. The original captured file is selected-page.txt, recorded as 40,280 bytes with SHA-256 0163d83baa2a5802847161b80dd85386cb604bdb25edb89412152ff37b79b546.

The page supplies documentation and illustrative code, with no accompanying execution receipts, benchmark results or independent reliability measurements. No code was executed and no integration was tested. Linked documentation, repositories, package contents and service endpoints were not inspected.

Access & freshness

Costs

The page provides no prices for model use, MCP services, hosting or third-party APIs. Total setup and running costs are unknown. Tool search is described as withholding unused tool definitions from context, but no token measurements, billing examples or quantified savings are supplied.

Rights

The supplied page contains no license text or explicit redistribution grant for the documentation, examples, SDK packages or referenced servers. Their reuse and redistribution terms are unknown from this evidence. Describing MCP as an open standard does not establish the licensing terms of each implementation. Service data-retention terms and commercial-use conditions are also not stated.

Freshness

The capture establishes the page's contents at the recorded time, not its publication date or the latest available SDK release. No publication or last-updated date is supplied.

The text contains specific compatibility thresholds. CLAUDE_CODE_MCP_STARTUP_WAIT_MS requires Claude Code v2.1.274 or later; naming a server had no first-turn wait effect before v2.1.284. Before v2.1.307, a locked Mac login keychain produced a different authentication status. Before TypeScript Agent SDK v0.3.286, one unconvertible tool schema could fail the server's entire tool listing. These references identify behavior changes rather than a complete supported-version matrix.

Next steps

Start with one server and a small task whose expected tool use is easy to inspect. Match the transport to that server's documentation, grant the required tool permissions and provide credentials with access appropriate to the task. For database access, configure an explicit read-only restriction before using the illustrated querying pattern.

Inspect the init message and actual MCP tool-use events. Poll mcpServerStatus() in TypeScript or get_mcp_status() through the Python client when pending status needs clarification or a connection later drops. Confirm required tool calls before relying on the answer, and check installed-version compatibility and applicable service costs separately.

Evidence for this report

selected-page.txtselected · checked 2026-11-14