Multiple MCP Servers Framework
Provides a framework for running multiple MCP servers using FastAPI, stdio, and streamable-http transports, with an echo tool for testing and integration with MCP Inspector.
README
🚀 Multiple MCP Servers using FastAPI and Testing with Inspector
This project provides a framework for running multiple Model Context Protocol (MCP) servers using different transport mechanisms: FastAPI-based HTTP servers, standalone streamable-http servers, and stdio-based servers.
✅ Supported MCP Transports:
- stdio
- streamable-http
- FastAPI-mounted
It includes three main scripts:
mcp-server-with-stdio.py➡️ A standalone MCP server usingstdiotransport.mcp-server-with-streamable-http.py➡️ A standalone MCP server usingstreamable-httptransport.mcp-server-fastapi.py➡️ A FastAPI server hosting two MCP instances (McpServer1andMcpServer2) withstreamable-httptransport.
Each server implements a simple echo tool for testing and demonstration, and the framework supports robust logging, environment configuration, and graceful shutdown handling. The servers are compatible with the MCP Inspector for interactive testing. 🕵️♂️
📑 Table of Contents
- 🎯 Purpose
- 🗂️ Project Structure
- ✨ Features
- 🛠️ Prerequisites
- 📦 Required Packages
- ⚙️ Installation
- 🚀 Usage
- 🧩 Workflow
- 📝 Logging
- 🛑 Graceful Shutdown
- 🧭 Notes
- 🛠️ Troubleshooting
🎯 Purpose
The MCP Server Framework is designed to:
- Demonstrate the flexibility of MCP servers using different transports (
streamable-httpandstdio). - Provide a production-ready FastAPI server hosting multiple MCP instances under a single application.
- Support standalone MCP servers for simpler use cases or environments requiring
stdiocommunication. - Ensure robust logging, configuration management, and graceful shutdown for all server types.
- Enable easy integration with the MCP Inspector for testing and tool interaction.
🗂️ Project Structure
The project is organized as follows:
/
├── src/
│ ├── mcp-server-fastapi.py # FastAPI server hosting two MCP instances
│ ├── mcp-server-with-streamable-http.py # Standalone MCP server with streamable-http
│ ├── mcp-server-with-stdio.py # Standalone MCP server with stdio
│ ├── fastapi_mcp_servers/ # MCP server implementations for FastAPI
│ │ ├── __init__.py
│ │ ├── mcp_server_1.py # First MCP server with echo1 tool
│ │ └── mcp_server_2.py # Second MCP server with echo2 tool
│ ├── config/ # Configuration and logging utilities
│ │ ├── __init__.py
│ │ ├── app_settings.py # Environment variable and settings management
│ │ └── logging_config.py # Logging setup with file rotation and cleanup
│ └── util/
│ └── mcp_server_shutdown_handler.py # Graceful shutdown handler for MCP servers
├── .env # Environment variables (e.g., PORT, LOG_DIR)
├── pyproject.toml # Project dependencies
└── README.md # Project documentation
✨ Features
- Multiple MCP Servers:
- Standalone MCP server using
stdiotransport for direct stdin/stdout communication. - Standalone MCP server using
streamable-httptransport athttp://0.0.0.0:8001/mcp/. - FastAPI server hosting two MCP instances at
/echo1/mcp/and/echo2/mcp/withstreamable-httptransport.
- Standalone MCP server using
- Echo Tool: A simple tool that echoes input messages, implemented across all servers for testing.
- Transport Flexibility: Supports
streamable-http(recommended for HTTP-based communication) andstdio(for Inspector-driven communication). - Robust Logging: Configurable logging with file rotation and console output, stored in a specified log directory.
- Environment Configuration: Loads settings from a
.envfile (e.g.,PORT,LOG_DIR,API_KEY). - Graceful Shutdown: Handles
SIGINTandSIGTERMsignals to ensure clean termination of MCP servers. - Lifespan Management (FastAPI): Manages startup and shutdown of MCP session managers in the FastAPI server.
- MCP Inspector Compatibility: All servers are compatible with the MCP Inspector for interactive tool testing.
🛠️ Prerequisites
To run the project, ensure you have the following installed:
- Python: Version 3.8 or higher
- Node.js: Required for running the MCP Inspector
- uv: A Python package manager for installing dependencies
- npx: For running the MCP Inspector
- Operating System: Compatible with Windows, macOS, or Linux
- Recommended: a modern terminal that supports UTF-8
📦 Required Packages
The project depends on the following Python packages:
mcp[cli]: Provides the MCP server and CLI tools.fastapi: The FastAPI framework for themcp-server-fastapi.pyscript.uvicorn: ASGI server implementation for running FastAPI.python-dotenv: For loading environment variables from a.envfile.
⚙️ Installation
-
Clone the Repository:
git clone https://github.com/ahmad-act/Multiple-MCP-Servers-Using-FastAPI-and-Testing-with-Inspector.git cd Multiple-MCP-Servers-Using-FastAPI-and-Testing-with-Inspector -
Set Up a Virtual Environment (optional but recommended):
uv venv source venv/bin/activate # On Windows: venv\Scripts\activate -
Install Dependencies:
Install the required Python packages using
uv:uv syncAlternatively,
uv add mcp[cli] fastapi uvicorn python-dotenv -
Configure Environment Variables:
Create a
.envfile in the project root with the following content:PORT=10000 LOG_DIR=logs
Usage
1️⃣ Standalone Stdio MCP Server (mcp-server-with-stdio.py)
Starting the MCP Server
You do not need to manually run the MCP server for stdio transport. MCP Inspector runs the MCP Server for stdio transport.
Starting the MCP Inspector
The stdio server is typically launched by the MCP Inspector, not manually. Run the Inspector with the following command, adjusting the --directory path to your src/ directory:
npx @modelcontextprotocol/inspector uv --directory "<your-src-directory>" run mcp-server-with-stdio.py --debug

