Was this page helpful?
Puoi montare un repository GitHub nel container della tua sessione e connetterti al GitHub MCP per creare pull request.
I repository GitHub vengono memorizzati nella cache, quindi le sessioni future che utilizzano lo stesso repository si avviano più velocemente.
Tutte le richieste all'API Managed Agents richiedono l'intestazione beta managed-agents-2026-04-01. L'SDK imposta automaticamente l'intestazione beta.
Prima, crea un agente che dichiari il server GitHub MCP. La definizione dell'agente contiene l'URL del server ma nessun token di autenticazione:
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
)Poi crea una sessione che monta il repository GitHub:
Il campo resources[].authorization_token autentica l'operazione di clonazione del repository e non viene restituito nelle risposte API.
Quando si fornisce un token GitHub, utilizzare i permessi minimi richiesti:
| Azione | Scope richiesti |
|---|---|
| Clonare repository privati | repo |
| Creare PR | repo |
| Leggere issue | repo (privato) o public_repo |
| Creare issue | repo (privato) o public_repo |
Utilizza token di accesso personale a grana fine con i permessi minimi richiesti. Evita di utilizzare token con accesso ampio al tuo account GitHub.
Monta più repository aggiungendo voci all'array resources:
Dopo la creazione di una sessione, puoi elencare le risorse del repository e ruotare i loro token di autorizzazione. Ogni risorsa ha un id restituito al momento della creazione della sessione (o tramite resources.list) che utilizzi per gli aggiornamenti. I repository sono collegati per tutta la durata della sessione; per modificare quali repository sono montati, crea una nuova sessione.
Con il server GitHub MCP, l'agente può creare branch, eseguire commit delle modifiche e inviarle:
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