Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 44 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,10 @@ func main() {
Prompt: "Create a simple hello.go file that prints 'Hello, World!'",
Options: &claudecode.Options{
MaxTurns: intPtr(3),
AllowedTools: []string{"Read", "Write"},
SystemPrompt: stringPtr("You are a helpful Go programming assistant"),
Tools: &claudecode.ToolConfig{
Allowed: []string{"Read", "Write"},
},
},
}

Expand Down Expand Up @@ -95,9 +97,11 @@ func main() {
request := claudecode.QueryRequest{
Prompt: "Analyze this Go project and suggest improvements",
Options: &claudecode.Options{
AllowedTools: []string{"Read", "LS", "Grep"},
OutputFormat: outputFormatPtr(claudecode.OutputFormatStreamJSON),
Verbose: boolPtr(true),
Tools: &claudecode.ToolConfig{
Allowed: []string{"Read", "LS", "Grep"},
},
},
}

Expand Down Expand Up @@ -143,9 +147,12 @@ type Options struct {
// Conversation control
MaxTurns *int // Limit conversation turns

// Tool configuration
AllowedTools []string // Tools Claude can use
DisallowedTools []string // Tools Claude cannot use
// Tool configuration (new structured approach)
Tools *ToolConfig // Tool access control

// Deprecated fields (maintained for backward compatibility)
AllowedTools []string // Use Tools.Allowed instead
DisallowedTools []string // Use Tools.Disallowed instead

// Session management
Resume *string // Resume session by ID
Expand All @@ -155,9 +162,14 @@ type Options struct {
OutputFormat *OutputFormat // text, json, stream-json
Verbose *bool // Enable verbose logging

// Permission configuration (new structured approach)
Permissions *PermissionConfig // Permission and security settings

// MCP (Model Context Protocol)
MCPConfig *string // Path to MCP config JSON
PermissionPromptTool *string // MCP tool for permissions

// Deprecated fields (maintained for backward compatibility)
PermissionPromptTool *string // Use Permissions.PromptTool instead

// System
WorkingDirectory *string // Working directory
Expand Down Expand Up @@ -230,14 +242,35 @@ claudecode.QueryWithRequest(ctx, claudecode.QueryRequest{
request := claudecode.QueryRequest{
Prompt: "Analyze project files",
Options: &claudecode.Options{
MCPConfig: stringPtr("mcp-servers.json"),
AllowedTools: []string{"mcp__filesystem__read_file", "mcp__filesystem__list_directory"},
PermissionPromptTool: stringPtr("mcp__permissions__approve"),
MCPConfig: stringPtr("mcp-servers.json"),
Tools: &claudecode.ToolConfig{
Allowed: []string{"mcp__filesystem__read_file", "mcp__filesystem__list_directory"},
},
Permissions: &claudecode.PermissionConfig{
PromptTool: stringPtr("mcp__permissions__approve"),
},
},
}
```

### Tool and Permission Configuration

#### New Structured Approach (Recommended)

```go
options := &claudecode.Options{
Tools: &claudecode.ToolConfig{
Allowed: []string{"Read", "LS", "Grep"},
Disallowed: []string{"Bash", "Write"},
},
Permissions: &claudecode.PermissionConfig{
Mode: stringPtr("acceptEdits"),
DangerouslySkip: boolPtr(false),
},
}
```

### Tool Restrictions
#### Legacy Approach (Deprecated but still supported)

```go
options := &claudecode.Options{
Expand Down Expand Up @@ -409,4 +442,4 @@ This project is licensed under the MIT License - see the [LICENSE](LICENSE) file
- [Claude Code Documentation](https://docs.anthropic.com/en/docs/claude-code)
- [Claude Code CLI](https://www.npmjs.com/package/@anthropic-ai/claude-code)
- [Claude Code TypeScript SDK](https://www.npmjs.com/package/@anthropic-ai/claude-code)
- [Claude Code Python SDK](https://pypi.org/project/claude-code-sdk/)
- [Claude Code Python SDK](https://pypi.org/project/claude-code-sdk/)
24 changes: 24 additions & 0 deletions claude.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,12 +393,23 @@ func addModelAndToolArgs(args []string, options *Options) []string {
if options.Model != nil && *options.Model != "" {
args = append(args, "--model", *options.Model)
}

if options.Tools != nil {
if len(options.Tools.Allowed) > 0 {
args = append(args, "--allowedTools", strings.Join(options.Tools.Allowed, ","))
}
if len(options.Tools.Disallowed) > 0 {
args = append(args, "--disallowedTools", strings.Join(options.Tools.Disallowed, ","))
}
}

if len(options.AllowedTools) > 0 {
args = append(args, "--allowedTools", strings.Join(options.AllowedTools, ","))
}
if len(options.DisallowedTools) > 0 {
args = append(args, "--disallowedTools", strings.Join(options.DisallowedTools, ","))
}

return args
}

Expand Down Expand Up @@ -444,6 +455,18 @@ func addMCPArgs(args []string, options *Options) []string {
}

func addPermissionArgs(args []string, options *Options) []string {
if options.Permissions != nil {
if options.Permissions.Mode != nil && *options.Permissions.Mode != "" {
args = append(args, "--permission-mode", *options.Permissions.Mode)
}
if options.Permissions.PromptTool != nil && *options.Permissions.PromptTool != "" {
args = append(args, "--permission-prompt-tool", *options.Permissions.PromptTool)
}
if options.Permissions.DangerouslySkip != nil && *options.Permissions.DangerouslySkip {
args = append(args, "--dangerously-skip-permissions")
}
}

if options.PermissionMode != nil && *options.PermissionMode != "" {
args = append(args, "--permission-mode", *options.PermissionMode)
}
Expand All @@ -453,6 +476,7 @@ func addPermissionArgs(args []string, options *Options) []string {
if options.DangerouslySkipPermissions != nil && *options.DangerouslySkipPermissions {
args = append(args, "--dangerously-skip-permissions")
}

return args
}

Expand Down
18 changes: 12 additions & 6 deletions examples/advanced/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,13 @@ go run main.go
### MCP Integration
```go
Options: &claudecode.Options{
MCPConfig: stringPtr("mcp-servers.json"),
AllowedTools: []string{"mcp__filesystem__read_file"},
PermissionPromptTool: stringPtr("mcp__permissions__approve"),
MCPConfig: stringPtr("mcp-servers.json"),
Tools: &claudecode.ToolConfig{
Allowed: []string{"mcp__filesystem__read_file"},
},
Permissions: &claudecode.PermissionConfig{
PromptTool: stringPtr("mcp__permissions__approve"),
},
}
```

Expand All @@ -49,8 +53,10 @@ Options: &claudecode.Options{
### Tool Restrictions
```go
Options: &claudecode.Options{
AllowedTools: []string{"Read", "LS", "Grep"},
DisallowedTools: []string{"Write", "Bash"},
Tools: &claudecode.ToolConfig{
Allowed: []string{"Read", "LS", "Grep"},
Disallowed: []string{"Write", "Bash"},
},
}
```

Expand Down Expand Up @@ -93,4 +99,4 @@ To use all features, you would need:

## Performance

The example includes real-time streaming analysis which may take several minutes to complete as it performs comprehensive project analysis.
The example includes real-time streaming analysis which may take several minutes to complete as it performs comprehensive project analysis.
36 changes: 24 additions & 12 deletions examples/advanced/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,16 @@ func main() {
_ = claudecode.QueryRequest{
Prompt: "Use filesystem tools to analyze the project structure",
Options: &claudecode.Options{
MaxTurns: intPtr(5),
AllowedTools: []string{"mcp__filesystem__read_file", "mcp__filesystem__list_directory"},
MCPConfig: stringPtr("mcp-servers.json"),
PermissionPromptTool: stringPtr("mcp__permissions__approve"),
OutputFormat: outputFormatPtr(claudecode.OutputFormatJSON),
Verbose: boolPtr(true),
MaxTurns: intPtr(5),
MCPConfig: stringPtr("mcp-servers.json"),
OutputFormat: outputFormatPtr(claudecode.OutputFormatJSON),
Verbose: boolPtr(true),
Tools: &claudecode.ToolConfig{
Allowed: []string{"mcp__filesystem__read_file", "mcp__filesystem__list_directory"},
},
Permissions: &claudecode.PermissionConfig{
PromptTool: stringPtr("mcp__permissions__approve"),
},
},
}

Expand All @@ -45,8 +49,10 @@ func main() {
Prompt: "Create a simple Go function that calculates fibonacci numbers",
Options: &claudecode.Options{
MaxTurns: intPtr(2),
AllowedTools: []string{"Write"},
OutputFormat: outputFormatPtr(claudecode.OutputFormatJSON),
Tools: &claudecode.ToolConfig{
Allowed: []string{"Write"},
},
},
}

Expand All @@ -73,8 +79,10 @@ func main() {
Options: &claudecode.Options{
Resume: stringPtr(sessionID),
MaxTurns: intPtr(2),
AllowedTools: []string{"Write"},
OutputFormat: outputFormatPtr(claudecode.OutputFormatText),
Tools: &claudecode.ToolConfig{
Allowed: []string{"Write"},
},
},
}

Expand All @@ -95,10 +103,12 @@ func main() {
SystemPrompt: stringPtr("You are a senior Go architect. Focus on performance, maintainability, and best practices."),
AppendSystemPrompt: stringPtr("Always provide specific, actionable recommendations."),
MaxTurns: intPtr(3),
AllowedTools: []string{"Read", "LS", "Grep"},
DisallowedTools: []string{"Write", "Bash"},
OutputFormat: outputFormatPtr(claudecode.OutputFormatStreamJSON),
Verbose: boolPtr(false),
Tools: &claudecode.ToolConfig{
Allowed: []string{"Read", "LS", "Grep"},
Disallowed: []string{"Write", "Bash"},
},
},
}

Expand Down Expand Up @@ -164,8 +174,10 @@ nextExample:
Prompt: "List the Go files in this project",
Options: &claudecode.Options{
MaxTurns: intPtr(1),
AllowedTools: []string{"LS", "Glob"},
OutputFormat: &format,
Tools: &claudecode.ToolConfig{
Allowed: []string{"LS", "Glob"},
},
},
}

Expand Down Expand Up @@ -210,4 +222,4 @@ func boolPtr(b bool) *bool {

func outputFormatPtr(format claudecode.OutputFormat) *claudecode.OutputFormat {
return &format
}
}
6 changes: 4 additions & 2 deletions examples/basic/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ func main() {
request := claudecode.QueryRequest{
Prompt: "Create a simple hello.go file that prints 'Hello, World!'",
Options: &claudecode.Options{
AllowedTools: []string{"Read", "Write"},
SystemPrompt: stringPtr("You are a helpful Go programming assistant"),
Tools: &claudecode.ToolConfig{
Allowed: []string{"Read", "Write"},
},
},
}

Expand Down Expand Up @@ -69,4 +71,4 @@ func stringPtr(s string) *string {

func outputFormatPtr(format claudecode.OutputFormat) *claudecode.OutputFormat {
return &format
}
}
6 changes: 4 additions & 2 deletions examples/quick_start.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,10 @@ func withToolsExample(ctx context.Context) {
request := claudecode.QueryRequest{
Prompt: "Create a file called hello.txt with 'Hello, World!' in it",
Options: &claudecode.Options{
AllowedTools: []string{"Read", "Write"},
SystemPrompt: stringPtr("You are a helpful file assistant."),
Tools: &claudecode.ToolConfig{
Allowed: []string{"Read", "Write"},
},
},
}

Expand Down Expand Up @@ -115,4 +117,4 @@ func intPtr(i int) *int {

func stringPtr(s string) *string {
return &s
}
}
12 changes: 8 additions & 4 deletions examples/streaming/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ func main() {
Prompt: "List the files in the current directory and create a simple README.md",
Options: &claudecode.Options{
MaxTurns: intPtr(3),
AllowedTools: []string{"Read", "Write"},
SystemPrompt: stringPtr("You are a helpful coding assistant. Be concise and direct."),
OutputFormat: outputFormatPtr(claudecode.OutputFormatStreamJSON),
Verbose: boolPtr(true),
Tools: &claudecode.ToolConfig{
Allowed: []string{"Read", "Write"},
},
},
}

Expand All @@ -34,10 +36,12 @@ func main() {
fmt.Println("\n=== Example 2: Traditional QueryStream with new features ===")
options := &claudecode.Options{
MaxTurns: intPtr(2),
AllowedTools: []string{"Read", "LS"},
AppendSystemPrompt: stringPtr("Focus on showing file structure clearly."),
OutputFormat: outputFormatPtr(claudecode.OutputFormatStreamJSON),
DisallowedTools: []string{"Bash"},
Tools: &claudecode.ToolConfig{
Allowed: []string{"Read", "LS"},
Disallowed: []string{"Bash"},
},
}

// Execute streaming query
Expand Down Expand Up @@ -114,4 +118,4 @@ func boolPtr(b bool) *bool {

func outputFormatPtr(format claudecode.OutputFormat) *claudecode.OutputFormat {
return &format
}
}
Loading