Prompts
🎯 Introduction
Suppose your operations team regularly uses AI to write product introductions. Only the product ID and writing style change; the other requirements stay much the same. Maintaining the same template separately in several AI applications makes updates cumbersome.
MCP Prompts let a server provide prompt templates centrally. Applications that support this capability can discover templates, supply arguments, and retrieve the resulting messages. In this article, you will register a product introduction template, define its arguments, and retrieve its messages through an MCP client.
🧱 What Prompts Provide
Suppose we provide a template named product-introduction that takes two arguments:
| Argument | Example |
|---|---|
| Product ID | P100 |
| Writing style | concise |
The server looks up the product by ID and combines its details with the writing requirements:
Write a concise introduction to Mechanical Keyboard. Its price is CNY 199, and it has 12 units in stock. Do not add information that is not provided.
Changing the style to “detailed” changes the generated prompt accordingly. The host can send these messages to the model to generate a product introduction.
An ordinary JavaScript template literal can also combine text. MCP Prompts standardize how templates are exposed: different AI applications can discover the same template, learn which arguments it takes, and retrieve its generated messages without implementing the template logic themselves.
Retrieving template messages does not automatically call a model.
🧱 Register a Prompt Template
To focus on prompt templates, this example uses two product records directly. Add the following outside createServer() in the existing 02.first-server.mjs:
const products = [
{ id: 'P100', name: 'Mechanical Keyboard', price: 199, stock: 12 },
{ id: 'P200', name: 'Wireless Mouse', price: 89, stock: 0 },
];
Define the Template
The SDK provides registerPrompt(). Registering a template requires a name, a configuration object, and a callback that generates messages. The configuration's argsSchema defines the arguments the template accepts.
Add the following inside createServer(), before return server;:
server.registerPrompt(
'product-introduction',
{
description: 'Generate a prompt for a product introduction from product details',
argsSchema: z.object({
id: z.enum(['P100', 'P200']).describe('Product ID'),
style: z.enum(['concise', 'detailed']).describe('Writing style'),
}),
},
({ id, style }) => {
const product = products.find(item => item.id === id);
return {
messages: [
{
role: 'user',
content: {
type: 'text',
text: `Write a ${style} introduction to ${product.name}. Its price is CNY ${product.price}, and it has ${product.stock} units in stock. Do not add information that is not provided.`,
},
},
],
};
}
);
Here, z.enum() restricts the allowed product IDs and writing styles. The SDK exposes argument information based on argsSchema and validates incoming arguments before running the callback.
The template callback must return messages, rather than a prompt string directly:
| Field | Purpose |
|---|---|
messages | The list of generated messages; it can contain multiple messages |
role | The message role, either user or assistant; this example uses user for the writing request |
content | The message content; this example supplies text through type: "text" and text |
A message list preserves message roles and order, making it easier for the host to convert them to the model API's input format.
🔍 Retrieve Prompt Templates
List Templates
How does an AI application find out which templates a server provides and which arguments they need? Use listPrompts() to retrieve template information.
Restart the MCP server. In the existing 02.first-client.mjs, replace the resource-reading and logging code after await client.connect(transport) inside try with:
const result = await client.listPrompts();
console.log(JSON.stringify(result, null, 4));
Run the client:
node 02.first-client.mjs
The key fields in the expected output are:
{
"prompts": [
{
"name": "product-introduction",
"description": "Generate a prompt for a product introduction from product details",
"arguments": [
{
"name": "id",
"description": "Product ID",
"required": true
},
{
"name": "style",
"description": "Writing style",
"required": true
}
]
}
]
}
arguments lists argument names, descriptions, and whether each argument is required. An AI application can use this information to display templates and input fields. This list contains template metadata, not messages with arguments filled in, and it is not guaranteed to include the complete Zod validation rules.
Retrieve Template Messages
After selecting a template and supplying its arguments, use getPrompt() to retrieve the generated messages.
Replace the two lines that call listPrompts() and print its result in 02.first-client.mjs with:
const result = await client.getPrompt({
name: 'product-introduction',
arguments: {
id: 'P100',
style: 'concise',
},
});
console.log(JSON.stringify(result, null, 4));
name identifies the template, and arguments supplies its arguments. The server validates the arguments, runs the registered callback, and returns the generated messages.
Run it again:
node 02.first-client.mjs
The key fields in the expected output are:
{
"messages": [
{
"role": "user",
"content": {
"type": "text",
"text": "Write a concise introduction to Mechanical Keyboard. Its price is CNY 199, and it has 12 units in stock. Do not add information that is not provided."
}
}
]
}
In an AI application, the host can include these messages in a model request to generate a product introduction. This article only tests MCP template generation, so no model call is needed.
Handle Invalid Arguments
Change style in arguments to "other" and run the client again. Because this value is not allowed by argsSchema, getPrompt() throws a protocol error without running the template callback. The error should indicate that style is invalid; its exact wording depends on the SDK.
Prompt argument validation and tool argument validation handle failures differently: the former causes getPrompt() to throw, whereas the latter can report failure through isError in a tool result. Restore style to "concise" after testing.
References: MCP SDK Prompts documentation, client calling documentation.
❓ Knowledge Check
Q1: Is an MCP prompt the same as a model's system prompt?
No. An MCP prompt is a message template provided by a server. The host decides how to use it. It does not automatically replace the application's system instructions or gain higher instruction priority simply because it comes from an MCP server.
Q2: Do you need to change client code after updating a server's prompt template?
Usually not, as long as the template name and arguments remain compatible. Once the updated server is running, the client receives the new message content the next time it retrieves the template.
🧪 Exercise
Add a prompt template named product-promotion inside createServer() in 02.first-server.mjs. It should accept a product ID id and promotion theme theme, then generate a message containing the product name, price, and promotional requirements.
Restart the server, use listPrompts() to confirm that both templates are registered, and retrieve the new template's messages with getPrompt(). Check that the product details and promotion theme are correct.
🎉 You can now provide and retrieve reusable prompt templates through MCP.
