Skip to main content

Crate rusty_claw

Crate rusty_claw 

Source
Expand description

Rusty Claw - Rust implementation of the Claude Agent SDK

This crate provides a Rust implementation of the Claude Agent SDK, architecturally inspired by Anthropic’s Python SDK (claude-agent-sdk-python) licensed under MIT.

§Overview

Rusty Claw enables building Claude-powered agents in Rust with support for:

  • Bidirectional JSONL transport over stdio
  • Claude Control Protocol (CCP) message handling
  • Model Context Protocol (MCP) tool integration
  • Hook system for lifecycle events
  • Procedural macros for ergonomic tool definitions

§Architecture

The SDK is organized into several key modules:

  • transport: Low-level JSONL message transport over stdio
  • control: Claude Control Protocol implementation
  • mcp: Model Context Protocol integration
  • hooks: Lifecycle event hooks
  • error: Error types and handling

§Subagent Support

Rusty Claw supports spawning and managing subagents with dedicated prompts and tool restrictions:

use rusty_claw::prelude::*;
use rusty_claw::options::AgentDefinition;
use std::collections::HashMap;

// Define specialized agents
let mut agents = HashMap::new();
agents.insert(
    "researcher".to_string(),
    AgentDefinition {
        description: "Research agent for code analysis".to_string(),
        prompt: "You are a research assistant".to_string(),
        tools: vec!["Read".to_string(), "Grep".to_string()],
        model: Some("claude-sonnet-4".to_string()),
    },
);

// Configure lifecycle hooks
let mut hooks = HashMap::new();
hooks.insert(
    HookEvent::SubagentStart,
    vec![HookMatcher::tool("Bash")],
);

// Build options and connect client
let options = ClaudeAgentOptions::builder()
    .agents(agents)
    .hooks(hooks)
    .build();

let mut client = ClaudeClient::new(options)?;
client.connect().await?;

See examples/subagent_usage.rs for a complete working example.

For detailed documentation on subagent lifecycle hooks, see docs/HOOKS.md.

§Example

use rusty_claw::prelude::*;
use tokio_stream::StreamExt;

#[tokio::main]
async fn main() -> Result<(), ClawError> {
    let options = ClaudeAgentOptions::builder()
        .allowed_tools(vec!["Read".into(), "Edit".into(), "Bash".into()])
        .permission_mode(PermissionMode::AcceptEdits)
        .build();

    let mut stream = query("Find and fix the bug in auth.py", Some(options)).await?;

    while let Some(message) = stream.next().await {
        match message? {
            Message::Assistant(msg) => {
                for block in msg.message.content {
                    if let ContentBlock::Text { text } = block {
                        println!("{}", text);
                    }
                }
            }
            Message::Result(ResultMessage::Success { result, .. }) => {
                println!("Done: {}", result);
            }
            _ => {}
        }
    }
    Ok(())
}

§License

Licensed under MIT. See LICENSE file for details.

Re-exports§

pub use query::query;

Modules§

client
Client for interactive sessions with Claude CLI
control
Claude Control Protocol (CCP) implementation
error
Error types and utilities
hooks
Hook system for lifecycle events
mcp_server
Model Context Protocol (MCP) integration
messages
Message types and structures
options
Configuration options and builder
permissions
Permission management for tool usage control
prelude
Common imports for rusty_claw users
query
Simple query API for one-shot Claude interactions
transport
Low-level transport layer for JSONL communication over stdio

Attribute Macros§

claw_tool
Attribute macro for defining MCP tools