Version: 0.2.0
Purpose: Ruby SDK that provides a native Ruby interface to Anthropic's Claude Code CLI
Key Focus: Dynamic hooks configuration and comprehensive Claude Code CLI integration
This is a Ruby wrapper around the Claude Code CLI that:
- Converts CLI interactions into Ruby objects with strongly-typed message parsing
- Provides dynamic hooks configuration via the
settingsparameter (crucial functionality!) - Handles both streaming and simple query patterns for Claude Code interactions
- Manages CLI process lifecycle with proper error handling and resource cleanup
- Offers Ruby-idiomatic APIs with blocks, enumerables, and familiar patterns
-
Main Interface (
lib/claude_code_sdk.rb)ClaudeCodeSDK.query(prompt, options, &block)- Primary streaming interfaceClaudeCodeSDK.ask(prompt)- Simple text response helperClaudeCodeSDK.configure- Global configuration setup
-
Transport Layer (
lib/claude_code_sdk/transport/)Base- Abstract transport interfaceSubprocessCLI- ✅ ACTIVELY USED - Full streaming via subprocess (line 12 ininternal/client.rb)SimpleCLI- Alternative implementation, NOT currently used
IMPORTANT: AllSpark Builder extends
SubprocessCLIwith custom overrides inconfig/initializers/zzz_claude_code_sdk_override.rbfor Docker environment compatibility. -
Message System (
lib/claude_code_sdk/messages.rb)Messagebase class withidandtypeUserMessage,AssistantMessage,SystemMessage,ResultMessage,ThinkingMessageContent::*blocks:TextBlock,ToolUseBlock,ToolResultBlock
-
Configuration System (
lib/claude_code_sdk/types.rb,configuration.rb)Options- Per-query configuration with all Claude Code CLI flagsConfiguration- Singleton for global defaultsPermissionModeconstants:DEFAULT,ACCEPT_EDITS,BYPASS_PERMISSIONS
This is the most important feature for AllSpark Builder integration!
The SDK supports dynamic hooks via the settings parameter in Options. This enables session-specific hooks without modifying global config files.
# Three supported input formats:
options = ClaudeCodeSDK::Options.new(
settings: hooks_hash # Hash -> converted to JSON
# OR
settings: '{"hooks": {...}}' # JSON string -> passed directly
# OR
settings: "/path/file.json" # File path -> passed to CLI
)hooks_config = {
"hooks" => {
"PostToolUse" => [
{
"matcher" => "Bash|Write|Edit", # Regex pattern for tool names
"hooks" => [
{
"type" => "command",
"command" => "curl -X POST webhook_url -d \"$HOOK_INPUT\""
}
]
}
],
"PreToolUse" => [...],
# Available events: PreToolUse, PostToolUse, UserPromptSubmit,
# Stop, SubagentStop, PreCompact, SessionStart, SessionEnd
}
}Location: lib/claude_code_sdk/types.rb:56-76 - The to_cli_args method handles settings conversion
JSON Detection Logic: lines 84-93 - Distinguishes between JSON strings and file paths:
- JSON strings: Start/end with
{}or[] - File paths: Everything else passed directly to CLI
Environment Variables Available in Hooks:
$HOOK_INPUT- JSON data about the tool execution- Standard environment variables
Security Considerations:
- Hook commands execute with full system privileges
- Always sanitize user input in hook commands
- Use absolute paths and validate webhook URLs
# Run all tests
bundle exec rake spec
# Run with coverage
bundle exec rake coverage
# Integration tests (requires Claude Code CLI installed)
bundle exec rake integration
# Code style
bundle exec rubocop
# Documentation
bundle exec yard
# Interactive console with gem loaded
bundle exec rake consoleRuntime: Only zeitwerk ~> 2.6 for autoloading
Development: RSpec, RuboCop, SimpleCov, WebMock, YARD, Pry
-
Internal::ClientusesSubprocessCLIfor all operations- This is the production transport with full streaming support
- AllSpark Builder extends it with custom overrides for Docker compatibility
-
Argument Ordering Critical
- Prompt MUST come before CLI options to avoid argument parsing issues
- See
transport/simple_cli.rb:32-38for proper command construction
- Must be installed:
npm install -g @anthropic-ai/claude-code - API Key required:
ANTHROPIC_API_KEYenvironment variable - CLI paths searched:
claude-code,claudein PATH + common install locations
- SDK receives
settingsparameter Options#to_cli_argsconverts to--settingsCLI flag- Claude Code CLI processes hooks and executes commands
- Hook commands receive
$HOOK_INPUTwith JSON tool data - Commands can post to webhooks, log to files, etc.
CLINotFoundError- Claude Code CLI not installed/foundProcessError- CLI process failed (includes exit code and stderr)CLIJSONDecodeError- Failed to parse CLI JSON output
Core Implementation:
lib/claude_code_sdk.rb- Main API surfacelib/claude_code_sdk/types.rb- Options & settings handling (HOOKS!)lib/claude_code_sdk/transport/subprocess_cli.rb- Current active transportlib/claude_code_sdk/internal/client.rb:12- Where SubprocessCLI is instantiated
Examples (Essential for Understanding):
examples/dynamic_hooks.rb- Comprehensive hooks examplesexamples/allspark_integration.rb- AllSpark Builder specific integration
Tests:
spec/claude_code_sdk/options_settings_spec.rb- Settings parameter testsspec/integration/cli_interaction_spec.rb- End-to-end CLI tests
Configuration:
claude_code_sdk.gemspec- Dependencies and metadataCLAUDE.md- Development guidance for Claude Code itself
- Session-specific hooks via
settingsparameter - Webhook integration for tool execution monitoring
- File operation tracking (Write, Edit, Read tools)
- Command execution monitoring (Bash, LS, Glob tools)
# In AllSpark Builder job:
hooks_json = CodeAgentHooksService.generate_settings_for_sdk(session)
options = ClaudeCodeSDK::Options.new(
settings: hooks_json, # JSON string with session-specific hooks
allowed_tools: ["Bash", "Write", "Edit", "Read"],
permission_mode: ClaudeCodeSDK::PermissionMode::ACCEPT_EDITS
)
ClaudeCodeSDK.query(user_prompt, options) do |message|
# Handle streaming responses
end- Transport Layer - SubprocessCLI is working well with AllSpark's custom overrides
- Hooks Testing - Integration tests for webhook delivery and hook reliability
- Error Recovery - Better handling of webhook failures and hook command errors
- Performance - Caching and optimization for high-volume hook executions
- README.md - Comprehensive usage examples and API documentation
- CLAUDE.md - Development guidance for Claude Code working on this project
- CHANGELOG.md - Version history (notably v0.2.0 added hooks support)
- examples/ - Working code examples for all major features
- spec/ - Tests that double as usage documentation
Bottom Line: This SDK is a robust Ruby wrapper around Claude Code CLI with excellent hooks support. The hooks functionality via settings parameter is the key differentiator and enables powerful session-specific customization for AllSpark Builder. Focus on the Options#to_cli_args method and the examples directory when working with hooks.