Skip to content

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/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 注册提示词

重新认识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-