Loading...
    • 开发者指南
    • API 参考
    • MCP
    • 资源
    • 发布说明
    Search...
    ⌘K

    第一步

    Claude 简介快速开始

    模型与定价

    模型概览选择模型Claude 4.5 的新功能迁移到 Claude 4.5模型弃用定价

    使用 Claude 构建

    功能概览使用 Messages API上下文窗口提示词最佳实践

    功能

    提示词缓存上下文编辑扩展思考流式消息批处理引用多语言支持Token 计数嵌入向量视觉PDF 支持Files API搜索结果Google Sheets 插件

    工具

    概述如何实现工具使用令牌高效的工具使用细粒度工具流式传输Bash 工具代码执行工具计算机使用工具文本编辑器工具Web fetch 工具网络搜索工具记忆工具

    代理技能

    概述在 API 中开始使用 Agent Skills技能创作最佳实践通过 API 使用 Agent Skills

    Agent SDK

    概览TypeScript SDKPython 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