Build MCP Server with Claude Desktop and Python
Try MCP by building a server and trying it as quickly as possible using Claude Desktop
MCP (Model Context Protocol) is gaining a lot of attention lately. It’s an open-source protocol that enables large language models (LLMs) to call external functions and enrich their context with data from sources like databases, APIs, and file systems.
Today, I’ll show you a quick way to experiment with it. For more details, check the official documentation.
Prerequisites
To follow along, make sure you have the following installed:
Step 1: Set up the project
Windows
# Create a new directory for our project
uv init fakedb
cd fakedb
# Create virtual environment and activate it
uv venv
.venv\Scripts\activate
# Install dependencies
uv add mcp[cli]
# Create our server file
code fakedb.py
macOS
# Create a new directory for our project
uv init fakedb
cd fakedb
# Create virtual environment and activate it
uv venv
source .venv/bin/activate
# Install dependencies
uv add "mcp[cli]"
# Create our server file
touch fakedb.py
Step 2: Build a simple MCP server
Let’s add an endpoint to our MCP server that retrieves user information by user ID.
fakedb.py
This script creates a simple MCP server named fakedb
with one tool, get_user_info
. The tool takes a user ID, looks it up in a hardcoded dictionary (fake_db
), and returns the matching user’s name and age. You could make this more interesting by integrating a real database and performing an actual query, but for simplicity, we’re keeping it in-memory here.
from typing import Any
from mcp.server.fastmcp import FastMCP
# Initialize FastMCP server
mcp = FastMCP("fakedb")
@mcp.tool()
def get_user_info(user_id: str) -> dict[str, Any] | None:
"""Get user information from the database.
Args:
user_id: The ID of the user to retrieve.
"""
# Here we're simulating a real database lookup
# (you can try with a real one to make it more interesting)
fake_db = {
"1234": {"name": "Alice", "age": 30},
"5678": {"name": "Bob", "age": 25},
}
return fake_db.get(user_id)
if __name__ == "__main__":
# Initialize and run the server
mcp.run(transport='stdio')
Step 3: Connect to Claude Desktop
Windows
Open:
%APPDATA%\Claude\claude_desktop_config.json
macOS
Open:
~/Library/Application Support/Claude/claude_desktop_config.json
Replace the file contents with:
{
"mcpServers": {
"userDatabase": {
"command": "uv",
"args": [
"--directory",
"<<FULL PATH TO YOUR PROJECT DIRECTORY>>",
"run",
"fakedb.py"
]
}
}
}
This configuration creates a new MCP server connection called userDatabase. The command
and args
simply run your MCP server.
Restart Claude Desktop, and you should see your new MCP server with one endpoint, called get_user_info.
Step 4: Test it in Claude
Now, if you ask Claude for user information (for example, “Give me all you have about user with id 1234 in tabular format”), the LLM will:
Detect the
get_user_info
tool.Call it with the provided
user_id
.Return the result in a neat, tabular format, exactly how we defined it.
You can see from the image above the prompt, the tool that was used and the output.
So this is it! A quick and neat way to test what MCP is and how it works without writing lots of complex code.
Thanks for reading!