Sub topic 8
Prompts: reusable templates the user selects
This chapter assumes you know the host, client, and server roles, the JSON-RPC message shapes, and the initialization handshake from the earlier chapters. It also assumes you have seen how tools work. Here you will learn what a prompt is, how a client discovers and fetches one, and how to write one with the Python SDK.
What a prompt is
A prompt is a named message template that a server publishes. The template can take arguments. When the user picks a prompt, the client sends the arguments to the server, and the server returns a list of ready-to-use messages. The host then places those messages into the conversation.
Prompts are user-controlled. The server offers them, and the user chooses when to use one. Hosts usually show them as slash commands, menu items, or buttons. The model does not decide to run a prompt on its own, and this is the main difference from a tool.
| Primitive | 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 conversation from a known template |
A prompt is useful when a task needs careful wording that you want to reuse. Examples are a code review checklist, a bug report summary format, and a "explain this error" workflow. The server author writes the instructions once, and every user and host gets the same result.
Declaring the capability
A server that offers prompts declares the prompts capability in its initialize response. The optional listChanged flag says whether the server will notify the client when its list of prompts changes.
{
"jsonrpc": "2.0",
"id": 1,
"result": {
"protocolVersion": "2025-06-18",
"capabilities": {
"prompts": { "listChanged": true }
},
"serverInfo": { "name": "demo", "version": "1.0.0" }
}
}A client must not call prompts/list or prompts/get if the server did not declare this capability. Check the negotiated capabilities first.
Discovering prompts with prompts/list
The client asks for the available prompts with prompts/list. The result describes each prompt: its name, an optional human-readable title and description, and its arguments.
{"jsonrpc": "2.0", "id": 2, "method": "prompts/list", "params": {}}{
"jsonrpc": "2.0",
"id": 2,
"result": {
"prompts": [
{
"name": "review_code",
"description": "Ask the model to review a piece of code.",
"arguments": [
{ "name": "code", "description": "The code to review", "required": true }
]
}
]
}
}Each argument has a name, an optional description, and a required flag. The host uses this metadata to build a form or to decide what to ask the user for. Prompt arguments are always string values, so a server that wants a number or a list has to parse it.
The list can be paginated. If the result includes a nextCursor, the client sends another prompts/list request with params.cursor set to that value. It repeats until no cursor comes back.
Fetching a prompt with prompts/get
When the user selects a prompt, the client sends prompts/get with the prompt name and the argument values.
{
"jsonrpc": "2.0",
"id": 3,
"method": "prompts/get",
"params": {
"name": "review_code",
"arguments": { "code": "def add(a, b): return a - b" }
}
}The server fills in the template and replies with a messages array.
{
"jsonrpc": "2.0",
"id": 3,
"result": {
"description": "Ask the model to review a piece of code.",
"messages": [
{
"role": "user",
"content": {
"type": "text",
"text": "Please review this code:\n\ndef add(a, b): return a - b"
}
}
]
}
}Each message has a role, either user or assistant, and a content object. Returning several messages lets a prompt set up a short exchange. For example, it can include an example answer as an assistant message so the model follows a particular format.
The content object can be one of these types:
text: plain text, as above.imageandaudio: base64-encoded data with a MIME type.resource: an embedded resource, which carries auriand eithertextor a base64blob. This lets a prompt pull server-held documents into the conversation without a second request.
The sequence below shows the whole flow, including where the model enters.
Writing prompts with FastMCP
The official Python SDK builds the prompts/list entry from your function's signature. Parameters without defaults become required arguments, and the docstring becomes the description. This server has the review_code prompt from the chapter plan, plus one that returns several messages.
from mcp.server.fastmcp import FastMCP
from mcp.server.fastmcp.prompts import base
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}"
@mcp.prompt()
def debug_error(error: str, language: str = "Python") -> list[base.Message]:
"""Start a debugging conversation about an error message."""
return [
base.UserMessage(f"I'm getting this {language} error:\n\n{error}"),
base.AssistantMessage("I'll start by asking what you were doing when it happened."),
]
if __name__ == "__main__":
mcp.run(transport="stdio")Returning a plain string produces one user message. Returning a list of UserMessage and AssistantMessage objects produces a multi-turn template. In debug_error, error is required and language is optional because it has a default value.
Calling prompts from a client
The client side mirrors the two requests. The SDK exposes them as list_prompts and get_prompt.
import asyncio
from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client
async def main():
params = StdioServerParameters(command="python", args=["server.py"])
async with stdio_client(params) as (read, write):
async with ClientSession(read, write) as session:
await session.initialize()
listing = await session.list_prompts()
print([p.name for p in listing.prompts])
result = await session.get_prompt(
"review_code", {"code": "def add(a, b): return a - b"}
)
for message in result.messages:
print(message.role, message.content.text)
asyncio.run(main())Note that get_prompt does not call the model. It only returns messages. Sending them to a model is the host's job.
Argument completion and list changes
Two related features help with usability.
Completion. If the server declares the completions capability, the client can send completion/complete while the user types an argument. The request refers to the prompt with "ref": {"type": "ref/prompt", "name": "debug_error"} and gives the partial value. The server returns suggested values, so the host can show an autocomplete list.
List changes. If a server declared listChanged, it sends a notifications/prompts/list_changed notification when it adds, removes, or edits prompts. This notification has no id and expects no reply. The client should call prompts/list again to refresh its menu.
Errors and edge cases
- An unknown prompt name or missing required argument returns a JSON-RPC error, normally code
-32602(invalid params). Hosts should show the message instead of failing silently. - Argument values come from the user and are untrusted text. If you build a prompt by pasting a value into a template, that value can contain instructions aimed at the model. Do not treat prompt arguments as trusted, and do not place secrets in templates.
- Prompt messages are not a security boundary. A prompt that embeds a resource can pull in sensitive content, so the host should let the user see what will be sent before it goes to the model.
- Keep prompts stable. Users build habits around names like
review_code, so renaming a prompt breaks their workflow. Add a new name instead. - Do not use a prompt where a tool is the right fit. If the model needs to trigger something by itself mid-conversation, expose a tool. If the user starts a task from a known template, expose a prompt.
Exercise
Exercise
A server offers a prompt named summarizeticket with one required argument, ticketid. Describe, in order, the JSON-RPC methods a client sends when a user picks this prompt, and say who is responsible for sending the resulting messages to the model.
Show answer
First the client checks that the server declared the prompts capability during initialization. It may have already called prompts/list to show the prompt in its menu. When the user picks summarizeticket and supplies a ticketid, the client sends prompts/get with params name "summarizeticket" and arguments {"ticketid": "..."}. The server replies with a messages array. The host, not the server, adds those messages to the conversation and sends them to the model.
Exercise
Why would a "fix this bug" workflow be a poor fit for a prompt if you want the model to start it automatically when it notices a failing test?
Show answer
Prompts are user-controlled. The model cannot invoke them, and a host presents them for the user to select. A workflow the model should trigger on its own belongs in a tool, which is model-controlled.