Please check the build logs for more information.
See Builds for ideas on how to fix a failed build, or Metadata for how to configure docs.rs builds.
If you believe this is docs.rs' fault, open an issue.
GitHub Copilot CLI SDK for Rust
A Rust SDK for programmatic access to the GitHub Copilot CLI.
Note: This SDK is in technical preview and may change in breaking ways.
See github/copilot-sdk for the equivalent SDKs in TypeScript, Python, Go, and .NET. The Rust SDK seeks parity with those SDKs; see Differences From Other SDKs below for the small set of intentional divergences.
Releases: github.com/github/copilot-sdk/releases?q=rust%2F — per-version release notes for the Rust crate.
Quick Start
use Arc;
use ;
use ApproveAllHandler;
# async
Architecture
Your Application
↓
github_copilot_sdk::Client (manages CLI process lifecycle)
↓
github_copilot_sdk::Session (per-session event loop + handler dispatch)
↓ JSON-RPC over stdio or TCP
copilot --server --stdio
The SDK manages the CLI process lifecycle: spawning, health-checking, and graceful shutdown. Communication uses JSON-RPC 2.0 over stdin/stdout with Content-Length framing (the same protocol used by LSP). TCP transport is also supported.
API Reference
Client
// Start a client (spawns CLI process)
let client = start.await?;
// Create a new session
let session = client.create_session.await?;
// Resume an existing session
let session = client.resume_session.await?;
// Low-level RPC
let result = client.call.await?;
let response = client.send_request.await?;
// Health check (echoes message back, returns typed PingResponse)
let pong = client.ping.await?;
// Shutdown
client.stop.await?;
ClientOptions:
| Field | Type | Description |
|---|---|---|
program |
CliProgram |
Resolve (default: auto-detect) or Path(PathBuf) (explicit) |
prefix_args |
Vec<OsString> |
Args before --server (e.g. script path for node) |
cwd |
PathBuf |
Working directory for CLI process |
env |
Vec<(OsString, OsString)> |
Environment variables for CLI process |
env_remove |
Vec<OsString> |
Environment variables to remove |
extra_args |
Vec<String> |
Extra CLI flags |
transport |
Transport |
Stdio (default), Tcp { port }, or External { host, port } |
With the default CliProgram::Resolve, Client::start() resolves the CLI in this order: an explicit CliProgram::Path(path), the COPILOT_CLI_PATH env var, then the bundled CLI that was embedded at build time. There is no PATH scanning — if you've opted out of bundling (default-features = false) you must supply either CliProgram::Path or COPILOT_CLI_PATH.
Session
Created via Client::create_session or Client::resume_session. Owns an internal event loop that dispatches CLI callbacks to the focused handler traits you install on SessionConfig, and broadcasts session events through subscribe().
use MessageOptions;
// Simple send — &str / String convert into MessageOptions automatically.
// Returns the assigned message ID for correlation with later events.
let _id = session.send.await?;
// Send with mode and attachments
let _id = session
.send
.await?;
// Message history
let messages = session.get_events.await?;
// Abort the current agent turn
session.abort.await?;
// Model management
session.set_model.await?;
// Generated typed RPCs cover lower-level session operations.
let model = session.rpc.model.get_current.await?;
let mode = session.rpc.mode.get.await?;
// Workspace files
let files = session.rpc.workspaces.list_files.await?;
let content = session
.rpc
.workspaces
.read_file
.await?;
// Plan management
let plan = session.rpc.plan.read.await?;
session
.rpc
.plan
.update
.await?;
// Fleet (sub-agents)
session
.rpc
.fleet
.start
.await?;
// Cleanup (preserves on-disk session state for later resume)
session.disconnect.await?;
Typed RPC namespace
High-level helpers are convenience wrappers over a fully-typed
JSON-RPC namespace generated from the GitHub Copilot CLI schema. Client::rpc()
and Session::rpc() give direct access to every method on the wire,
including ones with no helper today, with strongly-typed request and
response structs.
// Common generated RPCs.
let files = session.rpc.workspaces.list_files.await?.files;
let models = client.rpc.models.list.await?.models;
// Methods with no helper — full schema-typed access.
let agents = session.rpc.agent.list.await?.agents;
let tasks = session.rpc.tasks.list.await?.tasks;
let forked = client
.rpc
.sessions
.fork
.await?;
New RPCs land in the namespace immediately as the schema regenerates; helpers are added on top only when an ergonomic story is worth the maintenance.
Handler Traits
The SDK exposes five focused handler traits, one per CLI callback type. Implement only the traits you need and install each with the matching SessionConfig setter. Each trait has a single async fn handle(...) method:
| Trait | Setter | Purpose |
|---|---|---|
PermissionHandler |
with_permission_handler(...) |
Approve/deny tool-use permission requests |
ElicitationHandler |
with_elicitation_handler(...) |
Respond to structured elicitation prompts |
UserInputHandler |
with_user_input_handler(...) |
Answer free-form / choice user-input prompts |
ExitPlanModeHandler |
with_exit_plan_mode_handler(...) |
Respond when the agent exits plan mode |
AutoModeSwitchHandler |
with_auto_mode_switch_handler(...) |
Respond to automatic mode-switch proposals |
The CLI's requestPermission / requestElicitation / requestUserInput / etc. wire flags are derived automatically from which traits you've installed — clients that don't install a handler are silently skipped, letting another connected client handle the request.
use Arc;
use async_trait;
use ;
use ;
;
let config = default.with_permission_handler;
A single type can implement multiple handler traits — share one Arc<Self> across the setters by cloning:
let h = new;
let config = default
.with_permission_handler
.with_user_input_handler;
The built-in ApproveAllHandler and DenyAllHandler implement PermissionHandler for the common cases. To observe streamed session events (assistant messages, tool calls, etc.), call session.subscribe() — see Streaming below.
SessionConfig
let config = SessionConfig
.with_elicitation_handler
.with_permission_handler;
let session = client.create_session.await?;
Session Hooks
Hooks intercept CLI behavior at lifecycle points — tool use, prompt submission, session start/end, and errors. Install a SessionHooks impl with [SessionConfig::with_hooks] — the SDK auto-enables hooks in SessionConfig when one is set.
use Arc;
use *;
use async_trait;
;
let session = client
.create_session
.await?;
Hook events: PreToolUse, PostToolUse, UserPromptSubmitted, SessionStart, SessionEnd, ErrorOccurred. Each carries typed input/output structs. Return HookOutput::None for events you don't handle.
System Message Transforms
Transforms customize system message sections during session creation. The SDK injects action: "transform" entries for each section ID your transform handles.
use *;
use async_trait;
;
let session = client
.create_session
.await?;
Tool Registration
Define client-side tools as named types implementing ToolHandler and attach
them to Tool declarations via Tool::with_handler, then install via
SessionConfig::with_tools. Enable the derive feature for schema_for::<T>()
— it generates JSON Schema from Rust types via schemars.
use Arc;
use ApproveAllHandler;
use ;
use ;
use Deserialize;
use async_trait;
;
let tool = new
.with_description
.with_parameters
.with_handler;
let config = default
.with_permission_handler
.with_tools;
let session = client.create_session.await?;
Tools are named types (not closures) — visible in stack traces and navigable via "go to definition". The SDK registers each tool's handler under its Tool::name and surfaces the same Tool definitions to the CLI automatically.
Tools without an attached handler (Tool::with_handler never called) are declaration-only: the SDK advertises them on the wire but doesn't dispatch invocations to anything. Useful when another connected client services the tool.
For trivial tools that don't need a named type, the define_tool helper function (available with the derive feature) collapses the definition to a single expression and returns a fully-formed Tool with handler attached:
use ;
use ToolResult;
use Deserialize;
let tool = define_tool;
let config = default
.with_permission_handler
.with_tools;
The closure receives the full ToolInvocation alongside the deserialized parameters, so handlers that need inv.session_id or inv.tool_call_id for telemetry, streaming updates, or scoped lookups can use them directly. Use _inv when you don't need the metadata.
Reach for the ToolHandler trait directly when you need shared state across multiple methods or want a named type that shows up by name in stack traces.
Permission Policies
Set a permission policy directly on SessionConfig with the chainable builders. They install a synthesized PermissionHandler so only permission requests are intercepted; every other event flows through unchanged.
let session = client
.create_session
.await?;
The policy builders set the permission handler slot directly; they're equivalent to calling
with_permission_handler(...)with the corresponding built-in (ApproveAllHandler,DenyAllHandler, orpermission::approve_if(...)).
The permission module also exposes the policy primitives as standalone helpers for the rare case where you want to construct the handler value separately and install it via with_permission_handler:
use permission;
let handler = approve_if;
// or permission::approve_all() / permission::deny_all()
let session = client
.create_session
.await?;
Elicitation
To opt your client into receiving elicitation.requested broadcasts, install an ElicitationHandler on the session config. The wire flag requestElicitation is derived from the presence of the handler; clients without one are silently skipped, allowing other connected clients on the same CLI to handle the request.
use async_trait;
use ;
use ;
;
let config = default
.with_permission_handler
.with_elicitation_handler;
The handler receives a message, optional JSON Schema for form fields, and an optional mode. Known modes include Form and Url, but the mode may be absent or an unknown future value.
User Input Requests
Some sessions ask the user free-form questions (or multiple-choice prompts) outside the elicitation flow. Install a UserInputHandler and the SDK will forward userInput.request callbacks:
use async_trait;
use ;
use SessionId;
;
let config = default
.with_user_input_handler;
Return None to signal "no answer available" (the CLI falls back to its own prompt).
Slash Commands
Register named commands so users can invoke them as /name args from the TUI:
use ;
use async_trait;
;
let mut config = default;
config.commands = Some;
Only name and description are sent over the wire; the handler stays in your process. Returning Err(_) surfaces the message back through the TUI.
Streaming
Set streaming: true to receive incremental delta events alongside finalized messages:
let mut config = default;
config.streaming = Some;
let mut events = session.subscribe;
while let Ok = events.recv.await
When streaming is off (the default), only the final assistant.message and assistant.reasoning events fire. Delta events arrive in order; concatenating their delta text payloads reproduces the final message.
Infinite Sessions
Enable the SDK's session-store integration so conversations persist across CLI restarts and grow beyond the model's context window via automatic compaction:
use InfiniteSessionConfig;
let mut infinite = default;
infinite.workspace_path = Some;
let mut config = default;
config.infinite_sessions = Some;
The CLI emits session.compaction_start / session.compaction_complete events around each compaction. The session id remains stable across compactions; resume with Client::resume_session to pick up a prior conversation. Workspace state lives under ~/.copilot/session-state/{sessionId} by default — override with workspace_path to relocate.
Custom Providers (BYOK)
Route model traffic through your own inference endpoint instead of GitHub's hosted models:
use ProviderConfig;
let mut provider = default;
provider.provider_type = Some;
provider.base_url = "https://my-proxy.example.com/v1".to_string;
provider.bearer_token = Some;
let mut config = default;
config.provider = Some;
Provider types include "openai", "azure", and "anthropic". Set wire_api to "completions" or "responses" (OpenAI/Azure only). Custom headers go in provider.headers. The SDK forwards the configuration to the CLI verbatim — the CLI handles the upstream call, including authentication.
Telemetry
Forward OpenTelemetry signals from the spawned CLI process to your collector:
use ;
let mut telem = default;
telem.exporter_type = Some;
telem.otlp_endpoint = Some;
telem.source_name = Some;
let mut opts = default;
opts.telemetry = Some;
let client = start.await?;
The SDK injects the appropriate environment variables (COPILOT_OTEL_EXPORTER_TYPE, OTEL_EXPORTER_OTLP_ENDPOINT, ...) into the spawned CLI process. The SDK takes no OpenTelemetry dependency; the CLI itself owns the exporter pipeline. Caller-supplied ClientOptions::env entries override telemetry-injected values.
Progress Reporting (send_and_wait)
For fire-and-forget messaging where you need to block until the agent finishes:
use Duration;
use MessageOptions;
// Sends a message and blocks until session.idle or session.error
session
.send_and_wait
.await?;
Default timeout is 60 seconds. Only one send_and_wait can be active per session — concurrent calls return an error.
Newtypes
SessionId — a newtype wrapper around String that prevents accidentally passing workspace IDs or request IDs where session IDs are expected. Transparent serialization (#[serde(transparent)]), zero-cost Deref<Target=str>, and ergonomic comparisons with &str and String.
use SessionId;
let id = new;
assert_eq!; // compare with &str
let raw: String = id.into_inner; // unwrap when needed
Error Handling
The SDK uses a typed error enum:
// Check if the transport is broken (caller should discard the client)
if err.is_transport_failure
Differences From Other SDKs
The Rust SDK aligns closely with the Node, Python, Go, and .NET SDKs but diverges in a few places where Rust idiom or the type system gives a clearly better shape, and exposes a small additional surface where the language affords ergonomics the dynamically-typed SDKs don't.
Shape divergence
SessionFsProviderregistration is direct, not factory-closure. Where Node/Python/Go/.NET accept a closure that the runtime calls on each session-create to build a fresh provider, the Rust SDK takesArc<dyn SessionFsProvider>directly via [SessionConfig::with_session_fs_provider]. The factory pattern doesn't cleanly express in Rust at the session-config call site — there is noSessionvalue to thread in, and the SDK already prefers traits over boxed closures for handler-shaped APIs (PermissionHandler,ToolHandler,SessionHooks,SystemMessageTransform).
use Arc;
use ;
let mut options = default;
options.session_fs = Some;
let client = start.await?;
let session = client
.create_session
.await?;
See examples/session_fs.rs for a complete
in-memory provider implementation.
- Canvas action dispatch is a single trait method, not per-action closures.
The Node SDK binds an optional
handlerclosure on each entry of a canvas'sactions[]. The Rust SDK exposesCanvasHandler::on_actionand expects the implementor to match onctx.action_name. Same reasoning asSessionFsProvider: per-callbackBox<dyn Fn>fields fightSend + Sync + 'staticand skip exhaustiveness checks, and the SDK prefers trait + default-impl methods for handler-shaped extension points.
Rust-only API
A handful of conveniences exist only on the Rust SDK as of 0.1.0. These are surface areas where Rust idiom (newtypes, enums, trait objects) gives a clearly nicer shape than Node/Python/Go/.NET currently expose. Rust gets to be Rust here — cross-SDK parity for these is a post-release conversation, not a release blocker. None of these are deprecated and none of them are scheduled for removal.
- Typed newtypes —
SessionIdandRequestIdare#[serde(transparent)]newtypes aroundString, so the type system distinguishes a session identifier from an arbitraryStringat compile time. Node/Python/Go use bare strings. - Permission policy builders —
permission::approve_all,permission::deny_all, andpermission::approve_if(predicate)incrate::permissionprovide composable, no-handler-neededPermissionHandlershortcuts. Other SDKs require a full handler implementation for these patterns. Client::from_streams— connect to a CLI server over arbitrary caller-suppliedAsyncRead/AsyncWrite. Useful for testing, in-process embedding, or custom transports. Other SDKs are spawn-only or fixed-stdio.enum Transport { Stdio, Tcp, External }— explicit, exhaustive transport selector onClientOptions::transport. Node/Python/Go rely on conditional config field combinations instead.- Split
prefix_args/extra_argsonClientOptions— separate arg vectors for "prepend before subcommand" vs "append after the built-in flags", giving precise control over CLI invocation order without string-splicing.
Layout
| File | Description |
|---|---|
lib.rs |
Client, ClientOptions, CliProgram, Transport, Error |
session.rs |
Session struct, event loop, send/send_and_wait, Client::create_session/resume_session |
subscription.rs |
EventSubscription / LifecycleSubscription (Stream-able observer handles for subscribe() / subscribe_lifecycle()) |
handler.rs |
PermissionHandler, ElicitationHandler, UserInputHandler, ExitPlanModeHandler, AutoModeSwitchHandler traits; ApproveAllHandler, DenyAllHandler |
hooks.rs |
SessionHooks trait, HookEvent/HookOutput enums, typed hook inputs/outputs |
transforms.rs |
SystemMessageTransform trait, section-level system message customization |
tool.rs |
ToolHandler trait, define_tool, schema_for::<T>() (with derive feature) |
types.rs |
CLI protocol types (SessionId, SessionEvent, SessionConfig, Tool, etc.) |
resolve.rs |
Bundled-CLI resolution (copilot_binary) |
embeddedcli.rs |
Embedded CLI extraction (gated on the default bundled-cli feature) |
router.rs |
Internal per-session event demux |
jsonrpc.rs |
Internal Content-Length framed JSON-RPC transport |
Embedded CLI
The SDK bundles the Copilot CLI binary inside the consumer's compiled crate by default. No env var setup, no separate install — just cargo build and you get a self-contained binary.
To opt out (e.g. for binary-size-sensitive consumers, or environments that provide the CLI via PATH), set default-features = false:
= { = "0.1", = false }
How it works
-
Pinned at publish time. When the rust crate is published, a workflow step writes
bundled_cli_version.txt(CLI version + per-platform SHA-256 hashes) into the crate from the in-effectnodejs/package-lock.jsonand the matching GitHub Release'sSHA256SUMS.txt. This file is gitignored locally; it only exists in the published crate tarball. -
Build time: The SDK's
build.rsresolves the version + per-platform SHA-256:COPILOT_CLI_VERSIONenv var (advanced override; fetches liveSHA256SUMS.txt).- Otherwise,
bundled_cli_version.txtfrom the published crate. - Otherwise (mono-repo contributor build), live read from
../nodejs/package-lock.json+ live fetch ofSHA256SUMS.txt.
It then downloads the platform-appropriate archive from the
github/copilot-cliGitHub Releases (copilot-{platform}.tar.gzon macOS/Linux,.zipon Windows), verifies the SHA-256, extracts thecopilotbinary, compresses it with zstd, and embeds viainclude_bytes!(). -
Runtime: On the first call to
github_copilot_sdk::Client::start(), the embedded archive is lazily extracted to the platform cache dir (%LOCALAPPDATA%\github-copilot-sdk-{version}\on Windows,~/Library/Caches/github-copilot-sdk-{version}/on macOS,$XDG_CACHE_HOME/github-copilot-sdk-{version}/(or~/.cache/...) on Linux). Subsequent runs reuse the extracted binary.
Overriding the extraction location
Use [ClientOptions::with_bundled_cli_extract_dir] when you need to place the extracted binary somewhere other than the platform cache dir (CI runners with ephemeral homes, sandboxes that disallow cache paths, etc.):
use PathBuf;
use ;
let options = new
.with_bundled_cli_extract_dir;
let client = start.await?;
Resolution priority
copilot_binary() checks these sources in order:
- Explicit
CliProgram::Path(path)onClientOptions::program COPILOT_CLI_PATHenvironment variable- Embedded CLI (when the
bundled-clifeature is enabled, which it is by default)
There is no PATH scanning. If both 1+2 are unset and the SDK was built with default-features = false, Client::start returns Error::BinaryNotFound.
Platforms
Supported: darwin-arm64, darwin-x64, linux-x64, linux-arm64, win32-x64, win32-arm64. The target platform is auto-detected from CARGO_CFG_TARGET_OS and CARGO_CFG_TARGET_ARCH (cross-compilation works).
Features
| Feature | Default | Description |
|---|---|---|
bundled-cli |
✓ | Build-time CLI embedding. Pulls in dirs, tar+flate2 (Linux/macOS), or zip (Windows). Disable via default-features = false to opt out (e.g. when shipping a smaller binary or when always supplying the CLI via CliProgram::Path / COPILOT_CLI_PATH). |
derive |
— | schema_for::<T>() for generating JSON Schema from Rust types (adds schemars). Enable when defining tool parameters. |
# These examples use registry syntax for illustration; until the crate is
# published, use a path or git dependency instead.
# Default — bundles the Copilot CLI in your binary.
= "0.1"
# Opt out of bundling — resolve CLI from COPILOT_CLI_PATH or system PATH instead.
= { = "0.1", = false }
# Derive JSON Schema for tool parameters (adds to default bundled-cli).
= { = "0.1", = ["derive"] }