# Build Your First Copilot-Powered App In this tutorial, you'll use the Copilot SDK to build a command-line assistant. You'll start with the basics, add streaming responses, then add custom tools - giving Copilot the ability to call your code. **What you'll build:** ``` You: What's the weather like in Seattle? Copilot: Let me check the weather for Seattle... Currently 62°F and cloudy with a chance of rain. Typical Seattle weather! You: How about Tokyo? Copilot: In Tokyo it's 75°F and sunny. Great day to be outside! ``` ## Prerequisites Before you begin, make sure you have: - **GitHub Copilot CLI** installed and authenticated ([Installation guide](https://docs.github.com/en/copilot/how-tos/set-up/install-copilot-cli)) - Your preferred language runtime: - **Node.js** 18+ or **Python** 3.8+ or **Go** 1.21+ or **.NET** 8.0+ Verify the CLI is working: ```bash copilot --version ``` ## Step 1: Install the SDK
Node.js / TypeScript First, create a new directory and initialize your project: ```bash mkdir copilot-demo && cd copilot-demo npm init -y --init-type module ``` Then install the SDK and TypeScript runner: ```bash npm install @github/copilot-sdk tsx ```
Python ```bash pip install github-copilot-sdk ```
Go First, create a new directory and initialize your module: ```bash mkdir copilot-demo && cd copilot-demo go mod init copilot-demo ``` Then install the SDK: ```bash go get github.com/github/copilot-sdk/go ```
.NET First, create a new console project: ```bash dotnet new console -n CopilotDemo && cd CopilotDemo ``` Then add the SDK: ```bash dotnet add package GitHub.Copilot.SDK ```
## Step 2: Send Your First Message Create a new file and add the following code. This is the simplest way to use the SDK—about 5 lines of code.
Node.js / TypeScript Create `index.ts`: ```typescript import { CopilotClient } from "@github/copilot-sdk"; const client = new CopilotClient(); const session = await client.createSession({ 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); ``` Run it: ```bash npx tsx index.ts ```
Python Create `main.py`: ```python import asyncio from copilot import CopilotClient from copilot.session import 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("What is 2 + 2?") print(response.data.content) await client.stop() asyncio.run(main()) ``` Run it: ```bash python main.py ```
Go Create `main.go`: ```go package main import ( "context" "fmt" "log" "os" copilot "github.com/github/copilot-sdk/go" ) func main() { ctx := context.Background() client := copilot.NewClient(nil) if err := client.Start(ctx); err != nil { log.Fatal(err) } defer client.Stop() session, err := client.CreateSession(ctx, &copilot.SessionConfig{Model: "gpt-4.1"}) if err != nil { log.Fatal(err) } response, err := session.SendAndWait(ctx, copilot.MessageOptions{Prompt: "What is 2 + 2?"}) if err != nil { log.Fatal(err) } fmt.Println(*response.Data.Content) os.Exit(0) } ``` Run it: ```bash go run main.go ```
.NET Create a new console project and add this to `Program.cs`: ```csharp using GitHub.Copilot.SDK; await using var client = new CopilotClient(); await using var session = await client.CreateSessionAsync(new SessionConfig { Model = "gpt-4.1", OnPermissionRequest = PermissionHandler.ApproveAll }); var response = await session.SendAndWaitAsync(new MessageOptions { Prompt = "What is 2 + 2?" }); Console.WriteLine(response?.Data.Content); ``` Run it: ```bash dotnet run ```
**You should see:** ``` 4 ``` Congratulations! You just built your first Copilot-powered app. ## Step 3: Add Streaming Responses Right now, you wait for the complete response before seeing anything. Let's make it interactive by streaming the response as it's generated.
Node.js / TypeScript Update `index.ts`: ```typescript import { CopilotClient } from "@github/copilot-sdk"; const client = new CopilotClient(); const session = await client.createSession({ model: "gpt-4.1", streaming: true, }); // Listen for response chunks session.on("assistant.message_delta", (event) => { process.stdout.write(event.data.deltaContent); }); session.on("session.idle", () => { console.log(); // New line when done }); await session.sendAndWait({ prompt: "Tell me a short joke" }); await client.stop(); process.exit(0); ```
Python Update `main.py`: ```python import asyncio import sys from copilot import CopilotClient from copilot.session import 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) # Listen for response chunks 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() # New line when done session.on(handle_event) await session.send_and_wait("Tell me a short joke") await client.stop() asyncio.run(main()) ```
Go Update `main.go`: ```go package main import ( "context" "fmt" "log" "os" copilot "github.com/github/copilot-sdk/go" ) func main() { ctx := context.Background() client := copilot.NewClient(nil) if err := client.Start(ctx); err != nil { log.Fatal(err) } defer client.Stop() session, err := client.CreateSession(ctx, &copilot.SessionConfig{ Model: "gpt-4.1", Streaming: true, }) if err != nil { log.Fatal(err) } // Listen for response chunks 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(ctx, copilot.MessageOptions{Prompt: "Tell me a short joke"}) if err != nil { log.Fatal(err) } os.Exit(0) } ```
.NET Update `Program.cs`: ```csharp using GitHub.Copilot.SDK; await using var client = new CopilotClient(); await using var session = await client.CreateSessionAsync(new SessionConfig { Model = "gpt-4.1", OnPermissionRequest = PermissionHandler.ApproveAll, Streaming = true, }); // Listen for response chunks 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" }); ```
Run the code again. You'll see the response appear word by word. ### Event Subscription Methods The SDK provides methods for subscribing to session events: | Method | Description | |--------|-------------| | `on(handler)` | Subscribe to all events; returns unsubscribe function | | `on(eventType, handler)` | Subscribe to specific event type (Node.js/TypeScript only); returns unsubscribe function |
Node.js / TypeScript ```typescript // Subscribe to all events const unsubscribeAll = session.on((event) => { console.log("Event:", event.type); }); // Subscribe to specific event type const unsubscribeIdle = session.on("session.idle", (event) => { console.log("Session is idle"); }); // Later, to unsubscribe: unsubscribeAll(); unsubscribeIdle(); ```
Python ```python from copilot import CopilotClient from copilot.generated.session_events import SessionEvent, SessionEventType from copilot.session import PermissionRequestResult client = CopilotClient() session = await client.create_session(on_permission_request=lambda req, inv: PermissionRequestResult(kind="approved")) # Subscribe to all events unsubscribe = session.on(lambda event: print(f"Event: {event.type}")) # Filter by event type in your handler def handle_event(event: SessionEvent) -> None: if event.type == SessionEventType.SESSION_IDLE: print("Session is idle") elif event.type == SessionEventType.ASSISTANT_MESSAGE: print(f"Message: {event.data.content}") unsubscribe = session.on(handle_event) # Later, to unsubscribe: unsubscribe() ``` ```python # Subscribe to all events unsubscribe = session.on(lambda event: print(f"Event: {event.type}")) # Filter by event type in your handler def handle_event(event): if event.type == SessionEventType.SESSION_IDLE: print("Session is idle") elif event.type == SessionEventType.ASSISTANT_MESSAGE: print(f"Message: {event.data.content}") unsubscribe = session.on(handle_event) # Later, to unsubscribe: unsubscribe() ```
Go ```go package main import ( "fmt" copilot "github.com/github/copilot-sdk/go" ) func main() { session := &copilot.Session{} // Subscribe to all events unsubscribe := session.On(func(event copilot.SessionEvent) { fmt.Println("Event:", event.Type) }) // Filter by event type in your handler session.On(func(event copilot.SessionEvent) { if event.Type == "session.idle" { fmt.Println("Session is idle") } else if event.Type == "assistant.message" { fmt.Println("Message:", *event.Data.Content) } }) // Later, to unsubscribe: unsubscribe() } ``` ```go // Subscribe to all events unsubscribe := session.On(func(event copilot.SessionEvent) { fmt.Println("Event:", event.Type) }) // Filter by event type in your handler session.On(func(event copilot.SessionEvent) { if event.Type == "session.idle" { fmt.Println("Session is idle") } else if event.Type == "assistant.message" { fmt.Println("Message:", *event.Data.Content) } }) // Later, to unsubscribe: unsubscribe() ```
.NET ```csharp using GitHub.Copilot.SDK; public static class EventSubscriptionExample { public static void Example(CopilotSession session) { // Subscribe to all events var unsubscribe = session.On(ev => Console.WriteLine($"Event: {ev.Type}")); // Filter by event type using pattern matching session.On(ev => { switch (ev) { case SessionIdleEvent: Console.WriteLine("Session is idle"); break; case AssistantMessageEvent msg: Console.WriteLine($"Message: {msg.Data.Content}"); break; } }); // Later, to unsubscribe: unsubscribe.Dispose(); } } ``` ```csharp // Subscribe to all events var unsubscribe = session.On(ev => Console.WriteLine($"Event: {ev.Type}")); // Filter by event type using pattern matching session.On(ev => { switch (ev) { case SessionIdleEvent: Console.WriteLine("Session is idle"); break; case AssistantMessageEvent msg: Console.WriteLine($"Message: {msg.Data.Content}"); break; } }); // Later, to unsubscribe: unsubscribe.Dispose(); ```
## Step 4: Add a Custom Tool Now for the powerful part. Let's give Copilot the ability to call your code by defining a custom tool. We'll create a simple weather lookup tool.
Node.js / TypeScript Update `index.ts`: ```typescript import { CopilotClient, defineTool } from "@github/copilot-sdk"; // Define a tool that Copilot can call 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, you'd 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({ model: "gpt-4.1", streaming: true, tools: [getWeather], }); session.on("assistant.message_delta", (event) => { process.stdout.write(event.data.deltaContent); }); session.on("session.idle", () => { console.log(); // New line when done }); await session.sendAndWait({ prompt: "What's the weather like in Seattle and Tokyo?", }); await client.stop(); process.exit(0); ```
Python Update `main.py`: ```python import asyncio import random import sys from copilot import CopilotClient from copilot.session import PermissionHandler from copilot.tools import define_tool from copilot.generated.session_events import SessionEventType from pydantic import BaseModel, Field # Define the parameters for the tool using Pydantic class GetWeatherParams(BaseModel): city: str = Field(description="The name of the city to get weather for") # Define a tool that Copilot can call @define_tool(description="Get the current weather for a city") async def get_weather(params: GetWeatherParams) -> dict: city = params.city # In a real app, you'd call a weather API here 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() if event.type == SessionEventType.SESSION_IDLE: print() session.on(handle_event) await session.send_and_wait("What's the weather like in Seattle and Tokyo?") await client.stop() asyncio.run(main()) ```
Go Update `main.go`: ```go package main import ( "context" "fmt" "log" "math/rand" "os" copilot "github.com/github/copilot-sdk/go" ) // Define the parameter type type WeatherParams struct { City string `json:"city" jsonschema:"The city name"` } // Define the return type type WeatherResult struct { City string `json:"city"` Temperature string `json:"temperature"` Condition string `json:"condition"` } func main() { ctx := context.Background() // Define a tool that Copilot can call getWeather := copilot.DefineTool( "get_weather", "Get the current weather for a city", func(params WeatherParams, inv copilot.ToolInvocation) (WeatherResult, error) { // In a real app, you'd call a weather API here 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 }, ) client := copilot.NewClient(nil) if err := client.Start(ctx); err != nil { log.Fatal(err) } defer client.Stop() session, err := client.CreateSession(ctx, &copilot.SessionConfig{ Model: "gpt-4.1", Streaming: true, Tools: []copilot.Tool{getWeather}, }) if err != nil { log.Fatal(err) } 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(ctx, copilot.MessageOptions{ Prompt: "What's the weather like in Seattle and Tokyo?", }) if err != nil { log.Fatal(err) } os.Exit(0) } ```
.NET Update `Program.cs`: ```csharp using GitHub.Copilot.SDK; using Microsoft.Extensions.AI; using System.ComponentModel; await using var client = new CopilotClient(); // Define a tool that Copilot can call var getWeather = AIFunctionFactory.Create( ([Description("The city name")] string city) => { // In a real app, you'd call a weather API here 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 { Model = "gpt-4.1", OnPermissionRequest = PermissionHandler.ApproveAll, Streaming = true, Tools = [getWeather], }); 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 = "What's the weather like in Seattle and Tokyo?", }); ```
Run it and you'll see Copilot call your tool to get weather data, then respond with the results! ## Step 5: Build an Interactive Assistant Let's put it all together into a useful interactive assistant:
Node.js / TypeScript ```typescript import { CopilotClient, defineTool } 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({ model: "gpt-4.1", streaming: true, tools: [getWeather], }); session.on("assistant.message_delta", (event) => { 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(); ``` Run with: ```bash npx tsx weather-assistant.ts ```
Python Create `weather_assistant.py`: ```python import asyncio import random import sys from copilot import CopilotClient from copilot.session import 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) print("🌤️ Weather Assistant (type 'exit' to quit)") print(" Try: 'What's the weather in Paris?' or 'Compare weather in NYC and LA'\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(user_input) print("\n") await client.stop() asyncio.run(main()) ``` Run with: ```bash python weather_assistant.py ```
Go Create `weather-assistant.go`: ```go package main import ( "bufio" "context" "fmt" "log" "math/rand" "os" "strings" copilot "github.com/github/copilot-sdk/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"` } func main() { ctx := context.Background() 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 }, ) client := copilot.NewClient(nil) if err := client.Start(ctx); err != nil { log.Fatal(err) } defer client.Stop() session, err := client.CreateSession(ctx, &copilot.SessionConfig{ Model: "gpt-4.1", Streaming: true, Tools: []copilot.Tool{getWeather}, }) if err != nil { log.Fatal(err) } session.On(func(event copilot.SessionEvent) { if event.Type == "assistant.message_delta" { if event.Data.DeltaContent != nil { fmt.Print(*event.Data.DeltaContent) } } if event.Type == "session.idle" { fmt.Println() } }) fmt.Println("🌤️ Weather Assistant (type 'exit' to quit)") fmt.Println(" Try: 'What's the weather in Paris?' or 'Compare weather in NYC and LA'\n") scanner := bufio.NewScanner(os.Stdin) for { fmt.Print("You: ") if !scanner.Scan() { break } input := scanner.Text() if strings.ToLower(input) == "exit" { break } fmt.Print("Assistant: ") _, err = session.SendAndWait(ctx, copilot.MessageOptions{Prompt: input}) if err != nil { fmt.Fprintf(os.Stderr, "Error: %v\n", err) break } fmt.Println() } if err := scanner.Err(); err != nil { fmt.Fprintf(os.Stderr, "Input error: %v\n", err) } } ``` Run with: ```bash go run weather-assistant.go ```
.NET Create a new console project and update `Program.cs`: ```csharp using GitHub.Copilot.SDK; using Microsoft.Extensions.AI; using System.ComponentModel; // Define the weather tool using AIFunctionFactory 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 client = new CopilotClient(); await using var session = await client.CreateSessionAsync(new SessionConfig { Model = "gpt-4.1", OnPermissionRequest = PermissionHandler.ApproveAll, Streaming = true, Tools = [getWeather] }); // Listen for response chunks session.On(ev => { if (ev is AssistantMessageDeltaEvent deltaEvent) { Console.Write(deltaEvent.Data.DeltaContent); } if (ev is SessionIdleEvent) { Console.WriteLine(); } }); Console.WriteLine("🌤️ Weather Assistant (type 'exit' to quit)"); Console.WriteLine(" Try: 'What's the weather in Paris?' or 'Compare weather in NYC and LA'\n"); while (true) { Console.Write("You: "); var input = Console.ReadLine(); if (string.IsNullOrEmpty(input) || input.Equals("exit", StringComparison.OrdinalIgnoreCase)) { break; } Console.Write("Assistant: "); await session.SendAndWaitAsync(new MessageOptions { Prompt = input }); Console.WriteLine("\n"); } ``` Run with: ```bash dotnet run ```
**Example session:** ``` 🌤️ Weather Assistant (type 'exit' to quit) Try: 'What's the weather in Paris?' or 'Compare weather in NYC and LA' You: What's the weather in Seattle? Assistant: Let me check the weather for Seattle... It's currently 62°F and cloudy in Seattle. You: How about Tokyo and London? Assistant: I'll check both cities for you: - Tokyo: 75°F and sunny - London: 58°F and rainy You: exit ``` You've built an assistant with a custom tool that Copilot can call! --- ## How Tools Work When you define a tool, you're telling Copilot: 1. **What the tool does** (description) 2. **What parameters it needs** (schema) 3. **What code to run** (handler) Copilot decides when to call your tool based on the user's question. When it does: 1. Copilot sends a tool call request with the parameters 2. The SDK runs your handler function 3. The result is sent back to Copilot 4. Copilot incorporates the result into its response --- ## What's Next? Now that you've got the basics, here are more powerful features to explore: ### Connect to MCP Servers MCP (Model Context Protocol) servers provide pre-built tools. Connect to GitHub's MCP server to give Copilot access to repositories, issues, and pull requests: ```typescript const session = await client.createSession({ mcpServers: { github: { type: "http", url: "https://api.githubcopilot.com/mcp/", }, }, }); ``` 📖 **[Full MCP documentation →](./features/mcp.md)** - Learn about local vs remote servers, all configuration options, and troubleshooting. ### Create Custom Agents Define specialized AI personas for specific tasks: ```typescript const session = await client.createSession({ 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.", }], }); ``` > **Tip:** You can also set `agent: "pr-reviewer"` in the session config to pre-select this agent from the start. See the [Custom Agents guide](./features/custom-agents.md#selecting-an-agent-at-session-creation) for details. ### Customize the System Message Control the AI's behavior and personality by appending instructions: ```typescript const session = await client.createSession({ systemMessage: { content: "You are a helpful assistant for our engineering team. Always be concise.", }, }); ``` For more fine-grained control, use `mode: "customize"` to override individual sections of the system prompt while preserving the rest: ```typescript const session = await client.createSession({ systemMessage: { mode: "customize", sections: { tone: { action: "replace", content: "Respond in a warm, professional tone. Be thorough in explanations." }, code_change_rules: { action: "remove" }, guidelines: { action: "append", content: "\n* Always cite data sources" }, }, content: "Focus on financial analysis and reporting.", }, }); ``` Available section IDs: `identity`, `tone`, `tool_efficiency`, `environment_context`, `code_change_rules`, `guidelines`, `safety`, `tool_instructions`, `custom_instructions`, `last_instructions`. Each override supports four actions: `replace`, `remove`, `append`, and `prepend`. Unknown section IDs are handled gracefully — content is appended to additional instructions and a warning is emitted; `remove` on unknown sections is silently ignored. See the language-specific SDK READMEs for examples in [TypeScript](../nodejs/README.md), [Python](../python/README.md), [Go](../go/README.md), and [C#](../dotnet/README.md). --- ## Connecting to an External CLI Server By default, the SDK automatically manages the Copilot CLI process lifecycle, starting and stopping the CLI as needed. However, you can also run the CLI in server mode separately and have the SDK connect to it. This can be useful for: - **Debugging**: Keep the CLI running between SDK restarts to inspect logs - **Resource sharing**: Multiple SDK clients can connect to the same CLI server - **Development**: Run the CLI with custom settings or in a different environment ### Running the CLI in Server Mode Start the CLI in server mode using the `--headless` flag and optionally specify a port: ```bash copilot --headless --port 4321 ``` If you don't specify a port, the CLI will choose a random available port. ### Connecting the SDK to the External Server Once the CLI is running in server mode, configure your SDK client to connect to it using the "cli url" option:
Node.js / TypeScript ```typescript import { CopilotClient, approveAll } from "@github/copilot-sdk"; const client = new CopilotClient({ cliUrl: "localhost:4321" }); // Use the client normally const session = await client.createSession({ onPermissionRequest: approveAll }); // ... ```
Python ```python from copilot import CopilotClient from copilot.session import PermissionHandler client = CopilotClient({ "cli_url": "localhost:4321" }) await client.start() # Use the client normally session = await client.create_session(on_permission_request=PermissionHandler.approve_all) # ... ```
Go ```go package main import ( "context" "log" copilot "github.com/github/copilot-sdk/go" ) func main() { ctx := context.Background() client := copilot.NewClient(&copilot.ClientOptions{ CLIUrl: "localhost:4321", }) if err := client.Start(ctx); err != nil { log.Fatal(err) } defer client.Stop() // Use the client normally _, _ = client.CreateSession(ctx, &copilot.SessionConfig{ OnPermissionRequest: copilot.PermissionHandler.ApproveAll, }) } ``` ```go import copilot "github.com/github/copilot-sdk/go" client := copilot.NewClient(&copilot.ClientOptions{ CLIUrl: "localhost:4321", }) if err := client.Start(ctx); err != nil { log.Fatal(err) } defer client.Stop() // Use the client normally session, err := client.CreateSession(ctx, &copilot.SessionConfig{ OnPermissionRequest: copilot.PermissionHandler.ApproveAll, }) // ... ```
.NET ```csharp using GitHub.Copilot.SDK; using var client = new CopilotClient(new CopilotClientOptions { CliUrl = "localhost:4321", UseStdio = false }); // Use the client normally await using var session = await client.CreateSessionAsync(new() { OnPermissionRequest = PermissionHandler.ApproveAll }); // ... ```
**Note:** When `cli_url` / `cliUrl` / `CLIUrl` is provided, the SDK will not spawn or manage a CLI process - it will only connect to the existing server at the specified URL. --- ## Telemetry & Observability The Copilot SDK supports [OpenTelemetry](https://opentelemetry.io/) for distributed tracing. Provide a `telemetry` configuration to the client to enable trace export from the CLI process and automatic [W3C Trace Context](https://www.w3.org/TR/trace-context/) propagation between the SDK and CLI. ### Enabling Telemetry Pass a `telemetry` (or `Telemetry`) config when creating the client. This is the opt-in — no separate "enabled" flag is needed.
Node.js / TypeScript ```typescript import { CopilotClient } from "@github/copilot-sdk"; const client = new CopilotClient({ telemetry: { otlpEndpoint: "http://localhost:4318", }, }); ``` Optional peer dependency: `@opentelemetry/api`
Python ```python from copilot import CopilotClient, SubprocessConfig client = CopilotClient(SubprocessConfig( telemetry={ "otlp_endpoint": "http://localhost:4318", }, )) ``` Install with telemetry extras: `pip install copilot-sdk[telemetry]` (provides `opentelemetry-api`)
Go ```go client, err := copilot.NewClient(copilot.ClientOptions{ Telemetry: &copilot.TelemetryConfig{ OTLPEndpoint: "http://localhost:4318", }, }) ``` Dependency: `go.opentelemetry.io/otel`
.NET ```csharp var client = new CopilotClient(new CopilotClientOptions { Telemetry = new TelemetryConfig { OtlpEndpoint = "http://localhost:4318", }, }); ``` No extra dependencies — uses built-in `System.Diagnostics.Activity`.
### TelemetryConfig Options | Option | Node.js | Python | Go | .NET | Description | |---|---|---|---|---|---| | OTLP endpoint | `otlpEndpoint` | `otlp_endpoint` | `OTLPEndpoint` | `OtlpEndpoint` | OTLP HTTP endpoint URL | | File path | `filePath` | `file_path` | `FilePath` | `FilePath` | File path for JSON-lines trace output | | Exporter type | `exporterType` | `exporter_type` | `ExporterType` | `ExporterType` | `"otlp-http"` or `"file"` | | Source name | `sourceName` | `source_name` | `SourceName` | `SourceName` | Instrumentation scope name | | Capture content | `captureContent` | `capture_content` | `CaptureContent` | `CaptureContent` | Whether to capture message content | ### File Export To write traces to a local file instead of an OTLP endpoint: ```typescript const client = new CopilotClient({ telemetry: { filePath: "./traces.jsonl", exporterType: "file", }, }); ``` ### Trace Context Propagation Trace context is propagated automatically — no manual instrumentation is needed: - **SDK → CLI**: `traceparent` and `tracestate` headers from the current span/activity are included in `session.create`, `session.resume`, and `session.send` RPC calls. - **CLI → SDK**: When the CLI invokes tool handlers, the trace context from the CLI's span is propagated so your tool code runs under the correct parent span. 📖 **[OpenTelemetry Instrumentation Guide →](./observability/opentelemetry.md)** — TelemetryConfig options, trace context propagation, and per-language dependencies. --- ## Learn More - [Authentication Guide](./auth/index.md) - GitHub OAuth, environment variables, and BYOK - [BYOK (Bring Your Own Key)](./auth/byok.md) - Use your own API keys from Azure AI Foundry, OpenAI, etc. - [Node.js SDK Reference](../nodejs/README.md) - [Python SDK Reference](../python/README.md) - [Go SDK Reference](../go/README.md) - [.NET SDK Reference](../dotnet/README.md) - [Using MCP Servers](./features/mcp.md) - Integrate external tools via Model Context Protocol - [GitHub MCP Server Documentation](https://github.com/github/github-mcp-server) - [MCP Servers Directory](https://github.com/modelcontextprotocol/servers) - Explore more MCP servers - [OpenTelemetry Instrumentation](./observability/opentelemetry.md) - TelemetryConfig, trace context propagation, and per-language dependencies --- **You did it!** You've learned the core concepts of the GitHub Copilot SDK: - ✅ Creating a client and session - ✅ Sending messages and receiving responses - ✅ Streaming for real-time output - ✅ Defining custom tools that Copilot can call Now go build something amazing! 🚀