auto_optimize
Optimizes natural language text into Claude Code prompts by detecting and structuring conversational input for better coding results.
Instructions
Automatically detects and optimizes natural language text for Claude Code. Use this when the user is writing conversational text that should be translated into an optimized prompt.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| text | Yes | The natural language text to analyze and potentially optimize | |
| context | No | Any additional context about the current project or situation | |
| interactive | No | Whether to enable interactive questioning if the prompt is unclear or needs more info (default: auto-detect based on complexity) |
Implementation Reference
- index.ts:675-723 (handler)Main handler for the 'auto_optimize' tool in the CallToolRequestSchema handler. Validates arguments, calls autoOptimizeText, handles interactive questioning or direct optimization, and formats the response.case "auto_optimize": { if (!isAutoOptimizeArgs(args)) { throw new Error("Invalid arguments for auto_optimize"); } const { text, context, interactive } = args; const result = await autoOptimizeText(text, context, interactive); if (!result.shouldOptimize) { return { content: [{ type: "text", text: `**Analysis:** This text doesn't appear to need optimization.\n\nReason: ${result.analysis.reason}\nConfidence: ${(result.analysis.confidence * 100).toFixed(1)}%\n\n**Original text:** "${text}"` }], isError: false, }; } // If questions are needed, start interactive session if (result.needsQuestions && result.questions && result.sessionId) { return { content: [{ type: "text", text: `**๐ค I need more information to optimize this effectively!**\n\n**Analysis:** ${result.analysis.questioningReason || 'Request needs clarification'}\n\n**Questions to help me create the best prompt:**\n\n${result.questions.map((q, i) => `${i + 1}. ${q}`).join('\n')}\n\n**Next Step:** Use the answer_questions tool with session ID "${result.sessionId}" to provide your answers.\n\n---\n**Detected:** ${result.analysis.detectedLanguage || 'General'} | ${result.analysis.taskType} | ${result.analysis.complexity}` }], isError: false, }; } // Direct optimization result if (result.optimizedPrompt) { const analysis = result.analysis; return { content: [{ type: "text", text: `**๐ Auto-Optimized Prompt for Claude Code:**\n\n${result.optimizedPrompt}\n\n**Are you ready to use this prompt?**` }], isError: false, }; } else { return { content: [{ type: "text", text: `**Analysis:** This text appears to be a prompt that should be optimized, but optimization failed.\n\nReason: ${result.analysis.reason}\nError: ${result.analysis.error || 'Unknown error'}\n\n**Original text:** "${text}"` }], isError: false, }; } }
- index.ts:472-553 (helper)Core implementation function that performs the auto-optimization logic: analyzes text, detects if optimization is needed, handles interactive questioning, generates optimized prompt using engineerPrompt.async function autoOptimizeText( text: string, context?: string, forceInteractive?: boolean ): Promise<{ shouldOptimize: boolean; optimizedPrompt?: string; analysis: any; needsQuestions?: boolean; questions?: string[]; sessionId?: string; }> { const analysis = shouldAutoOptimize(text); if (!analysis.shouldOptimize) { return { shouldOptimize: false, analysis }; } const detectedInfo = await detectLanguageAndTaskType(text); // Check if we should ask questions (either forced or auto-detected) const shouldAskQuestions = forceInteractive || analysis.needsQuestions; if (shouldAskQuestions) { try { const questions = await generateClarifyingQuestions(text, detectedInfo.language, context); const sessionId = generateSessionId(); // Store session for later use activeSessions.set(sessionId, { originalPrompt: text, context: context ? [context] : [], refinements: [], language: detectedInfo.language, complexity: detectedInfo.complexity as any, taskType: detectedInfo.taskType as any }); return { shouldOptimize: true, analysis: { ...analysis, detectedLanguage: detectedInfo.language, taskType: detectedInfo.taskType, complexity: detectedInfo.complexity }, needsQuestions: true, questions, sessionId }; } catch (error) { // If question generation fails, fall back to direct optimization console.error('Question generation failed, falling back to direct optimization:', error); } } // Direct optimization (no questions needed or questions failed) try { const optimizedPrompt = await engineerPrompt(text, detectedInfo.language, context); return { shouldOptimize: true, optimizedPrompt, analysis: { ...analysis, detectedLanguage: detectedInfo.language, taskType: detectedInfo.taskType, complexity: detectedInfo.complexity, method: 'built-in' } }; } catch (error) { // If optimization fails, still return the analysis return { shouldOptimize: true, analysis: { ...analysis, error: error instanceof Error ? error.message : String(error) } }; } }
- index.ts:97-119 (schema)Tool schema definition for 'auto_optimize', including input schema with parameters text, context, interactive.const AUTO_OPTIMIZE_TOOL: Tool = { name: "auto_optimize", description: "Automatically detects and optimizes natural language text for Claude Code. Use this when the user is writing conversational text that should be translated into an optimized prompt.", inputSchema: { type: "object", properties: { text: { type: "string", description: "The natural language text to analyze and potentially optimize" }, context: { type: "string", description: "Any additional context about the current project or situation" }, interactive: { type: "boolean", description: "Whether to enable interactive questioning if the prompt is unclear or needs more info (default: auto-detect based on complexity)" }, }, required: ["text"], title: "auto_optimizeArguments" } };
- index.ts:570-572 (registration)Registration of available tools, including AUTO_OPTIMIZE_TOOL, in the ListToolsRequest handler.server.setRequestHandler(ListToolsRequestSchema, async () => ({ tools: [ENGINEER_PROMPT_TOOL, ASK_CLARIFICATION_TOOL, ANSWER_QUESTIONS_TOOL, AUTO_OPTIMIZE_TOOL], }));
- index.ts:556-567 (helper)Type guard function to validate arguments for the auto_optimize tool.function isAutoOptimizeArgs(args: unknown): args is { text: string; context?: string; interactive?: boolean; } { return ( typeof args === "object" && args !== null && "text" in args && typeof (args as { text: string }).text === "string" ); }