Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
您可以將 GitHub 儲存庫掛載到您的工作階段沙箱,並連接到 GitHub MCP 以建立 pull request。
GitHub 儲存庫會被快取,因此使用相同儲存庫的後續工作階段可以更快啟動。
首先,建立一個宣告 GitHub MCP 伺服器的代理程式。代理程式定義包含伺服器 URL,但不包含驗證權杖:
AGENT_ID=$(ant beta:agents create --transform id --raw-output < code-reviewer.agent.yaml)name: Code Reviewer
model:
id: claude-opus-5
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接著建立一個掛載 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 回應中回傳。
掛載儲存庫時,也會載入儲存在其根目錄 .claude/skills 資料夾中的任何技能。技能在每個工作階段中僅探索一次,依據工作階段啟動時所簽出的儲存庫狀態。請參閱從 GitHub 儲存庫載入技能。
提供 GitHub 權杖時,請使用最低必要權限:
| 動作 | 所需範圍 |
|---|---|
| 複製私人儲存庫 | repo |
| 建立 PR | repo |
| 讀取議題 | repo(私人)或 public_repo |
| 建立議題 | repo(私人)或 public_repo |
透過在 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",
},
]工作階段建立後,您可以列出其儲存庫資源並輪替其授權權杖。每個資源都有一個在工作階段建立時(或透過 resources.list)回傳的 id,您可以使用它來進行更新。儲存庫在工作階段的整個生命週期內都會保持掛載狀態;若要變更掛載的儲存庫,請建立新的工作階段。
# 列出 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",
)透過 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?