Claude Platform Docs
  • Messages
  • 託管代理
  • 管理

Search...
⌘K
第一步
概覽快速入門在 Console 中建立原型
定義您的代理
代理設定工具MCP 連接器權限政策代理技能
設定代理環境
雲端環境設定雲端沙箱參考
將工作委派給您的代理
啟動工作階段工作階段操作工作階段事件串流訂閱 Webhook定義結果使用保管庫進行驗證
管理代理上下文
存取 GitHub附加與下載檔案
進階協調
多代理工作階段排程部署
參考
託管代理參考
處理檔案
Files APIPDF 支援
技能
概覽最佳實踐企業技能
MCP
遠端 MCP 伺服器
雲端平台上的 Claude
AWS 上的 Claude Platform

Log in
存取 GitHub
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Claude Platform Docs

Solutions

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

Partners

  • Claude on AWS
  • Claude on Google Cloud

Learn

  • Blog
  • 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
  • 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=$(ant beta:agents create \
  --name "Code Reviewer" \
  --model '{id: claude-opus-4-8}' \
  --system "You are a code review assistant with access to GitHub." \
  --mcp-server '{type: url, name: github, url: https://api.githubcopilot.com/mcp/}' \
  --tool '{type: agent_toolset_20260401}' \
  --tool '{type: mcp_toolset, mcp_server_name: github}' \
  --transform id --raw-output)

接著建立一個掛載 GitHub 儲存庫的工作階段:

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

權杖權限

提供 GitHub 權杖時,請使用最低必要權限:

操作所需範圍
複製私有儲存庫repo
建立 PRrepo
讀取議題repo(私有)或 public_repo
建立議題repo(私有)或 public_repo


請使用具有最低必要權限的細粒度個人存取權杖。避免使用對您的 GitHub 帳戶具有廣泛存取權限的權杖。

多個儲存庫

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

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

工作階段建立後,您可以列出其儲存庫資源並輪替其授權權杖。每個資源都有一個在工作階段建立時(或透過 resources.list)回傳的 id,您可以使用它來進行更新。儲存庫在工作階段的整個生命週期內都會保持掛載狀態;若要變更掛載的儲存庫,請建立新的工作階段。

建立拉取請求

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

後續步驟


工作階段事件串流

在代理程式開啟拉取請求時串流事件並引導代理程式


MCP 連接器

連接更多 MCP 伺服器,為代理程式提供額外的工具


新增檔案

在沙箱中與您的儲存庫一起掛載檔案

Was this page helpful?

  • GitHub MCP 與工作階段資源
  • 權杖權限
  • 多個儲存庫
  • 管理執行中工作階段的儲存庫
  • 建立拉取請求
  • 後續步驟
session = client.beta.sessions.create(
    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",
        },
    ],
)
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",
    },
]
# 列出 session 上的資源
listed = client.beta.sessions.resources.list(session.id)
repo_resource_id = listed.data[0].id
print(repo_resource_id)  # "sesrsc_01ABC..."

# 輪替授權權杖
client.beta.sessions.resources.update(
    repo_resource_id,
    session_id=session.id,
    authorization_token="ghp_your_new_github_token",
)
client.beta.sessions.events.send(
    session.id,
    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.",
                },
            ],
        },
    ],
)