Managed Agents管理代理上下文
存取 GitHub
將您的代理連接到 GitHub 儲存庫,以進行複製、讀取及建立 pull request。
您可以將 GitHub 儲存庫掛載到您的工作階段沙箱中,並連接到 GitHub MCP 以建立 pull request。
GitHub 儲存庫會被快取,因此未來使用相同儲存庫的工作階段啟動速度會更快。
GitHub MCP 與工作階段資源
首先,建立一個宣告 GitHub MCP 伺服器的代理。代理定義中包含伺服器 URL,但不包含驗證權杖:
ant apply code-reviewer.mdcode-reviewer.md
---
name: Code Reviewer
model: claude-opus-5-5
mcp_servers:
- type: url
name: github
url: https://api.githubcopilot.com/mcp/
tools:
- type: agent_toolset_20260401
- type: mcp_toolset
mcp_server_name: github
---
You are a code review assistant with access to 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",
},
],
)github_repository 資源接受以下欄位:
| 欄位 | 說明 |
|---|---|
type | 必填。必須為 "github_repository"。 |
url | 必填。儲存庫的 HTTPS URL,格式為 https://github.com/<owner>/<repo>,不含 .git 後綴。其他格式(包括 SSH URL)會被拒絕並回傳 invalid_request_error。 |
authorization_token | 必填。用於複製儲存庫的 GitHub 權杖。此權杖不會在 API 回應中回傳。請參閱權杖權限。 |
mount_path | 選填。要將儲存庫複製到 /workspace 下的目錄。預設為 /workspace/<repo-name>。 |
checkout | 選填。要簽出的分支({"type": "branch", "name": "main"})或提交({"type": "commit", "sha": "..."})。預設為儲存庫的預設分支。 |
掛載儲存庫時也會載入儲存於其根目錄 .claude/skills 中的任何技能。技能在每個工作階段中只會探索一次,依據工作階段啟動時所簽出的儲存庫狀態。請參閱從 GitHub 儲存庫載入技能。
權杖權限
提供 GitHub 權杖時,請使用所需的最小權限:
| 動作 | 所需範圍 |
|---|---|
| 複製私有儲存庫 | repo |
| 建立 PR | repo |
| 讀取 issue | repo(私有)或 public_repo |
| 建立 issue | 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,您可使用它來進行更新。儲存庫會在工作階段的整個生命週期內保持附加狀態;若要變更掛載的儲存庫,請建立新的工作階段。
# 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",
)建立 pull request
透過 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?