UNPKG

promptrix-mcp

Version:

MCP Server for Promptrix - AI prompt enhancement and optimization for Claude Code

43 lines 2.31 kB
import { ErrorCode, McpError } from '@modelcontextprotocol/sdk/types.js'; export async function handleEnhancePrompt(args, apiClient, requestId) { const { prompt } = args; const reqId = requestId || 'unknown'; const timestamp = () => new Date().toISOString(); if (!prompt || typeof prompt !== 'string' || prompt.trim().length === 0) { console.error(`[PROMPTRIX-MCP] [${timestamp()}] [${reqId}] ERROR: Invalid prompt provided`); throw new McpError(ErrorCode.InvalidParams, 'Prompt is required and must be a non-empty string'); } try { console.error(`[PROMPTRIX-MCP] [${timestamp()}] [${reqId}] Starting enhancement for prompt: "${prompt.substring(0, 50)}${prompt.length > 50 ? '...' : ''}"`); const data = await apiClient.enhancePrompt(args, reqId); // Format the response for MCP const result = { enhanced_prompt: data.improvedPrompt, original_prompt: prompt, ...(data.rationale && { rationale: data.rationale }), ...(data.diff && { diff: data.diff }), ...(data.meta && { intent: data.meta.intent, model_hints: data.meta.modelHints, }), }; console.error(`[PROMPTRIX-MCP] [${timestamp()}] [${reqId}] Successfully enhanced prompt. Original length: ${prompt.length}, Enhanced length: ${data.improvedPrompt.length}`); return { content: [ { type: 'text', text: `**Enhanced Prompt:**\n${data.improvedPrompt}\n\n**Original Prompt:**\n${prompt}${data.rationale ? `\n\n**Rationale:**\n${data.rationale}` : ''}${data._requestId ? `\n\n**Request ID:** ${data._requestId}` : ''}${data._processedAt ? `\n**Processed At:** ${data._processedAt}` : ''}`, }, { type: 'text', text: JSON.stringify(result, null, 2), }, ], }; } catch (error) { console.error(`[PROMPTRIX-MCP] [${timestamp()}] [${reqId}] ERROR in handleEnhancePrompt:`, error); throw new McpError(ErrorCode.InternalError, `Failed to enhance prompt: ${error instanceof Error ? error.message : 'Unknown error'}`); } } //# sourceMappingURL=enhance-prompt.js.map