Building an MCP server used to mean dealing with protocol messages, schemas, and connection handling. FastMCP makes the process much simpler. You write normal Python functions, and FastMCP turns them into tools that compatible AI applications can discover and call.
For example, you can develop a simple MCP server that allows an AI agent to check system information, query a database, or interact with your own API.
What Is FastMCP?
FastMCP is an open-source Python framework for working with the Model Context Protocol (MCP). MCP is a standard that allows AI applications to communicate with external tools and information. Instead of implementing the protocol yourself, FastMCP handles communication, validation, and tool registration.
It supports three important server components:
- Tools: Python functions that AI applications can execute.
- Resources: Information that MCP clients can read.
- Prompts: Reusable prompt templates for AI applications.
FastMCP also includes an MCP client, authentication features, and support for local and remote servers. FastMCP 4 adds newer MCP capabilities while providing compatibility paths for existing applications. FastMCP is not a language model and does not replace Ollama or your AI agent. It connects compatible clients to your Python functions.
Installing FastMCP
Install FastMCP with Python’s package manager:
python -m pip install fastmcp
Alternatively, if you manage Python projects using uv:
uv add fastmcp
Verify the installation:
fastmcp version
Creating Your First MCP Server
Create a Python file named server.py:
import platform
from fastmcp import FastMCP
mcp = FastMCP("My Local Tools")
@mcp.tool
def system_info() -> dict[str, str]:
"""Return basic system information."""
return {
"os": platform.system(),
"release": platform.release(),
"python": platform.python_version()
}
@mcp.tool
def celsius_to_fahrenheit(celsius: float) -> float:
"""Convert Celsius to Fahrenheit."""
return (celsius * 9 / 5) + 32
@mcp.resource("info://about")
def about_server() -> str:
return "My first FastMCP server."
if __name__ == "__main__":
mcp.run()
The @mcp.tool decorator registers Python functions as MCP tools. FastMCP generates tool definitions from the function parameters, type hints, and docstrings. The @mcp.resource decorator exposes information that MCP clients can retrieve. You do not need to write protocol handlers or manually create JSON schemas.
Running the MCP Server
For clients that start local server processes, use standard input/output (stdio):
fastmcp run server.py:mcp
For local HTTP testing, run:
fastmcp run server.py:mcp --transport http --port 8000
The HTTP endpoint is available locally at http://127.0.0.1:8000/mcp. The HTTP transport is useful when other applications need to communicate with the server over a network.
Testing FastMCP with a Python Client
FastMCP includes an asynchronous MCP client. Create another file named client.py:
import asyncio
from fastmcp import Client
async def main():
async with Client("http://127.0.0.1:8000/mcp") as client:
result = await client.call_tool("system_info", {})
print(result.data)
asyncio.run(main())
Start the HTTP server first, then execute:
python client.py
The client invokes the system_info tool and prints the returned information.
Using FastMCP with AI Agents
FastMCP becomes more useful when connected to an AI agent or an MCP-compatible application. Some possible uses include:
- Checking the status of Docker containers.
- Querying a local SQLite or PostgreSQL database.
- Reading system monitoring information.
- Searching documents stored on a private server.
- Connecting an AI agent to an internal REST API.
For developers running Ollama or Open WebUI, an MCP-capable client or integration is necessary. Ollama itself does not automatically connect to an MCP server.
The main advantage is control. Instead of giving an AI agent unrestricted access to the operating system, you can expose only the functions it needs.
Security matters: Do not expose an unauthenticated MCP server to the public internet. Restrict access, validate inputs, and apply authentication before deploying tools that access sensitive information or modify system resources.
Final Thoughts
FastMCP is a practical framework for developers who want to build MCP tools without spending much time implementing the protocol. Start with a simple read-only Python function, test it locally, and gradually add new tools when they are needed.
For my own self-hosted AI experiments, the ability to expose selected Python functions through a standard protocol is particularly useful.
Further reading:
