Initialize the server
December 22, 2024 · View on GitHub
Implementing an MCP Server for Claude: Developer Guide
Model Context Protocol (MCP) is an open standard developed by Anthropic that enables seamless integration between Large Language Models (LLMs) and external data sources and tools. It provides a universal protocol for connecting AI systems with various data sources, replacing fragmented integrations with a standardized approach.
MCP is important because it addresses the challenge of isolated AI models by providing a common way for AI assistants to access contextual information from content repositories, business tools, and development environments. This standardization simplifies the process of connecting LLMs to data sources, making it easier to scale AI systems and enhance their relevance and accuracy.
The main specification of MCP includes: A client-server architecture JSON-RPC message format for communication Capability negotiation between clients and servers Support for resources, prompts, and tools as key features
This guide will walk you through the process of creating a Model Context Protocol (MCP) server for Claude. We'll cover the necessary files, their purposes, and provide step-by-step instructions for implementation.
Project Structure
First, let's set up the project structure:
my_mcp_server/
├── server.py
├── requirements.txt
└── claude_desktop_config.json
Step 1: Set Up the Environment
-
Create a new directory for your project:
mkdir my_mcp_server cd my_mcp_server -
Create a virtual environment:
python -m venv venv source venv/bin/activate # On Windows, use `venv\Scripts\activate` -
Create a
requirements.txtfile with the following content:mcp httpx -
Install the dependencies:
pip install -r requirements.txt
Step 2: Implement the Server
Create a file named server.py with the following content:
from typing import Any
import asyncio
import httpx
from mcp.server.models import InitializationOptions
import mcp.types as types
from mcp.server import NotificationOptions, Server
import mcp.server.stdio
# Initialize the server
server = Server("my_server")
# Define your tools
@server.tool("my_tool")
async def my_tool(params: dict[str, Any]) -> str:
# Implement your tool logic here
return "Tool result"
async def main():
# Run the server using stdin/stdout streams
async with mcp.server.stdio.stdio_server() as (read_stream, write_stream):
await server.run(
read_stream,
write_stream,
InitializationOptions(
server_name="my_server",
server_version="0.1.0",
capabilities=server.get_capabilities(
notification_options=NotificationOptions(),
experimental_capabilities={},
),
),
)
if __name__ == "__main__":
asyncio.run(main())
This server.py file sets up a basic MCP server with a single tool. You can add more tools by defining additional functions and decorating them with @server.tool().
Step 3: Configure Claude for Desktop
Create a claude_desktop_config.json file with the following content:
{
"mcpServers": {
"my_server": {
"command": "python",
"args": [
"/ABSOLUTE/PATH/TO/my_mcp_server/server.py"
]
}
}
}
Replace /ABSOLUTE/PATH/TO/ with the actual path to your project directory.
Step 4: Set Up Claude for Desktop
-
Install Claude for Desktop if you haven't already.
-
Locate the Claude for Desktop configuration file:
- On macOS:
~/Library/Application Support/Claude/claude_desktop_config.json - On Windows:
%APPDATA%\Claude\claude_desktop_config.json
- On macOS:
-
Copy the contents of your
claude_desktop_config.jsoninto this file. -
Restart Claude for Desktop.
Step 5: Test Your Server
-
Run your server:
python server.py -
Open Claude for Desktop and look for the hammer icon in the bottom right corner of the input box. This indicates that your MCP server is connected.
-
Click on the hammer icon to see your available tools.
-
Test your tool by asking Claude to use it in a conversation.
Additional Considerations
Error Handling: Implement robust error handling in your tool functions to gracefully handle unexpected inputs or API failures.
Asynchronous Operations: Use async/await for any I/O-bound operations to ensure your server remains responsive.
Security: Be cautious about the operations your tools perform, especially if they interact with the file system or external APIs.
Versioning: Implement a versioning system for your server and tools to manage updates and backwards compatibility.