Skip to main content
Prompts in MCP provide a way to create reusable, parameterized templates for AI model interactions. They enable standardized prompt generation with dynamic content based on user-provided arguments.

Prompt Structure

Every prompt has three main components:
server.prompt({
  name: 'prompt_name',              // Unique identifier
  description: 'What it generates',  // Clear description
  schema: z.object({...}),                 // Argument definitions
}, async (args) => {...}) // generator function

Input Validation with Zod

Prompts use Zod schemas for input validation. The server automatically validates inputs before calling your handler, so you can trust that the parameters match your schema.
import { z } from 'zod';

server.prompt({
  name: 'prompt_name',
  description: 'What it generates',
  schema: z.object({
    topic: z.enum(['math', 'science', 'history']),
    language: z.string().default('english'),
    ok: z.boolean()
  }),
}, async ({ topic }) => {
  return {
    messages: [
      {
        role: 'system',
        content: `You are a helpful assistant that can answer questions about ${topic}.`
      }
    ]
  }
})

Notifying Clients of Prompt Changes

When dynamically adding or removing prompts, notify clients to refresh their prompts cache:
// Register a new prompt dynamically
server.prompt({
  name: 'new_prompt',
  description: 'A dynamically added prompt',
  schema: z.object({ topic: z.string() })
}, async ({ topic }) => ({
  messages: [{ role: 'system', content: `Expert in ${topic}` }]
}));

// Notify all connected clients
await server.sendPromptsListChanged();
See Notifications for more details.

Next Steps