Loading...
  • 建構
  • 管理
  • 模型與定價
  • 客戶端 SDK
  • API 參考
Search...
⌘K
Log in
存取 GitHub
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
  • 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-7}' \
  --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 儲存庫的工作階段:

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[].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
listed = client.beta.sessions.resources.list(session.id)
repo_resource_id = listed.data[0].id
print(repo_resource_id)  # "sesrsc_01ABC..."

# Rotate the authorization token
client.beta.sessions.resources.update(
    repo_resource_id,
    session_id=session.id,
    authorization_token="ghp_your_new_github_token",
)

建立提取請求

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

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.",
                },
            ],
        },
    ],
)

Was this page helpful?

  • GitHub MCP 和工作階段資源