Skip to main content

Activity: Create an SSE server

Let's create an SSE server and also let's ensure we can reach it via clients like the Inspector and Visual Studio Code.

-1- Create the server

  1. Add the following code to server.py
from starlette.applications import Starlette
from starlette.routing import Mount, Host
from mcp.server.fastmcp import FastMCP


mcp = FastMCP("My App")

Now we have imported the correct things and created a server instance.

  1. Let's add the tools next:
@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()),
]
)

-2- Test the server

To test the server, we will test it in two different ways:

  • Using the Inspector, this is a great tool to quickly test your server and see the results in a nice UI.
  • Using Visual Studio Code. This also provide a nice UI but as it is a code editor, it can make your code development easier depending on what features you add to your MCP server.

-1- Test the server using the Inspector

uvicorn server:app --reload --port 8000

This should start the server.

Now start the inspector in a separate terminal:

npx @modelcontextprotocol/inspector

Select SSE as transport and in the url field, specificy http://localhost:8000/sse and connect.

This will kick off the Inspector and connect to the server.

You should see an inspector window like this:

SSE Inspector

Note how the following have been added:

In the screenshot, I've already selected "Connect" and "Tools" and have gone on to select the "random-joke" tool and what you're looking at is the result of running the tool.

-2- Test the server using Visual Studio Code

Now we know the server is working great using the inspector, let's test it using Visual Studio Code.

Like we did before, let's add an entry to mcp.json file in the .vscode folder:

 "sse-server": {
"type": "sse",
"url": "http://localhost:4321/sse",
}
note

Type the port here that corresponds to where your server is running.

Important to note here is:

  • The type is sse and not stdio.
  • url needs to be specified AND unlike with an stdio server, you need to start up the server before you click the play button.

The complete mcp.json file should look like this:

{
"inputs": [],
"servers": {
"sse-server": {
"type": "sse",
"url": "http://localhost:4321/sse",
}
}
}

To test the server do the following:

  • Start it up.
  • Click the play button in the .vscode/mcp.json file, this should add it to its tools.

Try typing a prompt like so in the Visual Studio Code GitHub Copilot Chat window:

tell me a joke

You should see a result where it's asking to run a tool like this random-joke.

Summary

You've just a simple SSE server that can be used with the Inspector and Visual Studio Code. If you host this server somewhere other people can connect to it.