Releases: jamesrochabrun/ClaudeCodeSDK
ClaudeCodeSDK v1.0.8
What's Changed
- Adding a suffix to the command by @jamesrochabrun in #12
Full Changelog: v1.0.7...v1.0.8
ClaudeCodeSDK v1.0.7
Full Changelog: v1.0.6...v1.0.7
ClaudeCodeSDK v1.0.6
What's Changed
- MCP by @jamesrochabrun in #5
- Adding Agent MD file by @jamesrochabrun in #6
- Add AGENT.md migration guide and update README by @jamesrochabrun in #7
- Remove executable-related options from Swift SDK by @jamesrochabrun in #8
- Unit tests by @jamesrochabrun in #9
- Align sdk with cli by @jamesrochabrun in #10
Full Changelog: v1.0.5...v1.0.6
ClaudeCodeSDK v1.0.5
Full Changelog: v1.0.4...v1.0.5
ClaudeCodeSDK v1.0.4
ClaudeCodeSDK v1.0.3
ClaudeCodeSDK v1.0.2
What's Changed
- Updating Models to match API by @jamesrochabrun in #2
Full Changelog: v1.0.1...v1.0.2
ClaudeCodeSDK v1.0.1
Fixing Decoding errors.
ClaudeCodeSDK v1.0.0
ClaudeCodeSDK
A Swift SDK for seamlessly integrating Claude Code into your iOS and macOS applications. Interact with Anthropic's Claude Code programmatically for AI-powered coding assistance.
📋 Overview
ClaudeCodeSDK allows you to integrate Claude Code's capabilities directly into your Swift applications. The SDK provides a simple interface to run Claude Code as a subprocess, enabling multi-turn conversations, custom system prompts, and various output formats.
✅ Requirements
- Platforms: iOS 15+ or macOS 13+
- Swift Version: Swift 6.0+
- Dependencies: Claude Code CLI installed (
npm install -g @anthropic/claude-code
)
🚀 Installation
Swift Package Manager
Add the package dependency to your Package.swift
file:
dependencies: [
.package(url: "https://github.com/jamesrochabrun/ClaudeCodeSDK", from: "1.0.0")
]
Or add it directly in Xcode:
- File > Add Package Dependencies...
- Enter:
https://github.com/jamesrochabrun/ClaudeCodeSDK
🔰 Basic Usage
Import the SDK and create a client:
import ClaudeCodeSDK
// Initialize the client
let client = ClaudeCodeClient(debug: true)
// Run a simple prompt
Task {
do {
let result = try await client.runSinglePrompt(
prompt: "Write a function to calculate Fibonacci numbers",
outputFormat: .text,
options: nil
)
switch result {
case .text(let content):
print("Response: \(content)")
default:
break
}
} catch {
print("Error: \(error)")
}
}
⚙️ Key Features
Different Output Formats
Choose from three output formats depending on your needs:
// Get plain text
let textResult = try await client.runSinglePrompt(
prompt: "Write a sorting algorithm",
outputFormat: .text,
options: nil
)
// Get JSON with metadata
let jsonResult = try await client.runSinglePrompt(
prompt: "Explain big O notation",
outputFormat: .json,
options: nil
)
// Stream responses as they arrive
let streamResult = try await client.runSinglePrompt(
prompt: "Create a React component",
outputFormat: .streamJson,
options: nil
)
Processing Streams
if case .stream(let publisher) = streamResult {
publisher.sink(
receiveCompletion: { completion in
// Handle completion
},
receiveValue: { chunk in
// Process each chunk as it arrives
}
)
.store(in: &cancellables)
}
Multi-turn Conversations
Maintain context across multiple interactions:
// Continue the most recent conversation
let continuationResult = try await client.continueConversation(
prompt: "Now refactor this for better performance",
outputFormat: .text,
options: nil
)
// Resume a specific session
let resumeResult = try await client.resumeConversation(
sessionId: "550e8400-e29b-41d4-a716-446655440000",
prompt: "Add error handling",
outputFormat: .text,
options: nil
)
Customization Options
Fine-tune Claude Code's behavior:
var options = ClaudeCodeOptions()
options.verbose = true
options.maxTurns = 5
options.systemPrompt = "You are a senior backend engineer specializing in Swift."
options.appendSystemPrompt = "After writing code, add comprehensive comments."
let result = try await client.runSinglePrompt(
prompt: "Create a REST API in Swift",
outputFormat: .text,
options: options
)
📱 Example Project
The repository includes a complete example project demonstrating how to integrate and use the SDK in a real application. You can find it in the Example/ClaudeCodeSDKExample
directory.
The example showcases:
- Creating a chat interface with Claude
- Handling streaming responses
- Managing conversation sessions
- Displaying loading states
- Error handling
Running the Example
- Clone the repository
- Open
Example/ClaudeCodeSDKExample/ClaudeCodeSDKExample.xcodeproj
- Build and run
🏗️ Architecture
The SDK is built with a protocol-based architecture for maximum flexibility:
ClaudeCode
: Protocol defining the interfaceClaudeCodeClient
: Concrete implementation that runs Claude Code CLI as a subprocessClaudeCodeOptions
: Configuration options for Claude Code executionClaudeCodeOutputFormat
: Output format options (text, JSON, streaming JSON)ClaudeCodeResult
: Result types returned by the SDKResponseChunk
: Individual chunks in streaming responses
📝 License
ClaudeCodeSDK is available under the MIT license. See the LICENSE
file for more info.
📚 Documentation
For more information about Claude Code and its capabilities, visit the [Anthropic Documentation](https://docs.anthropic.com/en/docs/claude-code/sdk).