refine_prompt
Improve existing prompts by applying targeted feedback to add missing context, clarify instructions, or adapt for different AI models while preserving original structure.
Instructions
Iteratively improve an existing prompt based on specific feedback.
Use this tool when you need to: • Improve a prompt that didn't get good results • Add missing context or constraints • Make a prompt more specific or clearer • Adapt a prompt for a different AI model
The tool preserves the original structure while applying targeted improvements.
IMPORTANT: When available, pass workspace context (file structure, package.json, tech stack) to ensure refined prompts comply with the user's project scope and original request.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| prompt | Yes | The current prompt to refine. | |
| feedback | Yes | What should be improved. Examples: "make it more specific", "add error handling requirements", "focus on performance". | |
| preserveStructure | No | Whether to keep the original structure. Default: true. | |
| targetModel | No | Target AI model for optimization. | |
| workspaceContext | No | Project context to ensure the refined prompt aligns with the codebase. Include: file/folder structure, package.json dependencies, tech stack (React, Node, etc.), relevant code snippets, and the original user request. This ensures the refined prompt complies with project conventions and scope. |
Implementation Reference
- src/tools/refinePrompt.ts:27-92 (handler)The core handler function that implements the refine_prompt tool logic. It calls the PromptArchitect API if available, otherwise falls back to basic refinements, and computes metadata.export async function refinePrompt(input: RefinePromptInput): Promise<{ refinedPrompt: string; changes: string[]; metadata: { originalWordCount: number; refinedWordCount: number; structurePreserved: boolean; }; }> { const { prompt, feedback, preserveStructure = true, targetModel, workspaceContext } = input; logger.info('Refining prompt', { promptLength: prompt.length, feedbackLength: feedback.length, preserveStructure, targetModel, hasWorkspaceContext: !!workspaceContext }); let refinedPrompt: string = ''; let changes: string[] = []; // Use PromptArchitect API if (isApiClientAvailable()) { try { const response = await apiRefinePrompt({ prompt, feedback, preserveStructure, targetModel, workspaceContext, }); refinedPrompt = response.refinedPrompt; changes = response.changes || ['Prompt refined based on feedback']; logger.info('Refined via PromptArchitect API'); } catch (error) { logger.warn('API request failed, using fallback', { error: error instanceof Error ? error.message : 'Unknown error' }); } } // Fallback basic refinement if (!refinedPrompt) { refinedPrompt = applyBasicRefinements(prompt, feedback); changes = ['Applied basic refinements']; logger.warn('Using fallback refinement'); } // Calculate metadata const originalWordCount = prompt.split(/\s+/).length; const refinedWordCount = refinedPrompt.split(/\s+/).length; const originalHasStructure = /^#+\s|^\d+\.|^-\s|^\*\s/m.test(prompt); const refinedHasStructure = /^#+\s|^\d+\.|^-\s|^\*\s/m.test(refinedPrompt); const structurePreserved = !originalHasStructure || refinedHasStructure; return { refinedPrompt, changes, metadata: { originalWordCount, refinedWordCount, structurePreserved, }, }; }
- src/tools/refinePrompt.ts:14-23 (schema)Zod schema defining the input validation for the refine_prompt tool.export const refinePromptSchema = z.object({ prompt: z.string().min(1).describe('The current prompt to refine'), feedback: z.string().min(1).describe('What should be improved or changed'), preserveStructure: z.boolean().optional().default(true).describe('Whether to preserve the original structure'), targetModel: z.enum(['gpt-4', 'claude', 'gemini', 'general']) .optional() .default('general') .describe('Target AI model to optimize for'), workspaceContext: z.string().optional().describe('Project context including file structure, tech stack, dependencies, and any relevant code snippets to ensure the refined prompt aligns with the project scope'), });
- src/server.ts:114-154 (registration)MCP tool list registration for 'refine_prompt' including name, description, and input schema.{ name: 'refine_prompt', description: `Iteratively improve an existing prompt based on specific feedback. Use this tool when you need to: • Improve a prompt that didn't get good results • Add missing context or constraints • Make a prompt more specific or clearer • Adapt a prompt for a different AI model The tool preserves the original structure while applying targeted improvements. IMPORTANT: When available, pass workspace context (file structure, package.json, tech stack) to ensure refined prompts comply with the user's project scope and original request.`, inputSchema: { type: 'object', properties: { prompt: { type: 'string', description: 'The current prompt to refine.', }, feedback: { type: 'string', description: 'What should be improved. Examples: "make it more specific", "add error handling requirements", "focus on performance".', }, preserveStructure: { type: 'boolean', description: 'Whether to keep the original structure. Default: true.', }, targetModel: { type: 'string', enum: ['gpt-4', 'claude', 'gemini', 'general'], description: 'Target AI model for optimization.', }, workspaceContext: { type: 'string', description: 'Project context to ensure the refined prompt aligns with the codebase. Include: file/folder structure, package.json dependencies, tech stack (React, Node, etc.), relevant code snippets, and the original user request. This ensures the refined prompt complies with project conventions and scope.', }, }, required: ['prompt', 'feedback'], }, },
- src/server.ts:220-231 (registration)MCP tool call handler for 'refine_prompt' that parses input using the schema and invokes the refinePrompt handler.case 'refine_prompt': { const input = refinePromptSchema.parse(args); const result = await refinePrompt(input); return { content: [ { type: 'text', text: JSON.stringify(result, null, 2), }, ], }; }
- src/tools/refinePrompt.ts:94-120 (helper)Fallback helper function for basic prompt refinements when API is unavailable.function applyBasicRefinements(prompt: string, feedback: string): string { let refined = prompt; const feedbackLower = feedback.toLowerCase(); // Basic transformations based on common feedback if (feedbackLower.includes('more specific') || feedbackLower.includes('more detail')) { refined += '\n\n## Additional Requirements\n- Provide specific, detailed information\n- Include concrete examples where applicable'; } if (feedbackLower.includes('shorter') || feedbackLower.includes('concise')) { // Try to simplify refined = refined.replace(/\n\n+/g, '\n\n'); } if (feedbackLower.includes('structure') || feedbackLower.includes('organize')) { if (!/^#+\s/m.test(refined)) { refined = '## Task\n' + refined + '\n\n## Output\nProvide a well-structured response.'; } } if (feedbackLower.includes('example')) { refined += '\n\n## Example\nProvide a relevant example in your response.'; } return refined; }