Was this page helpful?
Anda dapat memasang repositori GitHub ke container sesi Anda dan terhubung ke GitHub MCP untuk membuat pull request.
Repositori GitHub di-cache, sehingga sesi mendatang yang menggunakan repositori yang sama akan dimulai lebih cepat.
Semua permintaan Managed Agents API memerlukan header beta managed-agents-2026-04-01. SDK menetapkan header beta secara otomatis.
Pertama, buat agen yang mendeklarasikan server GitHub MCP. Definisi agen menyimpan URL server tetapi tidak menyimpan token autentikasi:
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
)Kemudian buat sesi yang memasang repositori GitHub:
resources[].authorization_token mengautentikasi operasi clone repositori dan tidak ditampilkan kembali dalam respons API.
Saat menyediakan token GitHub, gunakan izin minimum yang diperlukan:
| Tindakan | Cakupan yang diperlukan |
|---|---|
| Clone repositori privat | repo |
| Membuat PR | repo |
| Membaca isu | repo (privat) atau public_repo |
| Membuat isu | repo (privat) atau public_repo |
Gunakan personal access token berbutir halus dengan izin minimum yang diperlukan. Hindari penggunaan token dengan akses luas ke akun GitHub Anda.
Pasang beberapa repositori dengan menambahkan entri ke array resources:
Setelah sesi dibuat, Anda dapat mencantumkan sumber daya repositorinya dan merotasi token otorisasinya. Setiap sumber daya memiliki id yang dikembalikan pada saat pembuatan sesi (atau melalui resources.list) yang Anda gunakan untuk pembaruan. Repositori dilampirkan selama masa hidup sesi; untuk mengubah repositori mana yang dipasang, buat sesi baru.
Dengan server GitHub MCP, agen dapat membuat branch, melakukan commit perubahan, dan mendorongnya:
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='[
{
"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
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"
}
JSONcurl -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."
}
]
}
]
}
JSON