Resources in FastMCP
Resources represent data or files that an MCP client can read. They provide read-only access to data for the LLM.
Basic Resource Definition
from fastmcp import FastMCP
mcp = FastMCP(name="DataServer")
@mcp.resource("resource://greeting")
def get_greeting() -> str:
"""Provides a simple greeting message."""
return "Hello from FastMCP Resources!"
@mcp.resource("data://config")
def get_config() -> dict:
"""Provides application configuration as JSON."""
return {
"theme": "dark",
"version": "1.2.0",
"features": ["tools", "resources"],
}
Key Concepts
- URI: Unique identifier (e.g.,
"resource://greeting") - Lazy Loading: Function only executes when client requests the resource
- Auto-inference: Name from function name, description from docstring
Return Types
str: Sent as text (text/plain)dict,list: Auto-serialized to JSONbytes: Base64 encoded as binaryNone: Empty content
Resource Templates
Dynamic resources with parameters:
@mcp.resource("users://{user_id}/profile")
def get_user_profile(user_id: int) -> dict:
"""Retrieves a user's profile by ID."""
return {"id": user_id, "name": f"User {user_id}"}
Async Resources
import aiofiles
@mcp.resource("file:///app/log.txt", mime_type="text/plain")
async def read_log() -> str:
"""Reads log file asynchronously."""
async with aiofiles.open("/app/log.txt", mode="r") as f:
return await f.read()