GitHubリポジトリをセッションコンテナにマウントし、GitHubMCPに接続してプルリクエストを作成できます。
GitHubリポジトリはキャッシュされるため、同じリポジトリを使用する今後のセッションはより高速に開始されます。
すべてのManaged Agents APIリクエストには、managed-agents-2026-04-01ベータヘッダーが必要です。SDKはベータヘッダーを自動的に設定します。
まず、GitHub MCPサーバーを宣言するエージェントを作成します。エージェント定義はサーバーURLを保持しますが、認証トークンは保持しません。
agent_id=$(curl -fsS https://api.anthropic.com/v1/agents \
-H "x-api-key: $ANTHROPIC_API_KEY" \
-H "anthropic-version: 2023-06-01" \
-H "anthropic-beta: managed-agents-2026-04-01" \
-H "content-type: application/json" \
--data @- <<JSON | jq -r '.id'
{
"name": "Code Reviewer",
"model": "claude-sonnet-4-6",
"system": "You are a code review assistant with access to GitHub.",
"mcp_servers": [
{
"type": "url",
"name": "github",
"url": "https://api.githubcopilot.com/mcp/"
}
],
"tools": [
{"type": "agent_toolset_20260401"},
{
"type": "mcp_toolset",
"mcp_server_name": "github"
}
]
}
JSON
)次に、GitHubリポジトリをマウントするセッションを作成します。
session_id=$(curl -fsS https://api.anthropic.com/v1/sessions \
-H "x-api-key: $ANTHROPIC_API_KEY" \
-H "anthropic-version: 2023-06-01" \
-H "anthropic-beta: managed-agents-2026-04-01" \
-H "content-type: application/json" \
--data @- <<JSON | jq -r '.id'
{
"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"
}
]
}
JSON
)resources[].authorization_tokenはリポジトリクローン操作を認証し、APIレスポンスではエコーバックされません。
GitHubトークンを提供する場合は、必要最小限の権限を使用してください。
| アクション | 必要なスコープ |
|---|---|
| プライベートリポジトリのクローン | repo |
| プルリクエストの作成 | 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"
}
]'セッションが作成された後、そのリポジトリリソースをリストし、認証トークンをローテーションできます。各リソースには、セッション作成時(またはresources.list経由)に返されるidがあり、これを更新に使用します。リポジトリはセッションの存続期間中アタッチされます。マウントされるリポジトリを変更するには、新しいセッションを作成してください。
# List resources on the session
repo_resource_id=$(curl -fsS "https://api.anthropic.com/v1/sessions/$session_id/resources" \
-H "x-api-key: $ANTHROPIC_API_KEY" \
-H "anthropic-version: 2023-06-01" \
-H "anthropic-beta: managed-agents-2026-04-01" \
-H "content-type: application/json" | jq -r '.data[0].id')
echo "$repo_resource_id" # "sesrsc_01ABC..."
# Rotate the authorization token
curl -fsS "https://api.anthropic.com/v1/sessions/$session_id/resources/$repo_resource_id" \
-o /dev/null \
--data @- <<JSON
{
"authorization_token": "ghp_your_new_github_token"
}
JSONGitHub MCPサーバーを使用すると、エージェントはブランチを作成し、変更をコミットしてプッシュできます。
curl -fsS "https://api.anthropic.com/v1/sessions/$session_id/events" \
-H "x-api-key: $ANTHROPIC_API_KEY" \
-H "anthropic-version: 2023-06-01" \
-H "anthropic-beta: managed-agents-2026-04-01" \
-H "content-type: application/json" \
-o /dev/null \
--data @- <<JSON
{
"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."
}
]
}
]
}
JSONWas this page helpful?