The Inspector will manage the server lifecycle and communicate over stdio.
Opening the MCP Inspector
Open the link http://localhost:6274/?MCP_PROXY_AUTH_TOKEN=XXXXXXXXXXXXXXXXXX with its token in your browser:

Using the MCP Inspector
-
After running the above command, the Inspector will start and automatically connect to the
stdio-based server. -
In the Inspector UI (
http://127.0.0.1:6274), inspect the available tools (e.g.,echo). -
Test the
echotool:-
Input:
{ "message": "test" } -
Expected Output:
{ "echo": "Echo from MCP Server with stdio: test" }

-
2️⃣ Standalone Streamable-HTTP MCP Server (mcp-server-with-streamable-http.py)
Starting the MCP Server
Open new terminal and go to the project root folder:
cd "D:\My Study\AI\GitHub ahmad-act\Multiple-MCP-Servers-Using-FastAPI-and-Testing-with-Inspector"
Run the standalone MCP server:
uv run ./src/mcp-server-with-streamable-http.py
The server will start on http://0.0.0.0:8001/mcp/.

Starting the MCP Inspector
Run the MCP Inspector:
Open new terminal and run the command:
npx @modelcontextprotocol/inspector

Opening the MCP Inspector
Open the Inspector in your browser at http://127.0.0.1:6274.
http://localhost:6274/?MCP_PROXY_AUTH_TOKEN=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Using the MCP Inspector
-
Configure the Inspector to connect to
http://0.0.0.0:8001/mcp/.

-
Test the
echotool:- Input:
{ "message": "test" } - Expected Output:
{ "echo": "Echo from MCP Server with streamable-http: test" }

- Input:
3️⃣ FastAPI-Based MCP Server (mcp-server-fastapi.py)
Starting the MCP Server
Open new terminal and go to the project root folder:
cd "D:\My Study\AI\GitHub ahmad-act\Multiple-MCP-Servers-Using-FastAPI-and-Testing-with-Inspector"
Run the FastAPI server:
uv run ./src/mcp-server-fastapi.py
The server will start on http://0.0.0.0:10000 (or the port specified in the PORT environment variable). The MCP endpoints will be available at:
-
http://0.0.0.0:10000/echo1/mcp/ -
http://0.0.0.0:10000/echo2/mcp/
Starting the MCP Inspector
Run the MCP Inspector:
Open new terminal and run the command:
npx @modelcontextprotocol/inspector

Opening the MCP Inspector
Open the Inspector in your browser at http://127.0.0.1:6274.
http://localhost:6274/?MCP_PROXY_AUTH_TOKEN=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Using the MCP Inspector
-
Configure the Inspector to connect to
http://0.0.0.0:10000/echo1/mcp/forMcpServer1.

-
Test the
echotool:- Input:
{ "message": "test" } - Expected Output:
{ "echo": "Echo from MCP Server 1 using FastAPI: test" }

- Input:
-
Configure the Inspector to connect to
http://0.0.0.0:10000/echo2/mcp/forMcpServer2.

-
Test the
echotool:- Input:
{ "message": "test" } - Expected Output:
{ "echo": "Echo from MCP Server 2 using FastAPI: test" }

- Input:
🧩 Workflow
- Choose the Server Type:
- Use
mcp-server-fastapi.pyfor a multi-MCP HTTP server. - Use
mcp-server-with-streamable-http.pyfor a standalone HTTP-based MCP server. - Use
mcp-server-with-stdio.pyforstdio-based communication, typically managed by the MCP Inspector.
- Use
- Start the Server:
- For FastAPI or streamable-http servers, run the respective script with
uv run. - For the stdio server, use the MCP Inspector to launch the server.
- For FastAPI or streamable-http servers, run the respective script with
- Interact with MCP Tools: Use the MCP Inspector to connect to the server and test the
echotool. - Monitor Logs: Check logs in the
logs/directory (or the directory specified inLOG_DIR) for debugging and monitoring. - Graceful Shutdown: Press
Ctrl+Cor send aSIGTERMsignal to shut down HTTP-based servers gracefully. For thestdioserver, shutting down the Inspector will terminate the server.
📝 Logging
- Logs are stored in the directory specified by the
LOG_DIRenvironment variable (default:logs/). - Log files are named in the format
YYYYMM.log(e.g.,202507.logfor July 2025). - Logs rotate when they reach 5 MB, with up to 5 backup files.
- Old log files can be cleaned up using the
cleanup_old_logsfunction inlogging_config.py.
Example log output:
2025-07-01 13:06:00,123 - __main__ - INFO - Starting FastMCP server with streamable-http transport...
2025-07-01 13:06:00,125 - __main__ - INFO - FastMCP server initialized successfully.
🛑 Graceful Shutdown
All servers handle SIGINT (e.g., Ctrl+C) and SIGTERM signals to ensure graceful shutdown:
- MCP servers are shut down cleanly.
- Logs are updated with shutdown status.
- The process exits with a status code of
0.
🧭 Notes
- Transport Options:
streamable-httpis recommended for HTTP-based communication due to its efficiency.stdiois suitable for environments where direct stdin/stdout communication is preferred, typically with the MCP Inspector.
- Port Configuration:
- The FastAPI server uses port
10000by default (configurable viaPORT). - The streamable-http server uses port
8001(hardcoded). - The stdio server does not use a network port.
- The FastAPI server uses port
- MCP Inspector: Required for testing all servers. Ensure it is running to interact with MCP tools.
🛠️ Troubleshooting
- Server Fails to Start:
- Check logs in the
logs/directory for errors. - Ensure the
PORTenvironment variable is a valid integer for HTTP-based servers. - Verify that port
8001(for streamable-http) or10000(for FastAPI) is not in use.
- Check logs in the
- MCP Inspector Cannot Connect:
- For HTTP servers, verify the server is running and the endpoint URLs are correct.
- For the stdio server, ensure the Inspector command includes the correct
--directorypath.
- Log Files Not Created: Ensure the
LOG_DIRdirectory exists and is writable. - Dependency Issues: Run
uv syncto ensure all required packages are installed.
推荐服务器
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 模型以安全和受控的方式获取实时的网络信息。