A Nim port of the official Claude Code SDK, providing seamless integration with Claude Code functionality through a native Nim interface.
The Claude Code SDK for Nim allows you to interact with Claude Code programmatically, supporting both simple one-shot queries and complex interactive conversations. This SDK maintains API compatibility with the Python version while leveraging Nim's type safety and performance benefits.
- π Full Python SDK compatibility - Same API patterns adapted for Nim
- π Type-safe - Leverages Nim's powerful type system
- β‘ High performance - Native Nim implementation
- π§ͺ Well tested - 34 tests across 5 test suites with 100% pass rate
- π Flexible usage - Simple queries or interactive conversations
- π οΈ Tool integration - Support for Claude Code's tool ecosystem
Add this to your .nimble file:
requires "claude_code_sdk >= 0.0.19"Or install directly:
nimble install claude_code_sdk- Nim >= 2.2.4
- Claude Code CLI installed (
npm install -g @anthropic-ai/claude-code) - Node.js (required for Claude Code CLI)
For one-off questions or simple interactions:
import claude_code_sdk_nim
import std/asyncdispatch
proc simpleExample() {.async.} =
# Simple query (Note: Full async implementation available for CLI integration)
# This is a simplified example - see test files for working examples
var messageIter = query("What is the capital of France?")
while true:
let (hasMessage, message) = await messageIter.next()
if not hasMessage:
break
echo message
waitFor(simpleExample())For conversational interactions with follow-ups:
import claude_code_sdk_nim
import std/options
proc interactiveExample() =
# Create client with custom options
var options = newClaudeCodeOptions()
options.system_prompt = some("You are a helpful coding assistant")
options.max_thinking_tokens = 5000
let client = newSimpleClient(some(options))
try:
# Connect and query (simplified client for testing/basic usage)
client.connect()
client.query("Help me write a function to calculate fibonacci numbers")
# Note: This is a simplified synchronous client
# Full async implementation available for real CLI integration
finally:
client.disconnect()
interactiveExample()Configuration for Claude Code interactions:
var options = newClaudeCodeOptions()
options.system_prompt = some("Custom system prompt")
options.max_thinking_tokens = 8000
options.permission_mode = some(pmAcceptEdits)
options.allowed_tools = @["Read", "Write", "Bash"]
options.cwd = some("/path/to/working/directory")pmDefault- CLI prompts for dangerous toolspmAcceptEdits- Auto-accept file editspmBypassPermissions- Allow all tools (use with caution)
UserMessage- Messages from the userAssistantMessage- Claude's responses with content blocksSystemMessage- System-level messages with metadataResultMessage- Completion results with cost and usage info
TextBlock- Plain text contentToolUseBlock- Tool invocations with parametersToolResultBlock- Results from tool executions
Simple one-shot queries:
# Basic query (Note: Full async implementation needed for CLI integration)
# See test files for working examples with the current implementation
var messageIter = query("Explain recursion")
# Process messageIter as needed
# With options
var opts = newClaudeCodeOptions()
opts.cwd = some("/my/project")
var messageIter2 = query("Analyze this codebase", opts)
# Process messageIter2 as neededFor interactive conversations:
let client = newSimpleClient()
client.connect()
client.query("Hello Claude")
# Process responses...
client.disconnect()The SDK provides a comprehensive error hierarchy:
try:
# SDK operations
let client = newSimpleClient()
client.query("test")
except CLINotFoundError:
echo "Claude Code CLI not found - please install it"
except CLIConnectionError:
echo "Failed to connect to Claude Code"
except ProcessError as e:
echo "CLI process error: ", e.msg
except MessageParseError:
echo "Failed to parse Claude's response" ClaudeSDKError- Base error typeCLIConnectionError- Connection issuesCLINotFoundError- Claude Code CLI not foundProcessError- CLI process failuresCLIJSONDecodeError- JSON parsing errorsMessageParseError- Message parsing errors
Configure MCP (Model Context Protocol) servers:
var options = newClaudeCodeOptions()
options.mcp_servers["filesystem"] = McpServerConfig(
serverType: mstStdio,
command: some("mcp-server-filesystem"),
args: some(@["--root", "/path/to/files"])
)Run the test suite:
nimble testThe SDK includes comprehensive tests:
- 34 individual tests across 5 test suites
- 100% pass rate with safe testing approach
- No external dependencies or CLI invocation in tests
- Coverage of all major components and error conditions
The SDK follows a modular architecture:
claude_code_sdk_nim/
βββ types.nim # Type definitions and constructors
βββ errors.nim # Exception hierarchy
βββ message_parser.nim # JSON message parsing
βββ simple_client.nim # Simplified client for basic usage
βββ simple_transport.nim # Simplified transport layer
βββ (full async implementations available for advanced usage)
- Current version: 0.0.19
- Python SDK compatibility: Matches Python SDK v0.0.19 API
- Nim version: Requires Nim >= 2.2.4
- Claude Code CLI: Requires latest version
- Fork the repository
- Create a feature branch (
git checkout -b feature-name) - Make your changes
- Run tests (
nimble test) - Submit a pull request
- Follow existing code style and patterns
- Add tests for new functionality
- Update documentation as needed
- Ensure all tests pass before submitting
This project follows the same license as the original Python SDK.
For issues specific to the Nim SDK:
- Check the test suite (
tests/directory) for working usage examples - Review error messages for troubleshooting guidance
- The current implementation includes simplified components for testing and basic usage
For Claude Code CLI issues:
- Refer to the official Claude Code documentation
- Ensure Claude Code CLI is properly installed and accessible
- Full async CLI integration is available through the complete transport implementations
This is a community port of the official Claude SDK. For the latest features and official support, refer to the original Python SDK.