with one click
typescript-sdk-reference
Comprehensive reference for the TypeScript Agent SDK with functions, types, and interfaces for programmatic Claude Code interactions
Menu
Comprehensive reference for the TypeScript Agent SDK with functions, types, and interfaces for programmatic Claude Code interactions
| name | TypeScript SDK Reference |
| description | Comprehensive reference for the TypeScript Agent SDK with functions, types, and interfaces for programmatic Claude Code interactions |
| allowed-tools | ["Read","Write","Edit","Task"] |
A comprehensive reference for building applications with the Anthropic Agent SDK (TypeScript), enabling programmatic interactions with Claude Code and custom tool integrations.
npm install @anthropic-ai/claude-agent-sdk
| Function | Purpose | Key Use Case |
|---|---|---|
query() | Primary interface for Claude Code interactions | Send prompts, stream responses, manage multi-turn conversations |
tool() | Define MCP tools with type-safe schemas | Create custom tools for agents using Zod validation |
createSdkMcpServer() | Create in-process MCP servers | Host tools natively in your application |
import { query } from '@anthropic-ai/claude-agent-sdk';
const result = query({
prompt: "Analyze this code and suggest improvements",
options: {
allowedTools: ['Read', 'Grep'],
model: 'claude-opus'
}
});
for await (const message of result) {
if (message.type === 'assistant') {
console.log(message.message.content);
}
}
query()Streams Claude Code responses with full message type support. Returns an async generator of SDKMessage objects.
Parameters:
prompt: string or async iterable of messages (streaming mode)options: Configuration object (see Options below)Returns: Query object (extends AsyncGenerator<SDKMessage, void>)
Key Methods:
.interrupt() – Interrupt streaming (streaming mode only).setPermissionMode(mode) – Change permissions dynamically (streaming mode only)tool()Creates type-safe MCP tool definitions using Zod schemas. Handlers receive validated inputs and must return CallToolResult.
Parameters:
name: Tool identifierdescription: Natural language descriptioninputSchema: Zod schema defining inputshandler: Async function (args: z.infer<Schema>, extra?: unknown) => Promise<CallToolResult>Returns: SdkMcpToolDefinition<Schema>
createSdkMcpServer()Creates an in-process MCP server for hosting custom tools. Runs in the same Node.js process—no subprocess overhead.
Parameters:
name: Server identifierversion: Optional semantic versiontools: Array of tool definitions from tool()Returns: McpSdkServerConfigWithInstance (ready for mcpServers option)
| Option | Type | Default | Purpose |
|---|---|---|---|
allowedTools | string[] | All available | Restrict which tools Claude can use |
disallowedTools | string[] | [] | Explicitly block tools |
model | string | CLI default | Override Claude model (e.g., 'claude-opus') |
cwd | string | process.cwd() | Working directory for operations |
abortController | AbortController | New instance | Control task cancellation |
| Option | Type | Default | Purpose |
|---|---|---|---|
systemPrompt | string or preset object | None | Custom system instructions or Claude Code preset |
agents | Record<string, AgentDefinition> | undefined | Programmatically define subagents with custom prompts |
mcpServers | Record<string, McpServerConfig> | {} | Connect stdio, SSE, HTTP, or SDK MCP servers |
settingSources | ('user' | 'project' | 'local')[] | [] | Load filesystem settings (CLAUDE.md, settings.json) |
permissionMode | PermissionMode | 'default' | 'default', 'acceptEdits', 'bypassPermissions', or 'plan' |
maxThinkingTokens | number | undefined | Token limit for extended thinking |
maxTurns | number | undefined | Maximum conversation turns before stopping |
resume | string | undefined | Resume a previous session by ID |
forkSession | boolean | false | Fork to new session on resume instead of continuing |
The query() generator yields SDKMessage objects. Common types:
| Message Type | When Emitted | Key Fields |
|---|---|---|
'system' | Session initialization | tools, model, permissionMode, slash_commands |
'user' | User input sent | message (APIUserMessage), uuid |
'assistant' | Claude responds | message (APIAssistantMessage with content/tool_use) |
'result' | Query completes | subtype ('success' or error), usage, total_cost_usd |
'stream_event' | Partial streaming data | event (only if includePartialMessages: true) |
See Message Types Reference for complete definitions.
Options – Query configuration with 20+ properties (Tools, MCP, permissions, execution)PermissionMode – Control execution permissions: 'default', 'acceptEdits', 'bypassPermissions', 'plan'SettingSource – Filesystem config scope: 'user', 'project', 'local'AgentDefinition – Subagent config: description, prompt, tools, model overrideSdkMcpToolDefinition – Output of tool() function with Zod schema bindingMcpServerConfig – Union of stdio, SSE, HTTP, or SDK server configs| Config Type | Transport | Use Case |
|---|---|---|
McpStdioServerConfig | stdio (subprocess) | External tools, sandboxing |
McpSSEServerConfig | Server-Sent Events | Remote tools, pub/sub patterns |
McpHttpServerConfig | HTTP | REST-based tools, cloud integrations |
McpSdkServerConfigWithInstance | In-process | Custom tools, no subprocess overhead |
HookEvent – Event names: PreToolUse, PostToolUse, Notification, UserPromptSubmit, SessionStart, SessionEnd, etc.HookCallback – Async function receiving hook input and returning decisionsHookInput – Union of all hook input types; each extends BaseHookInputSee Types Reference for detailed type definitions.
All tool names map to input schemas:
| Tool | Purpose | Key Input |
|---|---|---|
Task | Delegate to subagent | description, prompt, subagent_type |
Bash | Run shell commands | command, optional timeout, run_in_background |
Read | Read files/images/PDFs | file_path, optional offset/limit |
Write / Edit | Modify files | file_path, content or old/new strings |
Glob | Pattern matching | pattern, optional path |
Grep | Regex search | pattern, optional filters (glob, type, -B/-A/-C) |
WebFetch | Fetch & analyze URLs | url, prompt for AI analysis |
WebSearch | Web search | query, optional domain filters |
NotebookEdit | Jupyter notebooks | notebook_path, new_source, edit_mode |
TodoWrite | Task tracking | todos array with status, content, activeForm |
See Tool Reference for complete input/output schemas and examples.
const result = query({
prompt: "Your task here",
options: {
includePartialMessages: true,
permissionMode: 'bypassPermissions'
}
});
for await (const msg of result) {
if (msg.type === 'stream_event') {
// Handle streaming events
console.log('Streaming:', msg.event);
} else if (msg.type === 'result') {
console.log('Cost:', msg.total_cost_usd, 'Usage:', msg.usage);
}
}
import { tool, createSdkMcpServer, query } from '@anthropic-ai/claude-agent-sdk';
import { z } from 'zod';
const myTool = tool(
'my-calculator',
'Add two numbers',
{ a: z.number(), b: z.number() },
async ({ a, b }) => ({
content: [{ type: 'text', text: `Result: ${a + b}` }]
})
);
const server = createSdkMcpServer({
name: 'my-tools',
tools: [myTool]
});
const result = query({
prompt: "Use my-calculator to add 5 + 3",
options: {
mcpServers: { 'my-tools': server }
}
});
const result = query({
prompt: "Add a feature following project conventions",
options: {
systemPrompt: {
type: 'preset',
preset: 'claude_code' // Enables Claude Code system prompt
},
settingSources: ['project'], // Load .claude/settings.json & CLAUDE.md
allowedTools: ['Read', 'Write', 'Edit', 'Bash']
}
});
Defines required structure, frontmatter format, and best practices for SKILL.md files. Use BEFORE creating or editing any skill - this is the spec to follow, not optional reference.
Diagnose and fix bugs through systematic investigation, root cause analysis, and targeted validation. Use when something is broken, errors occur, performance degrades, or unexpected behavior manifests.
Systematically evaluate code changes for security, correctness, performance, and spec alignment. Use when reviewing PRs, assessing code quality, or verifying implementation against requirements.
Maintain project documentation synchronized with code. Keep feature specs, API contracts, and README current with init-project standards. Use when updating docs after code changes, adding new features, or ensuring documentation completeness.
Systematically trace code flows, locate implementations, diagnose performance issues, and map system architecture. Use when understanding how existing systems work, researching concepts, exploring code structure, or answering "how/where/why is X implemented?" questions.
Coordinate concurrent task execution through agent delegation. Plan independent work, manage dependencies, and execute multiple agents simultaneously. Use when handling multiple unrelated tasks, research investigations, or layer-based implementations that can run concurrently.