diff --git a/README.md b/README.md index e9ce712ef..f8ce41cc0 100644 --- a/README.md +++ b/README.md @@ -176,7 +176,7 @@ Learn more in the [**Resources & Templates Documentation**](https://gofastmcp.co Prompts define reusable message templates to guide LLM interactions. Decorate functions with `@mcp.prompt`. Return strings or `Message` objects. ```python -@mcp.prompt() +@mcp.prompt def summarize_request(text: str) -> str: """Generate a prompt asking for a summary.""" return f"Please summarize the following text:\n\n{text}" diff --git a/docs/patterns/decorating-methods.mdx b/docs/patterns/decorating-methods.mdx index 3b2c22dd6..4e8ad8489 100644 --- a/docs/patterns/decorating-methods.mdx +++ b/docs/patterns/decorating-methods.mdx @@ -5,7 +5,7 @@ description: Properly use instance methods, class methods, and static methods wi icon: at --- -FastMCP's decorator system is designed to work with functions, but you may see unexpected behavior if you try to decorate an instance or class method. This guide explains the correct approach for using methods with all FastMCP decorators (`@tool`, `@resource()`, and `@prompt()`). +FastMCP's decorator system is designed to work with functions, but you may see unexpected behavior if you try to decorate an instance or class method. This guide explains the correct approach for using methods with all FastMCP decorators (`@tool`, `@resource`, and `.prompt`). ## Why Are Methods Hard? diff --git a/docs/servers/context.mdx b/docs/servers/context.mdx index 2818de3c0..0f075d264 100644 --- a/docs/servers/context.mdx +++ b/docs/servers/context.mdx @@ -71,7 +71,7 @@ async def get_user_profile(user_id: str, ctx: Context) -> dict: ```python -@mcp.prompt() +@mcp.prompt async def data_analysis_request(dataset: str, ctx: Context) -> str: """Generate a request to analyze data with contextual information.""" # Context is available as the ctx parameter diff --git a/docs/servers/fastmcp.mdx b/docs/servers/fastmcp.mdx index 40808fb95..13279525d 100644 --- a/docs/servers/fastmcp.mdx +++ b/docs/servers/fastmcp.mdx @@ -87,7 +87,7 @@ See [Resources & Templates](/servers/resources) for detailed documentation. Prompts are reusable message templates for guiding the LLM. ```python -@mcp.prompt() +@mcp.prompt def analyze_data(data_points: list[float]) -> str: """Creates a prompt asking for analysis of numerical data.""" formatted_data = ", ".join(str(point) for point in data_points) diff --git a/docs/servers/prompts.mdx b/docs/servers/prompts.mdx index 6a8f0a62a..47f19ff9e 100644 --- a/docs/servers/prompts.mdx +++ b/docs/servers/prompts.mdx @@ -33,13 +33,13 @@ from fastmcp.prompts.prompt import Message, PromptMessage, TextContent mcp = FastMCP(name="PromptServer") # Basic prompt returning a string (converted to user message automatically) -@mcp.prompt() +@mcp.prompt def ask_about_topic(topic: str) -> str: """Generates a user message asking for an explanation of a topic.""" return f"Can you please explain the concept of '{topic}'?" # Prompt returning a specific message type -@mcp.prompt() +@mcp.prompt def generate_code_request(language: str, task_description: str) -> PromptMessage: """Generates a user message requesting code generation.""" content = f"Write a {language} function that performs the following task: {task_description}" @@ -69,7 +69,7 @@ FastMCP intelligently handles different return types from your prompt function: ```python from fastmcp.prompts.prompt import Message -@mcp.prompt() +@mcp.prompt def roleplay_scenario(character: str, situation: str) -> list[Message]: """Sets up a roleplaying scenario with initial messages.""" return [ @@ -89,7 +89,7 @@ Type annotations are important for prompts. They: from pydantic import Field from typing import Literal, Optional -@mcp.prompt() +@mcp.prompt def generate_content_request( topic: str = Field(description="The main subject to cover"), format: Literal["blog", "email", "social"] = "blog", @@ -111,7 +111,7 @@ def generate_content_request( Parameters in your function signature are considered **required** unless they have a default value. ```python -@mcp.prompt() +@mcp.prompt def data_analysis_prompt( data_uri: str, # Required - no default value analysis_type: str = "summary", # Optional - has default value @@ -154,13 +154,13 @@ FastMCP seamlessly supports both standard (`def`) and asynchronous (`async def`) ```python # Synchronous prompt -@mcp.prompt() +@mcp.prompt def simple_question(question: str) -> str: """Generates a simple question to ask the LLM.""" return f"Question: {question}" # Asynchronous prompt -@mcp.prompt() +@mcp.prompt async def data_based_prompt(data_id: str) -> str: """Generates a prompt based on data that needs to be fetched.""" # In a real scenario, you might fetch data from a database or API @@ -183,7 +183,7 @@ from fastmcp import FastMCP, Context mcp = FastMCP(name="PromptServer") -@mcp.prompt() +@mcp.prompt async def generate_report_request(report_type: str, ctx: Context) -> str: """Generates a request for a report.""" return f"Please create a {report_type} report. Request ID: {ctx.request_id}" @@ -207,7 +207,7 @@ mcp = FastMCP( on_duplicate_prompts="error" # Raise an error if a prompt name is duplicated ) -@mcp.prompt() +@mcp.prompt def greeting(): return "Hello, how can I help you today?" # This registration attempt will raise a ValueError because diff --git a/tests/server/test_server_interactions.py b/tests/server/test_server_interactions.py index 921902819..9d6df95bc 100644 --- a/tests/server/test_server_interactions.py +++ b/tests/server/test_server_interactions.py @@ -1086,7 +1086,7 @@ class TestPrompts: async def test_prompt_decorator_with_parens(self): mcp = FastMCP() - @mcp.prompt() + @mcp.prompt def fn() -> str: return "Hello, world!"