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:Using with query()
Tools work with bothClaudeSDKClient and query():
Multiple MCP Servers
You can register multiple MCP servers:Best Practices
Clear Descriptions
Clear Descriptions
Write clear, specific descriptions that help Claude understand when to use each tool:
Validate Inputs
Validate Inputs
Always validate inputs and handle edge cases:
Return Meaningful Errors
Return Meaningful Errors
Provide helpful error messages that guide Claude:
Use Async Functions
Use Async Functions
Always define tools as async functions, even if they don’t use await:
Pre-approve Safe Tools
Pre-approve Safe Tools
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