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 儲存庫以進行複製、讀取和建立提取請求。

    Was this page helpful?

    • GitHub MCP 和工作階段資源

    您可以將 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 儲存庫的工作階段:

    resources[].authorization_token 驗證儲存庫複製操作,不會在 API 回應中回顯。

    令牌權限

    提供 GitHub 令牌時,請使用最少必需的權限:

    動作必需的範圍
    複製私人儲存庫repo
    建立 PRrepo
    讀取議題repo(私人)或 public_repo
    建立議題repo(私人)或 public_repo

    使用具有最少必需權限的細粒度個人存取令牌。避免使用對您的 GitHub 帳戶具有廣泛存取權限的令牌。

    多個儲存庫

    透過將項目新增到 resources 陣列來掛載多個儲存庫:

    在執行中的工作階段上管理儲存庫

    建立工作階段後,您可以列出其儲存庫資源並輪換其授權令牌。每個資源都有一個在工作階段建立時返回的 id(或透過 resources.list),您可以用它進行更新。儲存庫在工作階段的生命週期內附加;若要變更掛載的儲存庫,請建立新的工作階段。

    建立提取請求

    使用 GitHub MCP 伺服器,代理可以建立分支、提交變更並推送它們:

    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='[
      {
        "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"
      }
    ]'
    # 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
    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