@mcp.prompt decorator.
What Are Prompts?
Prompts provide parameterized message templates for LLMs. When a client requests a prompt:- FastMCP finds the corresponding prompt definition.
- If it has parameters, they are validated against your function signature.
- Your function executes with the validated inputs.
- The generated message(s) are returned to the LLM to guide its response.
Prompts
The @prompt Decorator
The most common way to define a prompt is by decorating a Python function. The decorator uses the function name as the prompt’s identifier.
- Name: By default, the prompt name is taken from the function name.
- Parameters: The function parameters define the inputs needed to generate the prompt.
- Inferred Metadata: By default:
- Prompt Name: Taken from the function name (
ask_about_topic). - Prompt Description: Taken from the function’s docstring.
- Prompt Name: Taken from the function name (
Decorator Arguments
While FastMCP infers the name and description from your function, you can override these and add additional metadata using arguments to the@mcp.prompt decorator:
@prompt Decorator Arguments
Sets the explicit prompt name exposed via MCP. If not provided, uses the function name
A human-readable title for the prompt
Provides the description exposed via MCP. If set, the function’s docstring is ignored for this purpose
A set of strings used to categorize the prompt. These can be used by the server and, in some cases, by clients to filter or group available prompts.
A boolean to enable or disable the prompt. See Component Visibility for the recommended approach.
New in version
2.13.0Optional list of icon representations for this prompt. See Icons for detailed examplesNew in version
2.11.0Optional meta information about the prompt. This data is passed through to the MCP client as the meta field of the client-side prompt object and can be used for custom metadata, versioning, or other application-specific purposes.New in version
3.0.0Optional version identifier for this prompt. See Versioning for details.Using with Methods
For decorating instance or class methods, use the standalone@prompt decorator and register the bound method. See Tools: Using with Methods for the pattern.
Argument Types
New in version2.9.0
The MCP specification requires that all prompt arguments be passed as strings, but FastMCP allows you to use typed annotations for better developer experience. When you use complex types like list[int] or dict[str, str], FastMCP:
- Automatically converts string arguments from MCP clients to the expected types
- Generates helpful descriptions showing the exact JSON string format needed
- Preserves direct usage - you can still call prompts with properly typed arguments
Return Values
Prompt functions must return one of these types:str: Sent as a single user message.list[Message | str]: A sequence of messages (a conversation). Strings are auto-converted to user Messages.PromptResult: Full control over messages, description, and metadata. See PromptResult below.
Message
New in version3.0.0
Message provides a user-friendly wrapper for prompt messages with automatic serialization.
Message accepts two fields:
content - The message content. Strings pass through directly. Other types (dict, list, BaseModel) are automatically JSON-serialized to text.
role - The message role, either "user" (default) or "assistant".
PromptResult
New in version3.0.0
PromptResult gives you explicit control over prompt responses: multiple messages, roles, and metadata at both the message and result level.
PromptResult:
PromptResult
Messages to return. Strings are wrapped as a single user Message.
Optional description of the prompt result. If not provided, defaults to the prompt’s docstring.
Result-level metadata, included in the MCP response’s
_meta field. Use this for runtime metadata like categorization, priority, or other client-specific data.The
meta field in PromptResult is for runtime metadata specific to this render response. This is separate from the meta parameter in @mcp.prompt(meta={...}), which provides static metadata about the prompt definition itself (returned when listing prompts).str or list[Message | str] from your prompt functions—PromptResult is opt-in for when you need to include metadata.
Required vs. Optional Parameters
Parameters in your function signature are considered required unless they have a default value.data_uri. If analysis_type or include_charts are omitted, their default values will be used.
Component Visibility
New in version3.0.0
You can control which prompts are enabled for clients using server-level enabled control. Disabled prompts don’t appear in list_prompts and can’t be called.
Async Prompts
FastMCP supports both standard (def) and asynchronous (async def) functions as prompts. Synchronous functions automatically run in a threadpool to avoid blocking the event loop.
async def when your prompt function performs I/O operations like network requests or database queries, since async is more efficient than threadpool dispatch.
Accessing MCP Context
New in version2.2.5
Prompts can access additional MCP information and features through the Context object. To access it, add a parameter to your prompt function with a type annotation of Context:
Notifications
New in version2.9.1
FastMCP automatically sends notifications/prompts/list_changed notifications to connected clients when prompts are added, enabled, or disabled. This allows clients to stay up-to-date with the current prompt set without manually polling for changes.
Server Behavior
Duplicate Prompts
New in version2.1.0
You can configure how the FastMCP server handles attempts to register multiple prompts with the same name. Use the on_duplicate_prompts setting during FastMCP initialization.
"warn"(default): Logs a warning, and the new prompt replaces the old one."error": Raises aValueError, preventing the duplicate registration."replace": Silently replaces the existing prompt with the new one."ignore": Keeps the original prompt and ignores the new registration attempt.
Versioning
New in version3.0.0
Prompts support versioning, allowing you to maintain multiple implementations under the same name while clients automatically receive the highest version. See Versioning for complete documentation on version comparison, retrieval, and migration patterns.
