Skip to main content
This example demonstrates how to create an in-process MCP (Model Context Protocol) server with calculator tools using the Claude Agent SDK.
Unlike external MCP servers that require separate processes, this server runs directly within your Python application, providing better performance and simpler deployment.

Overview

We’ll create a calculator server with the following tools:
  • add - Add two numbers
  • subtract - Subtract one number from another
  • multiply - Multiply two numbers
  • divide - Divide one number by another
  • sqrt - Calculate square root
  • power - Raise a number to a power

Defining Tools

Use the @tool decorator to create MCP tools:

Advanced Tools

Tools can include error handling and validation:

Creating the MCP Server

Use create_sdk_mcp_server to create your calculator server:
MCP tool names follow the pattern mcp__{server_name}__{tool_name}. In this example, the add tool becomes mcp__calc__add.

Running Multiple Calculations

Here’s a complete example with multiple prompts:

Tool Response Format

Tools must return a dictionary with:
  • content: List of content blocks (typically text)
  • is_error (optional): Boolean indicating if an error occurred

Key Takeaways

  • Use @tool decorator to define MCP tools
  • MCP tools are async functions that accept a dict and return a dict
  • Use create_sdk_mcp_server() to create the server
  • Add server to mcp_servers in ClaudeAgentOptions
  • Pre-approve tools using the mcp__{server}__{tool} naming pattern
  • Include error handling in your tools

Next Steps

Tool Permissions

Control tool access with callbacks

Hooks

Customize behavior with hooks