Copilot Supercharged SDK for Rust
A Rust client library for programmatic control of GitHub Copilot CLI via JSON-RPC 2.0.
This SDK communicates with the Copilot CLI server using JSON-RPC 2.0 over stdio or TCP, with Content-Length header framing (LSP protocol style). It follows the same architecture as all other SDKs in this repository (see the full list of 21 supported languages).
Installation
Add to your Cargo.toml:
[]
= { = "../rust" }
= { = "1", = ["full"] }
= "1"
Or if published to crates.io:
[]
= "0.1"
= { = "1", = ["full"] }
Quick Start
use *;
use Arc;
async
Custom Tools
You can register custom tools that the Copilot agent can invoke during a session:
use *;
use Arc;
// Define a tool
let tool_def = ToolDefinition ;
// Create a handler
let handler: ToolHandler = new;
// Create session with the tool
let config = SessionConfig ;
let session = client.create_session.await?;
// Register the handler
session.register_tool.await;
Permission Handling
Register a handler to approve or deny permission requests from the agent:
use *;
use Arc;
let perm_handler: PermissionHandlerFn = new;
session.register_permission_handler.await;
User Input Handling
Enable the ask_user tool by registering a user input handler:
use *;
use Arc;
let input_handler: UserInputHandlerFn = new;
session.register_user_input_handler.await;
Event Subscription
Subscribe to all events or specific event types:
// All events
let sub = session.on.await;
// Specific event type
let sub = session.on_event.await;
// Unsubscribe explicitly (also happens on drop)
sub.unsubscribe;
Event Types
| Event Type | Description |
|---|---|
session.start |
Session started |
session.resume |
Session resumed |
session.idle |
Session finished processing |
session.error |
Error occurred |
session.info |
Informational message |
session.model_change |
Model changed |
session.truncation |
Context truncated |
session.compaction_start |
Context compaction started |
session.compaction_complete |
Context compaction finished |
user.message |
User message sent |
assistant.message |
Final assistant response |
assistant.message_delta |
Streaming response chunk |
assistant.reasoning |
Reasoning content |
assistant.reasoning_delta |
Streaming reasoning chunk |
assistant.turn_start |
Assistant turn started |
assistant.turn_end |
Assistant turn ended |
assistant.usage |
Token usage information |
tool.execution_start |
Tool execution started |
tool.execution_complete |
Tool execution finished |
tool.execution_progress |
Tool execution progress |
abort |
Session aborted |
API Reference
CopilotClient
| Method | Description |
|---|---|
CopilotClient::new(options) |
Create a new client |
client.start() |
Start the CLI server and connect |
client.stop() |
Stop the server and clean up sessions |
client.force_stop() |
Force stop without graceful cleanup |
client.create_session(config) |
Create a new session |
client.resume_session(config) |
Resume an existing session |
client.delete_session(id) |
Delete a session permanently |
client.list_sessions() |
List all sessions |
client.get_last_session_id() |
Get the most recent session ID |
client.ping(message) |
Ping the server |
client.get_status() |
Get CLI version and protocol info |
client.get_auth_status() |
Get authentication status |
client.list_models() |
List available models (cached) |
client.get_state() |
Get current connection state |
client.on_lifecycle(handler) |
Subscribe to lifecycle events |
CopilotSession
| Method | Description |
|---|---|
session.send(options) |
Send a message (async, non-blocking) |
session.send_and_wait(options, timeout) |
Send and wait for idle |
session.on(handler) |
Subscribe to all events |
session.on_event(type, handler) |
Subscribe to specific event type |
session.register_tool(name, handler) |
Register a tool handler |
session.register_permission_handler(h) |
Register permission handler |
session.register_user_input_handler(h) |
Register user input handler |
session.register_hooks_handler(h) |
Register hooks handler |
session.get_messages() |
Get session history |
session.destroy() |
Destroy the session |
session.abort() |
Abort current processing |
session.session_id() |
Get session ID |
session.workspace_path() |
Get workspace path |
CopilotClientOptions
| Field | Type | Default | Description |
|---|---|---|---|
cli_path |
Option<String> |
None |
Path to CLI executable |
cli_args |
Vec<String> |
[] |
Extra CLI arguments |
cwd |
Option<String> |
None |
Working directory |
port |
u16 |
0 |
TCP port (0 = random) |
use_stdio |
bool |
true |
Use stdio transport |
cli_url |
Option<String> |
None |
External server URL |
log_level |
String |
"info" |
CLI log level |
auto_start |
bool |
true |
Auto-start on first use |
auto_restart |
bool |
true |
Auto-restart on crash |
env |
Option<HashMap> |
None |
Environment variables |
github_token |
Option<String> |
None |
GitHub auth token |
use_logged_in_user |
Option<bool> |
None |
Use stored OAuth |
Connection Modes
stdio (default)
The client spawns the CLI process and communicates via stdin/stdout pipes. This is the recommended mode for most use cases.
let client = new;
TCP
The client spawns the CLI process with a TCP listener and connects via socket.
let client = new;
External Server
Connect to an already-running CLI server.
let client = new;
Protocol Version
The SDK verifies protocol compatibility with the server on startup. The current protocol version is 2. If there is a mismatch, the start() call returns a CopilotError::ProtocolMismatch error.
Error Handling
All fallible operations return Result<T, CopilotError>. Error variants include:
CopilotError::JsonRpc- Server returned a JSON-RPC errorCopilotError::Timeout- Request timed outCopilotError::ConnectionClosed- Connection droppedCopilotError::ProtocolMismatch- SDK/server version incompatibleCopilotError::SessionError- Session-level errorCopilotError::NotConnected- Client not connectedCopilotError::ProcessSpawn- Failed to start CLI process
License
MIT - See LICENSE for details.