Loading...
    • 開發者指南
    • API 參考
    • MCP
    • 資源
    • 發行說明
    Search...
    ⌘K

    第一步

    Claude 介紹快速入門

    模型與定價

    模型概覽選擇模型Claude 4.5 的新功能遷移到 Claude 4.5模型棄用定價

    使用 Claude 建構

    功能概覽使用 Messages API上下文視窗提示詞最佳實踐

    功能

    提示詞快取上下文編輯延伸思考串流訊息批次處理引用多語言支援Token 計數嵌入向量視覺PDF 支援Files API搜尋結果Google Sheets 附加元件

    工具

    概述如何實現工具使用代幣高效工具使用細粒度工具串流Bash 工具代碼執行工具電腦使用工具文字編輯工具網頁擷取工具網路搜尋工具記憶工具

    代理技能

    概述在 API 中開始使用 Agent Skills技能編寫最佳實踐使用 Agent Skills 與 API

    Agent SDK

    概述Agent SDK 參考 - TypeScriptPython SDK

    指南

    串流輸入處理權限會話管理託管 Agent SDK修改系統提示SDK 中的 MCP自訂工具SDK 中的子代理SDK 中的斜線命令SDK 中的代理技能追蹤成本和使用量待辦事項清單SDK 中的外掛程式

    API 中的 MCP

    MCP 連接器遠端 MCP 伺服器

    Claude 在第三方平台上

    Amazon BedrockVertex AI

    提示工程

    概述提示詞生成器使用提示模板提示詞改進器保持清晰和直接使用範例(多樣提示)讓 Claude 思考(思維鏈)使用 XML 標籤給 Claude 分配角色(系統提示詞)預填 Claude 的回應串接複雜提示長文本技巧延伸思考技巧

    測試與評估

    定義成功標準開發測試案例使用評估工具降低延遲

    加強防護措施

    減少幻覺提高輸出一致性防範越獄handle-streaming-refusals減少提示詞洩漏保持 Claude 的角色特性

    管理和監控

    Admin API 概述使用量和成本 APIClaude Code 分析 API
    Console
    API 中的 MCP

    MCP 連接器

    Claude 的模型上下文協議 (MCP) 連接器功能讓您可以直接從 Messages API 連接到遠端 MCP 伺服器,無需單獨的 MCP 客戶端。

    Claude 的模型上下文協議 (MCP) 連接器功能讓您可以直接從 Messages API 連接到遠端 MCP 伺服器,無需單獨的 MCP 客戶端。

    此功能需要 beta 標頭:"anthropic-beta": "mcp-client-2025-04-04"

    主要功能

    • 直接 API 整合:無需實作 MCP 客戶端即可連接到 MCP 伺服器
    • 工具呼叫支援:透過 Messages API 存取 MCP 工具
    • OAuth 驗證:支援 OAuth Bearer 權杖用於已驗證的伺服器
    • 多個伺服器:在單一請求中連接到多個 MCP 伺服器

    限制

    • 在 MCP 規範 的功能集中,目前僅支援工具呼叫。
    • 伺服器必須透過 HTTP 公開暴露(支援 Streamable HTTP 和 SSE 傳輸)。本地 STDIO 伺服器無法直接連接。
    • MCP 連接器目前不支援 Amazon Bedrock 和 Google Vertex。

    在 Messages API 中使用 MCP 連接器

    要連接到遠端 MCP 伺服器,請在您的 Messages API 請求中包含 mcp_servers 參數:

    cURL
    curl https://api.anthropic.com/v1/messages \
      -H "Content-Type: application/json" \
      -H "X-API-Key: $ANTHROPIC_API_KEY" \
      -H "anthropic-version: 2023-06-01" \
      -H "anthropic-beta: mcp-client-2025-04-04" \
      -d '{
        "model": "claude-sonnet-4-5",
        "max_tokens": 1000,
        "messages": [{"role": "user", "content": "What tools do you have available?"}],
        "mcp_servers": [
          {
            "type": "url",
            "url": "https://example-server.modelcontextprotocol.io/sse",
            "name": "example-mcp",
            "authorization_token": "YOUR_TOKEN"
          }
        ]
      }'
    TypeScript
    import { Anthropic } from '@anthropic-ai/sdk';
    
    const anthropic = new Anthropic();
    
    const response = await anthropic.beta.messages.create({
      model: "claude-sonnet-4-5",
      max_tokens: 1000,
      messages: [
        {
          role: "user",
          content: "What tools do you have available?",
        },
      ],
      mcp_servers: [
        {
          type: "url",
          url: "https://example-server.modelcontextprotocol.io/sse",
          name: "example-mcp",
          authorization_token: "YOUR_TOKEN",
        },
      ],
      betas: ["mcp-client-2025-04-04"],
    });
    Python
    import anthropic
    
    client = anthropic.Anthropic()
    
    response = client.beta.messages.create(
        model="claude-sonnet-4-5",
        max_tokens=1000,
        messages=[{
            "role": "user",
            "content": "What tools do you have available?"
        }],
        mcp_servers=[{
            "type": "url",
            "url": "https://mcp.example.com/sse",
            "name": "example-mcp",
            "authorization_token": "YOUR_TOKEN"
        }],
        betas=["mcp-client-2025-04-04"]
    )

    MCP 伺服器配置

    mcp_servers 陣列中的每個 MCP 伺服器支援以下配置:

    {
      "type": "url",
      "url": "https://example-server.modelcontextprotocol.io/sse",
      "name": "example-mcp",
      "tool_configuration": {
        "enabled": true,
        "allowed_tools": ["example_tool_1", "example_tool_2"]
      },
      "authorization_token": "YOUR_TOKEN"
    }

    欄位說明

    屬性類型必需說明
    typestring是目前僅支援 "url"
    urlstring是MCP 伺服器的 URL。必須以 https:// 開頭
    namestring是此 MCP 伺服器的唯一識別符。它將在 mcp_tool_call 區塊中用於識別伺服器並向模型消除工具歧義。
    tool_configurationobject否配置工具使用
    tool_configuration.enabledboolean否是否啟用來自此伺服器的工具(預設:true)
    tool_configuration.allowed_toolsarray否限制允許的工具清單(預設情況下,允許所有工具)
    authorization_tokenstring否如果 MCP 伺服器需要,則為 OAuth 授權權杖。請參閱 MCP 規範。

    回應內容類型

    當 Claude 使用 MCP 工具時,回應將包含兩種新的內容區塊類型:

    MCP 工具使用區塊

    {
      "type": "mcp_tool_use",
      "id": "mcptoolu_014Q35RayjACSWkSj4X2yov1",
      "name": "echo",
      "server_name": "example-mcp",
      "input": { "param1": "value1", "param2": "value2" }
    }

    MCP 工具結果區塊

    {
      "type": "mcp_tool_result",
      "tool_use_id": "mcptoolu_014Q35RayjACSWkSj4X2yov1",
      "is_error": false,
      "content": [
        {
          "type": "text",
          "text": "Hello"
        }
      ]
    }

    多個 MCP 伺服器

    您可以透過在 mcp_servers 陣列中包含多個物件來連接到多個 MCP 伺服器:

    {
      "model": "claude-sonnet-4-5",
      "max_tokens": 1000,
      "messages": [
        {
          "role": "user",
          "content": "Use tools from both mcp-server-1 and mcp-server-2 to complete this task"
        }
      ],
      "mcp_servers": [
        {
          "type": "url",
          "url": "https://mcp.example1.com/sse",
          "name": "mcp-server-1",
          "authorization_token": "TOKEN1"
        },
        {
          "type": "url",
          "url": "https://mcp.example2.com/sse",
          "name": "mcp-server-2",
          "authorization_token": "TOKEN2"
        }
      ]
    }

    驗證

    對於需要 OAuth 驗證的 MCP 伺服器,您需要取得存取權杖。MCP 連接器 beta 版支援在 MCP 伺服器定義中傳遞 authorization_token 參數。 API 消費者需要處理 OAuth 流程並在進行 API 呼叫之前取得存取權杖,以及根據需要刷新權杖。

    取得用於測試的存取權杖

    MCP 檢查器可以引導您完成取得用於測試目的的存取權杖的過程。

    1. 使用以下命令執行檢查器。您需要在機器上安裝 Node.js。

      npx @modelcontextprotocol/inspector
    2. 在左側邊欄中,對於「傳輸類型」,選擇「SSE」或「Streamable HTTP」。

    3. 輸入 MCP 伺服器的 URL。

    4. 在右側區域中,在「需要配置驗證?」後點擊「開啟驗證設定」按鈕。

    5. 點擊「快速 OAuth 流程」並在 OAuth 畫面上授權。

    6. 按照檢查器「OAuth 流程進度」部分中的步驟,點擊「繼續」直到您到達「驗證完成」。

    7. 複製 access_token 值。

    8. 將其貼到您的 MCP 伺服器配置中的 authorization_token 欄位。

    使用存取權杖

    一旦您使用上述任一 OAuth 流程取得存取權杖,您就可以在 MCP 伺服器配置中使用它:

    {
      "mcp_servers": [
        {
          "type": "url",
          "url": "https://example-server.modelcontextprotocol.io/sse",
          "name": "authenticated-server",
          "authorization_token": "YOUR_ACCESS_TOKEN_HERE"
        }
      ]
    }

    有關 OAuth 流程的詳細說明,請參閱 MCP 規範中的授權部分。

    • 在 Messages API 中使用 MCP 連接器
    • MCP 伺服器配置
    • MCP 工具使用區塊
    • MCP 工具結果區塊
    • 多個 MCP 伺服器
    © 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