Claude Platform Docs
  • 消息
  • 托管智能体
  • 管理

Search...
⌘K
第一步
概览快速入门在控制台中构建原型
定义您的智能体
智能体设置工具MCP 连接器权限策略智能体技能
配置智能体环境
云环境设置云沙箱参考
将工作委派给智能体
启动会话会话操作会话事件流订阅 Webhook定义结果使用保管库进行身份验证
管理智能体上下文
访问 GitHub附加和下载文件
高级编排
多智能体会话计划部署
参考
托管智能体参考
处理文件
Files APIPDF 支持
技能
概览最佳实践企业技能
MCP
远程 MCP 服务器
云平台上的 Claude
AWS 上的 Claude Platform

Log in
访问 GitHub
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Claude Platform Docs

Solutions

  • AI agents
  • Code modernization
  • Coding
  • Customer support
  • Education
  • Financial services
  • Government
  • Life sciences

Partners

  • Claude on AWS
  • Claude on Google Cloud

Learn

  • Blog
  • Courses
  • Use cases
  • Connectors
  • Customer stories
  • Engineering at Anthropic
  • Events
  • Powered by Claude
  • Service partners
  • Startups program

Company

  • Anthropic
  • Careers
  • Economic Futures
  • Research
  • News
  • Responsible Scaling Policy
  • Security and compliance
  • Transparency

Learn

  • Blog
  • Courses
  • Use cases
  • Connectors
  • Customer stories
  • Engineering at Anthropic
  • Events
  • Powered by Claude
  • Service partners
  • Startups program

Help and security

  • Availability
  • Status
  • Support
  • Discord

Terms and policies

  • Privacy policy
  • Responsible disclosure policy
  • Terms of service: Commercial
  • Terms of service: Consumer
  • Usage policy
托管智能体/管理智能体上下文

访问 GitHub

将您的智能体连接到 GitHub 代码仓库,以进行克隆、读取和创建拉取请求。

您可以将 GitHub 代码仓库挂载到会话沙箱中,并连接到 GitHub MCP 以创建拉取请求。

GitHub 代码仓库会被缓存,因此使用相同代码仓库的后续会话启动速度会更快。



所有 Managed Agents API 请求都需要 managed-agents-2026-04-01 beta 标头。SDK 会自动设置该 beta 标头。

GitHub MCP 和会话资源

首先,创建一个声明 GitHub MCP 服务器的智能体。智能体定义中包含服务器 URL,但不包含身份验证令牌:

AGENT_ID=$(ant beta:agents create \
  --name "Code Reviewer" \
  --model '{id: claude-opus-4-8}' \
  --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
创建 PRrepo
读取 issuerepo(私有)或 public_repo
创建 issuerepo(私有)或 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",
    },
]

管理运行中会话的代码仓库

会话创建后,您可以列出其代码仓库资源并轮换其授权令牌。每个资源都有一个在会话创建时(或通过 resources.list)返回的 id,您可以使用它进行更新。代码仓库在会话的整个生命周期内保持挂载状态;如需更改挂载的代码仓库,请创建新会话。

# 列出会话上的资源
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.",
                },
            ],
        },
    ],
)

后续步骤


会话事件流

在智能体打开拉取请求时流式传输事件并引导智能体


MCP 连接器

连接更多 MCP 服务器,为智能体提供额外的工具


添加文件

在沙箱中与代码仓库一起挂载文件

Was this page helpful?

  • GitHub MCP 和会话资源
  • 令牌权限
  • 多个代码仓库
  • 管理运行中会话的代码仓库
  • 创建拉取请求
  • 后续步骤