SDK GitHub Copilot
Intégrez les workflows agentic de Copilot dans n'importe quelle application en utilisant Python, TypeScript, Go ou .NET.
Aperçu
Le SDK GitHub Copilot expose le même moteur derrière Copilot CLI : un runtime agent éprouvé en production que vous pouvez invoquer par programmation. Pas besoin de construire votre propre orchestration - vous définissez le comportement de l'agent, Copilot gère la planification, l'invocation d'outils, les modifications de fichiers, et bien plus.
Prérequis
- GitHub Copilot CLI installé et authentifié (Guide d'installation)
- Runtime de langage : Node.js 18+, Python 3.8+, Go 1.21+ ou .NET 8.0+
Vérifiez CLI : copilot --version
Installation
Node.js/TypeScript
mkdir copilot-demo && cd copilot-demo
npm init -y --init-type module
npm install @github/copilot-sdk tsx
Python
pip install github-copilot-sdk
Go
mkdir copilot-demo && cd copilot-demo
go mod init copilot-demo
go get github.com/github/copilot-sdk/go
.NET
dotnet new console -n CopilotDemo && cd CopilotDemo
dotnet add package GitHub.Copilot.SDK
Démarrage rapide
TypeScript
import { CopilotClient, approveAll } from "@github/copilot-sdk";
const client = new CopilotClient();
const session = await client.createSession({
onPermissionRequest: approveAll,
model: "gpt-4.1",
});
const response = await session.sendAndWait({ prompt: "What is 2 + 2?" });
console.log(response?.data.content);
await client.stop();
process.exit(0);
Exécution : npx tsx index.ts
Python
import asyncio
from copilot import CopilotClient, PermissionHandler
async def main():
client = CopilotClient()
await client.start()
session = await client.create_session({
"on_permission_request": PermissionHandler.approve_all,
"model": "gpt-4.1",
})
response = await session.send_and_wait({"prompt": "What is 2 + 2?"})
print(response.data.content)
await client.stop()
asyncio.run(main())
Go
package main
import (
"fmt"
"log"
"os"
copilot "github.com/github/copilot-sdk/go"
)
func main() {
client := copilot.NewClient(nil)
if err := client.Start(); err != nil {
log.Fatal(err)
}
defer client.Stop()
session, err := client.CreateSession(&copilot.SessionConfig{
OnPermissionRequest: copilot.PermissionHandler.ApproveAll,
Model: "gpt-4.1",
})
if err != nil {
log.Fatal(err)
}
response, err := session.SendAndWait(copilot.MessageOptions{Prompt: "What is 2 + 2?"}, 0)
if err != nil {
log.Fatal(err)
}
fmt.Println(*response.Data.Content)
os.Exit(0)
}
.NET (C#)
using GitHub.Copilot.SDK;
await using var client = new CopilotClient();
await using var session = await client.CreateSessionAsync(new SessionConfig
{
OnPermissionRequest = PermissionHandler.ApproveAll,
Model = "gpt-4.1",
});
var response = await session.SendAndWaitAsync(new MessageOptions { Prompt = "What is 2 + 2?" });
Console.WriteLine(response?.Data.Content);
Exécution : dotnet run
Réponses en streaming
Activez la sortie en temps réel pour une meilleure UX :
TypeScript
import { CopilotClient, approveAll, SessionEvent } from "@github/copilot-sdk";
const client = new CopilotClient();
const session = await client.createSession({
onPermissionRequest: approveAll,
model: "gpt-4.1",
streaming: true,
});
session.on((event: SessionEvent) => {
if (event.type === "assistant.message_delta") {
process.stdout.write(event.data.deltaContent);
}
if (event.type === "session.idle") {
console.log(); // New line when done
}
});
await session.sendAndWait({ prompt: "Tell me a short joke" });
await client.stop();
process.exit(0);
Python
import asyncio
import sys
from copilot import CopilotClient, PermissionHandler
from copilot.generated.session_events import SessionEventType
async def main():
client = CopilotClient()
await client.start()
session = await client.create_session({
"on_permission_request": PermissionHandler.approve_all,
"model": "gpt-4.1",
"streaming": True,
})
def handle_event(event):
if event.type == SessionEventType.ASSISTANT_MESSAGE_DELTA:
sys.stdout.write(event.data.delta_content)
sys.stdout.flush()
if event.type == SessionEventType.SESSION_IDLE:
print()
session.on(handle_event)
await session.send_and_wait({"prompt": "Tell me a short joke"})
await client.stop()
asyncio.run(main())
Go
session, err := client.CreateSession(&copilot.SessionConfig{
OnPermissionRequest: copilot.PermissionHandler.ApproveAll,
Model: "gpt-4.1",
Streaming: true,
})
session.On(func(event copilot.SessionEvent) {
if event.Type == "assistant.message_delta" {
fmt.Print(*event.Data.DeltaContent)
}
if event.Type == "session.idle" {
fmt.Println()
}
})
_, err = session.SendAndWait(copilot.MessageOptions{Prompt: "Tell me a short joke"}, 0)
.NET
await using var session = await client.CreateSessionAsync(new SessionConfig
{
OnPermissionRequest = PermissionHandler.ApproveAll,
Model = "gpt-4.1",
Streaming = true,
});
session.On(ev =>
{
if (ev is AssistantMessageDeltaEvent deltaEvent)
Console.Write(deltaEvent.Data.DeltaContent);
if (ev is SessionIdleEvent)
Console.WriteLine();
});
await session.SendAndWaitAsync(new MessageOptions { Prompt = "Tell me a short joke" });
Outils personnalisés
Définissez des outils que Copilot peut invoquer lors du raisonnement. Quand vous définissez un outil, vous dites à Copilot :
- Ce que fait l'outil (description)
- Les paramètres qu'il nécessite (schéma)
- Le code à exécuter (handler)
TypeScript (JSON Schema)
import { CopilotClient, approveAll, defineTool, SessionEvent } from "@github/copilot-sdk";
const getWeather = defineTool("get_weather", {
description: "Get the current weather for a city",
parameters: {
type: "object",
properties: {
city: { type: "string", description: "The city name" },
},
required: ["city"],
},
handler: async (args: { city: string }) => {
const { city } = args;
// In a real app, call a weather API here
const conditions = ["sunny", "cloudy", "rainy", "partly cloudy"];
const temp = Math.floor(Math.random() * 30) + 50;
const condition = conditions[Math.floor(Math.random() * conditions.length)];
return { city, temperature: `${temp}°F`, condition };
},
});
const client = new CopilotClient();
const session = await client.createSession({
onPermissionRequest: approveAll,
model: "gpt-4.1",
streaming: true,
tools: [getWeather],
});
session.on((event: SessionEvent) => {
if (event.type === "assistant.message_delta") {
process.stdout.write(event.data.deltaContent);
}
});
await session.sendAndWait({
prompt: "What's the weather like in Seattle and Tokyo?",
});
await client.stop();
process.exit(0);
Python (Pydantic)
import asyncio
import random
import sys
from copilot import CopilotClient, PermissionHandler
from copilot.tools import define_tool
from copilot.generated.session_events import SessionEventType
from pydantic import BaseModel, Field
class GetWeatherParams(BaseModel):
city: str = Field(description="The name of the city to get weather for")
@define_tool(description="Get the current weather for a city")
async def get_weather(params: GetWeatherParams) -> dict:
city = params.city
conditions = ["sunny", "cloudy", "rainy", "partly cloudy"]
temp = random.randint(50, 80)
condition = random.choice(conditions)
return {"city": city, "temperature": f"{temp}°F", "condition": condition}
async def main():
client = CopilotClient()
await client.start()
session = await client.create_session({
"on_permission_request": PermissionHandler.approve_all,
"model": "gpt-4.1",
"streaming": True,
"tools": [get_weather],
})
def handle_event(event):
if event.type == SessionEventType.ASSISTANT_MESSAGE_DELTA:
sys.stdout.write(event.data.delta_content)
sys.stdout.flush()
session.on(handle_event)
await session.send_and_wait({
"prompt": "What's the weather like in Seattle and Tokyo?"
})
await client.stop()
asyncio.run(main())
Go
type WeatherParams struct {
City string `json:"city" jsonschema:"The city name"`
}
type WeatherResult struct {
City string `json:"city"`
Temperature string `json:"temperature"`
Condition string `json:"condition"`
}
getWeather := copilot.DefineTool(
"get_weather",
"Get the current weather for a city",
func(params WeatherParams, inv copilot.ToolInvocation) (WeatherResult, error) {
conditions := []string{"sunny", "cloudy", "rainy", "partly cloudy"}
temp := rand.Intn(30) + 50
condition := conditions[rand.Intn(len(conditions))]
return WeatherResult{
City: params.City,
Temperature: fmt.Sprintf("%d°F", temp),
Condition: condition,
}, nil
},
)
session, _ := client.CreateSession(&copilot.SessionConfig{
OnPermissionRequest: copilot.PermissionHandler.ApproveAll,
Model: "gpt-4.1",
Streaming: true,
Tools: []copilot.Tool{getWeather},
})
.NET (Microsoft.Extensions.AI)
using GitHub.Copilot.SDK;
using Microsoft.Extensions.AI;
using System.ComponentModel;
var getWeather = AIFunctionFactory.Create(
([Description("The city name")] string city) =>
{
var conditions = new[] { "sunny", "cloudy", "rainy", "partly cloudy" };
var temp = Random.Shared.Next(50, 80);
var condition = conditions[Random.Shared.Next(conditions.Length)];
return new { city, temperature = $"{temp}°F", condition };
},
"get_weather",
"Get the current weather for a city"
);
await using var session = await client.CreateSessionAsync(new SessionConfig
{
OnPermissionRequest = PermissionHandler.ApproveAll,
Model = "gpt-4.1",
Streaming = true,
Tools = [getWeather],
});
Fonctionnement des outils
Quand Copilot décide d'appeler votre outil :
- Copilot envoie une demande d'appel d'outil avec les paramètres
- Le SDK exécute votre fonction handler
- Le résultat est renvoyé à Copilot
- Copilot intègre le résultat dans sa réponse
Copilot décide quand appeler votre outil en fonction de la question de l'utilisateur et de la description de votre outil.
Assistant CLI interactif
Construisez un assistant interactif complet :
TypeScript
import { CopilotClient, approveAll, defineTool, SessionEvent } from "@github/copilot-sdk";
import * as readline from "readline";
const getWeather = defineTool("get_weather", {
description: "Get the current weather for a city",
parameters: {
type: "object",
properties: {
city: { type: "string", description: "The city name" },
},
required: ["city"],
},
handler: async ({ city }) => {
const conditions = ["sunny", "cloudy", "rainy", "partly cloudy"];
const temp = Math.floor(Math.random() * 30) + 50;
const condition = conditions[Math.floor(Math.random() * conditions.length)];
return { city, temperature: `${temp}°F`, condition };
},
});
const client = new CopilotClient();
const session = await client.createSession({
onPermissionRequest: approveAll,
model: "gpt-4.1",
streaming: true,
tools: [getWeather],
});
session.on((event: SessionEvent) => {
if (event.type === "assistant.message_delta") {
process.stdout.write(event.data.deltaContent);
}
});
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
console.log("Weather Assistant (type 'exit' to quit)");
console.log("Try: 'What's the weather in Paris?'\n");
const prompt = () => {
rl.question("You: ", async (input) => {
if (input.toLowerCase() === "exit") {
await client.stop();
rl.close();
return;
}
process.stdout.write("Assistant: ");
await session.sendAndWait({ prompt: input });
console.log("\n");
prompt();
});
};
prompt();
Python
import asyncio
import random
import sys
from copilot import CopilotClient, PermissionHandler
from copilot.tools import define_tool
from copilot.generated.session_events import SessionEventType
from pydantic import BaseModel, Field
class GetWeatherParams(BaseModel):
city: str = Field(description="The name of the city to get weather for")
@define_tool(description="Get the current weather for a city")
async def get_weather(params: GetWeatherParams) -> dict:
conditions = ["sunny", "cloudy", "rainy", "partly cloudy"]
temp = random.randint(50, 80)
condition = random.choice(conditions)
return {"city": params.city, "temperature": f"{temp}°F", "condition": condition}
async def main():
client = CopilotClient()
await client.start()
session = await client.create_session({
"on_permission_request": PermissionHandler.approve_all,
"model": "gpt-4.1",
"streaming": True,
"tools": [get_weather],
})
def handle_event(event):
if event.type == SessionEventType.ASSISTANT_MESSAGE_DELTA:
sys.stdout.write(event.data.delta_content)
sys.stdout.flush()
session.on(handle_event)
print("Weather Assistant (type 'exit' to quit)")
print("Try: 'What's the weather in Paris?'\n")
while True:
try:
user_input = input("You: ")
except EOFError:
break
if user_input.lower() == "exit":
break
sys.stdout.write("Assistant: ")
await session.send_and_wait({"prompt": user_input})
print("\n")
await client.stop()
asyncio.run(main())
Intégration du serveur MCP
Connectez-vous à des serveurs MCP (Model Context Protocol) pour des outils préconstuits. Connectez-vous au serveur MCP GitHub pour accéder aux dépôts, problèmes et demandes de pull :
TypeScript
const session = await client.createSession({
onPermissionRequest: approveAll,
model: "gpt-4.1",
mcpServers: {
github: {
type: "http",
url: "https://api.githubcopilot.com/mcp/",
},
},
});
Python
session = await client.create_session({
"on_permission_request": PermissionHandler.approve_all,
"model": "gpt-4.1",
"mcp_servers": {
"github": {
"type": "http",
"url": "https://api.githubcopilot.com/mcp/",
},
},
})
Go
session, _ := client.CreateSession(&copilot.SessionConfig{
OnPermissionRequest: copilot.PermissionHandler.ApproveAll,
Model: "gpt-4.1",
MCPServers: map[string]copilot.MCPServerConfig{
"github": {
"type": "http",
"url": "https://api.githubcopilot.com/mcp/",
},
},
})
.NET
await using var session = await client.CreateSessionAsync(new SessionConfig
{
OnPermissionRequest = PermissionHandler.ApproveAll,
Model = "gpt-4.1",
McpServers = new Dictionary<string, McpServerConfig>
{
["github"] = new McpServerConfig
{
Type = "http",
Url = "https://api.githubcopilot.com/mcp/",
},
},
});
Agents personnalisés
Définissez des personas IA spécialisés pour des tâches spécifiques :
TypeScript
const session = await client.createSession({
onPermissionRequest: approveAll,
model: "gpt-4.1",
customAgents: [{
name: "pr-reviewer",
displayName: "PR Reviewer",
description: "Reviews pull requests for best practices",
prompt: "You are an expert code reviewer. Focus on security, performance, and maintainability.",
}],
});
Python
session = await client.create_session({
"on_permission_request": PermissionHandler.approve_all,
"model": "gpt-4.1",
"custom_agents": [{
"name": "pr-reviewer",
"display_name": "PR Reviewer",
"description": "Reviews pull requests for best practices",
"prompt": "You are an expert code reviewer. Focus on security, performance, and maintainability.",
}],
})
Message système
Personnalisez le comportement et la personnalité de l'IA :
TypeScript
const session = await client.createSession({
onPermissionRequest: approveAll,
model: "gpt-4.1",
systemMessage: {
content: "You are a helpful assistant for our engineering team. Always be concise.",
},
});
Python
session = await client.create_session({
"on_permission_request": PermissionHandler.approve_all,
"model": "gpt-4.1",
"system_message": {
"content": "You are a helpful assistant for our engineering team. Always be concise.",
},
})
Serveur CLI externe
Exécutez le CLI en mode serveur séparément et connectez le SDK à celui-ci. Utile pour le débogage, le partage de ressources ou des environnements personnalisés.
Démarrer CLI en mode serveur
copilot --server --port 4321
Connecter le SDK à un serveur externe
TypeScript
const client = new CopilotClient({
cliUrl: "localhost:4321"
});
const session = await client.createSession({
onPermissionRequest: approveAll,
model: "gpt-4.1",
});
Python
client = CopilotClient({
"cli_url": "localhost:4321"
})
await client.start()
session = await client.create_session({
"on_permission_request": PermissionHandler.approve_all,
"model": "gpt-4.1",
})
Go
client := copilot.NewClient(&copilot.ClientOptions{
CLIUrl: "localhost:4321",
})
if err := client.Start(); err != nil {
log.Fatal(err)
}
session, _ := client.CreateSession(&copilot.SessionConfig{
OnPermissionRequest: copilot.PermissionHandler.ApproveAll,
Model: "gpt-4.1",
})
.NET
using var client = new CopilotClient(new CopilotClientOptions
{
CliUrl = "localhost:4321"
});
await using var session = await client.CreateSessionAsync(new SessionConfig
{
OnPermissionRequest = PermissionHandler.ApproveAll,
Model = "gpt-4.1",
});
Remarque : Quand cliUrl est fourni, le SDK ne lancera pas ou ne gérera pas un processus CLI - il se connecte uniquement au serveur existant.
Types d'événements
| Événement | Description |
|---|---|
user.message |
Entrée utilisateur ajoutée |
assistant.message |
Réponse de modèle complète |
assistant.message_delta |
Fragment de réponse en streaming |
assistant.reasoning |
Raisonnement du modèle (dépendant du modèle) |
assistant.reasoning_delta |
Fragment de raisonnement en streaming |
tool.execution_start |
Invocation d'outil lancée |
tool.execution_complete |
Exécution d'outil terminée |
session.idle |
Pas de traitement actif |
session.error |
Erreur survenue |
Configuration du client
| Option | Description | Défaut |
|---|---|---|
cliPath |
Chemin vers l'exécutable Copilot CLI | System PATH |
cliUrl |
Se connecter à un serveur existant (ex: "localhost:4321") | Aucun |
port |
Port de communication serveur | Aléatoire |
useStdio |
Utiliser le transport stdio au lieu de TCP | true |
logLevel |
Verbosité de journalisation | "info" |
autoStart |
Lancer le serveur automatiquement | true |
autoRestart |
Redémarrer en cas de crash | true |
cwd |
Répertoire de travail du processus CLI | Hérité |
Configuration de session
| Option | Description |
|---|---|
model |
LLM à utiliser ("gpt-4.1", "claude-sonnet-4.5", etc.) |
sessionId |
Identifiant de session personnalisé |
tools |
Définitions d'outils personnalisés |
mcpServers |
Connexions de serveur MCP |
customAgents |
Personas d'agent personnalisés |
systemMessage |
Remplacer le prompt système par défaut |
streaming |
Activer les fragments de réponse incrémentales |
availableTools |
Liste blanche d'outils autorisés |
excludedTools |
Liste noire d'outils désactivés |
Persistance de session
Sauvegardez et reprenez les conversations entre les redémarrages :
Créer avec ID personnalisé
const session = await client.createSession({
onPermissionRequest: approveAll,
sessionId: "user-123-conversation",
model: "gpt-4.1"
});
Reprendre une session
const session = await client.resumeSession("user-123-conversation", { onPermissionRequest: approveAll });
await session.send({ prompt: "What did we discuss earlier?" });
Lister et supprimer des sessions
const sessions = await client.listSessions();
await client.deleteSession("old-session-id");
Gestion des erreurs
try {
const client = new CopilotClient();
const session = await client.createSession({
onPermissionRequest: approveAll,
model: "gpt-4.1",
});
const response = await session.sendAndWait(
{ prompt: "Hello!" },
30000 // timeout in ms
);
} catch (error) {
if (error.code === "ENOENT") {
console.error("Copilot CLI not installed");
} else if (error.code === "ECONNREFUSED") {
console.error("Cannot connect to Copilot server");
} else {
console.error("Error:", error.message);
}
} finally {
await client.stop();
}
Arrêt gracieux
process.on("SIGINT", async () => {
console.log("Shutting down...");
await client.stop();
process.exit(0);
});
Modèles courants
Conversation multi-tour
const session = await client.createSession({
onPermissionRequest: approveAll,
model: "gpt-4.1",
});
await session.sendAndWait({ prompt: "My name is Alice" });
await session.sendAndWait({ prompt: "What's my name?" });
// Response: "Your name is Alice"
Pièces jointes de fichiers
await session.send({
prompt: "Analyze this file",
attachments: [{
type: "file",
path: "./data.csv",
displayName: "Sales Data"
}]
});
Interrompre les opérations longues
const timeoutId = setTimeout(() => {
session.abort();
}, 60000);
session.on((event) => {
if (event.type === "session.idle") {
clearTimeout(timeoutId);
}
});
Modèles disponibles
Interrogez les modèles disponibles au moment de l'exécution :
const models = await client.getModels();
// Returns: ["gpt-4.1", "gpt-4o", "claude-sonnet-4.5", ...]
Bonnes pratiques
- Toujours nettoyer : Utilisez
try-finallyoudeferpour vous assurer queclient.stop()est appelé - Définir les délais d'expiration : Utilisez
sendAndWaitavec timeout pour les opérations longues - Gérer les événements : S'abonner aux événements d'erreur pour une gestion d'erreur robuste
- Utiliser le streaming : Activez le streaming pour une meilleure UX sur les longues réponses
- Persister les sessions : Utilisez des IDs de session personnalisés pour les conversations multi-tour
- Définir des outils clairs : Écrivez des noms et descriptions d'outils descriptifs
Architecture
Your Application
|
SDK Client
| JSON-RPC
Copilot CLI (server mode)
|
GitHub (models, auth)
Le SDK gère automatiquement le cycle de vie du processus CLI. Toute la communication se fait via JSON-RPC sur stdio ou TCP.
Ressources
- GitHub Repository: https://github.com/github/copilot-sdk
- Getting Started Tutorial: https://github.com/github/copilot-sdk/blob/main/docs/tutorials/first-app.md
- GitHub MCP Server: https://github.com/github/github-mcp-server
- MCP Servers Directory: https://github.com/modelcontextprotocol/servers
- Cookbook: https://github.com/github/copilot-sdk/tree/main/cookbook
- Samples: https://github.com/github/copilot-sdk/tree/main/samples
Statut
Ce SDK est en Technical Preview et peut contenir des changements cassants. Non recommandé pour une utilisation en production pour l'instant.