[MCP] Register Prompt
Prompts
MCP 支持 3 种上下文能力:
- tools:工具
- resources:资源
- prompts:提示词
在 MCP 中,prompts 表示服务端内置的提示词模板(prompt templates)集合,通过 prompt 模板机制,客户端无需硬编码 prompt,而是复用服务端定义的标准提示词,实现统一、版本化、模块化的调用方式。
Prompt相关方法
1. 获取提示词列表
{
jsonrpc: "2.0",
id: 1,
method: "prompts/list", // 工具:tools/list、资源:resources/list
params: {}
}
返回:
{
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/read
params: {
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 的平台,例如:
[!tip]
不过目前第三方平台的 MCP Server 的质量参差不齐,推荐优先使用官方推出的 MCP Server。
-EOF-