Claude Code CLI Client Library - A TypeScript library for interacting with Claude CLI programmatically.
- ClaudeClient: Execute Claude CLI commands with stream parsing
- ProcessManager: Manage multiple concurrent Claude CLI executions
- SessionManager: Track and persist Claude CLI sessions
- StreamParser: Parse Claude CLI stream JSON output
- ClaudeQueryAPI: Schema-validated queries with Zod integration
- Entry Point System: Pre-configured execution contexts with schema management
- Error Handling: Comprehensive error types and handling
npm install @context-action/code-api
# peer dependency
npm install zodimport { ClaudeClient } from '@context-action/code-api';
const client = new ClaudeClient({
cwd: './my-project',
onStream: (event) => console.log(event),
onError: (error) => console.error(error),
onComplete: (code) => console.log('Done:', code)
});
client.execute('Explain this codebase');import { ProcessManager } from '@context-action/code-api';
const manager = new ProcessManager();
const sessionId = await manager.startExecution({
projectPath: './my-project',
query: 'Fix the bug in auth.ts',
onStream: (sid, event) => console.log(event)
});
// Kill execution
manager.killExecution(sessionId);import { ClaudeQueryAPI } from '@context-action/code-api';
import { z } from 'zod';
const api = new ClaudeQueryAPI();
const schema = z.object({
summary: z.string(),
issues: z.array(z.string()),
suggestions: z.array(z.string())
});
const result = await api.queryWithZod(
'./my-project',
'Analyze this code and provide a summary',
schema
);
if (result.success) {
console.log(result.data.summary);
console.log(result.data.issues);
}The Entry Point system allows you to create pre-configured execution contexts where output formats and options are defined upfront. This is ideal for creating reusable query interfaces with consistent behavior.
Important: When using structured output type, the corresponding schema must exist in workflow/schemas/ before creating the entry point. The system validates schema existence automatically.
Create JSON schema definitions in workflow/schemas/*.json:
import { SchemaManager } from '@context-action/code-api';
const schemaManager = new SchemaManager('./my-project');
const codeReviewSchema = {
name: 'code-review',
description: 'Code review and quality analysis results',
schema: {
file: {
type: 'string',
description: 'Analyzed file path',
required: true
},
score: {
type: 'number',
description: 'Overall quality score',
min: 1,
max: 10,
required: true
},
issues: {
type: 'array',
arrayItemType: 'object',
description: 'List of issues found',
required: true
}
}
};
schemaManager.saveSchema(codeReviewSchema);Define entry points in workflow/entry-points.json:
import { EntryPointManager } from '@context-action/code-api';
const entryPointManager = new EntryPointManager('./my-project');
const codeReviewEntry = {
name: 'code-review',
description: 'Analyze code files for quality, complexity, and improvements',
outputStyle: 'structured-json',
outputFormat: {
type: 'structured',
schemaName: 'code-review'
},
options: {
model: 'sonnet',
timeout: 120000,
filterThinking: true
},
tags: ['code-quality', 'analysis']
};
entryPointManager.setEntryPoint(codeReviewEntry);
// ✅ Automatically validates that 'code-review' schema exists
// ❌ Throws error if schema not found: "Schema 'code-review' does not exist in workflow/schemas/"Schema Validation Process:
- When calling
setEntryPoint()withstructuredoutput type - System automatically checks if referenced schema exists in
workflow/schemas/ - If schema not found, throws validation error with helpful message
- Prevents runtime errors by catching issues at configuration time
Before execution, check what output format to expect:
const detail = entryPointManager.getEntryPointDetail('code-review');
console.log('Expected Output Type:', detail.expectedOutput.type);
console.log('Description:', detail.expectedOutput.description);
// For structured types, see exact fields
if (detail.expectedOutput.fields) {
Object.entries(detail.expectedOutput.fields).forEach(([key, field]) => {
console.log(`- ${key}: ${field.type} ${field.required ? '(required)' : ''}`);
console.log(` ${field.description}`);
});
}
// See example output
if (detail.expectedOutput.examples) {
console.log('Example:', JSON.stringify(detail.expectedOutput.examples[0], null, 2));
}This is the core value: Executors know exactly what data structure they'll receive before running the query!
Execute queries through pre-configured entry points:
import { EntryPointExecutor } from '@context-action/code-api';
const executor = new EntryPointExecutor('./my-project');
const result = await executor.execute({
entryPoint: 'code-review',
projectPath: './my-project',
query: 'Analyze src/main.ts for code quality'
});
if (result.success) {
console.log('File:', result.data.file);
console.log('Score:', result.data.score);
console.log('Issues:', result.data.issues);
}text: Plain text output (no schema)json: JSON output without schema validationstructured: JSON output with schema validation
System Prompt vs Output Style:
- Output Style: Controls response format (JSON, tone, structure)
- System Prompt: Controls AI role and behavior (domain expertise, guidelines, persona)
These are independent and complementary - use both for powerful execution contexts!
const entryPoint: EntryPointConfig = {
name: 'security-review',
outputFormat: {
type: 'structured',
schemaName: 'security-review'
},
// Define AI's role and expertise
systemPrompt: {
useClaudeCodePreset: true, // Keep Claude Code's base behavior
append: `You are a security auditor.
Focus on:
- Input validation
- SQL injection risks
- XSS vulnerabilities
- Authentication/Authorization
Always assume adversarial input.`
}
};- Custom (Complete replacement):
systemPrompt: {
custom: 'You are a Python specialist. Always use type hints.'
}- Append (Add to default):
systemPrompt: {
append: 'Always include comprehensive error handling.'
}- Preset + Append (Recommended):
systemPrompt: {
useClaudeCodePreset: true,
append: 'Prioritize OAuth 2.0 compliance'
}const sreIncident: EntryPointConfig = {
name: 'sre-incident',
description: 'SRE expert incident investigation',
// Output: Structured incident report
outputFormat: {
type: 'structured',
schemaName: 'incident-report'
},
// AI: SRE expert with specific methodology
systemPrompt: {
custom: `You are an expert SRE with 10+ years experience.
Investigation process:
1. Identify symptoms
2. Check recent changes
3. Review logs systematically
4. Propose fixes
5. Document learnings
Always prioritize service restoration over root cause.`
},
options: {
model: 'opus',
timeout: 180000
}
};Result: You get both structured data AND expert-level analysis!
See System Prompt vs Output Style for detailed comparison and patterns.
Execute Claude CLI commands with stream handling.
interface ClaudeClientOptions {
cwd: string;
model?: 'sonnet' | 'opus' | 'haiku';
sessionId?: string;
mcpConfig?: string;
onStream?: (event: StreamEvent) => void;
onError?: (error: string) => void;
onComplete?: (code: number) => void;
}Manage multiple concurrent executions with lifecycle management.
interface StartExecutionParams {
projectPath: string;
query: string;
sessionId?: string;
mcpConfig?: string;
model?: 'sonnet' | 'opus' | 'haiku';
skillId?: string;
skillScope?: 'global' | 'project';
outputStyle?: string;
onStream?: (sessionId: string, event: StreamEvent) => void;
onError?: (sessionId: string, error: string) => void;
onComplete?: (sessionId: string, code: number) => void;
}Execute queries with automatic JSON extraction and schema validation.
// With Zod schema
queryWithZod<T>(
projectPath: string,
instruction: string,
schema: ZodType<T>,
options?: QueryOptions
): Promise<JSONExtractionResult<T>>
// With Standard Schema
queryWithStandardSchema<T>(
projectPath: string,
instruction: string,
schema: StandardSchemaV1,
options?: QueryOptions
): Promise<JSONExtractionResult<T>># Run all tests
npm test
# Run specific tests
npm run test:schema # Schema validation tests
npm run test:zod # Zod schema tests
npm run test:schema-validation # Schema + Claude integration
npm run test:zod-claude # Zod + Claude integration
npm run test:pipeline # Full pipeline test# Query API usage
npm run example:query
# JSON extraction pipeline
npm run example:json
# Entry Point system usage
npm run example:entrypoint- ✅ Schema prompt building and validation
- ✅ Zod schema integration
- ✅ Standard Schema compliance
- ✅ JSON extraction from Claude output
- ✅ Type guards and runtime validation
- ✅ Full pipeline (Schema → Claude → Extract → Validate)
MIT