Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Was this page helpful?
您可以將 GitHub 儲存庫掛載到您的工作階段容器,並連接到 GitHub MCP 以進行提取請求。
GitHub 儲存庫已快取,因此使用相同儲存庫的未來工作階段啟動速度更快。
所有 Managed Agents API 請求都需要 managed-agents-2026-04-01 beta 標頭。SDK 會自動設定 beta 標頭。
首先,建立一個宣告 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 |
| 建立 PR | repo |
| 讀取議題 | 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"
}
JSONcurl -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