openpencil/vendor/anthropic-agent-sdk
Kayshen-X 12747384c6 fix(agent): tolerate unknown Claude stream message types
Newer Claude Code CLIs emit stream message types the bundled SDK
was not compiled against — `rate_limit_event` being the one that
surfaced — and serde aborted the whole chat stream on the first
one ("unknown variant rate_limit_event").

The `Message` enum gains a `#[serde(other)] Unknown` catch-all so
any unmodelled `type` deserializes cleanly instead of failing the
parse; `hooks.rs` and `chat_claude.rs` match it as a silent no-op
(an unknown event carries no hook payload and no chat turn).
2026-05-22 11:39:57 +08:00
..
.cargo chore: vendor anthropic-agent-sdk out of crates/ 2026-05-16 13:14:20 +08:00
src fix(agent): tolerate unknown Claude stream message types 2026-05-22 11:39:57 +08:00
.codannaignore chore: vendor anthropic-agent-sdk out of crates/ 2026-05-16 13:14:20 +08:00
.gitignore chore: vendor anthropic-agent-sdk out of crates/ 2026-05-16 13:14:20 +08:00
Cargo.lock chore: vendor anthropic-agent-sdk out of crates/ 2026-05-16 13:14:20 +08:00
Cargo.toml chore: vendor anthropic-agent-sdk out of crates/ 2026-05-16 13:14:20 +08:00
CHANGELOG.md chore: vendor anthropic-agent-sdk out of crates/ 2026-05-16 13:14:20 +08:00
LICENSE chore: vendor anthropic-agent-sdk out of crates/ 2026-05-16 13:14:20 +08:00
README.md chore: vendor anthropic-agent-sdk out of crates/ 2026-05-16 13:14:20 +08:00
README_API.md chore: vendor anthropic-agent-sdk out of crates/ 2026-05-16 13:14:20 +08:00
SECURITY.md chore: vendor anthropic-agent-sdk out of crates/ 2026-05-16 13:14:20 +08:00

Claude Agent SDK for Rust

Rust SDK for building AI agents powered by Claude Code. Mirrors the TypeScript Claude Agent SDK with idiomatic Rust patterns.

Installation

Prerequisites:

  • Rust 1.85.0+ (edition 2024)
  • Node.js
  • Claude Code 2.0.75+: npm install -g @anthropic-ai/claude-code
[dependencies]
anthropic-agent-sdk = "0.2"
tokio = { version = "1", features = ["rt-multi-thread", "macros"] }

Quick Start

use anthropic_agent_sdk::{query, Message, ContentBlock, StreamExt};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let stream = query("What is 2 + 2?", None).await?;
    let mut stream = Box::pin(stream);

    while let Some(message) = stream.next().await {
        if let Message::Assistant { message, .. } = message? {
            for block in &message.content {
                if let ContentBlock::Text { text } = block {
                    println!("{}", text);
                }
            }
        }
    }
    Ok(())
}

Examples

# Core functionality
cargo run --example simple_query
cargo run --example convenience_methods
cargo run --example bidirectional_demo
cargo run --example message_queue_demo
cargo run --example session_binding_demo
cargo run --example interactive_client

# Hooks and permissions
cargo run --example hooks_demo
cargo run --example permissions_demo
cargo run --example hooks_lifecycle_test

# Introspection and runtime
cargo run --example introspection_demo
cargo run --example result_fields_demo
cargo run --example runtime_setters_demo

# Security
cargo run --example security_demo

# OAuth authentication
cargo run --example oauth_demo
cargo run --example oauth_demo -- status
cargo run --example oauth_demo -- logout

# Structured output
cargo run --example structured_output_demo

# MCP (requires --features rmcp for mcp_server)
cargo run --example mcp_integration
cargo run --example mcp_server --features rmcp

TypeScript SDK Parity

Hook Events

Event TypeScript Rust SDK
PreToolUse ✓ ✓
PostToolUse ✓ ✓
PostToolUseFailure ✓ ✓
Notification ✓ ✓
UserPromptSubmit ✓ ✓
SessionStart ✓ ✓
SessionEnd ✓ ✓
Stop ✓ ✓
SubagentStart ✓ ✓
SubagentStop ✓ ✓
PreCompact ✓ ✓
PermissionRequest ✓ ✓

Query/Client Methods

Method TypeScript Rust SDK
interrupt() ✓ ✓
setPermissionMode() ✓ ✓
setModel() ✓ ✓
setMaxThinkingTokens() ✓ ✓
supportedCommands() ✓ ✓
supportedModels() ✓ ✓
mcpServerStatus() ✓ ✓
accountInfo() ✓ ✓

Options

Option TypeScript Rust SDK
allowedTools ✓ ✓
disallowedTools ✓ ✓
systemPrompt ✓ ✓
mcpServers ✓ ✓
permissionMode ✓ ✓
canUseTool ✓ ✓
hooks ✓ ✓
agents ✓ ✓
maxTurns ✓ ✓
model ✓ ✓
cwd ✓ ✓
env ✓ ✓
resume ✓ ✓
forkSession ✓ ✓
settingSources ✓ ✓
maxBudgetUsd ✓ ✓
maxThinkingTokens ✓ ✓
fallbackModel ✓ ✓
outputFormat ✓ ✓
sandbox ✓ ✓
plugins ✓ ✓
betas ✓ ✓
strictMcpConfig ✓ ✓
resumeSessionAt ✓ ✓
allowDangerouslySkipPermissions ✓ ✓
pathToClaudeCodeExecutable ✓ ✓
stderr ✓ ✓
tools (preset) ✓ ✓
enableFileCheckpointing ✓ ✓
sessionId ✓ ✓

MCP Server Types

Type TypeScript Rust SDK
stdio ✓ ✓
sse ✓ ✓
http ✓ ✓
sdk (in-process) ✓ ✓

Result Message Fields

Field TypeScript Rust SDK
modelUsage ✓ ✓
permission_denials ✓ ✓
structured_output ✓ ✓
errors ✓ ✓

Development

cargo build
cargo test
cargo clippy
cargo doc --open

Security

The SDK implements strict security measures:

  • Environment variables: Dangerous vars (LD_PRELOAD, PATH, etc.) cause errors
  • CLI arguments: Only allowlisted flags permitted, others rejected
  • Session binding: Auto-binds on first Result, validates on send
  • Buffer limits: Configurable max buffer size (default 1MB)
  • 100% safe Rust: No unsafe code

See SECURITY.md for full documentation.

OAuth Authentication

For Claude Max/Pro subscribers, authenticate without API keys:

use anthropic_agent_sdk::auth::OAuthClient;

let client = OAuthClient::new()?;
let token = client.authenticate().await?;
// Token is cached in platform-specific config directory

See oauth_demo example for full usage including status check and logout.

Documentation

License

MIT