claude-wrapper
A type-safe Claude Code CLI wrapper for Rust
Overview
claude-wrapper provides a type-safe builder-pattern interface for invoking the claude CLI programmatically. It follows the same design philosophy as docker-wrapper and terraform-wrapper: each CLI subcommand is a builder struct that produces typed output.
Perfect for Rust applications that need to integrate with Claude Code CLI.
Installation
Quick Start
use ;
async
Two-Layer Builder Architecture
The Claude client holds shared configuration (binary path, environment, timeout). Command builders hold per-invocation options and call execute(&claude).
Claude Client
Configure once, reuse across commands:
let claude = builder
.env
.timeout_secs
.build?;
Options:
binary_path()- Path toclaudebinary (auto-detected by default)working_dir()- Working directory for commandsenv()- Set environment variablestimeout_secs()- Command timeout (default: 300)global_args()- Additional global args
Command Builders
Each command is a separate builder. Available commands:
- QueryCommand - Execute queries with full option coverage (28 options)
- McpListCommand - List MCP servers
- McpAddCommand - Add HTTP or stdio MCP server
- McpAddJsonCommand - Add server from JSON config
- McpRemoveCommand - Remove MCP server
- McpAddFromDesktopCommand - Add desktop MCP server
- McpResetProjectChoicesCommand - Reset project-specific choices
- PluginListCommand - List plugins
- PluginInstallCommand - Install plugin
- PluginUninstallCommand - Uninstall plugin
- PluginEnableCommand - Enable plugin
- PluginDisableCommand - Disable plugin
- PluginUpdateCommand - Update plugin
- PluginValidateCommand - Validate plugin
- AuthStatusCommand - Check authentication status
- VersionCommand - Get CLI version
- DoctorCommand - Run health check
- AgentsListCommand - List available agents
- RawCommand - Escape hatch for unsupported options
QueryCommand: The Workhorse
Full coverage of claude -p (print mode) options.
All QueryCommand Options
| Method | CLI Flag | Type | Description |
|---|---|---|---|
model() |
--model |
&str |
Model alias or full ID |
system_prompt() |
--system-prompt |
&str |
Replace default system prompt |
append_system_prompt() |
--append-system-prompt |
&str |
Append to system prompt |
output_format() |
--output-format |
OutputFormat |
text, json, stream-json |
max_budget_usd() |
--max-budget-usd |
f64 |
Spending cap |
permission_mode() |
--permission-mode |
PermissionMode |
default, acceptEdits, bypassPermissions, plan, auto |
allowed_tools() |
--allowed-tools |
&[&str] |
Tool permission allow list |
disallowed_tools() |
--disallowed-tools |
&[&str] |
Tool permission deny list |
tools() |
--tools |
&[&str] |
Restrict available tools |
mcp_config() |
--mcp-config |
&str |
MCP server config file |
strict_mcp_config() |
--strict-mcp-config |
- | Only use MCP from config |
add_dir() |
--add-dir |
&str |
Additional accessible directories |
effort() |
--effort |
Effort |
low, medium, high |
max_turns() |
--max-turns |
u32 |
Conversation turn limit |
json_schema() |
--json-schema |
&str |
Structured output validation |
agent() |
--agent |
&str |
Agent for session |
agents_json() |
--agents |
&str |
Custom agents JSON |
continue_session() |
--continue |
- | Resume most recent session |
resume() |
--resume |
&str |
Resume by session ID |
session_id() |
--session-id |
&str |
Use specific session ID |
fork_session() |
--fork-session |
- | Fork when resuming |
fallback_model() |
--fallback-model |
&str |
Fallback model |
no_session_persistence() |
--no-session-persistence |
- | Don't save session |
dangerously_skip_permissions() |
--dangerously-skip-permissions |
- | Bypass permissions |
file() |
--file |
&str |
File resources to download |
input_format() |
--input-format |
InputFormat |
text or stream-json |
include_partial_messages() |
--include-partial-messages |
- | Partial chunks |
settings() |
--settings |
&str |
Settings JSON file |
Usage Examples
Simple query with model override:
let output = new
.model
.execute
.await?;
println!;
JSON output with schema validation:
let output = new
.output_format
.json_schema
.execute
.await?;
With permissions and tools:
let output = new
.model
.permission_mode
.allowed_tools
.max_budget_usd
.execute
.await?;
Session management (low-level):
// Start new session
let output = new
.session_id
.execute
.await?;
// Resume later
let output = new
.resume
.execute
.await?;
Session management (type-safe):
use Session;
// Create from a previous query result
let mut session = from_result;
// Auto-resumes the session
let next = session.query.execute.await?;
// Fork to branch the conversation
let mut forked = session.fork;
let alt = forked.query.execute.await?;
// Track cumulative cost and turns
println!;
println!;
Streaming NDJSON Events
For real-time output, use stream_query():
use stream_query;
let output = stream_query.await?;
MCP Server Commands
List Servers
let output = new
.execute
.await?;
println!;
Add HTTP Server
new
.transport
.execute
.await?;
Add Stdio Server
new
.server_args
.env
.execute
.await?;
Add from JSON
new
.execute
.await?;
Remove Server
new
.execute
.await?;
MCP Config Builder
Generate .mcp.json files programmatically:
use McpConfigBuilder;
new
.http_server
.stdio_server
.write_to?;
Also supports:
env()- Environment variables for serversenvironment_file()- Load from env file
Plugin Management
// List plugins
new.execute.await?;
// Install plugin
new
.execute
.await?;
// Enable plugin
new
.execute
.await?;
// Update plugin
new
.execute
.await?;
Other Commands
// Check auth
new.execute.await?;
// Get version
new.execute.await?;
// Health check
new.execute.await?;
// List agents
new.execute.await?;
Escape Hatch: RawCommand
For unsupported options, use RawCommand:
let output = new
.arg
.arg
.arg
.execute
.await?;
Error Handling
All commands return Result<T>, where errors are typed with thiserror:
use Error;
match new.execute.await
Features
Optional Cargo features (all enabled by default):
json- JSON output parsing via serde_jsontempfile- Temporary file support for MCP config generation
Testing
Requires the claude CLI binary to be installed:
License
MIT OR Apache-2.0