Running mcp server in a more realistic environment - close to production
Here is a way to productionzing a simple mcp server application. The code for this is here
import logging
from fastapi import FastAPI
from mcp.server.fastmcp import FastMCP
# 1. Initialize your MCP logic
mcp = FastMCP("ProductionToolbox")
# Add a sample tool
@mcp.tool()
def greeting_time(server_id: str) -> str:
"""Returns a greeting hello."""
return f"Server hello {server_id}."
# 2. Create your Production Web App (FastAPI)
app = FastAPI(title="MCP Cloud Gateway")
# --- THE INTEGRATION POINT ---
# This line connects the MCP protocol to the web server.
# It automatically creates endpoints like /sse and /messages.
app.mount("/mcp", mcp.sse_app())
# -----------------------------
@app.get("/health")
def health_check():
"""A standard production health check endpoint."""
return {"status": "ok", "mcp_enabled": True}
if __name__ == "__main__":
import uvicorn
# This creates the "http://127.0.0.1:8000" that the client is looking for
uvicorn.run(app, host="127.0.0.1", port=8000)
And the mcp.json configuration is given here:
{
"inputs": [
{
"type": "promptString",
"id": "server_id",
"description": "server id to check uptime",
"password": false
}
],
"servers": {
"app-mcp-function": {
"type": "sse",
"url": "http://127.0.0.1:8000/mcp/sse"
}
}
}
Run the app
python main.py
Or you can also run in a production environment
uvicorn main:app --host 0.0.0.0 --port 8000 --workers 1
Run your mcp.json
And you get the following returns when you trigger it via VS code. In the chat box, under the Tool -> you can see your app-mcp-function appearing. Then it says it has discovered 1 tool.
And then you can see what is the results of the output and it asking permission to run the mcp function.
Outputs from the console
Some basic requirements
- You must be SSE for integration with vscode.
Link to the repo
https://github.com/kepungnzai/mcp-uvicorn-integration
Comments