claude-codes
A typed Rust interface for the Claude Code JSON protocol.
Overview
This library provides type-safe bindings for communicating with the Claude CLI via its JSON Lines protocol. It handles message serialization, streaming responses, and session management.
Note: The Claude CLI protocol is unstable and may change between versions. This crate tracks protocol changes and will warn if you're using an untested CLI version.
Features
- Type-safe message encoding/decoding
- JSON Lines protocol support with streaming
- Async and sync client implementations
- Image support (JPEG, PNG, GIF, WebP) with base64 encoding
- Tool use blocks for Claude's tool capabilities
- OAuth and API key authentication via environment variables
- UUID-based session management
Installation
Default Installation (All Features)
Requires the Claude CLI (claude binary) to be installed and available in PATH.
Feature Flags
types- Core message types only (WASM-compatible, minimal dependencies)sync-client- Synchronous client with blocking I/Oasync-client- Asynchronous client with tokio runtime- Default - All features enabled
Types Only
[]
= { = "2", = false, = ["types"] }
Sync Client Only
[]
= { = "2", = false, = ["sync-client"] }
Async Client Only
[]
= { = "2", = false, = ["async-client"] }
WASM Support
The types feature is fully compatible with wasm32-unknown-unknown, making it suitable for sharing Claude message types between native and browser/WASM code:
[]
= { = "2", = false, = ["types"] }
This gives you access to all the typed message structures (ClaudeInput, ClaudeOutput, ContentBlock, etc.) without pulling in tokio or other native-only dependencies. Useful for:
- Frontend applications that communicate with a Claude proxy
- Shared type definitions across native backend and WASM frontend
- Any WASM context needing Claude protocol types
Usage
Async Client
use AsyncClient;
async
Sync Client
use ;
use Uuid;
Sending Images
use ;
use ;
async
Raw Protocol Access
use ;
let json_line = r#"{"type":"assistant","message":{...}}"#;
let output: ClaudeOutput = deserialize?;
let serialized = serialize?;
Compatibility
Tested version: Claude CLI 2.1.3
If you're using a different CLI version, please report whether it works at: https://github.com/meawoppl/rust-claude-codes/issues
Include:
- Your Claude CLI version (
claude --version) - Whether messages serialized/deserialized correctly
- Any errors encountered
License
Apache-2.0. See LICENSE.
Contributing
Contributions welcome. Any contribution submitted for inclusion will be licensed under Apache-2.0.