SOP-005: Building Your First MCP Server
FreshSource: Introduction to Model Context Protocol
Document Control
| Field | Value |
|---|---|
| SOP ID | SOP-005 |
| Version | 1.0 |
| Status | Active |
| Last Updated | 2024-12 |
Overview
Build an MCP (Model Context Protocol) server to expose tools, resources, and prompts to Claude.
Prerequisites
- Python 3.10+
- Basic async/await knowledge
- Understanding of API concepts
MCP Architecture
Step 1: Install MCP SDK
bash
pip install mcpStep 2: Create Basic Server
python
# server.py
from mcp.server import Server
from mcp.server.stdio import stdio_server
from mcp.types import Tool, TextContent
# Create server instance
server = Server("my-mcp-server")
# Define a tool
@server.list_tools()
async def list_tools():
return [
Tool(
name="hello",
description="Say hello to someone",
inputSchema={
"type": "object",
"properties": {
"name": {
"type": "string",
"description": "Name to greet"
}
},
"required": ["name"]
}
)
]
# Implement the tool
@server.call_tool()
async def call_tool(name: str, arguments: dict):
if name == "hello":
return [TextContent(
type="text",
text=f"Hello, {arguments['name']}!"
)]
# Run the server
async def main():
async with stdio_server() as (read, write):
await server.run(read, write)
if __name__ == "__main__":
import asyncio
asyncio.run(main())Step 3: Test with MCP Inspector
bash
# Install inspector
npx @anthropic-ai/mcp-inspector
# Run your server
python server.pyMCP Primitives
| Primitive | Control | Description | Examples |
|---|---|---|---|
| Tools | Model-controlled | Actions/functions the model can call | Search database, Send email, Create file |
| Resources | Application-controlled | Data accessed via URIs | config://settings, db://users/123 |
| Prompts | User-controlled | Pre-defined templates/workflows | Summarize document, Generate tests |
Step 4: Add to Claude Desktop
Edit claude_desktop_config.json:
json
{
"mcpServers": {
"my-server": {
"command": "python",
"args": ["/path/to/server.py"]
}
}
}Verification Checklist
- [ ] MCP SDK installed
- [ ] Server runs without errors
- [ ] Tools listed in inspector
- [ ] Tool execution returns expected result
- [ ] Server connects to Claude
Troubleshooting
| Issue | Solution |
|---|---|
| Import error | Check mcp package installed |
| Connection refused | Check server is running |
| Tool not found | Verify tool name matches |
| Timeout | Check async handling |