FastAPI MCP Server
A comprehensive MCP server providing 21+ tools for mathematical operations, string manipulation, file handling, utilities, and web requests via FastAPI and WebSocket.
README
FastAPI MCP Server
A comprehensive FastAPI application with Model Context Protocol (MCP) server functionality, providing 21+ useful tools for mathematical operations, string manipulation, file handling, utilities, and web requests.
🎥 Demo Video
📺 Watch the Demo
Direct Video Link: FastAPI MCP Server Demo Video
Click the link above to watch the demonstration video showing all 21+ tools in action!
🎬 Video Preview
The demo showcases:
- ✅ Mathematical Operations - Addition, subtraction, multiplication, division, power, square root
- ✅ String Manipulation - Uppercase, lowercase, reverse, length operations
- ✅ File Operations - Reading, writing, and directory listing
- ✅ Utility Functions - Time, random numbers, UUID generation, hashing
- ✅ Web Utilities - URL validation and HTTP requests
- ✅ MCP Protocol - WebSocket communication and tool calling
📋 Download Instructions:
- Click the video link above to view in GitHub
- Right-click on the video → "Save video as" to download
- Play locally for the best viewing experience
🔗 Alternative Hosting Options:
- YouTube - Best for public demos with embed preview
- Google Drive - Easy sharing and preview
- GitHub Releases - For downloadable files
- Vimeo - Professional video hosting
Features
🔢 Mathematical Operations
- add - Add two numbers together
- subtract - Subtract second number from first number
- multiply - Multiply two numbers
- divide - Divide first number by second number (with zero-division protection)
- power - Raise first number to the power of second number
- sqrt - Calculate square root of a number
📝 String Operations
- echo - Echo back the input text
- uppercase - Convert text to uppercase
- lowercase - Convert text to lowercase
- reverse_string - Reverse a string
- string_length - Get the length of a string
📁 File Operations
- read_file - Read contents of a file
- write_file - Write content to a file
- list_directory - List files and directories in a path
🛠️ Utility Functions
- get_time - Get current server time
- random_number - Generate a random number between min and max
- generate_uuid - Generate a random UUID
- hash_md5 - Generate MD5 hash of input text
- hash_sha256 - Generate SHA256 hash of input text
🌐 Web Utilities
- validate_url - Validate if a string is a valid URL
- make_request - Make an HTTP request to a URL
Installation
- Clone the repository:
git clone https://github.com/moawizbinyamin/Fastapi-mcp-server-.git
cd Fastapi-mcp-server-
- Create a virtual environment:
python -m venv venv
- Activate the virtual environment:
# Windows
venv\Scripts\activate
# Linux/Mac
source venv/bin/activate
- Install dependencies:
pip install -r requirements.txt
Usage
Running the Server
python main.py
The server will start on http://localhost:8000
Available Endpoints
- GET / - Root endpoint with server info
- GET /health - Health check endpoint
- GET /tools - List all available tools
- POST /tools/call - Call tools via HTTP
- WebSocket /mcp - MCP protocol endpoint
- GET /docs - FastAPI automatic documentation
Testing Tools via HTTP
List all tools:
curl http://localhost:8000/tools
Call a tool:
curl -X POST http://localhost:8000/tools/call \
-H "Content-Type: application/json" \
-d '{"name": "add", "arguments": {"a": 5, "b": 3}}'
Example: Mathematical operations on 2 and 4:
# Addition: 2 + 4 = 6
curl -X POST http://localhost:8000/tools/call \
-H "Content-Type: application/json" \
-d '{"name": "add", "arguments": {"a": 2, "b": 4}}'
# Subtraction: 2 - 4 = -2
curl -X POST http://localhost:8000/tools/call \
-H "Content-Type: application/json" \
-d '{"name": "subtract", "arguments": {"a": 2, "b": 4}}'
# Multiplication: 2 × 4 = 8
curl -X POST http://localhost:8000/tools/call \
-H "Content-Type: application/json" \
-d '{"name": "multiply", "arguments": {"a": 2, "b": 4}}'
# Division: 2 ÷ 4 = 0.5
curl -X POST http://localhost:8000/tools/call \
-H "Content-Type: application/json" \
-d '{"name": "divide", "arguments": {"a": 2, "b": 4}}'
# Power: 2^4 = 16
curl -X POST http://localhost:8000/tools/call \
-H "Content-Type: application/json" \
-d '{"name": "power", "arguments": {"base": 2, "exponent": 4}}'
MCP Protocol Support
This server implements the Model Context Protocol (MCP) specification, allowing it to be used with MCP-compatible clients. The WebSocket endpoint /mcp handles MCP protocol communication.
MCP Configuration
For use with MCP clients, configure your MCP client with:
{
"mcpServers": {
"fastapi-mcp-server": {
"command": "python",
"args": ["main.py"],
"cwd": "/path/to/Fastapi-mcp-server-",
"env": {
"PYTHONPATH": "/path/to/Fastapi-mcp-server-"
}
}
}
}
Dependencies
- FastAPI - Modern, fast web framework for building APIs
- Uvicorn - ASGI server implementation
- Pydantic - Data validation using Python type annotations
- httpx - HTTP client for making web requests
- websockets - WebSocket implementation
Error Handling
All tools include comprehensive error handling:
- Division by zero protection
- File not found handling
- Invalid input validation
- Network request error handling
Development
Project Structure
Fastapi-mcp-server-/
├── main.py # Main FastAPI application
├── requirements.txt # Python dependencies
├── README.md # This file
├── .gitignore # Git ignore rules
├── start_server.bat # Windows startup script
└── start_server.sh # Linux/Mac startup script
Adding New Tools
To add a new tool:
- Add the tool definition to the
toolsdictionary in theMCPServer.__init__()method - Add the tool implementation to the
call_tool()method - Test the tool via the HTTP endpoint
License
This project is open source and available under the MIT License.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Support
For support, please open an issue on the GitHub repository.
推荐服务器
Baidu Map
百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
Audiense Insights MCP Server
通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。
VeyraX
一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。
graphlit-mcp-server
模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。
Kagi MCP Server
一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。
e2b-mcp-server
使用 MCP 通过 e2b 运行代码。
Neon MCP Server
用于与 Neon 管理 API 和数据库交互的 MCP 服务器
Exa MCP Server
模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。