mirror of
https://github.com/PrefectHQ/fastmcp.git
synced 2026-08-27 15:51:33 +02:00
Co-authored-by: marvin-context-protocol[bot] <225465937+marvin-context-protocol[bot]@users.noreply.github.com>
201 lines
7.2 KiB
Text
201 lines
7.2 KiB
Text
---
|
|
title: prompt
|
|
sidebarTitle: prompt
|
|
---
|
|
|
|
# `fastmcp.prompts.prompt`
|
|
|
|
|
|
Base classes for FastMCP prompts.
|
|
|
|
## Functions
|
|
|
|
### `Message` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/prompts/prompt.py#L38" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
Message(content: str | ContentBlock, role: Role | None = None, **kwargs: Any) -> PromptMessage
|
|
```
|
|
|
|
|
|
A user-friendly constructor for PromptMessage.
|
|
|
|
|
|
## Classes
|
|
|
|
### `PromptArgument` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/prompts/prompt.py#L61" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
|
|
An argument that can be passed to a prompt.
|
|
|
|
|
|
### `PromptResult` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/prompts/prompt.py#L73" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
|
|
Canonical result type for prompt rendering.
|
|
|
|
This is the internal type that all prompt renders return. It wraps the
|
|
messages with optional description and metadata.
|
|
|
|
|
|
**Methods:**
|
|
|
|
#### `from_value` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/prompts/prompt.py#L89" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
from_value(cls, value: list[PromptMessage] | PromptResult, description: str | None = None, meta: dict[str, Any] | None = None) -> PromptResult
|
|
```
|
|
|
|
Convert various types to PromptResult.
|
|
|
|
|
|
#### `to_mcp_prompt_result` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/prompts/prompt.py#L109" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
to_mcp_prompt_result(self) -> GetPromptResult
|
|
```
|
|
|
|
Convert to MCP GetPromptResult.
|
|
|
|
|
|
### `Prompt` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/prompts/prompt.py#L118" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
|
|
A prompt template that can be rendered with parameters.
|
|
|
|
|
|
**Methods:**
|
|
|
|
#### `to_mcp_prompt` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/prompts/prompt.py#L127" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
to_mcp_prompt(self, **overrides: Any) -> SDKPrompt
|
|
```
|
|
|
|
Convert the prompt to an MCP prompt.
|
|
|
|
|
|
#### `from_function` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/prompts/prompt.py#L155" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
from_function(fn: Callable[..., _PromptFnReturn | Awaitable[_PromptFnReturn]], 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:
|
|
- A string (converted to a message)
|
|
- A Message object
|
|
- A dict (converted to a message)
|
|
- A sequence of any of the above
|
|
|
|
|
|
#### `render` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/prompts/prompt.py#L184" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
render(self, arguments: dict[str, Any] | None = None) -> list[PromptMessage] | PromptResult
|
|
```
|
|
|
|
Render the prompt with arguments.
|
|
|
|
This method is not implemented in the base Prompt class and must be
|
|
implemented by subclasses. The preferred return type is PromptResult,
|
|
but list\[PromptMessage] is still supported for backwards compatibility.
|
|
|
|
|
|
#### `convert_result` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/prompts/prompt.py#L196" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
convert_result(self, raw_value: Any) -> PromptResult
|
|
```
|
|
|
|
Convert a raw return value to PromptResult.
|
|
|
|
Handles PromptResult passthrough and converts raw values to messages.
|
|
|
|
|
|
#### `register_with_docket` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/prompts/prompt.py#L279" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
register_with_docket(self, docket: Docket) -> None
|
|
```
|
|
|
|
Register this prompt with docket for background execution.
|
|
|
|
|
|
#### `add_to_docket` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/prompts/prompt.py#L285" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
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` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/prompts/prompt.py#L309" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
|
|
A prompt that is a function.
|
|
|
|
|
|
**Methods:**
|
|
|
|
#### `from_function` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/prompts/prompt.py#L315" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
from_function(cls, fn: Callable[..., _PromptFnReturn | Awaitable[_PromptFnReturn]], 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:
|
|
- A string (converted to a message)
|
|
- A Message object
|
|
- A dict (converted to a message)
|
|
- A sequence of any of the above
|
|
|
|
|
|
#### `render` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/prompts/prompt.py#L467" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
render(self, arguments: dict[str, Any] | None = None) -> PromptResult
|
|
```
|
|
|
|
Render the prompt with arguments.
|
|
|
|
|
|
#### `register_with_docket` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/prompts/prompt.py#L498" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
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` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/prompts/prompt.py#L508" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
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()
|
|
|