Skip to content

SOP-005: Building Your First MCP Server

Fresh

Source: Introduction to Model Context Protocol

Document Control

FieldValue
SOP IDSOP-005
Version1.0
StatusActive
Last Updated2024-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 mcp

Step 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.py

MCP Primitives

PrimitiveControlDescriptionExamples
ToolsModel-controlledActions/functions the model can callSearch database, Send email, Create file
ResourcesApplication-controlledData accessed via URIsconfig://settings, db://users/123
PromptsUser-controlledPre-defined templates/workflowsSummarize 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

IssueSolution
Import errorCheck mcp package installed
Connection refusedCheck server is running
Tool not foundVerify tool name matches
TimeoutCheck async handling

See Also

Based on Anthropic Academy courses