About Fastmcp Streamable Middleware
Fastmcp Streamable Middleware is an MCP server in the Analytics category: an MCP server that utilizes FastMCP and FastAPI while using a middleware for logging.
Install
git clone https://github.com/ajafry/fastmcp-streamable-middlewareThis server has no ConduID identity, so agent calls to it are not receipted. Pin the version you install and review the source before granting it credentials.
Ask AI
Ask AI about Fastmcp Streamable Middleware
Powered by Claude · Grounded in docs
Security checks
- ✓README presentThe repository ships documentation.
5 checks have not been run yet.
README
Remote MCP Server (Streamable HTTP) utilizing Middleware
A demonstration project showcasing how to build and integrate a Model Context Protocol (MCP) server using FastMCP with FastAPI. This project implements a mathematical operations server that exposes tools for addition, subtraction, and multiplication through the MCP protocol.
🚀 Features
- MCP Server Implementation: Built with FastMCP for seamless LLM tool integration
- FastAPI Integration: Mounts the MCP server within a FastAPI application
- Streamable HTTP Protocol: Uses efficient streamable HTTP transport for client-server communication
- Custom Middleware: Demonstrates middleware implementation for request logging and tool management
- Mathematical Tools: Exposes three mathematical operations (add, subtract, multiply) as MCP tools
- Health Monitoring: Includes health check endpoints for monitoring server status
🏗️ Architecture
Server Components
-
FastMCP Server (
server.py):- Initializes a FastMCP server with mathematical tools
- Implements custom middleware for logging and tool management
- Uses streamable HTTP protocol for efficient communication
- Mounts within FastAPI for unified deployment
-
Custom Middleware (
shared/simple_fastmcp_middleware.py):- Logs incoming requests and tool invocations
- Provides visibility into tool availability and usage
- Demonstrates middleware lifecycle hooks
-
MCP Client (
client.py):- Connects to the MCP server using StreamableHttpTransport
- Lists available tools from the server
- Demonstrates basic client-server interaction
📋 Prerequisites
- Python 3.8+
- FastMCP library
- FastAPI framework
🛠️ Installation
- Clone the repository:
git clone <repository-url>
cd fast-mcp
- Install dependencies:
pip install -r requirements.txt
🚀 Usage
Starting the Server
Run the MCP server with uvicorn:
uvicorn server:app --reload --port 8000
The server will start on http://localhost:8000 with the following endpoints:
- Health Check:
GET /health - MCP Protocol:
/simple-mcp/mcp/*(mounted MCP server)
Running the Client
In a separate terminal, run the client to test the connection:
python client.py
This will connect to the server and list all available tools.
🔧 Key Implementation Details
Mounting MCP Server in FastAPI
The project demonstrates how to mount a FastMCP server within a FastAPI application:
# Initialize FastMCP server
mcp = FastMCP("Math Operations Server")
mcp.add_middleware(SimpleMiddleware())
# Create FastAPI app and mount MCP server
mcp_app = mcp.http_app(transport="streamable-http")
app = FastAPI(lifespan=mcp_app.lifespan)
app.mount("/simple-mcp", mcp_app)
Streamable HTTP Transport
The project uses streamable HTTP protocol for efficient client-server communication:
# Server side
mcp_app = mcp.http_app(transport="streamable-http")
# Client side
client = Client(StreamableHttpTransport("http://localhost:8000/simple-mcp/mcp"))
Custom Middleware Implementation
The SimpleMiddleware class demonstrates how to implement custom middleware for:
- Request logging and monitoring
- Tool availability tracking
- Tool invocation logging
class SimpleMiddleware(Middleware):
async def on_request(self, context: MiddlewareContext, call_next):
self.log_info(f"*** on_request inside MCP server")
return await call_next(context)
async def on_call_tool(self, context: MiddlewareContext, call_next):
# Log tool invocations
return await call_next(context)
Tool Definition
Mathematical tools are defined using FastMCP decorators:
@mcp.tool()
def add(a: float, b: float) -> float:
"""Add two numbers together."""
return a + b
🧪 Available Tools
The server exposes three mathematical tools:
- add(a, b): Adds two numbers
- subtract(a, b): Subtracts second number from first
- multiply(a, b): Multiplies two numbers
🔍 API Endpoints
GET /health- Health check endpointPOST /simple-mcp/mcp/tools/list- List available MCP toolsPOST /simple-mcp/mcp/tools/call- Invoke MCP toolsGET /simple-mcp/mcp/protocol- MCP protocol information
🏁 Benefits of This Architecture
- Unified Deployment: Both REST APIs and MCP tools under one server
- Efficient Communication: Streamable HTTP protocol reduces overhead
- Middleware Support: Custom logging, authentication, and monitoring
- Scalability: FastAPI's performance with MCP's protocol efficiency
- Development Experience: Hot reloading and comprehensive logging
🤝 Contributing
Feel free to submit issues, feature requests, or pull requests to improve this demonstration project.
📄 License
This project is provided as a demonstration and learning resource.
README copy captured when this server was indexed; it may lag the repository. The original is authoritative.