Claude Agent SDK for Ruby
⚠️ DISCLAIMER: This is an unofficial, community-maintained Ruby SDK for Claude Agent. It is not officially supported or maintained by Anthropic. For official SDK support, please refer to the Python SDK.
This implementation is based on the official Python SDK and aims to provide feature parity for Ruby developers. Use at your own risk.
Ruby SDK for Claude Agent. See the Claude Agent SDK documentation for more information.
Installation
Add this line to your application's Gemfile:
gem 'claude-agent-sdk'
And then execute:
bundle install
Or install it yourself as:
gem install claude-agent-sdk
Prerequisites:
- Ruby 3.0+
- Node.js
- Claude Code 2.0.0+:
npm install -g @anthropic-ai/claude-code
Quick Start
require 'claude_agent_sdk'
ClaudeAgentSDK.query(prompt: "What is 2 + 2?") do ||
puts
end
Basic Usage: query()
query() is a function for querying Claude Code. It yields response messages to a block. See lib/claude_agent_sdk.rb.
require 'claude_agent_sdk'
# Simple query
ClaudeAgentSDK.query(prompt: "Hello Claude") do ||
if .is_a?(ClaudeAgentSDK::AssistantMessage)
.content.each do |block|
puts block.text if block.is_a?(ClaudeAgentSDK::TextBlock)
end
end
end
# With options
= ClaudeAgentSDK::ClaudeAgentOptions.new(
system_prompt: "You are a helpful assistant",
max_turns: 1
)
ClaudeAgentSDK.query(prompt: "Tell me a joke", options: ) do ||
puts
end
Using Tools
= ClaudeAgentSDK::ClaudeAgentOptions.new(
allowed_tools: ['Read', 'Write', 'Bash'],
permission_mode: 'acceptEdits' # auto-accept file edits
)
ClaudeAgentSDK.query(
prompt: "Create a hello.rb file",
options:
) do ||
# Process tool use and results
end
Working Directory
= ClaudeAgentSDK::ClaudeAgentOptions.new(
cwd: "/path/to/project"
)
Streaming Input
The query() function supports streaming input, allowing you to send multiple messages dynamically instead of a single prompt string.
require 'claude_agent_sdk'
# Create a stream of messages
= ['Hello!', 'What is 2+2?', 'Thanks!']
stream = ClaudeAgentSDK::Streaming.from_array()
# Query with streaming input
ClaudeAgentSDK.query(prompt: stream) do ||
puts if .is_a?(ClaudeAgentSDK::AssistantMessage)
end
You can also create custom streaming enumerators:
# Dynamic message generation
stream = Enumerator.new do |yielder|
yielder << ClaudeAgentSDK::Streaming.("First message")
# Do some processing...
yielder << ClaudeAgentSDK::Streaming.("Second message")
yielder << ClaudeAgentSDK::Streaming.("Third message")
end
ClaudeAgentSDK.query(prompt: stream) do ||
# Process responses
end
For a complete example, see examples/streaming_input_example.rb.
Client
ClaudeAgentSDK::Client supports bidirectional, interactive conversations with Claude Code. Unlike query(), Client enables custom tools, hooks, and permission callbacks, all of which can be defined as Ruby procs/lambdas.
The Client class automatically uses streaming mode for bidirectional communication, allowing you to send multiple queries dynamically during a single session without closing the connection. This matches the Python SDK's ClaudeSDKClient behavior.
See lib/claude_agent_sdk.rb for implementation details.
Custom Tools (SDK MCP Servers)
A custom tool is a Ruby proc/lambda that you can offer to Claude, for Claude to invoke as needed.
Custom tools are implemented as in-process MCP servers that run directly within your Ruby application, eliminating the need for separate processes that regular MCP servers require.
For a complete example, see examples/mcp_calculator.rb.
Creating a Simple Tool
require 'claude_agent_sdk'
require 'async'
# Define a tool using create_tool
greet_tool = ClaudeAgentSDK.create_tool('greet', 'Greet a user', { name: :string }) do |args|
{ content: [{ type: 'text', text: "Hello, #{args[:name]}!" }] }
end
# Create an SDK MCP server
server = ClaudeAgentSDK.create_sdk_mcp_server(
name: 'my-tools',
version: '1.0.0',
tools: [greet_tool]
)
# Use it with Claude
= ClaudeAgentSDK::ClaudeAgentOptions.new(
mcp_servers: { tools: server },
allowed_tools: ['mcp__tools__greet']
)
Async do
client = ClaudeAgentSDK::Client.new(options: )
client.connect
client.query("Greet Alice")
client.receive_response { |msg| puts msg }
client.disconnect
end.wait
Benefits Over External MCP Servers
- No subprocess management - Runs in the same process as your application
- Better performance - No IPC overhead for tool calls
- Simpler deployment - Single Ruby process instead of multiple
- Easier debugging - All code runs in the same process
- Direct access - Tools can directly access your application's state
Calculator Example
# Define calculator tools
add_tool = ClaudeAgentSDK.create_tool('add', 'Add two numbers', { a: :number, b: :number }) do |args|
result = args[:a] + args[:b]
{ content: [{ type: 'text', text: "#{args[:a]} + #{args[:b]} = #{result}" }] }
end
divide_tool = ClaudeAgentSDK.create_tool('divide', 'Divide numbers', { a: :number, b: :number }) do |args|
if args[:b] == 0
{ content: [{ type: 'text', text: 'Error: Division by zero' }], is_error: true }
else
result = args[:a] / args[:b]
{ content: [{ type: 'text', text: "Result: #{result}" }] }
end
end
# Create server
calculator = ClaudeAgentSDK.create_sdk_mcp_server(
name: 'calculator',
tools: [add_tool, divide_tool]
)
= ClaudeAgentSDK::ClaudeAgentOptions.new(
mcp_servers: { calc: calculator },
allowed_tools: ['mcp__calc__add', 'mcp__calc__divide']
)
Mixed Server Support
You can use both SDK and external MCP servers together:
= ClaudeAgentSDK::ClaudeAgentOptions.new(
mcp_servers: {
internal: sdk_server, # In-process SDK server
external: { # External subprocess server
type: 'stdio',
command: 'external-server'
}
}
)
MCP Resources and Prompts
SDK MCP servers can also expose resources (data sources) and prompts (reusable templates):
# Create a resource (data source Claude can read)
config_resource = ClaudeAgentSDK.create_resource(
uri: 'config://app/settings',
name: 'Application Settings',
description: 'Current app configuration',
mime_type: 'application/json'
) do
config_data = { app_name: 'MyApp', version: '1.0.0' }
{
contents: [{
uri: 'config://app/settings',
mimeType: 'application/json',
text: JSON.pretty_generate(config_data)
}]
}
end
# Create a prompt template
review_prompt = ClaudeAgentSDK.create_prompt(
name: 'code_review',
description: 'Review code for best practices',
arguments: [
{ name: 'code', description: 'Code to review', required: true }
]
) do |args|
{
messages: [{
role: 'user',
content: {
type: 'text',
text: "Review this code: #{args[:code]}"
}
}]
}
end
# Create server with tools, resources, and prompts
server = ClaudeAgentSDK.create_sdk_mcp_server(
name: 'dev-tools',
tools: [my_tool],
resources: [config_resource],
prompts: [review_prompt]
)
For complete examples, see examples/mcp_resources_prompts_example.rb.
Basic Client Usage
require 'claude_agent_sdk'
require 'async'
Async do
client = ClaudeAgentSDK::Client.new
begin
# Connect automatically uses streaming mode for bidirectional communication
client.connect
# Send a query
client.query("What is the capital of France?")
# Receive the response
client.receive_response do |msg|
if msg.is_a?(ClaudeAgentSDK::AssistantMessage)
msg.content.each do |block|
puts block.text if block.is_a?(ClaudeAgentSDK::TextBlock)
end
elsif msg.is_a?(ClaudeAgentSDK::ResultMessage)
puts "Cost: $#{msg.total_cost_usd}" if msg.total_cost_usd
end
end
ensure
client.disconnect
end
end.wait
Hooks
A hook is a Ruby proc/lambda that the Claude Code application (not Claude) invokes at specific points of the Claude agent loop. Hooks can provide deterministic processing and automated feedback for Claude. Read more in Claude Code Hooks Reference.
For more examples, see examples/hooks_example.rb.
Example
require 'claude_agent_sdk'
require 'async'
Async do
# Define a hook that blocks dangerous bash commands
bash_hook = lambda do |input, tool_use_id, context|
tool_name = input[:tool_name]
tool_input = input[:tool_input]
return {} unless tool_name == 'Bash'
command = tool_input[:command] || ''
block_patterns = ['rm -rf', 'foo.sh']
block_patterns.each do |pattern|
if command.include?(pattern)
return {
hookSpecificOutput: {
hookEventName: 'PreToolUse',
permissionDecision: 'deny',
permissionDecisionReason: "Command contains forbidden pattern: #{pattern}"
}
}
end
end
{} # Allow if no patterns match
end
# Create options with hook
= ClaudeAgentSDK::ClaudeAgentOptions.new(
allowed_tools: ['Bash'],
hooks: {
'PreToolUse' => [
ClaudeAgentSDK::HookMatcher.new(
matcher: 'Bash',
hooks: [bash_hook]
)
]
}
)
client = ClaudeAgentSDK::Client.new(options: )
client.connect
# Test: Command with forbidden pattern (will be blocked)
client.query("Run the bash command: ./foo.sh --help")
client.receive_response { |msg| puts msg }
client.disconnect
end.wait
Permission Callbacks
A permission callback is a Ruby proc/lambda that allows you to programmatically control tool execution. This gives you fine-grained control over what tools Claude can use and with what inputs.
For more examples, see examples/permission_callback_example.rb.
Example
require 'claude_agent_sdk'
require 'async'
Async do
# Define a permission callback
= lambda do |tool_name, input, context|
# Allow Read operations
if tool_name == 'Read'
return ClaudeAgentSDK::PermissionResultAllow.new
end
# Block Write to sensitive files
if tool_name == 'Write'
file_path = input[:file_path] || input['file_path']
if file_path && file_path.include?('/etc/')
return ClaudeAgentSDK::PermissionResultDeny.new(
message: 'Cannot write to sensitive system files',
interrupt: false
)
end
return ClaudeAgentSDK::PermissionResultAllow.new
end
# Default: allow
ClaudeAgentSDK::PermissionResultAllow.new
end
# Create options with permission callback
= ClaudeAgentSDK::ClaudeAgentOptions.new(
allowed_tools: ['Read', 'Write', 'Bash'],
can_use_tool:
)
client = ClaudeAgentSDK::Client.new(options: )
client.connect
# This will be allowed
client.query("Create a file called test.txt with content 'Hello'")
client.receive_response { |msg| puts msg }
# This will be blocked
client.query("Write to /etc/passwd")
client.receive_response { |msg| puts msg }
client.disconnect
end.wait
Advanced Client Features
The Client class supports several advanced features:
Async do
client = ClaudeAgentSDK::Client.new
client.connect
# Send interrupt signal
client.interrupt
# Change permission mode during conversation
client.('acceptEdits')
# Change AI model during conversation
client.set_model('claude-sonnet-4-5')
# Get server initialization info
info = client.server_info
puts "Available commands: #{info}"
client.disconnect
end.wait
Types
See lib/claude_agent_sdk/types.rb for complete type definitions:
ClaudeAgentOptions- Configuration optionsAssistantMessage,UserMessage,SystemMessage,ResultMessage- Message typesTextBlock,ToolUseBlock,ToolResultBlock- Content blocks
Error Handling
require 'claude_agent_sdk'
begin
ClaudeAgentSDK.query(prompt: "Hello") do ||
puts
end
rescue ClaudeAgentSDK::CLINotFoundError
puts "Please install Claude Code"
rescue ClaudeAgentSDK::ProcessError => e
puts "Process failed with exit code: #{e.exit_code}"
rescue ClaudeAgentSDK::CLIJSONDecodeError => e
puts "Failed to parse response: #{e}"
end
Error types:
ClaudeSDKError- Base errorCLINotFoundError- Claude Code not installedCLIConnectionError- Connection issuesProcessError- Process failedCLIJSONDecodeError- JSON parsing issuesMessageParseError- Message parsing issues
See lib/claude_agent_sdk/errors.rb for all error types.
Available Tools
See the Claude Code documentation for a complete list of available tools.
Examples
See the following examples for complete working code:
- examples/quick_start.rb - Basic
query()usage with options - examples/client_example.rb - Interactive Client usage
- examples/streaming_input_example.rb - Streaming input for multi-turn conversations
- examples/mcp_calculator.rb - Custom tools with SDK MCP servers
- examples/mcp_resources_prompts_example.rb - MCP resources and prompts
- examples/hooks_example.rb - Using hooks to control tool execution
- examples/permission_callback_example.rb - Dynamic tool permission control
Development
After checking out the repo, run bundle install to install dependencies. Then, run bundle exec rspec to run the tests.
License
The gem is available as open source under the terms of the MIT License.