当前位置: 首页 > news >正文

[MCP] Register Prompt

Prompts

MCP 支持 3 种上下文能力:

  1. tools:工具
  2. resources:资源
  3. prompts:提示词

在 MCP 中,prompts 表示服务端内置的提示词模板(prompt templates)集合,通过 prompt 模板机制,客户端无需硬编码 prompt,而是复用服务端定义的标准提示词,实现统一、版本化、模块化的调用方式。

Prompt相关方法

1. 获取提示词列表

{jsonrpc: "2.0",id: 1,method: "prompts/list", // 工具:tools/list、资源:resources/listparams: {}
}

返回:

{jsonrpc: "2.0",id: 1,// prompts对应是一个数组,因为可能有多个提示词模板prompts: [{name: "analyze-code", // 提示词模板的名称description: "Analyze code for potential improvements",// 提示词模板需要接收的参数arguments: [{name: "language",description: "Programming language",required: true,},],},];
}

2. 使用提示词

{jsonrpc: "2.0",id: 2,method: "prompts/get", // 固定的,工具:tools/call、资源:resources/readparams: {name: "analyze-code", // 要使用的提示词模板arguments: {language: "python" // 传递给提示词模板的参数}}
}

返回:

{jsonrpc: "2.0",id: 2,description: "Analyze Python code for potential improvements",// 返回具体的提示词信息messages: [{role: "user",content: {type: "text",text: "Please analyze the following Python code for potential improvements:\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```"}}]
}

课堂练习

为 MCP Server 注册提示词

import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";const server = new McpServer({name: "resources-server",version: "0.1.0",
});server.registerPrompt("总结代码", // 提示词模板的名称{description: "生成代码总结的prompt模板",arguments: [{name: "code",description: "需要总结的代码",required: false,},{name: "language",description: "代码对应的编程语言",required: false,},],},async (request) => {const args = request.arguments || {}; // 拿到对应的参数const { code, language } = args;// 因为 inspector 调试工具不支持传参,这里我们给一个默认值const codeContent =code ||`function calculateTotal(items) {let total = 0;for (const item of items) {total += item.price * item.quantity;}return total;`;const codeLanguage = language || "javascript";return {messages: [{role: "user",content: {type: "text",text: `请帮我总结以下${codeLanguage}代码的功能和主要逻辑:\`\`\`${codeLanguage.toLowerCase()}${codeContent}\`\`\`请用中文回答,包括:1. 代码的主要功能2. 关键的逻辑流程  3. 使用的主要技术或库4. 可能的改进建议${!code? "\n*注意:由于未提供代码参数,这里使用了示例代码进行演示。*": ""}`,},},],};}
);server.registerPrompt("生成测试的提示词模板", // 第一个参数:提示词模板的名称{// 第二个参数:配置对象description: "为指定函数生成测试用例的prompt模板",arguments: [{name: "function_name",description: "要测试的函数名称",required: false,},{name: "function_code",description: "函数的完整代码",required: false,},{name: "test_framework",description: "使用的测试框架 (例如: Jest, Mocha, Vitest)",required: false,},],},async (request) => {// 第三个参数:处理函数const args = request.arguments || {};const { function_name, function_code, test_framework } = args;// 提供默认值const functionName = function_name || "calculateTotal";const functionCode =function_code ||`function calculateTotal(items) {if (!Array.isArray(items)) {throw new Error('参数必须是数组');}let total = 0;for (const item of items) {if (!item.price || !item.quantity) {throw new Error('每个商品必须有价格和数量');}total += item.price * item.quantity;}return total;
}`;const testFramework = test_framework || "Jest";return {messages: [{role: "user",content: {type: "text",text: `请为以下函数生成${testFramework}测试用例:函数名:${functionName}函数代码:
\`\`\`javascript
${functionCode}
\`\`\`请生成包括以下场景的测试用例:
1. 正常情况的测试
2. 边界情况的测试
3. 错误情况的测试
4. 输入验证的测试请用中文注释说明每个测试用例的目的。${!function_name && !function_code? "\n*注意:由于未提供函数参数,这里使用了示例函数进行演示。*": ""
}`,},},],};}
);const transport = new StdioServerTransport();
await server.connect(transport);

重新认识MCP

MCP,全称 Model Context Protocol, 模型上下文协议。 其旨在为AI 应用与外部程序之间建立通信标准,从而使得外部程序可以被部署到任意AI(满足MCP协议), 也使得AI应用可以使用任意的外部程序(MCP Server)。

🤔为什么称之为模型上下文?

无论是工具、资源、提示词,这些信息最终都会作为上下文的一部分,提供给大模型。也就是说,大模型是最终信息的消费者。

MCP 资源聚合平台

官方组织推出了一些 MCP Server

除了官方以外,也有一些第三方的 MCP Server 的平台,例如:

  1. MCP.So
  2. Awesome MCP Servers

[!tip]

不过目前第三方平台的 MCP Server 的质量参差不齐,推荐优先使用官方推出的 MCP Server。


-EOF-

http://www.zskr.cn/news/16332.html

相关文章:

  • Software Foundations Vol.I : Coq函数式编程(Basics)
  • CSS - transition 粗浅记忆
  • P4550 收集邮票
  • P1654 OSU!
  • 10/4
  • DynamoDB十年演进:云原生数据库的技术革新
  • NotImplementedError: Cannot convert a symbolic Tensor (lstm/strided_slice:0) to a numpy array.
  • HTML基础学习 - 教程
  • 7_如何构建知识图谱
  • WPF ContentControl Content Binding
  • 盛世华诞 举国同庆|热烈庆祝 LEWISAK 英勇重创消火栓 1 周年!
  • 完整教程:<el-table>构建树形结构
  • CF2115 VP 记录
  • 深入解析:低秩矩阵、奇异值矩阵和正交矩阵
  • POLIR-Society-Philosophy- Hegels 形而上学System Philosophy Dialectics 系统化哲学辩证法: 自由意志+封闭的绝对精神
  • luogu P8816 [CSP-J 2022] 上升点列 题解
  • 集成测试 maestro-我的第一个flow以及第一次云端测试 - 详解
  • 2025 年生物除臭设备厂家最新推荐排行榜:覆盖污水处理厂 / 垃圾中转站等多场景,助力企业精准挑选优质设备
  • 从理念到沙盘:用悟空博弈模拟器点亮人机共治的曙光
  • Perplexity发布搜索API,驱动下一代AI应用开发
  • 20251005 总结
  • OKR1
  • 钉钉红包性能优化之路 - 实践
  • 2025 --【J+S 二十连测】-- 第二套 总结
  • 如何将 WSL 的 Ubuntu-24.04 迁移到其他电脑 - 详解
  • 题解:Luogu P11976 [KTSC 2021] 通信网络 / communication
  • 弦振动方程
  • 理论构建尝试整理
  • 基于springboot的家政服务预约系统 - 指南
  • 05-springAOP的实现