8000 Add Rust support by solatticus · Pull Request #64 · github/copilot-sdk · GitHub
[go: up one dir, main page]

Skip to content

Conversation

@solatticus
Copy link

Rust SDK for the Copilot CLI

I'm sure there's plenty to improve or replace, but just to get the ball rolling ->

  • async/await
  • typed tool definitions with automatic JSON schema generation via schemars
  • streaming support for assistant messages and reasoning
  • stdio and TCP transport modes
  • documentation and examples
  • feature parity with the NodeJS and Python SDKs
  • includes custom tool support, session management, and auto-reconnect

Files Added

  rust/
  ├── Cargo.toml          # Package manifest
  ├── README.md        # Documentation
  ├── src/
  │   ├── lib.rs                # Public API exports
  │   ├── client.rs           # CopilotClient implementation
  │   ├── session.rs        # CopilotSession implementation
  │   ├── jsonrpc.rs        # JSON-RPC 2.0 client
  │   ├── error.rs            # Error types
  │   ├── tool.rs             # Custom tool support
  │   ├── types.rs           # Configuration types
  │   └── generated/      # Auto-generated session event types
  ├── examples/
  │   └── basic.rs            # Usage example
  └── e2e/                      # End-to-end test harness

Usage

  use copilot_sdk::{CopilotClient, ClientOptions, SessionConfig, MessageOptions};
  use std::sync::Arc;

  #[tokio::main]
  async fn main() -> Result<(), Box<dyn std::error::Error>> {
      let client = Arc::new(CopilotClient::new(Some(ClientOptions {
          auto_restart: Some(true),
          ..Default::default()
      }))?);

      client.start_arc().await?;

      let session = client.create_session(None).await?;

      session.on(Arc::new(|event| {
          println!("Event: {:?}", event.event_type);
      }));

      session.send(MessageOptions {
          prompt: "Hello!".to_string(),
          ..Default::default()
      }).await?;

      client.stop().await;
      Ok(())
  }

Testing

  • cargo build - compiles successfully
  • cargo test - all unit tests pass
  • cargo clippy -- -D warnings - no warnings
  • E2E tests with live CLI server

solatticus and others added 4 commits January 20, 2026 22:58
Add Rust language support to the Copilot SDK repository with:

- CopilotClient for process spawning and connection management (stdio/TCP)
- CopilotSession for event subscriptions and message handling
- JSON-RPC 2.0 client with Content-Length framing
- Tool definition system using schemars for JSON Schema generation
- Session event types matching other SDK implementations
- E2E test harness and basic example
- README with usage documentation

Update justfile with format-rust, lint-rust, test-rust targets.
Update repository README and CONTRIBUTING docs.
BREAKING CHANGES:
- CopilotClient::new() now returns Result<Self, CopilotError>
- SessionEventHandler now takes Arc<SessionEvent> instead of SessionEvent

Fixes:
- Add MAX_MESSAGE_SIZE (100MB) limit to prevent DoS via Content-Length
- Remove panics on invalid user input, return errors instead
- Replace block_on() calls with proper async handling
- Add bounded task spawning with semaphore (limit 100)
- Fix destroy()/dispatch_event() race condition with AtomicBool
- Remove dead code (response_write_tx field)

Also adds comprehensive documentation to all public types.
Add automatic reconnection when the CLI process exits or the connection
is lost. This matches the behavior of the NodeJS SDK.

Changes:
- Add disconnect callback to JsonRpcClient that fires when read loop exits
- Add start_arc() method for Arc-wrapped clients with auto-reconnect
- Add reconnect logic that stops and restarts the connection
- Add session invalidation to notify handlers of connection loss
- Sessions receive SessionError events when connection is lost

The implementation uses a channel-based approach to decouple the sync
disconnect callback from async reconnection handling, ensuring all
futures remain Send-safe for tokio::spawn.
@solatticus solatticus requested a review from a team as a code owner January 21, 2026 05:35
@SteveSandersonMS
Copy link
Contributor

Thanks for offering to contribute this, @solatticus!

Sorry we didn't previously clarify this, but currently we are not accepting additional language SDKs in this repo. We're evolving both Copilot CLI and its SDK extremely quickly right now, and don't have capacity to let the number of languages we maintain grow beyond the existing four. This might change in the future but we've decided to lock it to the four languages we see most demand for in the short term (JS/TS on Node, Python, Go, and C#).

Instead, would you be interested in maintaining a Rust Copilot SDK as an external project? If so, we'd love to link to it from our docs.

(Sidenote: to clarify this in the future, I've filed #68)

@solatticus
Copy link
Author

Fair enough! Thanks for the clarification.

I'll leave it up, but can't make any maintenance promises!

cheers

@solatticus solatticus closed this Jan 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

0