How to use Claude with custom MCP tools
Quick answer
Use the
mcp Python SDK to create a Server that connects Claude to your custom MCP tools. Implement your tools as classes with defined commands, then run the server with stdio_server to enable Claude to invoke these tools during conversations.PREREQUISITES
Python 3.8+Anthropic API key (set as ANTHROPIC_API_KEY in environment)pip install anthropic>=0.20 mcp>=0.1
Setup
Install the required packages and set your environment variable for the Anthropic API key.
- Install packages:
pip install anthropic mcp - Set environment variable in your shell:
export ANTHROPIC_API_KEY='your_api_key_here'
pip install anthropic mcp Step by step
Create a custom MCP tool by subclassing mcp.server.Tool and implement commands. Then start the MCP server with stdio_server to connect Claude to your tools.
import os
from anthropic import Anthropic
from mcp.server import Server, Tool
from mcp.server.stdio import stdio_server
# Define a custom tool
class CalculatorTool(Tool):
name = "calculator"
def add(self, x: int, y: int) -> int:
"""Add two integers."""
return x + y
def multiply(self, x: int, y: int) -> int:
"""Multiply two integers."""
return x * y
# Instantiate the Anthropic client
client = Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])
# Create the MCP server with the Claude model and the custom tool
server = Server(
client=client,
model="claude-3-5-sonnet-20241022",
tools=[CalculatorTool()]
)
# Run the server using stdio transport
if __name__ == "__main__":
stdio_server(server) Common variations
You can use different Claude models by changing the model parameter in Server. For async usage, integrate MCP with async event loops. You can also add multiple tools by passing a list of tool instances.
# Example: Using a different Claude model and multiple tools
from mcp.server import Server, Tool
class EchoTool(Tool):
name = "echo"
def repeat(self, message: str) -> str:
return message
server = Server(
client=client,
model="claude-opus-4",
tools=[CalculatorTool(), EchoTool()]
)
# Run with stdio_server(server) as before Troubleshooting
- If you see authentication errors, verify your
ANTHROPIC_API_KEYenvironment variable is set correctly. - If the server does not respond, ensure you run the script in a terminal that supports stdio communication.
- For command errors, check your tool method signatures and names match the commands invoked by Claude.
Key Takeaways
- Use the official
mcpPython SDK to connect Claude with custom tools via MCP protocol. - Define tools as subclasses of
Toolwith command methods callable by Claude. - Run the MCP server with
stdio_serverfor local stdio communication. - Switch Claude models easily by changing the
modelparameter inServer. - Always set
ANTHROPIC_API_KEYin your environment to authenticate API calls.