MCP Server Example (Python)
A simple Model Context Protocol (MCP) server implementation in Python.
What is MCP?
The Model Context Protocol (MCP) is an open protocol that enables AI assistants like Claude to securely interact with external tools and data sources. MCP servers expose tools that clients can discover and call.
Project Structure
Setup
Prerequisites
Python 3.13
uv - Fast Python package installer and manager
Installing uv
If you don't have uv installed yet:
Installation
Clone this repository (if you haven't already)
Install dependencies using uv:
This will:
Automatically install Python 3.13 if needed
Create a virtual environment
Install all dependencies from
pyproject.toml
Running the Server
To run the MCP server:
Or activate the virtual environment first:
The server uses stdio (standard input/output) for communication, which is the standard way MCP servers communicate with clients.
Available Tools
This example server provides three simple tools:
1. add_numbers
Adds two numbers together.
Parameters:
a
(number): First numberb
(number): Second number
Example:
2. greet
Generates a personalized greeting.
Parameters:
name
(string): Name of the person to greet
Example:
3. reverse_string
Reverses a string.
Parameters:
text
(string): Text to reverse
Example:
Using with Claude Code
To use this MCP server with Claude Code, you need to configure it in your MCP settings:
Open your Claude Code MCP configuration file (typically at
~/.config/claude/claude_desktop_config.json
)Add this server to the
mcpServers
section:
Restart Claude Code
The tools will now be available for Claude to use!
Customizing the Server
Adding New Tools
To add a new tool to the server:
Add the tool definition to the
list_tools()
functionAdd the tool implementation to the
call_tool()
function
Example:
Advanced Features
The MCP protocol supports additional features beyond basic tools:
Resources: Expose data that can be read (files, API data, etc.)
Prompts: Provide templated prompts
Sampling: Allow the server to request LLM completions
Check the MCP documentation for more information.
Development
Running in Development Mode
For development, you might want to add logging:
Testing
You can test your MCP server using the MCP Inspector tool or by integrating it directly with Claude Code.
Troubleshooting
"Module not found" errors
Make sure you've installed the dependencies:
Server not appearing in Claude Code
Check that the path in your MCP configuration is correct
Make sure the virtual environment is activated (if using one)
Try restarting Claude Code
Tool calls failing
Check the server logs for error messages
Verify that the input schema matches what you're passing
Make sure all required parameters are provided
Why uv?
uv is a modern Python package manager that's:
10-100x faster than pip
Automatically manages Python versions
Has built-in virtual environment support
Compatible with pip and pyproject.toml standards
Resources
License
This is an example project for educational purposes.
This server cannot be installed
local-only server
The server can only run on the client's local machine because it depends on local resources.
A simple educational MCP server providing basic math operations, string manipulation, and greeting functionality. Demonstrates how to implement MCP tools for learning purposes.