ARTICLE DETAIL

建站实战干货

来自一线的建站与推广经验沉淀,每一条都经过真实交付验证。

MCP Toolbox Prompts 配置指南:可复用提示模板、参数插值与 Gemini CLI 斜杠命令实战

2026/9/14 5:17:04 拓冰建站 浏览量
MCP Toolbox Prompts 配置指南:可复用提示模板、参数插值与 Gemini CLI 斜杠命令实战 MCP Toolbox Prompts 配置指南可复用提示模板、参数插值与 Gemini CLI 斜杠命令实战【免费下载链接】mcp-toolboxMCP Toolbox for Databases is an open source MCP server for databases.项目地址: https://gitcode.com/GitHub_Trending/ge/mcp-toolbox本文面向 MCP Toolbox for Databases 的使用者与配置开发者围绕tools.yaml中kind: prompt声明式配置展开系统讲解 prompt 的完整 Schema、消息与参数结构、占位符插值机制并给出与 Gemini CLI 集成生成自定义斜杠命令的完整工作流。读完本文你将能够编写单消息与多消息的自定义提示模板理解其底层解析与插值实现并让它在支持 MCP 的客户端中一键复用。在 MCP Toolbox 中prompt代表一个可复用的提示模板它定义了一段或一组面向大语言模型LLM的结构化消息以及可供客户端传入的命名参数。Toolbox 服务器实现了 Model Context ProtocolMCP规范中的prompts/list与prompts/get两个方法客户端因此可以发现并检索这些提示。本文以 Prompts 配置文档 为骨架结合 internal/prompts 下的源码实现从 YAML 声明、Schema 字段、参数插值到客户端集成逐层展开。什么是 Toolbox 中的 Prompt一个prompt本质上是一条或一系列消息的模板它最终会被发送给 LLM。与把指令硬编码在客户端相比prompt 的价值在于复用同一条提示如代码审查、角色扮演、SQL 生成可以在不同会话中被反复调用参数化通过{{.argument_name}}占位符让每次调用的内容随入参变化标准化通过 MCP 的prompts/list列出和prompts/get获取并插值协议暴露给任何兼容客户端。一个最基本的 prompt 声明如下来自 Prompts 配置文档kind: prompt name: code_review description: Asks the LLM to analyze code quality and suggest improvements. messages: - content: Please review the following code for quality, correctness, and potential improvements: \n\n{{.code}} arguments: - name: code description: The code to review该配置定义了一个名为code_review的提示模板消息内容中包含{{.code}}占位符客户端在调用prompts/get时提供code实参服务器将其替换为真实代码后返回最终提示。用 Groups 组织 Prompt提示可以借助 Groups 配置 进行归类。当客户端连接到某个 group 的端点时prompts/list只返回该 group 内的 prompts而默认端点/mcp返回全部 prompts。典型声明如下kind: group name: data_analyst description: Tools, prompts, and resources for exploratory data analysis. prompts: - summarize_results这意味着你既可以在全局范围提供通用提示也可以为特定角色如数据分析师、管理员定制精简的提示集合。Prompt Schema 字段详解fieldtyperequireddescriptiondescriptionstringNoA brief explanation of what the prompt does.typestringNoThe type of prompt. Defaults tocustom.messages[][Message](#message-schema)YesA list of one or more message objects that make up the prompts content.arguments[][Argument](#argument-schema)NoA list of arguments that can be interpolated into the prompts content.关于type字段在源码中可以看到其处理逻辑internal/prompts/prompts.go 的DecodeConfig会先按type查找已注册的 prompt 工厂当type为空字符串时自动回退为custom若类型仍无法识别则返回unknown prompt type错误。这一设计表明type 是可选的缺省即customprompt 类型是可扩展的任意类型可通过Register注册自己的配置工厂prompts.go当前仓库内置的默认类型为custom详见 Custom Prompts 文档。messages是唯一必填字段必须包含一条或多条消息对象arguments定义可插值的参数列表二者均在下文详述。Message Schema角色与内容fieldtyperequireddescriptionrolestringNoThe role of the sender. Can beuserorassistant. Defaults touser.contentstringYesThe text of the message. You can include placeholders for arguments using{{.argument_name}}syntax.role合法取值仅user与assistant。这一约束在 internal/prompts/messages.go 的Message.UnmarshalYAML中强制校验若未指定role则默认补为user若填写了其他值则直接报错invalid role。content支持 Go 模板风格的占位符语法{{.argument_name}}。Argument Schema参数即 ParameterArgument 可以复用 Tools 参数规范 中的任意 Parameter 类型如果未指定type默认是string。这一默认行为的底层实现在 internal/prompts/arguments.goArguments.UnmarshalYAML对每个参数项先检查是否存在type字段缺失时写入parameters.TypeString再调用与 tools 共用的parameters.ParseParameter完成解析。这意味着 prompt 参数与 tool 参数共享同一套类型系统可用的类型包括类型关键字说明string字符串支持default、escape、allowedValues、excludedValues等约束integer整数支持minValue/maxValue范围校验float浮点数同样支持范围校验boolean布尔值array数组map键值映射参数还可以声明required缺省视为必填见 parameters.go 的GetRequired、default、allowedValues、excludedValues等公共字段从而在插值前完成类型转换与取值校验。实战示例从单消息到多消息提示单消息提示单消息模板适合一条指令 若干输入的场景例如上文code_review。此处再给出带参数描述与多参数的完整版本kind: prompt name: code_review description: Asks the LLM to analyze code quality and suggest improvements. messages: - content: Please review the following code for quality, correctness, and potential improvements: \n\n{{.code}} arguments: - name: code description: The code to review多消息提示通过多条消息可以搭建更复杂的对话上下文例如角色扮演场景来自 Custom Prompts 文档kind: prompt name: roleplay_scenario description: Sets up a roleplaying scenario with initial messages. arguments: - name: character description: The character the AI should embody. - name: situation description: The initial situation for the roleplay. messages: - role: user content: Lets roleplay. You are {{.character}}. The situation is: {{.situation}} - role: assistant content: Okay, I understand. I am ready. What happens next?这里第一条消息user携带两个参数占位符第二条消息assistant给出开场白。客户端传入character与situation后服务器返回两条完整消息为 LLM 提供明确的角色设定与初始情境。插值机制Go 模板的底层实现当客户端调用prompts/get传入参数值后服务器需要把{{.xxx}}占位符替换为真实值。这条链路在源码中清晰可见custom.go 中Prompt.SubstituteParams委托给prompts.SubstituteMessagesmessages.go 的SubstituteMessages遍历每条消息调用parameters.ResolveTemplateParams执行替换parameters.go 使用标准库text/template解析消息内容并以参数名到值的映射作为模板上下文执行最终返回替换后的字符串。该实现还额外注册了array模板函数当消息中需要以逗号分隔形式输出字符串数组参数时可用{{array .tag}}形式将其渲染为a, b, c对应 ConvertArrayParamToString。这意味着除简单标量外数组参数同样可以直接进入模板渲染。从调用链可进一步看出prompts/get处理流程包含参数解析ParseArgs完成类型转换、默认值与必填校验与模板插值SubstituteParams两个阶段二者共同保证了最终提示内容既符合参数约束、又完成占位符替换。与 Gemini CLI 集成把 Prompt 变成斜杠命令在 tools.yaml 等配置文件中定义的 prompts 可以与 Gemini CLI 无缝集成自动成为 CLI 中的自定义斜杠命令。完整工作流如下发现DiscoveryGemini CLI 连接 Toolbox 服务器后自动调用prompts/list发现所有可用提示转换Conversion每个被发现的 prompt 被转换为对应的斜杠命令。例如名为code_review的 prompt 变成 CLI 中的/code_review命令执行Execution用户直接执行该命令并通过--参数名形式传入实参/code_review --codedef hello():\n print(world)插值Interpolation参数收集完毕后CLI 调用prompts/get并携带你提供的值取回完成插值的最终提示例如Please review the following code for quality, correctness, and potential improvements: def hello(): print(world)响应Response完整的提示被发送给 Gemini 模型模型的回答直接显示在 CLI 中。这套流程意味着你只需在 YAML 中声明一次 prompt即可在 Gemini CLI 中获得一个类型安全、参数自动校验的命令入口无需在客户端侧重复编写提示逻辑。Types of promptsCustom 是默认类型custom是用户自定义、通过 MCP 服务器对外暴露的提示类型也是当前默认且内置的类型。其定义位于 Custom Prompts 文档源码实现在 internal/prompts/custom/custom.go通过init()注册类型名custom与对应配置工厂custom.goConfig结构体直接对应 YAML 中的name、description、messages、arguments四个字段custom.goInitialize生成带描述与参数清单的Manifest用于prompts/list时向客户端暴露元信息custom.go其 Schema 与默认 prompt 一致type必须为custommessages必填description、arguments可选Message Schema 与 Argument Schema 均参照 Prompts 主文档。从注册机制prompts.go可以推断prompt 类型体系是开放可扩展的后续若新增其他内置类型只需以同样方式注册新的工厂函数即可配置文件中的type字段随之生效无需改动协议层代码。小结与进一步阅读围绕kind: prompt本文覆盖了从 YAML 声明、Schema 字段、参数与消息结构、Go 模板插值原理到 Gemini CLI 斜杠命令集成与custom类型的完整知识链。建议按以下路径继续深入查看 Prompts 配置主文档 与 Custom Prompts 文档 获取权威字段说明阅读 internal/prompts/prompts.go、custom/custom.go、messages.go、arguments.go 理解类型注册、校验与插值实现配套的 prompts_test.go、custom_test.go、messages_test.go、arguments_test.go 提供了丰富的解析与插值用例如需了解参数类型系统全貌参见 tools 参数规范 与 internal/util/parameters若要按角色组织提示集合参见 Groups 配置文档。【免费下载链接】mcp-toolboxMCP Toolbox for Databases is an open source MCP server for databases.项目地址: https://gitcode.com/GitHub_Trending/ge/mcp-toolbox创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考