Create reusable prompt templates and workflowsPrompts enable servers to define reusable prompt templates and workflows that clients can easily surface to users and LLMs. They provide a powerful way to standardize and share common LLM interactions.Prompts are designed to be user-controlled, meaning they are exposed from servers to clients with the intention of the user being able to explicitly select them for use.Prompts in MCP are predefined templates that can:Include context from resources
Chain multiple interactions
Surface as UI elements (like slash commands)
Each prompt is defined with:Clients can discover available prompts through the prompts/list
endpoint:To use a prompt, clients make a prompts/get
request:Prompts can be dynamic and include:{
"name": "analyze-project",
"description": "Analyze project logs and code",
"arguments": [
{
"name": "timeframe",
"description": "Time period to analyze logs",
"required": true
},
{
"name": "fileUri",
"description": "URI of code file to review",
"required": true
}
]
}
When handling the prompts/get
request:{
"messages": [
{
"role": "user",
"content": {
"type": "text",
"text": "Analyze these system logs and the code file for any issues:"
}
},
{
"role": "user",
"content": {
"type": "resource",
"resource": {
"uri": "logs://recent?timeframe=1h",
"text": "[2024-03-14 15:32:11] ERROR: Connection timeout in network.py:127\n[2024-03-14 15:32:15] WARN: Retrying connection (attempt 2/3)\n[2024-03-14 15:32:20] ERROR: Max retries exceeded",
"mimeType": "text/plain"
}
}
},
{
"role": "user",
"content": {
"type": "resource",
"resource": {
"uri": "file:///path/to/code.py",
"text": "def connect_to_service(timeout=30):\n retries = 3\n for attempt in range(retries):\n try:\n return establish_connection(timeout)\n except TimeoutError:\n if attempt == retries - 1:\n raise\n time.sleep(5)\n\ndef establish_connection(timeout):\n # Connection implementation\n pass",
"mimeType": "text/x-python"
}
}
}
]
}
Here’s a complete example of implementing prompts in an MCP server:When implementing prompts:1.
Use clear, descriptive prompt names
2.
Provide detailed descriptions for prompts and arguments
3.
Validate all required arguments
4.
Handle missing arguments gracefully
5.
Consider versioning for prompt templates
6.
Cache dynamic content when appropriate
8.
Document expected argument formats
9.
Consider prompt composability
10.
Test prompts with various inputs
Prompts can be surfaced in client UIs as:Servers can notify clients about prompt changes:1.
Server capability: prompts.listChanged
2.
Notification: notifications/prompts/list_changed
3.
Client re-fetches prompt list
When implementing prompts:Implement access controls
Handle sensitive data appropriately
Validate generated content
Consider prompt injection risks
Document security requirements
Modified at 2025-03-12 07:20:55