|
| 1 | +--- |
| 2 | +title: Debugging Copilot SDK |
| 3 | +shortTitle: Debug Copilot SDK |
| 4 | +intro: Enable debug logging and resolve common connection, authentication, and tool execution issues in {% data variables.copilot.copilot_sdk_short %}. |
| 5 | +product: '{% data reusables.gated-features.copilot-sdk %}' |
| 6 | +versions: |
| 7 | + feature: copilot |
| 8 | +contentType: how-tos |
| 9 | +--- |
| 10 | + |
| 11 | +> [!NOTE] |
| 12 | +> {% data reusables.copilot.copilot-sdk.technical-preview-note %} |
| 13 | +
|
| 14 | +## Enable debug logging |
| 15 | + |
| 16 | +To begin troubleshooting, enable verbose logging to gain visibility into the underlying processes. |
| 17 | + |
| 18 | +```typescript copy |
| 19 | +import { CopilotClient } from "@github/copilot-sdk"; |
| 20 | + |
| 21 | +const client = new CopilotClient({ |
| 22 | + logLevel: "debug", // Options: "none", "error", "warning", "info", "debug", "all" |
| 23 | +}); |
| 24 | +``` |
| 25 | + |
| 26 | +For examples in Python, Go, and .NET, see [Debugging Guide](https://github.com/github/copilot-sdk/blob/main/docs/troubleshooting/debugging.md) in the `github/copilot-sdk` repository. |
| 27 | + |
| 28 | +### Log directory |
| 29 | + |
| 30 | +The CLI writes logs to the ABC directory by default. You can specify a different location using the `--log-dir` argument: |
| 31 | + |
| 32 | +```typescript copy |
| 33 | +const client = new CopilotClient({ |
| 34 | + cliArgs: ["--log-dir", "/path/to/logs"], |
| 35 | +}); |
| 36 | +``` |
| 37 | + |
| 38 | +For Python and Go, which do not currently support passing extra CLI arguments, run the CLI manually with `--log-dir` and connect via the `cliUrl` option. For more information, see [Debugging Guide](https://github.com/github/copilot-sdk/blob/main/docs/troubleshooting/debugging.md) in the `github/copilot-sdk` repository. |
| 39 | + |
| 40 | +## Common issues |
| 41 | + |
| 42 | +### "CLI not found" or "copilot: command not found" |
| 43 | + |
| 44 | +**Cause:** {% data variables.copilot.copilot_cli %} is not installed or not in PATH. |
| 45 | + |
| 46 | +**Solution:** |
| 47 | + |
| 48 | +1. Install the CLI. For more information, see [AUTOTITLE](/copilot/how-tos/copilot-cli/install-copilot-cli). |
| 49 | + |
| 50 | +1. Verify installation: |
| 51 | + |
| 52 | + ```bash copy |
| 53 | + copilot --version |
| 54 | + ``` |
| 55 | + |
| 56 | +1. Or specify the full path: |
| 57 | + |
| 58 | + ```typescript copy |
| 59 | + const client = new CopilotClient({ |
| 60 | + cliPath: "/usr/local/bin/copilot", |
| 61 | + }); |
| 62 | + ``` |
| 63 | + |
| 64 | + For examples in Python, Go, and .NET, see [Debugging Guide](https://github.com/github/copilot-sdk/blob/main/docs/troubleshooting/debugging.md) in the `github/copilot-sdk` repository. |
| 65 | + |
| 66 | +### "Not authenticated" |
| 67 | + |
| 68 | +**Cause:** The CLI is not authenticated with {% data variables.product.github %}. |
| 69 | + |
| 70 | +**Solution:** |
| 71 | + |
| 72 | +1. Authenticate the CLI: |
| 73 | + |
| 74 | + ```bash copy |
| 75 | + copilot auth login |
| 76 | + ``` |
| 77 | + |
| 78 | +1. Or provide a token programmatically: |
| 79 | + |
| 80 | + ```typescript copy |
| 81 | + const client = new CopilotClient({ |
| 82 | + githubToken: process.env.GITHUB_TOKEN, |
| 83 | + }); |
| 84 | + ``` |
| 85 | + |
| 86 | + For examples in Python, Go, and .NET, see [Debugging Guide](https://github.com/github/copilot-sdk/blob/main/docs/troubleshooting/debugging.md) in the `github/copilot-sdk` repository. |
| 87 | + |
| 88 | +### "Session not found" |
| 89 | + |
| 90 | +**Cause:** Attempting to use a session that was destroyed or doesn't exist. |
| 91 | + |
| 92 | +**Solution:** |
| 93 | + |
| 94 | +1. Ensure you're not calling methods after `disconnect()`: |
| 95 | + |
| 96 | + ```typescript copy |
| 97 | + await session.disconnect(); |
| 98 | + // Don't use session after this! |
| 99 | + ``` |
| 100 | + |
| 101 | +1. For resuming sessions, verify the session ID exists: |
| 102 | + |
| 103 | + ```typescript copy |
| 104 | + const sessions = await client.listSessions(); |
| 105 | + console.log("Available sessions:", sessions); |
| 106 | + ``` |
| 107 | + |
| 108 | +### "Connection refused" or "ECONNREFUSED" |
| 109 | + |
| 110 | +**Cause:** The CLI server process crashed or failed to start. |
| 111 | + |
| 112 | +**Solution:** |
| 113 | + |
| 114 | +1. Check if the CLI runs correctly standalone: |
| 115 | + |
| 116 | + ```bash copy |
| 117 | + copilot --server --stdio |
| 118 | + ``` |
| 119 | + |
| 120 | +1. Check for port conflicts if using TCP mode: |
| 121 | + |
| 122 | + ```typescript copy |
| 123 | + const client = new CopilotClient({ |
| 124 | + useStdio: false, |
| 125 | + port: 0, // Use random available port |
| 126 | + }); |
| 127 | + ``` |
| 128 | + |
| 129 | +## MCP server debugging |
| 130 | + |
| 131 | +MCP (Model Context Protocol) servers can be tricky to debug. For comprehensive MCP debugging guidance, see [AUTOTITLE](/copilot/how-tos/copilot-sdk/troubleshooting/debug-mcp-servers). |
| 132 | + |
| 133 | +### Quick MCP checklist |
| 134 | + |
| 135 | +* [ ] MCP server executable exists and runs independently |
| 136 | +* [ ] Command path is correct (use absolute paths) |
| 137 | +* [ ] Tools are enabled: `tools: ["*"]` |
| 138 | +* [ ] Server responds to `initialize` request correctly |
| 139 | +* [ ] Working directory (`cwd`) is set if needed |
| 140 | + |
| 141 | +### Test your MCP server |
| 142 | + |
| 143 | +Before integrating with the SDK, verify your MCP server works: |
| 144 | + |
| 145 | +```bash copy |
| 146 | +echo '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"test","version":"1.0"}}}' | /path/to/your/mcp-server |
| 147 | +``` |
| 148 | + |
| 149 | +For detailed troubleshooting, see [AUTOTITLE](/copilot/how-tos/copilot-sdk/troubleshooting/debug-mcp-servers). |
| 150 | + |
| 151 | +## Connection issues |
| 152 | + |
| 153 | +### Stdio vs TCP mode |
| 154 | + |
| 155 | +The SDK supports two transport modes: |
| 156 | + |
| 157 | +| Mode | Description | Use case | |
| 158 | +|------|-------------|----------| |
| 159 | +| **Stdio** (default) | CLI runs as subprocess, communicates via pipes | Local development, single process | |
| 160 | +| **TCP** | CLI runs separately, communicates via TCP socket | Multiple clients, remote CLI | |
| 161 | + |
| 162 | +**Stdio mode (default):** |
| 163 | + |
| 164 | +```typescript copy |
| 165 | +const client = new CopilotClient({ |
| 166 | + useStdio: true, // This is the default |
| 167 | +}); |
| 168 | +``` |
| 169 | + |
| 170 | +**TCP mode:** |
| 171 | + |
| 172 | +```typescript copy |
| 173 | +const client = new CopilotClient({ |
| 174 | + useStdio: false, |
| 175 | + port: 8080, // Or 0 for random port |
| 176 | +}); |
| 177 | +``` |
| 178 | + |
| 179 | +**Connect to existing server:** |
| 180 | + |
| 181 | +```typescript copy |
| 182 | +const client = new CopilotClient({ |
| 183 | + cliUrl: "localhost:8080", // Connect to running server |
| 184 | +}); |
| 185 | +``` |
| 186 | + |
| 187 | +### Diagnosing connection failures |
| 188 | + |
| 189 | +1. Check client state: |
| 190 | + |
| 191 | + ```typescript copy |
| 192 | + console.log("Connection state:", client.getState()); |
| 193 | + // Should be "connected" after start() |
| 194 | + ``` |
| 195 | + |
| 196 | +1. Listen for state changes: |
| 197 | + |
| 198 | + ```typescript copy |
| 199 | + client.on("stateChange", (state) => { |
| 200 | + console.log("State changed to:", state); |
| 201 | + }); |
| 202 | + ``` |
| 203 | + |
| 204 | +1. Verify the CLI process is running: |
| 205 | + |
| 206 | + ```bash copy |
| 207 | + # Check for copilot processes |
| 208 | + ps aux | grep copilot |
| 209 | + ``` |
| 210 | + |
| 211 | +## Tool execution issues |
| 212 | + |
| 213 | +### Custom tool not being called |
| 214 | + |
| 215 | +1. Verify tool registration: |
| 216 | + |
| 217 | + ```typescript copy |
| 218 | + const session = await client.createSession({ |
| 219 | + tools: [myTool], |
| 220 | + }); |
| 221 | + |
| 222 | + // Check registered tools |
| 223 | + console.log("Registered tools:", session.getTools?.()); |
| 224 | + ``` |
| 225 | + |
| 226 | +1. Check the tool schema is valid JSON Schema: |
| 227 | + |
| 228 | + ```typescript copy |
| 229 | + const myTool = { |
| 230 | + name: "get_weather", |
| 231 | + description: "Get weather for a location", |
| 232 | + parameters: { |
| 233 | + type: "object", |
| 234 | + properties: { |
| 235 | + location: { type: "string", description: "City name" }, |
| 236 | + }, |
| 237 | + required: ["location"], |
| 238 | + }, |
| 239 | + handler: async (args) => { |
| 240 | + return { temperature: 72 }; |
| 241 | + }, |
| 242 | + }; |
| 243 | + ``` |
| 244 | + |
| 245 | +1. Ensure the handler returns a valid result: |
| 246 | + |
| 247 | + ```typescript copy |
| 248 | + handler: async (args) => { |
| 249 | + // Must return something JSON-serializable |
| 250 | + return { success: true, data: "result" }; |
| 251 | + |
| 252 | + // Don't return undefined or non-serializable objects |
| 253 | + } |
| 254 | + ``` |
| 255 | + |
| 256 | +### Tool errors not surfacing |
| 257 | + |
| 258 | +Subscribe to error events: |
| 259 | + |
| 260 | +```typescript copy |
| 261 | +session.on("tool.execution_error", (event) => { |
| 262 | + console.error("Tool error:", event.data); |
| 263 | +}); |
| 264 | + |
| 265 | +session.on("error", (event) => { |
| 266 | + console.error("Session error:", event.data); |
| 267 | +}); |
| 268 | +``` |
| 269 | + |
| 270 | +## Platform-specific issues |
| 271 | + |
| 272 | +### Windows |
| 273 | + |
| 274 | +1. **Path separators:** Use raw strings or forward slashes. For .NET-specific examples, see [Debugging Guide](https://github.com/github/copilot-sdk/blob/main/docs/troubleshooting/debugging.md) in the `github/copilot-sdk` repository. |
| 275 | + |
| 276 | +1. **Console encoding:** Ensure UTF-8 for proper JSON handling. For .NET-specific examples, see [Debugging Guide](https://github.com/github/copilot-sdk/blob/main/docs/troubleshooting/debugging.md) in the `github/copilot-sdk` repository. |
| 277 | + |
| 278 | +### macOS |
| 279 | + |
| 280 | +1. **Gatekeeper issues:** If CLI is blocked: |
| 281 | + |
| 282 | + ```bash copy |
| 283 | + xattr -d com.apple.quarantine /path/to/copilot |
| 284 | + ``` |
| 285 | + |
| 286 | +1. **PATH issues in GUI apps:** GUI applications may not inherit shell PATH: |
| 287 | + |
| 288 | + ```typescript copy |
| 289 | + const client = new CopilotClient({ |
| 290 | + cliPath: "/opt/homebrew/bin/copilot", // Full path |
| 291 | + }); |
| 292 | + ``` |
| 293 | + |
| 294 | +### Linux |
| 295 | + |
| 296 | +1. **Permission issues:** |
| 297 | + |
| 298 | + ```bash copy |
| 299 | + chmod +x /path/to/copilot |
| 300 | + ``` |
| 301 | + |
| 302 | +1. **Missing libraries:** Check for required shared libraries: |
| 303 | + |
| 304 | + ```bash copy |
| 305 | + ldd /path/to/copilot |
| 306 | + ``` |
| 307 | + |
| 308 | +## Getting help |
| 309 | + |
| 310 | +If you're still stuck, collect the following debug information before opening an issue: |
| 311 | + |
| 312 | +* SDK version |
| 313 | +* CLI version (`copilot --version`) |
| 314 | +* Operating system |
| 315 | +* Debug logs |
| 316 | +* Minimal reproduction code |
| 317 | + |
| 318 | +Search for existing issues or open a new issue in the [github/copilot-sdk](https://github.com/github/copilot-sdk/issues) repository. |
0 commit comments