当前位置: 首页 > 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.hskmm.com/?act=detail&tid=25242

相关文章:

  • [Node.js] Server-Sent Events
  • day1 Gitlab Runner 学习
  • Software Foundations Vol.I : 使用结构化的数据(Lists)
  • Software Foundations Vol.I : 归纳证明(Induction)
  • Software Foundations Vol.I : Coq函数式编程(Basics)
  • Python 在自然语言处理中的应用与发展
  • Python 在网络爬虫与数据采集中的应用
  • 15_spring_data_neo4j简单教程
  • CF2152G Query Jungle(线段树,重链剖分,*)
  • 代码随想录算法训练营第九天 | leetcode 151 卡特55
  • [题解] 分竹子
  • 分数规划
  • CSS - transition 粗浅记忆
  • 【MC】LittleTiles模组结构数据解析和版本迁移方案
  • 容器魔方导致盒子满了
  • 课程学习笔记——[大一秋]遗传学
  • P3067 [USACO12OPEN] Balanced Cow Subsets G
  • Vivado 2025 界面中文设置
  • 词汇学习——专业词汇
  • P4556 [Vani有约会] 雨天的尾巴 [模板] 线段树合并
  • P4550 收集邮票
  • P8110 [Cnoi2021] 矩阵
  • P9751 [CSP-J 2023] 旅游巴士
  • P9234 [蓝桥杯 2023 省 A] 买瓜
  • P1044 [NOIP 2003 普及组] 栈
  • P1080 [NOIP 2012 提高组] 国王游戏
  • 音响没声音
  • P1654 OSU!
  • DynamoDB十年演进:云原生数据库的技术革新
  • 完整教程:MySQL全量、增量备份与恢复