By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
You want to build an MCP server that exposes tools and resources to an AI client, but you don't want to hand-write JSON schemas or protocol boilerplate. The FastMCP decorator library in the official Python SDK handles that translation for you.
FastMCP is a decorator-based helper included in the official Python SDK. You write ordinary Python functions with type hints and docstrings. The SDK extracts those annotations and converts them into the JSON Schemas and tool descriptions that MCP clients need to call your server.
Start by installing the SDK:
pip install "mcp[cli]" # official Python SDK (check the README for your version)Then write your server. The example below stores notes in memory (fine for learning), exposes two tools and one resource, and includes a prompt that guides the model:
# server.py
from mcp.server.fastmcp import FastMCP
mcp = FastMCP("notes")
NOTES: dict[int, str] = {} # in-memory: fine for learning only
@mcp.tool()
def add_note(text: str) -> str:
"""Save a short note and return its id."""
note_id = len(NOTES) + 1
NOTES[note_id] = text
return f"Saved note #{note_id}"
@mcp.tool()
def search_notes(query: str) -> list[str]:
"""Return notes whose text contains the query (case-insensitive)."""
return [t for t in NOTES.values() if query.lower() in t.lower()]
@mcp.resource("notes://{note_id}")
def read_note(note_id: int) -> str:
"""The full text of one note."""
return NOTES[note_id]
@mcp.prompt()
def summarize_notes() -> str:
return "Read every note resource and write a 3-bullet summary."
if __name__ == "__main__":
mcp.run(transport="stdio") # or "streamable-http" for remote use
The @mcp.tool() decorator marks functions as tools the model can call. The @mcp.resource() decorator exposes data at a URI pattern. The @mcp.prompt() decorator provides a template the model can request. Running mcp.run(transport="stdio") starts the server on standard input and output.
When you decorate a function, FastMCP reads its name, type hints, and docstring, then builds a tool definition with a name, description, and input schema. That schema is what the client receives when it calls tools/list. The model uses the tool name and docstring to decide whether to call it:
Type hints matter. A parameter typed as str becomes a string in the schema. A return type of list[str] tells the client what to expect. Docstrings become the description field. The model sees all of this when deciding which tool to invoke.
Run the MCP Inspector, the official debugging UI, against your server. Call each tool by hand and watch the requests and responses. This catches schema errors and logic bugs before you wire it into a host application.
Once you're confident, register the server in a host's config. For stdio servers, that usually means a command and args entry. Then ask the assistant to perform an action—save a note, for example—and observe what the model sees and how it calls your tools.
Further reading: modelcontextprotocol.io — Build a server and github.com/modelcontextprotocol/python-sdk
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.