Repository navigation
Expand file tree
/
Copy pathpermission_callback_example.rb
More file actions
120 lines (102 loc) · 3.68 KB
/
Copy pathpermission_callback_example.rb
File metadata and controls
120 lines (102 loc) · 3.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'bundler/setup'
require 'claude_agent_sdk'
require 'async'
# Example: Using permission callbacks for dynamic tool control
Async do
# Define a permission callback that validates tool usage
permission_callback = lambda do |tool_name, input, context|
puts "\n[Permission Check]"
puts " Tool: #{tool_name}"
puts " Input: #{input.inspect}"
# Allow Read operations
if tool_name == 'Read'
puts " Decision: ALLOW (reading is safe)"
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/') || file_path.include?('passwd'))
puts " Decision: DENY (sensitive file)"
return ClaudeAgentSDK::PermissionResultDeny.new(
message: 'Cannot write to sensitive system files',
interrupt: false
)
end
puts " Decision: ALLOW (safe file write)"
return ClaudeAgentSDK::PermissionResultAllow.new
end
# Block dangerous bash commands
if tool_name == 'Bash'
command = input[:command] || input['command'] || ''
dangerous_patterns = ['rm -rf', 'sudo', '>']
dangerous_patterns.each do |pattern|
if command.include?(pattern)
puts " Decision: DENY (dangerous pattern: #{pattern})"
return ClaudeAgentSDK::PermissionResultDeny.new(
message: "Command contains dangerous pattern: #{pattern}",
interrupt: false
)
end
end
puts " Decision: ALLOW (safe command)"
return ClaudeAgentSDK::PermissionResultAllow.new
end
# Default: allow
puts " Decision: ALLOW (default)"
ClaudeAgentSDK::PermissionResultAllow.new
end
# Create options with permission callback
options = ClaudeAgentSDK::ClaudeAgentOptions.new(
allowed_tools: ['Read', 'Write', 'Bash'],
can_use_tool: permission_callback
)
client = ClaudeAgentSDK::Client.new(options: options)
begin
puts "Connecting with permission callback..."
client.connect
puts "Connected!\n"
# Test 1: Safe file write (should be allowed)
puts "\n=== Test 1: Safe File Write ==="
client.query("Create a file called test_output.txt with the content 'Hello World'")
client.receive_response do |msg|
if msg.is_a?(ClaudeAgentSDK::AssistantMessage)
msg.content.each do |block|
puts "Claude: #{block.text}" if block.is_a?(ClaudeAgentSDK::TextBlock)
end
elsif msg.is_a?(ClaudeAgentSDK::ResultMessage)
puts "\nTest 1 completed"
end
end
# Test 2: Dangerous file write (should be blocked)
puts "\n=== Test 2: Dangerous File Write ==="
client.query("Write to /etc/passwd")
client.receive_response do |msg|
if msg.is_a?(ClaudeAgentSDK::AssistantMessage)
msg.content.each do |block|
puts "Claude: #{block.text}" if block.is_a?(ClaudeAgentSDK::TextBlock)
end
elsif msg.is_a?(ClaudeAgentSDK::ResultMessage)
puts "\nTest 2 completed"
end
end
# Test 3: Safe bash command (should be allowed)
puts "\n=== Test 3: Safe Bash Command ==="
client.query("Run the command: echo 'Permission callbacks work!'")
client.receive_response do |msg|
if msg.is_a?(ClaudeAgentSDK::AssistantMessage)
msg.content.each do |block|
puts "Claude: #{block.text}" if block.is_a?(ClaudeAgentSDK::TextBlock)
end
elsif msg.is_a?(ClaudeAgentSDK::ResultMessage)
puts "\nTest 3 completed"
end
end
ensure
puts "\nDisconnecting..."
client.disconnect
puts "Done!"
end
end.wait