UNPKG

promptrix-mcp

Version:

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

70 lines • 4.25 kB
import { ErrorCode, McpError } from '@modelcontextprotocol/sdk/types.js'; import { getQualityThreshold } from '../lib/constants.js'; export async function handleAutoAnalyzePrompt(args, apiClient, qualityAnalyzer, requestId) { const defaultThreshold = getQualityThreshold(); const { prompt, threshold = defaultThreshold } = args; const reqId = requestId || 'unknown'; const timestamp = () => new Date().toISOString(); console.error(`[PROMPTRIX-MCP] [${timestamp()}] [${reqId}] Auto-analyzing prompt: "${prompt.substring(0, 50)}${prompt.length > 50 ? '...' : ''}"`); console.error(`[PROMPTRIX-MCP] [${timestamp()}] [${reqId}] Quality threshold: ${threshold}`); 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 { // Analyze prompt quality const qualityScore = qualityAnalyzer.analyzePromptQuality(prompt.trim()); console.error(`[PROMPTRIX-MCP] [${timestamp()}] [${reqId}] Quality analysis complete. Score: ${qualityScore.overall}`); const needsImprovement = qualityScore.overall < threshold; if (needsImprovement) { console.error(`[PROMPTRIX-MCP] [${timestamp()}] [${reqId}] Prompt quality below threshold. Suggesting enhancement.`); return { content: [ { type: 'text', text: `**Prompt Quality Analysis**\n\n**Quality Score:** ${qualityScore.overall}/100 (Below threshold of ${threshold})\n\n**Issues Detected:**\n${qualityAnalyzer.formatIssues(qualityScore.issues)}\n\n**Suggestions:**\n${qualityScore.suggestions.map((s) => `• ${s}`).join('\n')}\n\n**Recommendation:**\nšŸ”„ Use the 'enhance_prompt' tool to automatically improve this prompt based on the issues detected above.`, }, { type: 'text', text: JSON.stringify({ quality_score: qualityScore.overall, needs_improvement: needsImprovement, threshold, issues: qualityScore.issues, suggestions: qualityScore.suggestions, confidence: qualityScore.confidence, recommendation: "Use 'enhance_prompt' tool to improve this prompt", }, null, 2), }, ], }; } else { console.error(`[PROMPTRIX-MCP] [${timestamp()}] [${reqId}] Prompt quality acceptable. No enhancement needed.`); return { content: [ { type: 'text', text: `**Prompt Quality Analysis**\n\n**Quality Score:** ${qualityScore.overall}/100 (Above threshold of ${threshold})\n\n**Status:** āœ… Your prompt quality is good! No immediate improvements needed.\n\n**Analysis Details:**\n${qualityScore.suggestions.length > 0 ? qualityScore.suggestions.map((s) => `• ${s}`).join('\n') : '• No significant issues detected'}`, }, { type: 'text', text: JSON.stringify({ quality_score: qualityScore.overall, needs_improvement: needsImprovement, threshold, issues: qualityScore.issues, suggestions: qualityScore.suggestions, confidence: qualityScore.confidence, }, null, 2), }, ], }; } } catch (error) { console.error(`[PROMPTRIX-MCP] [${timestamp()}] [${reqId}] ERROR in handleAutoAnalyzePrompt:`, error); throw new McpError(ErrorCode.InternalError, `Failed to analyze prompt: ${error instanceof Error ? error.message : 'Unknown error'}`); } } //# sourceMappingURL=auto-analyze.js.map