Loading...
    • Developer Guide
    • API Reference
    • MCP
    • Resources
    • Release Notes
    Search...
    ⌘K

    First steps

    Intro to ClaudeQuickstart

    Models & pricing

    Models overviewChoosing a modelWhat's new in Claude 4.5Migrating to Claude 4.5Model deprecationsPricing

    Build with Claude

    Features overviewUsing the Messages APIContext windowsPrompting best practices

    Capabilities

    Prompt cachingContext editingExtended thinkingStreaming MessagesBatch processingCitationsMultilingual supportToken countingEmbeddingsVisionPDF supportFiles APISearch resultsGoogle Sheets add-on

    Tools

    OverviewHow to implement tool useToken-efficient tool useFine-grained tool streamingBash toolCode execution toolComputer use toolText editor toolWeb fetch toolWeb search toolMemory tool

    Agent Skills

    OverviewQuickstartBest practicesUsing Skills with the API

    Agent SDK

    OverviewTypeScript SDKPython SDK

    Guides

    Streaming InputHandling PermissionsSession ManagementHosting the Agent SDKModifying system promptsMCP in the SDKCustom ToolsSubagents in the SDKSlash Commands in the SDKAgent Skills in the SDKTracking Costs and UsageTodo ListsPlugins in the SDK

    MCP in the API

    MCP connectorRemote MCP servers

    Claude on 3rd-party platforms

    Amazon BedrockVertex AI

    Prompt engineering

    OverviewPrompt generatorUse prompt templatesPrompt improverBe clear and directUse examples (multishot prompting)Let Claude think (CoT)Use XML tagsGive Claude a role (system prompts)Prefill Claude's responseChain complex promptsLong context tipsExtended thinking tips

    Test & evaluate

    Define success criteriaDevelop test casesUsing the Evaluation ToolReducing latency

    Strengthen guardrails

    Reduce hallucinationsIncrease output consistencyMitigate jailbreaksStreaming refusalsReduce prompt leakKeep Claude in character

    Administration and monitoring

    Admin API overviewUsage and Cost APIClaude Code Analytics API
    Console
    Guides

    MCP in the SDK

    Extend Claude Code with custom tools using Model Context Protocol servers

    Overview

    Model Context Protocol (MCP) servers extend Claude Code with custom tools and capabilities. MCPs can run as external processes, connect via HTTP/SSE, or execute directly within your SDK application.

    Configuration

    Basic Configuration

    Configure MCP servers in .mcp.json at your project root:

    TypeScript
    {
      "mcpServers": {
        "filesystem": {
          "command": "npx",
          "args": ["@modelcontextprotocol/server-filesystem"],
          "env": {
            "ALLOWED_PATHS": "/Users/me/projects"
          }
        }
      }
    }
    Python
    {
      "mcpServers": {
        "filesystem": {
          "command": "python",
          "args": ["-m", "mcp_server_filesystem"],
          "env": {
            "ALLOWED_PATHS": "/Users/me/projects"
          }
        }
      }
    }

    Using MCP Servers in SDK

    import { query } from "@anthropic-ai/claude-agent-sdk";
    
    for await (const message of query({
      prompt: "List files in my project",
      options: {
        mcpServers: {
          "filesystem": {
            command: "npx",
            args: ["@modelcontextprotocol/server-filesystem"],
            env: {
              ALLOWED_PATHS: "/Users/me/projects"
            }
          }
        },
        allowedTools: ["mcp__filesystem__list_files"]
      }
    })) {
      if (message.type === "result" && message.subtype === "success") {
        console.log(message.result);
      }
    }

    Transport Types

    stdio Servers

    External processes communicating via stdin/stdout:

    // .mcp.json configuration
    {
      "mcpServers": {
        "my-tool": {
          "command": "node",
          "args": ["./my-mcp-server.js"],
          "env": {
            "DEBUG": "${DEBUG:-false}"
          }
        }
      }
    }

    HTTP/SSE Servers

    Remote servers with network communication:

    // SSE server configuration
    {
      "mcpServers": {
        "remote-api": {
          "type": "sse",
          "url": "https://api.example.com/mcp/sse",
          "headers": {
            "Authorization": "Bearer ${API_TOKEN}"
          }
        }
      }
    }
    
    // HTTP server configuration
    {
      "mcpServers": {
        "http-service": {
          "type": "http",
          "url": "https://api.example.com/mcp",
          "headers": {
            "X-API-Key": "${API_KEY}"
          }
        }
      }
    }

    SDK MCP Servers

    In-process servers running within your application. For detailed information on creating custom tools, see the Custom Tools guide:

    Resource Management

    MCP servers can expose resources that Claude can list and read:

    import { query } from "@anthropic-ai/claude-agent-sdk";
    
    // List available resources
    for await (const message of query({
      prompt: "What resources are available from the database server?",
      options: {
        mcpServers: {
          "database": {
            command: "npx",
            args: ["@modelcontextprotocol/server-database"]
          }
        },
        allowedTools: ["mcp__list_resources", "mcp__read_resource"]
      }
    })) {
      if (message.type === "result") console.log(message.result);
    }

    Authentication

    Environment Variables

    // .mcp.json with environment variables
    {
      "mcpServers": {
        "secure-api": {
          "type": "sse",
          "url": "https://api.example.com/mcp",
          "headers": {
            "Authorization": "Bearer ${API_TOKEN}",
            "X-API-Key": "${API_KEY:-default-key}"
          }
        }
      }
    }
    
    // Set environment variables
    process.env.API_TOKEN = "your-token";
    process.env.API_KEY = "your-key";

    OAuth2 Authentication

    OAuth2 MCP authentication in-client is not currently supported.

    Error Handling

    Handle MCP connection failures gracefully:

    import { query } from "@anthropic-ai/claude-agent-sdk";
    
    for await (const message of query({
      prompt: "Process data",
      options: {
        mcpServers: {
          "data-processor": dataServer
        }
      }
    })) {
      if (message.type === "system" && message.subtype === "init") {
        // Check MCP server status
        const failedServers = message.mcp_servers.filter(
          s => s.status !== "connected"
        );
        
        if (failedServers.length > 0) {
          console.warn("Failed to connect:", failedServers);
        }
      }
      
      if (message.type === "result" && message.subtype === "error_during_execution") {
        console.error("Execution failed");
      }
    }

    Related Resources

    • Custom Tools Guide - Detailed guide on creating SDK MCP servers
    • TypeScript SDK Reference
    • Python SDK Reference
    • SDK Permissions
    • Common Workflows
    • Overview
    • Configuration
    • Basic Configuration
    • Using MCP Servers in SDK
    • Transport Types
    • stdio Servers
    • HTTP/SSE Servers
    • SDK MCP Servers
    • Resource Management
    • Authentication
    • Environment Variables
    • OAuth2 Authentication
    • Error Handling
    • Related Resources
    © 2025 ANTHROPIC PBC

    Products

    • Claude
    • Claude Code
    • Max plan
    • Team plan
    • Enterprise plan
    • Download app
    • Pricing
    • Log in

    Features

    • Claude and Slack
    • Claude in Excel

    Models

    • Opus
    • Sonnet
    • Haiku

    Solutions

    • AI agents
    • Code modernization
    • Coding
    • Customer support
    • Education
    • Financial services
    • Government
    • Life sciences

    Claude Developer Platform

    • Overview
    • Developer docs
    • Pricing
    • Amazon Bedrock
    • Google Cloud’s Vertex AI
    • Console login

    Learn

    • Blog
    • Catalog
    • Courses
    • Use cases
    • Connectors
    • Customer stories
    • Engineering at Anthropic
    • Events
    • Powered by Claude
    • Service partners
    • Startups program

    Company

    • Anthropic
    • Careers
    • Economic Futures
    • Research
    • News
    • Responsible Scaling Policy
    • Security and compliance
    • Transparency

    Help and security

    • Availability
    • Status
    • Support center

    Terms and policies

    • Privacy policy
    • Responsible disclosure policy
    • Terms of service: Commercial
    • Terms of service: Consumer
    • Usage policy

    Products

    • Claude
    • Claude Code
    • Max plan
    • Team plan
    • Enterprise plan
    • Download app
    • Pricing
    • Log in

    Features

    • Claude and Slack
    • Claude in Excel

    Models

    • Opus
    • Sonnet
    • Haiku

    Solutions

    • AI agents
    • Code modernization
    • Coding
    • Customer support
    • Education
    • Financial services
    • Government
    • Life sciences

    Claude Developer Platform

    • Overview
    • Developer docs
    • Pricing
    • Amazon Bedrock
    • Google Cloud’s Vertex AI
    • Console login

    Learn

    • Blog
    • Catalog
    • Courses
    • Use cases
    • Connectors
    • Customer stories
    • Engineering at Anthropic
    • Events
    • Powered by Claude
    • Service partners
    • Startups program

    Company

    • Anthropic
    • Careers
    • Economic Futures
    • Research
    • News
    • Responsible Scaling Policy
    • Security and compliance
    • Transparency

    Help and security

    • Availability
    • Status
    • Support center

    Terms and policies

    • Privacy policy
    • Responsible disclosure policy
    • Terms of service: Commercial
    • Terms of service: Consumer
    • Usage policy
    © 2025 ANTHROPIC PBC