Managed Agents管理智能体上下文
访问 GitHub
将您的智能体连接到 GitHub 仓库,以进行克隆、读取和创建拉取请求。
您可以将 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 中的所有技能(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",
)创建拉取请求
借助 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?