Build Your Own MCP Server and Connect it To Claude
In this comprehensive lecture note, we explore the Model Context Protocol (MCP)—an open standard created by Anthropic that standardizes how AI models like Claude, ChatGPT, and Gemini securely discover and invoke external tools and data sources. We will walk through setting up a local development environment, writing a custom Python MCP server containing a deliberate calculation quirk, testing it via the interactive MCP developer UI, and registering it inside the Claude Desktop configuration file.
1. The Core Mental Model: Kitchens, Dishes, and Customers
To understand how the Model Context Protocol (MCP) operates in modern AI architectures, we can use a straightforward real-world analogy.
Real-Life Analogy
The Restaurant Workflow:
- The Server = The Kitchen: A company or developer builds an MCP server, which functions exactly like a professional kitchen. It houses specific equipment and prepares certain items.
- The Tools = The Dishes: The individual Python functions (such as a web search, database query, or calculator) written inside the MCP server are the dishes prepared in that kitchen.
- The AI Model = The Customer: Claude, ChatGPT, or Gemini acts as the customer sitting at the table. When the customer needs a specific result that it cannot calculate or fetch internally, it places an order with the kitchen ("Give me the result of this addition using your calculator dish").
This client-server architecture abstracts away the underlying code complexity. Third-party providers (like Tavily for web search or internal enterprise databases) build their own MCP kitchen once, and any compatible AI client can seamlessly discover and consume their tools without requiring custom integration code for every single model.
2. Why Do We Need MCP? (The Status Quo vs. Standardized Protocol)
Historically, connecting large language models to external tools required writing brittle, custom API wrappers, manual schema definitions, and tightly coupled function-calling logic for each specific model provider.
- The Old Approach: Every time you wanted an LLM to use a new tool, you had to manually define complex JSON schemas, explain parameter types in system prompts, and handle parsing errors across different model endpoints.
- The MCP Approach: The Model Context Protocol establishes a universal communication layer. The MCP server automatically inspects Python functions, extracts their signatures, and exposes them in a format that AI clients understand natively.
- Zero-Config Discovery: The AI client discovers available tools automatically upon connection. You do not need to manually teach Claude the syntax or implementation details of your custom functions; the protocol handles the negotiation.
3. Project Setup & Environment Configuration
Before writing our MCP server, we must initialize a dedicated Python workspace and install the necessary command-line interface tools using modern Python packaging (uv).
Step-by-Step Terminal Initialization
Open your terminal inside VS Code, create a dedicated workspace folder, and set up your virtual environment:
# 1. Create project directories
mkdir -p week-six/my-mcp-server
cd week-six/my-mcp-server
# 2. Initialize and activate virtual environment (Python 3.11)
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# 3. Install the MCP Command-Line Interface via uv
uv add mcp-cli
4. Writing the Custom Python MCP Server
Next, create a file named mcp_server.py inside your my-mcp-server directory. We will implement a simple calculator tool that adds two numbers together, but with an intentional mathematical "bug" (adding an extra 100 to the result). This intentional quirk serves as a verifiable proof-of-concept to confirm whether Claude solved the math internally or successfully routed the request through our custom MCP tool.
from mcp.server import Server
# Initialize the MCP Server instance representing our "kitchen"
mcp = Server("Pratyush_MCP_Kitchen")
@mcp.tool()
def pratyush(a: int, b: int) -> int:
"""
A custom calculator tool that adds two numbers and adds 100 to the sum.
Args:
a (int): First number
b (int): Second number
Returns:
int: The modified sum (a + b + 100)
"""
# Intentional bug for testing verification:
# If Claude calls this tool with 10 and 30, it returns 140 instead of 40.
return a + b + 100
if __name__ == "__main__":
# Run the MCP server over standard input/output (stdio transport)
mcp.run()
5. Testing the MCP Server Locally via Developer UI
Before integrating your server with Claude Desktop, you should verify that your server starts correctly and exposes its tools as expected. The MCP ecosystem provides an interactive developer interface accessible via uv run.
Launching the Developer UI
Run the following command in your terminal:
uv run mcp dev mcp_server.py
- What happens next: A browser window automatically opens displaying the interactive MCP development dashboard.
- Connection & Testing: Click Connect, inspect the discovered tools list at the top of the page, and test the
pratyushtool with sample inputs (e.g., inputting10and30). You will receive140, confirming that your custom tool logic and decorator are functioning correctly.
6. Connecting the MCP Server to Claude Desktop
To allow the Claude Desktop application to discover and communicate with your local MCP server, you must register its execution path in the Claude Desktop configuration JSON file.
Step 1: Locate uv.exe and Project Path
Run the following commands in your terminal to retrieve the absolute paths required for the configuration file:
# For Windows users to find uv executable path:
where uv.exe
# Output example: C:\Users\Pratyush\.local\bin\uv.exe
# Get current absolute working directory path (PWD):
pwd
# Output example: C:\Users\Pratyush\Padho with Pratyush AI Engineer\week-six\my-mcp-server
Step 2: Edit claude_desktop_config.json
Locate your Claude desktop configuration file (ask your AI assistant if you are unsure of its exact OS-specific path, typically found in %APPDATA%\Claude\ on Windows or ~/Library/Application Support/Claude/ on macOS). Open it and add your server definition under the mcpServers block:
{
"mcpServers": {
"Pratyush MCP Kitchen": {
"command": "C:\\Users\\Pratyush\\.local\\bin\\uv.exe",
"args": [
"run",
"--directory",
"C:\\Users\\Pratyush\\Padho with Pratyush AI Engineer\\week-six\\my-mcp-server",
"mcp_server.py"
]
}
}
}
Note: Pay extreme attention to path formatting in JSON files. On Windows, backslashes must be escaped (doubled as
\\), and hidden directory names (such as.local) must be exact. A single typographical error will prevent Claude from spawning the server process.
7. Complete End-to-End Flow Walkthrough
Once your configuration file is saved, complete the following shutdown and verification cycle to test the end-to-end integration:
8. Summary and Key Takeaways
- MCP Definition: The Model Context Protocol (MCP) is an open standard that decouples AI models from external tool implementations, standardizing how clients discover and execute tools.
- Client-Server Architecture: The MCP server acts as a kitchen serving dishes (tools), while AI models like Claude act as customers ordering specific capabilities.
- Zero Schema Overhead: Using framework decorators like
@mcp.tool(), Python function signatures and docstrings are automatically parsed into tool definitions without manual JSON schema authoring. - Interactive Testing: Developers can rapidly test and inspect server behavior locally using
uv run mcp dev <filename.py>. - Desktop Integration: Claude Desktop discovers local servers via standard input/output (stdio) by registering executable paths and working directories inside
claude_desktop_config.json. - Path Precision: Configuration file paths must be meticulously accurate—single character typos or incorrect backslash escaping will cause connection failures.
- Process Restart: When updating MCP configurations or server code, you must completely terminate and restart the Claude Desktop client via Task Manager to ensure the background subprocess reloads correctly.