Skip to main content

Overview

The Claude Agent SDK allows you to create custom tools that run in-process within your Python application. These tools extend Claude’s capabilities with your own functions and logic.
SDK MCP servers run in-process within your application, providing better performance than external MCP servers that require separate processes.

Benefits of SDK MCP Servers

Better Performance

No IPC overhead - tools run directly in your process

Simpler Deployment

Single process - no need to manage separate server processes

Easier Debugging

Debug tools alongside your application code

State Access

Direct access to your application’s variables and state

Creating a Simple Tool

Use the @tool decorator to define a custom tool:
Tool functions must be async (defined with async def) and return a dictionary with a content key containing the response.

Tool Structure

Every tool needs three components:
1

Name

A unique identifier that Claude uses to reference the tool:
2

Description

Human-readable description that helps Claude understand when to use the tool:
3

Input Schema

Schema defining the tool’s input parameters:

Complete Calculator Example

Here’s a complete example from the SDK examples:

Error Handling

Handle errors gracefully in your tools:

Accessing Application State

Tools can directly access your application’s state:

Advanced Input Schemas

Multiple Parameter Types

JSON Schema

For complex validation, use full JSON Schema:

Tool Naming Convention

When using MCP servers, tools are prefixed with the server name:
Always use the mcp__{server}__{tool} format in allowed_tools when pre-approving MCP server tools.

Using with query()

Tools work with both ClaudeSDKClient and query():

Multiple MCP Servers

You can register multiple MCP servers:

Best Practices

Write clear, specific descriptions that help Claude understand when to use each tool:
Always validate inputs and handle edge cases:
Provide helpful error messages that guide Claude:
Always define tools as async functions, even if they don’t use await:
Add safe tools to allowed_tools to avoid permission prompts:

Complete Working Example

Here’s a complete, runnable example:

Next Steps

Hooks

Implement hooks to control tool execution

Permissions

Learn about tool permission management

MCP Servers

Explore external MCP servers

Examples

See the full calculator example