Jeremiah Lowin Claude commited on
Commit
5c9c10c
·
1 Parent(s): 50745e5

Update docs

Browse files

Co-Authored-By: Claude <noreply@anthropic.com>

Files changed (1) hide show
  1. docs/servers/prompts.mdx +76 -27
docs/servers/prompts.mdx CHANGED
@@ -57,6 +57,82 @@ def generate_code_request(language: str, task_description: str) -> PromptMessage
57
  Functions with `*args` or `**kwargs` are not supported as prompts. This restriction exists because FastMCP needs to generate a complete parameter schema for the MCP protocol, which isn't possible with variable argument lists.
58
  </Tip>
59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
  ### Return Values
61
 
62
  FastMCP intelligently handles different return types from your prompt function:
@@ -78,33 +154,6 @@ def roleplay_scenario(character: str, situation: str) -> list[Message]:
78
  ]
79
  ```
80
 
81
- ### Type Annotations
82
-
83
- Type annotations are important for prompts. They:
84
- 1. Inform FastMCP about the expected types for each parameter.
85
- 2. Allow validation of parameters received from clients.
86
- 3. Are used to generate the prompt's schema for the MCP protocol.
87
-
88
- ```python
89
- from pydantic import Field
90
- from typing import Literal, Optional
91
-
92
- @mcp.prompt
93
- def generate_content_request(
94
- topic: str = Field(description="The main subject to cover"),
95
- format: Literal["blog", "email", "social"] = "blog",
96
- tone: str = "professional",
97
- word_count: Optional[int] = None
98
- ) -> str:
99
- """Create a request for generating content in a specific format."""
100
- prompt = f"Please write a {format} post about {topic} in a {tone} tone."
101
-
102
- if word_count:
103
- prompt += f" It should be approximately {word_count} words long."
104
-
105
- return prompt
106
- ```
107
-
108
 
109
  ### Required vs. Optional Parameters
110
 
 
57
  Functions with `*args` or `**kwargs` are not supported as prompts. This restriction exists because FastMCP needs to generate a complete parameter schema for the MCP protocol, which isn't possible with variable argument lists.
58
  </Tip>
59
 
60
+ ### Argument Types
61
+
62
+ 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:
63
+
64
+ 1. **Automatically converts** string arguments from MCP clients to the expected types
65
+ 2. **Generates helpful descriptions** showing the exact JSON string format needed
66
+ 3. **Preserves direct usage** - you can still call prompts with properly typed arguments
67
+
68
+ Since the MCP specification only allows string arguments, clients need to know what string format to use for complex types. FastMCP solves this by automatically enhancing the argument descriptions with JSON schema information, making it clear to both humans and LLMs how to format their arguments.
69
+
70
+ <CodeGroup>
71
+
72
+ ```python Python Code
73
+ @mcp.prompt
74
+ def analyze_data(
75
+ numbers: list[int],
76
+ metadata: dict[str, str],
77
+ threshold: float
78
+ ) -> str:
79
+ """Analyze numerical data."""
80
+ avg = sum(numbers) / len(numbers)
81
+ return f"Average: {avg}, above threshold: {avg > threshold}"
82
+ ```
83
+
84
+ ```json Resulting MCP Prompt
85
+ {
86
+ "name": "analyze_data",
87
+ "description": "Analyze numerical data.",
88
+ "arguments": [
89
+ {
90
+ "name": "numbers",
91
+ "description": "Provide as a JSON string matching the following schema: {\"items\":{\"type\":\"integer\"},\"type\":\"array\"}",
92
+ "required": true
93
+ },
94
+ {
95
+ "name": "metadata",
96
+ "description": "Provide as a JSON string matching the following schema: {\"additionalProperties\":{\"type\":\"string\"},\"type\":\"object\"}",
97
+ "required": true
98
+ },
99
+ {
100
+ "name": "threshold",
101
+ "description": "Provide as a JSON string matching the following schema: {\"type\":\"number\"}",
102
+ "required": true
103
+ }
104
+ ]
105
+ }
106
+ ```
107
+
108
+ </CodeGroup>
109
+
110
+ **MCP clients will call this prompt with string arguments:**
111
+ ```json
112
+ {
113
+ "numbers": "[1, 2, 3, 4, 5]",
114
+ "metadata": "{\"source\": \"api\", \"version\": \"1.0\"}",
115
+ "threshold": "2.5"
116
+ }
117
+ ```
118
+
119
+ **But you can still call it directly with proper types:**
120
+ ```python
121
+ # This also works for direct calls
122
+ result = await prompt.render({
123
+ "numbers": [1, 2, 3, 4, 5],
124
+ "metadata": {"source": "api", "version": "1.0"},
125
+ "threshold": 2.5
126
+ })
127
+ ```
128
+
129
+ <Warning>
130
+ Keep your type annotations simple when using this feature. Complex nested types or custom classes may not convert reliably from JSON strings. The automatically generated schema descriptions are the only guidance users receive about the expected format.
131
+
132
+ Good choices: `list[int]`, `dict[str, str]`, `float`, `bool`
133
+ Avoid: Complex Pydantic models, deeply nested structures, custom classes
134
+ </Warning>
135
+
136
  ### Return Values
137
 
138
  FastMCP intelligently handles different return types from your prompt function:
 
154
  ]
155
  ```
156
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
157
 
158
  ### Required vs. Optional Parameters
159