Skip to main content

Servers with SSE

The stdio transport, that you've been using so far, enables communication through standard input and output streams. This is particularly useful for local integrations and command-line tools.

Now, we will cover SSE Server-Sent Events (SSE) transport. SSE is a standard for server-to-client streaming, allowing servers to push real-time updates to clients over HTTP. This is particularly useful for applications that require live updates, such as chat applications, notifications, or real-time data feeds. Also, your server can be used by multiple clients at the same time as it lives on a server that can be run somewhere in the cloud for example.

Here's what such a server can look like:

from starlette.applications import Starlette
from starlette.routing import Mount, Host
from mcp.server.fastmcp import FastMCP


mcp = FastMCP("My App")

@mcp.tool()
def add(a: int, b: int) -> int:
"""Add two numbers"""
return a + b

# Mount the SSE server to the existing ASGI server
app = Starlette(
routes=[
Mount('/', app=mcp.sse_app()),
]
)

In the preceding code we set up a Starlette server that listens to two endpoints:

  • /sse: This endpoint is used to establish a connection with the server. When a client connects to this endpoint, a new SSEServerTransport instance is created and added to the transports object. The server is then ready to start receiving messages from the client.
  • /messages: This endpoint is used to handle incoming messages from the client. When a message is received, the server looks up the corresponding SSEServerTransport instance and then proceeds to handle the message.

Now that we understand what SSE is, how to create such a server, let's practice it in the next activity.