How to run MCP server with SSE
Quick answer
Run an MCP server with SSE transport by using the official
mcp Python SDK's stdio_server with SSE enabled. Import Server and stdio_server from mcp.server, then start the server with stdio_server() configured for SSE. This enables event-stream communication for AI agents.PREREQUISITES
Python 3.8+pip install mcpBasic knowledge of Python async programming
Setup
Install the official MCP Python SDK and ensure Python 3.8 or higher is installed. The MCP SDK provides the Server class and stdio_server helper to run MCP servers with different transports including SSE.
pip install mcp Step by step
This example shows how to run a basic MCP server using SSE transport. The server listens for MCP messages over Server-Sent Events and responds accordingly.
from mcp.server import Server
from mcp.server.stdio import stdio_server
# Define a simple MCP server handler
class MyMCPServer(Server):
async def handle_message(self, message):
# Echo back the received message content
response = {
"id": message.get("id"),
"type": "response",
"data": f"Received: {message.get('data', '')}"
}
return response
# Run the MCP server with SSE transport
if __name__ == "__main__":
# stdio_server runs the server using SSE by default
stdio_server(MyMCPServer()) output
Running MCP server with SSE transport... (Server listens for incoming MCP messages and responds)
Common variations
- Async server: The MCP server handler methods are async, allowing concurrency.
- Different transports: Besides SSE, MCP supports stdio transport; use
stdio_server()for SSE. - Custom message handling: Override
handle_messageto implement your logic.
Troubleshooting
- If the server does not respond, ensure the client supports SSE and the connection is not blocked by proxies.
- Check Python version is 3.8 or higher to support async features.
- Use logging inside
handle_messageto debug message flow.
Key Takeaways
- Use the official
mcpPython SDK to run MCP servers with SSE transport easily. - Implement async
handle_messagein yourServersubclass for custom logic. - Run the server with
stdio_server(MyMCPServer())to enable SSE communication. - Ensure clients support SSE and Python version is 3.8+ for async compatibility.