Loading...
    • 构建
    • 管理
    • 模型与定价
    • 客户端 SDK
    • API 参考
    Search...
    ⌘K
    入门步骤
    Claude 简介快速入门
    使用 Claude 构建
    功能概览使用 Messages API处理停止原因
    模型能力
    扩展思考自适应思考努力程度快速模式(测试版:研究预览)结构化输出引用流式消息批量处理搜索结果流式拒绝多语言支持嵌入
    工具
    概览工具使用原理网页搜索工具网页抓取工具代码执行工具记忆工具Bash 工具计算机使用工具文本编辑器工具
    工具基础设施
    工具搜索程序化工具调用细粒度工具流式传输
    上下文管理
    上下文窗口压缩上下文编辑提示词缓存Token 计数
    文件处理
    Files APIPDF 支持图像与视觉
    技能
    概览快速入门最佳实践企业级技能API 中的技能
    MCP
    远程 MCP 服务器MCP 连接器
    提示词工程
    概览提示词最佳实践Console 提示词工具
    测试与评估
    定义成功标准并构建评估在 Console 中使用评估工具降低延迟
    加强安全护栏
    减少幻觉提高输出一致性防范越狱减少提示词泄露
    资源
    术语表
    发布说明
    Claude Platform
    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
    将工作委托给智能体

    访问 GitHub

    将你的代理连接到 GitHub 仓库,用于克隆、读取和创建拉取请求。

    你可以将 GitHub 仓库挂载到你的会话容器中,并连接到 GitHub MCP 以进行拉取请求。

    GitHub 仓库被缓存,因此使用相同仓库的后续会话启动速度更快。

    所有 Managed Agents API 请求都需要 managed-agents-2026-04-01 beta 头。SDK 会自动设置 beta 头。

    GitHub MCP 和会话资源

    首先,创建一个声明 GitHub MCP 服务器的代理。代理定义包含服务器 URL,但不包含身份验证令牌:

    agent_id=$(curl -fsS https://api.anthropic.com/v1/agents \
      -H "x-api-key: $ANTHROPIC_API_KEY" \
      -H "anthropic-version: 2023-06-01" \
      -H "anthropic-beta: managed-agents-2026-04-01" \
      -H "content-type: application/json" \
      --data @- <<JSON | jq -r '.id'
    {
      "name": "Code Reviewer",
      "model": "claude-sonnet-4-6",
      "system": "You are a code review assistant with access to GitHub.",
      "mcp_servers": [
        {
          "type": "url",
          "name": "github",
          "url": "https://api.githubcopilot.com/mcp/"
        }
      ],
      "tools": [
        {"type": "agent_toolset_20260401"},
        {
          "type": "mcp_toolset",
          "mcp_server_name": "github"
        }
      ]
    }
    JSON
    )

    然后创建一个挂载 GitHub 仓库的会话:

    session_id=$(curl -fsS https://api.anthropic.com/v1/sessions \
      -H "x-api-key: $ANTHROPIC_API_KEY" \
      -H "anthropic-version: 2023-06-01" \
      -H "anthropic-beta: managed-agents-2026-04-01" \
      -H "content-type: application/json" \
      --data @- <<JSON | jq -r '.id'
    {
      "agent": "$agent_id",
      "environment_id": "$environment_id",
      "resources": [
        {
          "type": "github_repository",
          "url": "https://github.com/org/repo",
          "mount_path": "/workspace/repo",
          "authorization_token": "ghp_your_github_token"
        }
      ]
    }
    JSON
    )

    resources[].authorization_token 对仓库克隆操作进行身份验证,不会在 API 响应中回显。

    令牌权限

    提供 GitHub 令牌时,使用最少所需的权限:

    操作所需作用域
    克隆私有仓库repo
    创建 PRrepo
    读取问题repo(私有)或 public_repo
    创建问题repo(私有)或 public_repo

    使用具有最少所需权限的细粒度个人访问令牌。避免使用对你的 GitHub 账户具有广泛访问权限的令牌。

    多个仓库

    通过向 resources 数组添加条目来挂载多个仓库:

    resources='[
      {
        "type": "github_repository",
        "url": "https://github.com/org/frontend",
        "mount_path": "/workspace/frontend",
        "authorization_token": "ghp_your_github_token"
      },
      {
        "type": "github_repository",
        "url": "https://github.com/org/backend",
        "mount_path": "/workspace/backend",
        "authorization_token": "ghp_your_github_token"
      }
    ]'

    在运行的会话上管理仓库

    创建会话后,你可以列出其仓库资源并轮换其授权令牌。每个资源都有一个在会话创建时返回的 id(或通过 resources.list),你可以用它进行更新。仓库在会话的生命周期内被附加;要更改挂载的仓库,请创建一个新会话。

    # List resources on the session
    repo_resource_id=$(curl -fsS "https://api.anthropic.com/v1/sessions/$session_id/resources" \
      -H "x-api-key: $ANTHROPIC_API_KEY" \
      -H "anthropic-version: 2023-06-01" \
      -H "anthropic-beta: managed-agents-2026-04-01" \
      -H "content-type: application/json" | jq -r '.data[0].id')
    echo "$repo_resource_id"  # "sesrsc_01ABC..."
    
    # Rotate the authorization token
    curl -fsS "https://api.anthropic.com/v1/sessions/$session_id/resources/$repo_resource_id" \
    # ...
      -o /dev/null \
      --data @- <<JSON
    {
      "authorization_token": "ghp_your_new_github_token"
    }
    JSON

    创建拉取请求

    使用 GitHub MCP 服务器,代理可以创建分支、提交更改并推送它们:

    curl -fsS "https://api.anthropic.com/v1/sessions/$session_id/events" \
      -H "x-api-key: $ANTHROPIC_API_KEY" \
      -H "anthropic-version: 2023-06-01" \
      -H "anthropic-beta: managed-agents-2026-04-01" \
      -H "content-type: application/json" \
      -o /dev/null \
      --data @- <<JSON
    {
      "events": [
        {
          "type": "user.message",
          "content": [
            {
              "type": "text",
              "text": "Fix the type error in src/utils.ts, commit it to a new branch, and push it."
            }
          ]
        }
      ]
    }
    JSON

    Was this page helpful?

    • GitHub MCP 和会话资源