loading…
Search for a command to run...
loading…
Provides a framework for running multiple MCP servers using FastAPI, stdio, and streamable-http transports, with an echo tool for testing and integration with M
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.
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:
It includes three main scripts:
mcp-server-with-stdio.py ➡️ A standalone MCP server using stdio transport.mcp-server-with-streamable-http.py ➡️ A standalone MCP server using streamable-http transport.mcp-server-fastapi.py ➡️ A FastAPI server hosting two MCP instances (McpServer1 and McpServer2) with streamable-http transport.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. 🕵️♂️
The MCP Server Framework is designed to:
streamable-http and stdio).stdio communication.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
stdio transport for direct stdin/stdout communication.streamable-http transport at http://0.0.0.0:8001/mcp/./echo1/mcp/ and /echo2/mcp/ with streamable-http transport.streamable-http (recommended for HTTP-based communication) and stdio (for Inspector-driven communication)..env file (e.g., PORT, LOG_DIR, API_KEY).SIGINT and SIGTERM signals to ensure clean termination of MCP servers.To run the project, ensure you have the following installed:
The project depends on the following Python packages:
mcp[cli]: Provides the MCP server and CLI tools.fastapi: The FastAPI framework for the mcp-server-fastapi.py script.uvicorn: ASGI server implementation for running FastAPI.python-dotenv: For loading environment variables from a .env file.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 sync
Alternatively,
uv add mcp[cli] fastapi uvicorn python-dotenv
Configure Environment Variables:
Create a .env file in the project root with the following content:
PORT=10000
LOG_DIR=logs
mcp-server-with-stdio.py)You do not need to manually run the MCP server for stdio transport. MCP Inspector runs the MCP Server for stdio transport.
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.
Open the link http://localhost:6274/?MCP_PROXY_AUTH_TOKEN=XXXXXXXXXXXXXXXXXX with its token in your browser:

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 echo tool:
Input:
{
"message": "test"
}
Expected Output:
{
"echo": "Echo from MCP Server with stdio: test"
}

mcp-server-with-streamable-http.py)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/.

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

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

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

Test the echo tool:
{
"message": "test"
}
{
"echo": "Echo from MCP Server with streamable-http: test"
}

mcp-server-fastapi.py)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/

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

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

Configure the Inspector to connect to http://0.0.0.0:10000/echo1/mcp/ for McpServer1.

Test the echo tool:
{
"message": "test"
}
{
"echo": "Echo from MCP Server 1 using FastAPI: test"
}

Configure the Inspector to connect to http://0.0.0.0:10000/echo2/mcp/ for McpServer2.

Test the echo tool:
{
"message": "test"
}
{
"echo": "Echo from MCP Server 2 using FastAPI: test"
}

mcp-server-fastapi.py for a multi-MCP HTTP server.mcp-server-with-streamable-http.py for a standalone HTTP-based MCP server.mcp-server-with-stdio.py for stdio-based communication, typically managed by the MCP Inspector.uv run.echo tool.logs/ directory (or the directory specified in LOG_DIR) for debugging and monitoring.Ctrl+C or send a SIGTERM signal to shut down HTTP-based servers gracefully. For the stdio server, shutting down the Inspector will terminate the server.LOG_DIR environment variable (default: logs/).YYYYMM.log (e.g., 202507.log for July 2025).cleanup_old_logs function in logging_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.
All servers handle SIGINT (e.g., Ctrl+C) and SIGTERM signals to ensure graceful shutdown:
0.streamable-http is recommended for HTTP-based communication due to its efficiency.stdio is suitable for environments where direct stdin/stdout communication is preferred, typically with the MCP Inspector.10000 by default (configurable via PORT).8001 (hardcoded).logs/ directory for errors.PORT environment variable is a valid integer for HTTP-based servers.8001 (for streamable-http) or 10000 (for FastAPI) is not in use.--directory path.LOG_DIR directory exists and is writable.uv sync to ensure all required packages are installed.from github.com/ahmad-act/Multiple-MCP-Servers-Using-FastAPI-and-Testing-with-Inspector
Выполни в терминале:
claude mcp add multiple-mcp-servers-framework -- npx Не уверен что выбрать?
Найди свой стек за 60 секунд
Автор?
Embed-бейдж для README
Похожее
Все в категории development