Sub topic 8
Prompts: reusable templates the user selects
This chapter assumes you have read the chapters on the JSON-RPC wire format, session initialization, and tools. You should be able to read a simple MCP server written with the Python SDK.
What a prompt is
A prompt in MCP is a reusable message template that a server publishes. The user picks it, usually from a menu or a slash command in the host. The host then asks the server to fill in the template and sends the resulting messages to the model.
Control is the main difference between the three server features:
| Feature | Who decides to use it | Typical use |
|---|---|---|
| Tool | The model | Take an action or compute a result |
| Resource | The application | Supply context to read |
| Prompt | The user | Start a task from a vetted template |
Use a prompt when you want to give users a well-written starting point, such as "review this code" or "summarize this document", without making them type the instructions each time.
Declaring the capability
A server that offers prompts announces the prompts capability during initialization. It can add listChanged: true to say it will send a notification when its prompt list changes. A client should only call the prompt methods if the server declared this capability.
The two requests
Two methods cover prompts.
prompts/listreturns each prompt's name, description, and arguments.prompts/gettakes a prompt name and argument values, and returns the finished messages.
The list reply describes the arguments so the host can build a form for the user:
{"jsonrpc": "2.0", "id": 3, "result": {"prompts": [{"name": "review_code", "description": "Ask the model to review a piece of code.", "arguments": [{"name": "code", "required": true}]}]}}The get request supplies the values. Argument values are always strings.
{"jsonrpc": "2.0", "id": 4, "method": "prompts/get", "params": {"name": "review_code", "arguments": {"code": "print(1 + 1)"}}}The server replies with a list of messages. Each message has a role of user or assistant and a content item:
{"jsonrpc": "2.0", "id": 4, "result": {"description": "Ask the model to review a piece of code.", "messages": [{"role": "user", "content": {"type": "text", "text": "Please review this code:\n\nprint(1 + 1)"}}]}}The server does not call the model. It only builds the messages. The host decides what to do with them, normally adding them to the conversation.
Defining a prompt with the Python SDK
In FastMCP, the @mcp.prompt() decorator registers a function. The function name becomes the prompt name, the docstring becomes the description, and the parameters become the arguments.
from mcp.server.fastmcp import FastMCP
mcp = FastMCP("demo")
@mcp.prompt()
def review_code(code: str) -> str:
"""Ask the model to review a piece of code."""
return f"Please review this code:\n\n{code}"
if __name__ == "__main__":
mcp.run(transport="stdio")Returning a string produces one user message. For a multi-turn template, return a list of messages. The SDK provides UserMessage and AssistantMessage in mcp.server.fastmcp.prompts.base.
Common errors
- Treating prompts like tools. The model does not choose prompts. If you want the model to trigger something on its own, write a tool.
- Expecting typed arguments. Over the wire, every argument is a string. Convert numbers or booleans inside your function.
- Missing required arguments. If the client omits a required argument, the server returns a JSON-RPC error. Check the
requiredflag inprompts/listbefore calling. - Unvalidated input in templates. Argument text goes straight into the message. Treat it as untrusted, especially if it came from a file or a web page.
- Calling without the capability. If the server did not declare
prompts,prompts/listfails with a method-not-found error.
Exercise
A server author wants the model to run a "summarize this document" workflow only when the user asks for it by name from a menu. Should this be a tool or a prompt, and which request returns the filled-in messages?
Show answer
It should be a prompt, because prompts are chosen by the user, while tools are chosen by the model. The prompts/get request returns the filled-in messages.
Exercise
A prompt defines an argument count, and the client sends {"count": 5} as a number. What does the protocol say about argument types, and how should your function handle it?
Show answer
Prompt argument values are strings, so the client should send "5". Inside the function, convert the value with int(count) and handle values that cannot be converted.