Deploy a remote, stateless MCP server on Modal with FastMCP
This example demonstrates how to deploy a simple MCP server on Modal.
The server provides a tool to get the current date and time in a given timezone. It is a stateless MCP server, meaning that it does not store any state between requests, which is important for mapping onto Modal’s serverless Functions. It uses the “streamable HTTP” transport type.
Building the MCP server
First, we define our dependencies.
We use the FastMCP library to create the MCP server. We wrap with a FastAPI server to expose it to the Internet.
Next, we create the MCP server itself using FastMCP and add a tool to it that allows LLMs to get the current date and time in a given timezone.
We then use FastMCP to create a Starlette app with streamable-http as transport
type, and set stateless_http=True to make it stateless.
This will be mounted by the FastAPI app, which we deploy as a Modal web endpoint using the asgi_app decorator:
And we’re done!
Testing the MCP server
Now you can serve the MCP server by running:
Then open the MCP inspector:
Enter the URL of the MCP server that was printed by the modal serve command above,
suffixed with /mcp/ (so for example https://modal-labs-examples--datetime-mcp-server-web-dev.modal.run/mcp/). Also
make sure to select “Streamable HTTP” as the “Transport Type”.
After connecting and clicking “List Tools” in the “Tools” tab you should see your current_date_and_time tool listed, and if you “Run Tool” it should give you the
current date and time in UTC!
To automatically test the MCP server, we spin up a client and have it list the tools.
This test is executed by running the script with modal run:
Deploying the MCP server
modal serve creates an ephemeral, hot-reloading server,
which is useful for testing and development.
When it’s time to move to production, you can deploy the server with