Labsco
hannguyendd logo

FastAPI with MCP

from hannguyendd

A FastAPI application demonstrating MCP integration for mathematical operations and tool registration.

๐Ÿ”ฅโœ“ VerifiedFreeAdvanced setup

FastAPI with Model Context Protocol (MCP)

A FastAPI application integrated with Model Context Protocol (MCP) for mathematical operations and tool registration examples. This project demonstrates how to build MCP servers using FastAPI and shows different approaches to registering tools.

Features

  • FastAPI web framework integration with MCP
  • Multiple mathematical operations (add, multiply, subtract)
  • Different tool registration patterns (decorators vs functions)
  • Server-Sent Events (SSE) support for real-time communication
  • Example configurations for MCP client integration
  • Pandas integration for data manipulation demonstrations

Project Structure

fastapi-with-mcp/
โ”œโ”€โ”€ fastapi_mcp.py          # Main FastAPI + MCP application
โ”œโ”€โ”€ test_tool_registration.py # Tool registration testing examples
โ”œโ”€โ”€ config.json             # MCP client configuration
โ”œโ”€โ”€ pyproject.toml          # Python project configuration
โ”œโ”€โ”€ .python-version         # Python version specification
โ”œโ”€โ”€ .gitignore              # Git ignore rules
โ”œโ”€โ”€ uv.lock                 # UV lock file for dependencies
โ””โ”€โ”€ README.md               # This file

API Endpoints

FastAPI Endpoints

  • GET /add?a={int}&b={int} - Add two numbers
  • GET /multiply?a={int}&b={int} - Multiply two numbers
  • GET /docs - Interactive API documentation
  • GET /redoc - ReDoc API documentation

MCP Endpoints

  • GET /mcp-server/sse - Server-Sent Events endpoint for MCP communication
  • MCP tools are accessible through the MCP protocol via the mounted server at /mcp-server

Development

Testing Tool Registration

Run the tool registration test to see different registration methods:

python test_tool_registration.py

Simple MCP Server Example

For testing tool registration methods, see test_tool_registration.py:

python test_tool_registration.py

Dependencies

  • FastAPI[standard]: Modern, fast web framework for building APIs with standard extras
  • FastMCP: FastAPI integration for Model Context Protocol
  • Pandas: Data manipulation library (used for mathematical operations)
  • Pydantic: Data validation library
  • MCP: Model Context Protocol implementation

Environment Variables

This project uses dotenv to load environment variables. Create a .env file for environment-specific configurations:

# Add any environment variables here if needed
# Example:
# DEBUG=true
# LOG_LEVEL=info

Learn More