GitHubリポジトリをセッションコンテナにマウントし、GitHubMCPに接続してプルリクエストを作成できます。
GitHubリポジトリはキャッシュされるため、同じリポジトリを使用する今後のセッションはより高速に開始されます。
すべてのManaged Agents APIリクエストには、managed-agents-2026-04-01ベータヘッダーが必要です。SDKはベータヘッダーを自動的に設定します。
まず、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リポジトリをマウントするセッションを作成します。
resources[].authorization_tokenはリポジトリクローン操作を認証し、APIレスポンスではエコーバックされません。
GitHubトークンを提供する場合は、必要最小限の権限を使用してください。
| アクション | 必要なスコープ |
|---|---|
| プライベートリポジトリのクローン | repo |
| プルリクエストの作成 | repo |
| イシューの読み取り | repo(プライベート)またはpublic_repo |
| イシューの作成 | repo(プライベート)またはpublic_repo |
最小限の必要な権限を持つきめ細かいパーソナルアクセストークンを使用してください。GitHubアカウントへの広範なアクセス権を持つトークンの使用は避けてください。
resources配列にエントリを追加して、複数のリポジトリをマウントします。
セッションが作成された後、そのリポジトリリソースをリストし、認証トークンをローテーションできます。各リソースには、セッション作成時(またはresources.list経由)に返されるidがあり、これを更新に使用します。リポジトリはセッションの存続期間中アタッチされます。マウントされるリポジトリを変更するには、新しいセッションを作成してください。
GitHub MCPサーバーを使用すると、エージェントはブランチを作成し、変更をコミットしてプッシュできます。
Was this page helpful?
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 = [
{
"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",
},
]# 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",
)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.",
},
],
},
],
)