模型上下文协议(MCP)提示词概念的解析

创建可重用的提示模板和工作流 提示词使MCP服务器能够定义可重用的提示模板和工作流,MCP客户端可以轻松将其呈现给用户和LLM。它们为标准化和共享常见的LLM交互提供了一种强大的方式。 提示词被设计为用户控制的,这意味着它们从服务器提供给客户端,用户可以显式选择使用这些提示词。

模型上下文协议(MCP)提示词概念的解析

创建可重用的提示模板和工作流

提示词使MCP服务器能够定义可重用的提示模板和工作流,MCP客户端可以轻松将其呈现给用户和LLM。它们为标准化和共享常见的LLM交互提供了一种强大的方式。

提示词被设计为用户控制的,这意味着它们从服务器提供给客户端,用户可以显式选择使用这些提示词。

概述

MCP 中的提示词是预定义的模板,具有以下功能:

  • 接受动态参数
  • 包含资源上下文
  • 链接多个交互
  • 引导特定工作流
  • 显示为UI元素(如斜杠命令)

提示词结构

每个提示词定义如下:

{
  name: string;              // 提示词的唯一标识符
  description?: string;      // 人类可读的描述
  arguments?: [              // 可选参数列表
    {
      name: string;          // 参数标识符
      description?: string;  // 参数描述
      required?: boolean;    // 参数是否必填
    }
  ]
}

发现提示词

客户端可以通过调用 prompts/list 端点发现可用的提示词:

// 请求
{
  method: "prompts/list"
}

// 响应
{
  prompts: [
    {
      name: "analyze-code",
      description: "分析代码以发现潜在改进",
      arguments: [
        {
          name: "language",
          description: "编程语言",
          required: true
        }
      ]
    }
  ]
}

使用提示词

要使用提示词,客户端可以发起 prompts/get 请求:

// 请求
{
  method: "prompts/get",
  params: {
    name: "analyze-code",
    arguments: {
      language: "python"
    }
  }
}

// 响应
{
  description: "分析 Python 代码以发现潜在改进",
  messages: [
    {
      role: "user",
      content: {
        type: "text",
        text: "请分析以下 Python 代码并提供改进建议:\n\n
python\ndef calculate_sum(numbers):\n    total = 0\n    for num in numbers:\n        total = total + num\n    return total\n\nresult = calculate_sum([1, 2, 3, 4, 5])\nprint(result)\n
"
      }
    }
  ]
}

动态提示词

提示词可以是动态的,并包含以下功能:

嵌入资源上下文

{
  "name": "analyze-project",
  "description": "分析项目日志和代码",
  "arguments": [
    {
      "name": "timeframe",
      "description": "日志分析的时间范围",
      "required": true
    },
    {
      "name": "fileUri",
      "description": "需要审查的代码文件 URI",
      "required": true
    }
  ]
}

处理 prompts/get 请求时:

{
  "messages": [
    {
      "role": "user",
      "content": {
        "type": "text",
        "text": "请分析这些系统日志和代码文件,找出任何问题:"
      }
    },
    {
      "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"
        }
      }
    }
  ]
}

多步工作流

const debugWorkflow = {
  name: "debug-error",
  async getMessages(error: string) {
    return [
      {
        role: "user",
        content: {
          type: "text",
          text: `这是我遇到的错误:${error}`
        }
      },
      {
        role: "assistant",
        content: {
          type: "text",
          text: "让我帮你分析这个错误。你目前尝试了哪些解决方法?"
        }
      },
      {
        role: "user",
        content: {
          type: "text",
          text: "我尝试过重启服务,但错误仍然存在。"
        }
      }
    ];
  }
};

示例实现

以下是MCP服务器中实现提示词的完整示例:

TypeScript

import { Server } from "@modelcontextprotocol/sdk/server";
import {
  ListPromptsRequestSchema,
  GetPromptRequestSchema
} from "@modelcontextprotocol/sdk/types";

const PROMPTS = {
  "git-commit": {
    name: "git-commit",
    description: "Generate a Git commit message",
    arguments: [
      {
        name: "changes",
        description: "Git diff or description of changes",
        required: true
      }
    ]
  },
  "explain-code": {
    name: "explain-code",
    description: "Explain how code works",
    arguments: [
      {
        name: "code",
        description: "Code to explain",
        required: true
      },
      {
        name: "language",
        description: "Programming language",
        required: false
      }
    ]
  }
};

