UNPKG

prompt-format-mcp

Version:

Simple prompt optimization MCP server with SiliconFlow API integration

109 lines (103 loc) 3.83 kB
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"; import { z } from "zod"; import { SiliconFlowClient } from './api/siliconflow.js'; import { Logger, retry } from './utils/helpers.js'; export class PromptFormatMcpServer { constructor() { const apiKey = process.env.SILICONFLOW_API_KEY; if (!apiKey) { throw new Error('SILICONFLOW_API_KEY environment variable is required'); } this.apiClient = new SiliconFlowClient(apiKey, process.env.SILICONFLOW_BASE_URL, process.env.MODEL_NAME); this.server = new McpServer({ name: "prompt-format-mcp", version: "1.1.1", }, { capabilities: { tools: {}, }, }); this.registerTools(); } registerTools() { this.server.tool("optimize-prompt", "Optimize prompt for better AI model performance", { content: z.string().describe("Prompt content to optimize") }, async ({ content }) => { try { Logger.log(`优化提示词,长度: ${content.length} 字符`); const optimized = await retry(() => this.apiClient.optimizePrompt(content), 3, 1000); const result = `## 🔧 提示词优化结果 ### 原始提示词 \`\`\` ${content} \`\`\` ### 优化后的提示词 \`\`\` ${optimized} \`\`\` --- ### 📋 下一步操作 **请您编辑确认:** 1. 查看上面的优化结果 2. 根据需要编辑修改优化后的提示词(可选) 3. 使用 \`confirm-and-continue\` 工具提交您的最终版本 > 💡 **说明**: 使用 \`confirm-and-continue\` 后,AI将基于您确认的提示词来回答您的问题。`; return { content: [ { type: "text", text: result } ] }; } catch (error) { Logger.error("优化失败:", error); return { isError: true, content: [ { type: "text", text: `优化失败: ${error.message}` } ] }; } }); this.server.tool("confirm-and-continue", "Confirm the final prompt and signal ready for new conversation", { finalPrompt: z.string().describe("The final confirmed prompt to use for AI conversation") }, async ({ finalPrompt }) => { try { Logger.log(`确认使用提示词`); const result = `✅ 上述提示词已确认。现在,请直接根据新的提示词:${finalPrompt}回答用户问题。**无需用户重新提问**`; return { content: [ { type: "text", text: result } ] }; } catch (error) { Logger.error("确认失败:", error); return { isError: true, content: [ { type: "text", text: `确认失败: ${error.message}` } ] }; } }); } async startStdio() { const transport = new StdioServerTransport(); await this.server.connect(transport); Logger.log("Prompt Format MCP Server started on stdio"); } } //# sourceMappingURL=server.js.map