Create a FastMCP Server
A FastMCP server is a collection of tools, resources, and other MCP components. To create a server, start by instantiating theFastMCP class.
Create a new file called my_server.py and add the following code:
my_server.py
Add a Tool
To add a tool that returns a simple greeting, write a function and decorate it with@mcp.tool to register it with the server:
my_server.py
Run the Server
The simplest way to run your FastMCP server is to call itsrun() method. You can choose between different transports, like stdio for local servers, or http for remote access:
python my_server.py. The stdio transport is the traditional way to connect MCP servers to clients, while the HTTP transport enables remote connections.
Using the FastMCP CLI
You can also use thefastmcp run command to start your server. Note that the FastMCP CLI does not execute the __main__ block of your server file. Instead, it imports your server object and runs it with whatever transport and options you provide.
For example, to run this server with the default stdio transport (no matter how you called mcp.run()), you can use the following command:
Call Your Server
Once your server is running with HTTP transport, you can connect to it with a FastMCP client or any LLM client that supports the MCP protocol:my_client.py
- FastMCP clients are asynchronous, so we need to use
asyncio.runto run the client - We must enter a client context (
async with client:) before using the client - You can make multiple client calls within the same context
Deploy to Prefect Horizon
Prefect Horizon is the enterprise MCP platform built by the FastMCP team at Prefect. It provides managed hosting, authentication, access control, and observability for MCP servers.Horizon is free for personal projects and offers enterprise governance for teams.
- Push your
my_server.pyfile to a GitHub repository - Sign in to Prefect Horizon with your GitHub account
- Create a new project from your repository and enter
my_server.py:mcpas the server entrypoint
https://your-project.fastmcp.app/mcp. You can chat with it to test its functionality, or connect to it from any LLM client that supports the MCP protocol.
For more details, see the Prefect Horizon guide.
