Functions
query()
The primary function for interacting with Claude Code. Creates an async generator that streams messages as they arrive.
Parameters
Parameter | Type | Description |
---|---|---|
prompt | string | AsyncIterable< SDKUserMessage > | The input prompt as a string or async iterable for streaming mode |
options | Options | Optional configuration object (see Options type below) |
Returns
Returns aQuery
object that extends AsyncGenerator<
SDKMessage
, void>
with additional methods.
tool()
Creates a type-safe MCP tool definition for use with SDK MCP servers.
Parameters
Parameter | Type | Description |
---|---|---|
name | string | The name of the tool |
description | string | A description of what the tool does |
inputSchema | Schema extends ZodRawShape | Zod schema defining the tool’s input parameters |
handler | (args, extra) => Promise< CallToolResult > | Async function that executes the tool logic |
createSdkMcpServer()
Creates an MCP server instance that runs in the same process as your application.
Parameters
Parameter | Type | Description |
---|---|---|
options.name | string | The name of the MCP server |
options.version | string | Optional version string |
options.tools | Array<SdkMcpToolDefinition> | Array of tool definitions created with tool() |
Types
Options
Configuration object for the query()
function.
Property | Type | Default | Description |
---|---|---|---|
abortController | AbortController | new AbortController() | Controller for cancelling operations |
additionalDirectories | string[] | [] | Additional directories Claude can access |
allowedTools | string[] | All tools | List of allowed tool names |
appendSystemPrompt | string | undefined | Text to append to the default system prompt |
canUseTool | CanUseTool | undefined | Custom permission function for tool usage |
continue | boolean | false | Continue the most recent conversation |
customSystemPrompt | string | undefined | Replace the default system prompt entirely |
cwd | string | process.cwd() | Current working directory |
disallowedTools | string[] | [] | List of disallowed tool names |
env | Dict<string> | process.env | Environment variables |
executable | 'bun' | 'deno' | 'node' | Auto-detected | JavaScript runtime to use |
executableArgs | string[] | [] | Arguments to pass to the executable |
extraArgs | Record<string, string | null> | {} | Additional arguments |
fallbackModel | string | undefined | Model to use if primary fails |
hooks | Partial<Record< HookEvent , HookCallbackMatcher []>> | {} | Hook callbacks for events |
includePartialMessages | boolean | false | Include partial message events |
maxThinkingTokens | number | undefined | Maximum tokens for thinking process |
maxTurns | number | undefined | Maximum conversation turns |
mcpServers | Record<string, McpServerConfig > | {} | MCP server configurations |
model | string | Default from CLI | Claude model to use |
pathToClaudeCodeExecutable | string | Auto-detected | Path to Claude Code executable |
permissionMode | PermissionMode | 'default' | Permission mode for the session |
permissionPromptToolName | string | undefined | MCP tool name for permission prompts |
resume | string | undefined | Session ID to resume |
stderr | (data: string) => void | undefined | Callback for stderr output |
strictMcpConfig | boolean | false | Enforce strict MCP validation |
Query
Interface returned by the query()
function.
Methods
Method | Description |
---|---|
interrupt() | Interrupts the query (only available in streaming input mode) |
setPermissionMode() | Changes the permission mode (only available in streaming input mode) |
PermissionMode
CanUseTool
Custom permission function type for controlling tool usage.
PermissionResult
Result of a permission check.
McpServerConfig
Configuration for MCP servers.
McpStdioServerConfig
McpSSEServerConfig
McpHttpServerConfig
McpSdkServerConfigWithInstance
Message Types
SDKMessage
Union type of all possible messages returned by the query.
SDKAssistantMessage
Assistant response message.
SDKUserMessage
User input message.
SDKUserMessageReplay
Replayed user message with required UUID.
SDKResultMessage
Final result message.
SDKSystemMessage
System initialization message.
SDKPartialAssistantMessage
Streaming partial message (only when includePartialMessages
is true).
SDKCompactBoundaryMessage
Message indicating a conversation compaction boundary.
SDKPermissionDenial
Information about a denied tool use.
Hook Types
HookEvent
Available hook events.
HookCallback
Hook callback function type.
HookCallbackMatcher
Hook configuration with optional matcher.
HookInput
Union type of all hook input types.
BaseHookInput
Base interface that all hook input types extend.
PreToolUseHookInput
PostToolUseHookInput
NotificationHookInput
UserPromptSubmitHookInput
SessionStartHookInput
SessionEndHookInput
StopHookInput
SubagentStopHookInput
PreCompactHookInput
HookJSONOutput
Hook return value.
AsyncHookJSONOutput
SyncHookJSONOutput
Tool Input Types
Documentation of input schemas for all built-in Claude Code tools. These types are exported from@anthropic/claude-code-sdk
and can be used for type-safe tool interactions.
ToolInput
Note: This is a documentation-only type for clarity. It represents the union of all tool input types.
Task
Tool name:Task
Bash
Tool name:Bash
BashOutput
Tool name:BashOutput
Edit
Tool name:Edit
MultiEdit
Tool name:MultiEdit
Read
Tool name:Read
Write
Tool name:Write
Glob
Tool name:Glob
Grep
Tool name:Grep
KillBash
Tool name:KillBash
NotebookEdit
Tool name:NotebookEdit
WebFetch
Tool name:WebFetch
WebSearch
Tool name:WebSearch
TodoWrite
Tool name:TodoWrite
ExitPlanMode
Tool name:ExitPlanMode
ListMcpResources
Tool name:ListMcpResources
ReadMcpResource
Tool name:ReadMcpResource
Tool Output Types
Documentation of output schemas for all built-in Claude Code tools. These types represent the actual response data returned by each tool.ToolOutput
Note: This is a documentation-only type for clarity. It represents the union of all tool output types.
Task
Tool name:Task
Bash
Tool name:Bash
BashOutput
Tool name:BashOutput
Edit
Tool name:Edit
MultiEdit
Tool name:MultiEdit
Read
Tool name:Read
Write
Tool name:Write
Glob
Tool name:Glob
Grep
Tool name:Grep
KillBash
Tool name:KillBash
NotebookEdit
Tool name:NotebookEdit
WebFetch
Tool name:WebFetch
WebSearch
Tool name:WebSearch
TodoWrite
Tool name:TodoWrite
ExitPlanMode
Tool name:ExitPlanMode
ListMcpResources
Tool name:ListMcpResources
ReadMcpResource
Tool name:ReadMcpResource
Permission Types
PermissionUpdate
Operations for updating permissions.
PermissionBehavior
PermissionUpdateDestination
PermissionRuleValue
Other Types
ApiKeySource
ConfigScope
NonNullableUsage
A version of Usage
with all nullable fields made non-nullable.
Usage
Token usage statistics (from @anthropic-ai/sdk
).
CallToolResult
MCP tool result type (from @modelcontextprotocol/sdk/types.js
).
AbortError
Custom error class for abort operations.
See also
- TypeScript SDK guide - Tutorial and examples
- SDK overview - General SDK concepts
- Python SDK reference - Python SDK documentation
- CLI reference - Command-line interface
- Common workflows - Step-by-step guides