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?