Loading...
    • 開發者指南
    • API 參考
    • MCP
    • 資源
    • 發行說明
    Search...
    ⌘K
    開始使用
    Claude 簡介快速開始
    模型與定價
    模型概覽選擇模型Claude 4.5 新功能遷移至 Claude 4.5模型棄用定價
    使用 Claude 構建
    功能概覽使用 Messages API上下文窗口提示詞最佳實踐
    功能
    提示詞快取上下文編輯擴展思考努力串流消息批次處理引用多語言支援Token 計數嵌入視覺PDF 支援Files API搜尋結果結構化輸出
    工具
    概覽如何實現工具使用細粒度工具串流Bash 工具代碼執行工具程式化工具調用計算機使用工具文字編輯器工具網頁擷取工具網頁搜尋工具記憶體工具工具搜尋工具
    Agent Skills
    概覽快速開始最佳實踐使用 API 的 Skills
    Agent SDK
    概覽快速開始TypeScript SDKTypeScript V2 (預覽)Python SDK遷移指南
    串流輸入處理權限使用鉤子控制執行會話管理檔案檢查點SDK 中的結構化輸出託管 Agent SDK安全部署 AI agents修改系統提示詞SDK 中的 MCP自訂工具SDK 中的子 agentsSDK 中的斜線命令SDK 中的 Agent Skills追蹤成本和使用量待辦事項清單SDK 中的外掛程式
    API 中的 MCP
    MCP 連接器遠端 MCP 伺服器
    第三方平台上的 Claude
    Amazon BedrockMicrosoft FoundryVertex AI
    提示詞工程
    概覽提示詞生成器使用提示詞範本提示詞改進器清晰直接使用範例 (多次提示)讓 Claude 思考 (CoT)使用 XML 標籤給 Claude 一個角色 (系統提示詞)預填 Claude 的回應鏈接複雜提示詞長上下文提示擴展思考提示
    測試與評估
    定義成功標準開發測試案例使用評估工具降低延遲
    加強防護欄
    減少幻覺增加輸出一致性緩解越獄串流拒絕減少提示詞洩露保持 Claude 的角色
    管理和監控
    Admin API 概覽使用量和成本 APIClaude Code Analytics API
    Console
    Log in
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...

    Solutions

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

    Partners

    • Amazon Bedrock
    • Google Cloud's Vertex AI

    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

    Learn

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

    Help and security

    • Availability
    • Status
    • Support
    • Discord

    Terms and policies

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

    SDK 中的 MCP

    使用模型上下文協議伺服器透過自訂工具擴展 Claude Code

    概述

    模型上下文協議 (MCP) 伺服器透過自訂工具和功能擴展 Claude Code。MCP 可以作為外部程序運行、透過 HTTP/SSE 連接,或直接在您的 SDK 應用程式中執行。

    配置

    基本配置

    在專案根目錄的 .mcp.json 中配置 MCP 伺服器:

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

    在 SDK 中使用 MCP 伺服器

    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);
      }
    }

    傳輸類型

    stdio 伺服器

    透過 stdin/stdout 通訊的外部程序:

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

    HTTP/SSE 伺服器

    具有網路通訊的遠端伺服器:

    // 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 伺服器

    在您的應用程式中運行的程序內伺服器。有關建立自訂工具的詳細資訊,請參閱自訂工具指南:

    資源管理

    MCP 伺服器可以公開 Claude 可以列出和讀取的資源:

    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);
    }

    身份驗證

    環境變數

    // .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 身份驗證

    目前不支援客戶端內的 OAuth2 MCP 身份驗證。

    錯誤處理

    優雅地處理 MCP 連接失敗:

    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");
      }
    }

    相關資源

    • 自訂工具指南 - 建立 SDK MCP 伺服器的詳細指南
    • TypeScript SDK 參考
    • Python SDK 參考
    • SDK 權限
    • 常見工作流程
    • 在 SDK 中使用 MCP 伺服器
    • stdio 伺服器
    • HTTP/SSE 伺服器
    • SDK MCP 伺服器
    • OAuth2 身份驗證