FastMCP: Quickstart
Relevant source files
The following files were used as context for generating this wiki page:FastMCP is a high-level framework for rapidly developing Model Context Protocol (MCP) servers in Python. It simplifies the process of exposing Python functions as MCP features (Tools, Resources, and Prompts) by using decorators. This approach minimizes boilerplate code and allows developers to focus on the core logic of their application.
The central component is the FastMCP class, which acts as a registry for features and a factory for creating server applications. It automatically handles the conversion of Python functions, type hints, and docstrings into the corresponding MCP protocol definitions, such as input/output schemas.
Sources: src/mcp/server/fastmcp/server.py:20-24, examples/fastmcp/readme-quickstart.py:1-10
Core Concepts
The FastMCP workflow revolves around instantiating the FastMCP class and then using its decorator methods (@mcp.tool, @mcp.resource, @mcp.prompt) to register functions.
The FastMCP Class
The FastMCP class is the entry point for building a server. It is initialized with server-level information and provides methods to generate web server applications.
# examples/fastmcp/readme-quickstart.py:6-7
mcp = FastMCP(
name="Demo",
)
This class manages internal ToolManager, ResourceManager, and PromptManager instances to handle the registration and logic for each feature type.
Sources: src/mcp/server/fastmcp/server.py:20-24, tests/server/fastmcp/test_server.py:24-27
Decorator-Based Feature Definition
FastMCP uses decorators to transform standard Python functions into MCP features. This is the primary mechanism for building a server.
| Decorator | Purpose |
|---|---|
@mcp.tool() | Exposes a function as a callable MCP Tool. |
@mcp.resource() | Exposes a function as a readable MCP Resource, often using a URI template. |
@mcp.prompt() | Exposes a function that generates a prompt (a list of messages) for an LLM. |
This diagram illustrates how a decorated Python function is registered with the FastMCP instance.
Defining Features
Tools with @mcp.tool()
The @mcp.tool() decorator exposes a function as a remote procedure that a client can call. Python type hints are used to automatically generate the inputSchema and outputSchema for the tool.
Example: An add tool
# examples/fastmcp/readme-quickstart.py:11-13
@mcp.tool()
def add(a: int, b: int) -> int:
return a + b
- Function Name (
add): Becomes the tool name. - Parameters (
a: int,b: int): Define the tool's input arguments. - Return Type (
-> int): Defines the tool's output. The server wraps primitive return types in a structured JSON object (e.g.,{"result": 30}).
Sources: examples/fastmcp/readme-quickstart.py:11-13, tests/server/fastmcp/test_server.py:410-415
Resources with @mcp.resource()
The @mcp.resource() decorator makes data available for clients to read. It requires a URI or URI template that defines how the resource is accessed. Parameters in the URI template must match the decorated function's arguments.
Example: A greeting resource
# examples/fastmcp/readme-quickstart.py:16-18
@mcp.resource("greeting://{name}")
def greeting(name: str) -> str:
return f"Hello, {name}!"
- URI Template (
"greeting://{name}"): Defines the access pattern. When a client requestsgreeting://Alice, thegreetingfunction is called withname="Alice". - Function (
greeting): The function's return value becomes the content of the resource.
Sources: examples/fastmcp/readme-quickstart.py:16-18, tests/server/fastmcp/test_server.py:641-647
Prompts with @mcp.prompt()
The @mcp.prompt() decorator registers a function that generates a dynamic prompt. The function's docstring is used as the prompt's description unless a description argument is provided to the decorator.
Example: A basic_prompt
# tests/server/fastmcp/test_title.py:50-52
@mcp.prompt(description="Basic prompt")
def basic_prompt(topic: str) -> str:
return f"Tell me about {topic}"
- Function (
basic_prompt): Returns the content of the prompt, typically as a string or aMessageobject. - Arguments (
topic: str): Allow the client to provide context to customize the generated prompt.
Sources: tests/server/fastmcp/test_title.py:49-53, tests/server/fastmcp/test_server.py:993-997
Running the Server
Once features are defined, the FastMCP instance can generate a server application compatible with ASGI servers like Uvicorn. FastMCP supports multiple transport protocols.
mcp.sse_app(): Creates a server application that communicates over Server-Sent Events (SSE).mcp.streamable_http_app(): Creates a server application that uses a streaming HTTP transport.
The following diagram shows the server startup process.
This process is demonstrated in integration tests, where a server is started in a separate process for a client to connect to.
Sources: src/mcp/server/fastmcp/server.py:91-94, src/mcp/server/fastmcp/server.py:113-116, tests/server/fastmcp/test_integration.py:121-126
Client Interaction and Testing
The test_integration.py file provides a clear example of how a client interacts with a FastMCP server. A test connects to the quickstart server, initializes a session, and then calls the defined tool and resource.
Test Flow
This sequence diagram outlines the interaction during the test_fastmcp_quickstart test.
Test Implementation
The test code uses an mcp.client.session.ClientSession to perform these actions.
Calling the add tool:
# tests/server/fastmcp/test_integration.py:531-534
# Test add tool
tool_result = await session.call_tool("add", {"a": 10, "b": 20})
assert len(tool_result.content) == 1
assert isinstance(tool_result.content[0], TextContent)
assert tool_result.content[0].text == "30"
Reading the greeting resource:
# tests/server/fastmcp/test_integration.py:537-541
# Test greeting resource directly
from pydantic import AnyUrl
resource_result = await session.read_resource(AnyUrl("greeting://Alice"))
assert len(resource_result.contents) == 1
assert isinstance(resource_result.contents[0], TextResourceContents)
assert resource_result.contents[0].text == "Hello, Alice!"
Sources: tests/server/fastmcp/test_integration.py:516-541
Conclusion
The FastMCP framework significantly accelerates the development of MCP-compliant servers. By leveraging Python decorators and type hints, it provides an intuitive and "Pythonic" way to define complex server features with minimal code. The quickstart example demonstrates how to create a functional server with both a tool and a resource in fewer than 20 lines of code, highlighting the framework's efficiency and ease of use.
Sources: examples/fastmcp/readme-quickstart.py, src/mcp/server/fastmcp/server.py