const server = new Server({
  name: "example-prompts-server",
  version: "1.0.0"
}, {
  capabilities: {
    prompts: {}
  }
});

// List available prompts
server.setRequestHandler(ListPromptsRequestSchema, async () => {
  return {
    prompts: Object.values(PROMPTS)
  };
});

// Get specific prompt
server.setRequestHandler(GetPromptRequestSchema, async (request) => {
  const prompt = PROMPTS[request.params.name];
  if (!prompt) {
    throw new Error(`Prompt not found: ${request.params.name}`);
  }

  if (request.params.name === "git-commit") {
    return {
      messages: [
        {
          role: "user",
          content: {
            type: "text",
            text: `Generate a concise but descriptive commit message for these changes:\n\n${request.params.arguments?.changes}`
          }
        }
      ]
    };
  }

  if (request.params.name === "explain-code") {
    const language = request.params.arguments?.language || "Unknown";
    return {
      messages: [
        {
          role: "user",
          content: {
            type: "text",
            text: `Explain how this ${language} code works:\n\n${request.params.arguments?.code}`
          }
        }
      ]
    };
  }

  throw new Error("Prompt implementation not found");
});

Python

from mcp.server import Server
import mcp.types as types

# Define available prompts
PROMPTS = {
    "git-commit": types.Prompt(
        name="git-commit",
        description="Generate a Git commit message",
        arguments=[
            types.PromptArgument(
                name="changes",
                description="Git diff or description of changes",
                required=True
            )
        ],
    ),
    "explain-code": types.Prompt(
        name="explain-code",
        description="Explain how code works",
        arguments=[
            types.PromptArgument(
                name="code",
                description="Code to explain",
                required=True
            ),
            types.PromptArgument(
                name="language",
                description="Programming language",
                required=False
            )
        ],
    )
}

# Initialize server
app = Server("example-prompts-server")

@app.list_prompts()
async def list_prompts() -> list[types.Prompt]:
    return list(PROMPTS.values())

@app.get_prompt()
async def get_prompt(
    name: str, arguments: dict[str, str] | None = None
) -> types.GetPromptResult:
    if name not in PROMPTS:
        raise ValueError(f"Prompt not found: {name}")

    if name == "git-commit":
        changes = arguments.get("changes") if arguments else ""
        return types.GetPromptResult(
            messages=[
                types.PromptMessage(
                    role="user",
                    content=types.TextContent(
                        type="text",
                        text=f"Generate a concise but descriptive commit message "
                        f"for these changes:\n\n{changes}"
                    )
                )
            ]
        )

    if name == "explain-code":
        code = arguments.get("code") if arguments else ""
        language = arguments.get("language", "Unknown") if arguments else "Unknown"
        return types.GetPromptResult(
            messages=[
                types.PromptMessage(
                    role="user",
                    content=types.TextContent(
                        type="text",
                        text=f"Explain how this {language} code works:\n\n{code}"
                    )
                )
            ]
        )

    raise ValueError("Prompt implementation not found")


最佳实践

实现提示词时:

  1. 使用清晰、描述性强的提示词名称
  2. 为提示词和参数提供详细的描述
  3. 验证所有必填参数
  4. 优雅地处理缺失参数
  5. 考虑提示词模板的版本管理
  6. 动态内容适当缓存
  7. 实现错误处理
  8. 编写参数格式的文档
  9. 考虑提示词的可组合性
  10. 用各种输入测试提示词

UI 集成

提示词可以在客户端UI中以以下形式呈现:

  • 斜杠命令
  • 快捷操作
  • 上下文菜单项
  • 命令面板条目
  • 引导式工作流
  • 交互式表单

更新与变更

MCP服务器可以通过以下方式通知MCP客户端提示词的变更:

  1. 服务器能力:prompts.listChanged
  2. 通知:notifications/prompts/list_changed
  3. 客户端重新获取提示词列表

安全性考虑

实现提示词时需要注意:

  • 验证所有参数
  • 清理用户输入
  • 考虑速率限制
  • 实施访问控制
  • 审计提示词使用情况
  • 适当处理敏感数据
  • 验证生成内容
  • 实现超时机制
  • 考虑提示词注入风险
  • 编写安全需求文档