An MCP Server is a simple program that lets AI models securely access data and tools using the Model Context Protocol (MCP). FastMCP is a Python framework that helps you build MCP servers and clients. It makes working with the Model Context Protocol easier by providing high-level tools for AI interactions. In this post, we will see how to create an MCP Server using FastMCP.
Create your own MCP Server using FastMCP
The Model Context Protocol (MCP) securely connects LLM applications with external data and functionality, like a web API designed for AI interactions. MCP servers expose Resources (GET-like data retrieval), Tools (action execution similar to POST/PUT), and Prompts (structured templates for interactions).
FastMCP makes it easy to use the MCP protocol by removing unnecessary steps like setting up servers, handling protocols, and managing errors. It has a simple and user-friendly design that lets us create MCP tools quickly, often just by adding decorators to their functions. The new version, FastMCP 2.0, adds features like client libraries, server proxying, composition patterns, and integration with OpenAPI/FastAPI. Its main goals are speed, simplicity, and full compatibility with the MCP protocol.
If you want to create your own MCP Server using FastMCP, you can follow the steps mentioned below.
1] Download FastMCP Server in your environment
Downloading FastMCP Server is pretty simple, but we recommend you create an environment and then download it. Even though you can do the installation using PowerShell or the Command Prompt app, we are going to use Visual Studio Terminal. So, open Visual Studio Code, and then open the folder where you are going to set up your project. Now, open Terminal > New Terminal and run the following commands.
uv venv
source .venv/bin/activate
uv pip install fastmcp
Wait for a little while as FastMCP installs on your computer.
2] Create a sample MCP Server
Now that we have installed FastMCP, let us go ahead and create a sample script that will create an MCP Server. You can write your own script or use the one we have mentioned below. So, in Visual Studio Code, inside your project directory, create a file and name it MCPWeatherBot.py; paste the following lines of code.
from fastmcp import FastMCP # Initialize the MCP server with a weather-related function weather_server = FastMCP( name="WeatherBot", instructions="This server provides real-time weather updates for requested locations." ) # Run the server when the script is executed directly if __name__ == "__main__": weather_server.run()
This script sets up an MCP server called “MCPWeatherBot”, designed to provide real-time weather updates. The instructions specify its purpose, ensuring users can request weather data for different locations. When executed, the weather_server.run() command starts the server, making it accessible for AI interactions.
If you are not able to figure it out yourself, the MCP Server follows the syntax given below.
from fastmcp import FastMCP # Create a server instance mcp = FastMCP(name="ServerName")
To run the script, you need to go to Run > Start debugging. Let the script run and do its job.
Additionally, if you want to run the MCP Server on the Local Port, you can run the following command.
fastmcp run servername.py
That’s how you can run the MCP Server.
3] Understand FastMCP Tools, Prompt, and other resources
Now, let us learn more about FastMCP and its components.
FastMCP tools help language models run Python functions easily, linking AI reasoning to real-world actions. You can use the @mcp.tool() decorator to add computations, API calls, or image generation (with fastmcp.Image) to their workflows. FastMCP makes this easier by automatically creating schemas from type hints and docstrings. This is useful for tasks like multiplying numbers, getting live data, or activating external systems. The tools work for both synchronous and asynchronous requests and can produce different types of output, such as plain text and structured data, making them flexible for various applications.
FastMCP resources provide read-only access to static and dynamic data. Static resources, like `config://version`, deliver fixed content such as software versions. Dynamic templates, such as `users://{user_id}/profile`, use placeholders to fetch data, like user profiles by ID. This method works like RESTful patterns, letting clients request specific data while keeping server-side logic clear and efficient. These resources are great for databases, APIs, or configuration files. They ensure secure and efficient data sharing without unnecessary code.
Prompts standardize interactions with language models by using customizable templates for tasks like summarizing or analyzing information. This consistency ensures uniform outputs. Templates can provide simple text or detailed Message objects for better input control. For example, a summarization prompt can format user-provided text. By organizing prompts this way, teams maintain consistency and reduce repetition in AI interactions.
FastMCP’s Context parameter enhances tools, resources, and prompts. Use `ctx: Context` in decorated functions to access real-time features. You can log messages with `ctx.info()`, fetch data using `ctx.http_request()`, and delegate tasks via `ctx.sample()`. Track progress with `ctx.report_progress()` and retrieve data with `ctx.read_resource()`. This system connects functions into state-aware components, ideal for tasks like data pipelines or multi-step AI collaborations.
Read: How to configure MCP server on Windows 11 using Claude?
How do I integrate dynamic data and LLM interactions into my MCP server?
FastMCP streamlines dynamic workflows with resource templates, like users://{user_id}/profile, which fetch specific data subsets using parameters. By utilizing the Context parameter, you can enable interactions with language models via ctx.sample(), allowing server delegation of tasks like text summarization to client models. Additionally, you can use ctx.http_request() for external APIs and ctx.read_resource() for internal data access.
Read: How to find best MCP Servers for your AI framework?
How do I secure my MCP server when exposing tools and resources?
FastMCP enhances security through FastAPI middleware, allowing developers to implement authentication tokens, rate limiting, or OAuth2 integration to control access. Input validation ensures tools and resources handle data securely, while ctx.log() enables auditing and monitoring of suspicious activity, providing a robust security framework for MCP applications.
Also Read: Install Tavily MCP server in VS Code on Windows 11.
Leave a Reply