Sample MCP Fastmcp Python
No description available
Ask AI about Sample MCP Fastmcp Python
Powered by Claude Β· Grounded in docs
I know everything about Sample MCP Fastmcp Python. Ask me about installation, configuration, usage, or troubleshooting.
0/500
Reviews
Documentation
Sample MCP Python
π Overview
Sample MCP Python is a modern, container-ready MCP (Model Context Protocol) server built with FastMCP and FastAPI. It provides simple arithmetic tools and is designed for easy extension and cloud deployment.
β¨ Features
- FastMCP Server: Implements MCP protocol for agent communication.
- Arithmetic Tools: Includes
addandsubtractas MCP tools. - Custom Middleware: Logging middleware for all MCP operations.
- FastAPI Exception Handling: Handles session and route errors gracefully.
- Docker Support: Ready for containerized deployment.
- Configurable Logging: Debug and info logs for all operations.
ποΈ Project Structure
sample-mcp-python/
βββ server.py # Main MCP server with tools and middleware
βββ test_server.py # (Empty) Placeholder for tests
βββ Dockerfile # Container build instructions
βββ pyproject.toml # Python project metadata and dependencies
βββ uv.lock # Dependency lock file
βββ .gitignore # Git ignore rules
βββ .dockerignore # Docker ignore rules
π οΈ Usage
Prerequisites
- Python 3.12+
- Docker (optional, for container deployment)
Local Development
-
Install dependencies:
uv sync -
Run the server:
uv run server.py
Docker
-
Build the image:
docker build -t sample-mcp-python . -
Run the container:
docker run -p 3000:3000 sample-mcp-python
π§© MCP Tools
add(a: int, b: int) -> int: Adds two numbers.subtract(a: int, b: int) -> int: Subtracts the second number from the first.
βοΈ Configuration
- Port: Set the
PORTenvironment variable to change the server port (default: 3000). - Logging: Controlled via
fastmcp.settings.log_leveland Python logging config.
π Extending
Add new MCP tools by decorating functions with @mcp.tool() in server.py.
Custom middleware can be added for logging, authentication, etc.
π§ͺ Testing
Tests are written in test_server.py using pytest and pytest-asyncio for async support.
Run tests
Then, run the tests:
uv run pytest
Example async test:
import pytest
from fastmcp import Client
from server import mcp
client = Client(mcp)
@pytest.mark.asyncio
async def test_add_tool():
async with client:
result = await client.call_tool("add", {"a": 3, "b": 5})
assert result == 8
π¦ Dependencies
fastmcp==2.10.6fastapi>=0.116.1
MCP Inspector
To test with MCP inspector, run it with npx: npx @modelcontextprotocol/inspector
π License
MIT License (add your license file if needed)
Made with β€οΈ using FastMCP and Python.
