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