Skip to main content
Glama
grandinh

MCP Prompt Optimizer

by grandinh

optimize_prompt

Analyze and optimize AI prompts to improve clarity, detect risks, and add domain-specific requirements for better AI interaction quality.

Instructions

Analyze and optimize a user prompt using the OTA (Optimize-Then-Answer) Framework. Returns clarity score, domain classification, risk flags, targeted questions (if needed), and an enhanced prompt ready for AI processing.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
promptYesThe user prompt to optimize
contextNoOptional additional context about the request

Implementation Reference

  • The handler function that executes the 'optimize_prompt' tool logic. It extracts the prompt and context from the request, calls the PromptOptimizer.optimize method, formats the response including analysis, questions if needed, and the optimized prompt, then returns it in the MCP format.
    if (request.params.name === 'optimize_prompt') { const { prompt, context } = request.params.arguments as { prompt: string; context?: string }; try { const result = await optimizer.optimize(prompt, context); let response = `${result.optimization_header}\n\n`; if (result.needs_clarification && result.questions.length > 0) { response += `**⚠️ Clarification Needed** (Clarity: ${(result.clarity_score * 100).toFixed(0)}%)\n\n`; response += `**Please answer these questions before I proceed:**\n\n`; result.questions.forEach((q, i) => { response += `${i + 1}. ${q}\n`; }); response += `\n---\n\n`; } else { response += `**✓ Ready to Process** (Clarity: ${(result.clarity_score * 100).toFixed(0)}%)\n\n`; } response += `**Domain:** ${result.domain}\n`; response += `**Risk Flags:** ${result.risk_flags.length > 0 ? result.risk_flags.join(', ') : 'None'}\n\n`; if (!result.needs_clarification) { response += `**Optimized Prompt:**\n\`\`\`\n${result.optimized_prompt}\n\`\`\`\n\n`; response += `Use this enhanced prompt for the AI request to ensure comprehensive, domain-appropriate output.`; } return { content: [ { type: 'text', text: response, }, ], }; } catch (error) { return { content: [ { type: 'text', text: `Error optimizing prompt: ${error}`, }, ], isError: true, }; } }
  • Tool schema definition including name, description, and input schema specifying required 'prompt' and optional 'context' parameters.
    const OPTIMIZE_TOOL: Tool = { name: 'optimize_prompt', description: 'Analyze and optimize a user prompt using the OTA (Optimize-Then-Answer) Framework. ' + 'Returns clarity score, domain classification, risk flags, targeted questions (if needed), ' + 'and an enhanced prompt ready for AI processing.', inputSchema: { type: 'object', properties: { prompt: { type: 'string', description: 'The user prompt to optimize', }, context: { type: 'string', description: 'Optional additional context about the request', }, }, required: ['prompt'], }, };
  • src/index.ts:419-423 (registration)
    Registers the 'optimize_prompt' tool by returning it in the ListToolsRequest handler response.
    server.setRequestHandler(ListToolsRequestSchema, async () => { return { tools: [OPTIMIZE_TOOL], }; });
  • Core helper method in the PromptOptimizer class that orchestrates prompt analysis, domain detection, clarity scoring, risk assessment, question generation, and creation of the optimized prompt.
    async optimize(prompt: string, context?: string): Promise<PromptAnalysis> { if (!this.framework) { await this.loadFramework(); } const analysis = this.analyzePrompt(prompt); const domain = analysis.domain!; const clarityScore = analysis.clarity_score!; const risks = analysis.risk_flags!; const questions = this.generateQuestions(prompt, domain, clarityScore); const needsClarification = clarityScore < 0.6 || risks.some(r => ['policy', 'safety'].includes(r)); const optimizedPrompt = this.createOptimizedPrompt(prompt, domain, clarityScore, risks); const header = `[OPTIMIZED] Domain: ${domain} | Clarity: ${(clarityScore * 100).toFixed(0)}% | ` + `Risks: ${risks.length > 0 ? risks.join(', ') : 'none'}`; return { domain, clarity_score: clarityScore, risk_flags: risks, questions, optimized_prompt: optimizedPrompt, optimization_header: header, assumptions: [], needs_clarification: needsClarification, }; }
Install Server

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/grandinh/mcp-prompt-optimizer'

If you have feedback or need assistance with the MCP directory API, please join our Discord server