Skip to main content

fastmcp.prompts.prompt

Base classes for FastMCP prompts.

Functions

prompt

prompt(name_or_fn: str | AnyFunction | None = None) -> Callable[[AnyFunction], FunctionPrompt] | FunctionPrompt | partial[Callable[[AnyFunction], FunctionPrompt] | FunctionPrompt]
Standalone decorator to create a prompt without registering it to a server. This decorator creates a FunctionPrompt object from a function. Unlike @server.prompt(), this does NOT register the prompt with any server - you must explicitly add it using server.add_prompt(). This is useful for:
  • Creating prompts that will be modified before registration
  • Defining prompts in modules that are discovered by FileSystemProvider
  • Creating reusable prompt definitions
This decorator supports multiple calling patterns:
  • @prompt (without parentheses)
  • @prompt() (with empty parentheses)
  • @prompt(“custom_name”) (with name as first argument)
  • @prompt(name=“custom_name”) (with name as keyword argument)
Args:
  • name_or_fn: Either a function (when used as @prompt), a string name, or None
  • name: Optional name for the prompt (keyword-only, alternative to name_or_fn)
  • title: Optional title for the prompt
  • description: Optional description of what the prompt does
  • icons: Optional icons for the prompt
  • tags: Optional set of tags for categorizing the prompt
  • meta: Optional meta information about the prompt
  • task: Optional task configuration for background execution (default False)
Returns:
  • A FunctionPrompt when decorating a function, or a decorator function when
  • called with parameters.

Classes

Message

Wrapper for prompt message with auto-serialization. Accepts any content - strings pass through, other types (dict, list, BaseModel) are JSON-serialized to text. Methods:

to_mcp_prompt_message

to_mcp_prompt_message(self) -> PromptMessage
Convert to MCP PromptMessage.

PromptArgument

An argument that can be passed to a prompt.

PromptResult

Canonical result type for prompt rendering. Provides explicit control over prompt responses: multiple messages, roles, and metadata at both the message and result level. Methods:

to_mcp_prompt_result

to_mcp_prompt_result(self) -> GetPromptResult
Convert to MCP GetPromptResult.

Prompt

A prompt template that can be rendered with parameters. Methods:

to_mcp_prompt

to_mcp_prompt(self, **overrides: Any) -> SDKPrompt
Convert the prompt to an MCP prompt.

from_function

from_function(fn: Callable[..., Any], name: str | None = None, title: str | None = None, description: str | None = None, icons: list[Icon] | None = None, tags: set[str] | None = None, meta: dict[str, Any] | None = None, task: bool | TaskConfig | None = None) -> FunctionPrompt
Create a Prompt from a function. The function can return:
  • str: wrapped as single user Message
  • list[Message | str]: converted to list[Message]
  • PromptResult: used directly

render

render(self, arguments: dict[str, Any] | None = None) -> str | list[Message | str] | PromptResult
Render the prompt with arguments. Subclasses must implement this method. Return one of:
  • str: Wrapped as single user Message
  • list[Message | str]: Converted to list[Message]
  • PromptResult: Used directly

convert_result

convert_result(self, raw_value: Any) -> PromptResult
Convert a raw return value to PromptResult. Raises:
  • TypeError: for unsupported types

register_with_docket

register_with_docket(self, docket: Docket) -> None
Register this prompt with docket for background execution.

add_to_docket

add_to_docket(self, docket: Docket, arguments: dict[str, Any] | None, **kwargs: Any) -> Execution
Schedule this prompt for background execution via docket. Args:
  • docket: The Docket instance
  • arguments: Prompt arguments
  • fn_key: Function lookup key in Docket registry (defaults to self.key)
  • task_key: Redis storage key for the result
  • **kwargs: Additional kwargs passed to docket.add()

FunctionPrompt

A prompt that is a function. Methods:

from_function

from_function(cls, fn: Callable[..., Any], name: str | None = None, title: str | None = None, description: str | None = None, icons: list[Icon] | None = None, tags: set[str] | None = None, meta: dict[str, Any] | None = None, task: bool | TaskConfig | None = None) -> FunctionPrompt
Create a Prompt from a function. The function can return:
  • str: wrapped as single user Message
  • list[Message | str]: converted to list[Message]
  • PromptResult: used directly

render

render(self, arguments: dict[str, Any] | None = None) -> PromptResult
Render the prompt with arguments.

register_with_docket

register_with_docket(self, docket: Docket) -> None
Register this prompt with docket for background execution. FunctionPrompt registers the underlying function, which has the user’s Depends parameters for docket to resolve.

add_to_docket

add_to_docket(self, docket: Docket, arguments: dict[str, Any] | None, **kwargs: Any) -> Execution
Schedule this prompt for background execution via docket. FunctionPrompt splats the arguments dict since .fn expects **kwargs. Args:
  • docket: The Docket instance
  • arguments: Prompt arguments
  • fn_key: Function lookup key in Docket registry (defaults to self.key)
  • task_key: Redis storage key for the result
  • **kwargs: Additional kwargs passed to docket.add()