Model Context Protocol(MCP)
  1. Concepts
Model Context Protocol(MCP)
  • Get Started
    • Introduction
    • Example Servers
    • Example Clients
    • Quickstart
      • For Server Developers
      • For Client Developers
      • For Claude Desktop Users
  • Tutorials
    • Building MCP with LLMs
    • Debugging
    • Inspector
  • Concepts
    • Core architecture
    • Resources
    • Prompts
    • Tools
    • Sampling
    • Roots
    • Transports
  • Development
    • What's New
    • Roadmap
    • Contributing
  1. Concepts

Prompts

Create reusable prompt templates and workflows
Prompts 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.

Overview#

Prompts in MCP are predefined templates that can:
Accept dynamic arguments
Include context from resources
Chain multiple interactions
Guide specific workflows
Surface as UI elements (like slash commands)

Prompt structure#

Each prompt is defined with:

Discovering prompts#

Clients can discover available prompts through the prompts/list endpoint:

Using prompts#

To use a prompt, clients make a prompts/get request:
Copy

Dynamic prompts#

Prompts can be dynamic and include:

Embedded resource context#

{
  "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"
        }
      }
    }
  ]
}

Multi-step workflows#

Example implementation#

Here’s a complete example of implementing prompts in an MCP server:
TypeScript
Python

Best practices#

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
7.
Implement error handling
8.
Document expected argument formats
9.
Consider prompt composability
10.
Test prompts with various inputs

UI integration#

Prompts can be surfaced in client UIs as:
Slash commands
Quick actions
Context menu items
Command palette entries
Guided workflows
Interactive forms

Updates and changes#

Servers can notify clients about prompt changes:
1.
Server capability: prompts.listChanged
2.
Notification: notifications/prompts/list_changed
3.
Client re-fetches prompt list

Security considerations#

When implementing prompts:
Validate all arguments
Sanitize user input
Consider rate limiting
Implement access controls
Audit prompt usage
Handle sensitive data appropriately
Validate generated content
Implement timeouts
Consider prompt injection risks
Document security requirements
Modified at 2025-03-12 07:20:55
Previous
Resources
Next
Tools
Built with