sequential_thinking
Enables AI assistants to break down complex problems into structured, step-by-step processes, revise thinking, and explore alternative reasoning paths effectively.
Instructions
Facilitates a detailed, step-by-step thinking process for problem-solving and analysis
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| branchFromThought | No | Branching point thought number | |
| branchId | No | Branch identifier | |
| isRevision | No | Whether this revises previous thinking | |
| needsMoreThoughts | No | If more thoughts are needed | |
| nextThoughtNeeded | Yes | Whether another thought step is needed | |
| revisesThought | No | Which thought is being reconsidered | |
| thought | Yes | The current thinking step | |
| thoughtNumber | Yes | Current thought number | |
| totalThoughts | Yes | Estimated total thoughts needed |
Implementation Reference
- src/index-backup.ts:103-235 (registration)Registers the 'sequential_thinking' tool with the MCP server, providing description, input schema using Zod, and the handler function.server.tool( "sequential_thinking", "Engage in systematic step-by-step thinking to analyze complex problems, make decisions, or explore ideas. Each thought builds on the previous ones, creating a chain of reasoning.", { thought: z.string().describe("Your current thought or analysis step"), reasoning: z.string().optional().describe("Optional: Explain why this thought follows from previous ones"), category: z.string().optional().describe("Optional: Categorize this thought (analysis, synthesis, evaluation, etc.)"), revise_step: z.number().optional().describe("Optional: Revise a previous step by its number"), branch_from_step: z.number().optional().describe("Optional: Create a new reasoning branch from a specific step number"), target_steps: z.number().optional().describe("Optional: Target number of thinking steps for this session"), complete_thinking: z.boolean().optional().describe("Optional: Mark the thinking session as complete") }, async ({ thought, reasoning, category, revise_step, branch_from_step, target_steps, complete_thinking }) => { try { const timestamp = new Date().toISOString(); // Handle revision of existing step if (revise_step !== undefined) { const stepIndex = thinkingSession.currentSteps.findIndex(step => step.id === revise_step); if (stepIndex !== -1) { thinkingSession.currentSteps[stepIndex] = { ...thinkingSession.currentSteps[stepIndex], thought, reasoning, category, timestamp }; } else { return { content: [{ type: "text", text: `❌ Step ${revise_step} not found. Available steps: ${thinkingSession.currentSteps.map(s => s.id).join(', ')}` }] }; } } // Handle branching from existing step else if (branch_from_step !== undefined) { const branchPoint = thinkingSession.currentSteps.find(step => step.id === branch_from_step); if (!branchPoint) { return { content: [{ type: "text", text: `❌ Cannot branch from step ${branch_from_step}. Step not found.` }] }; } // Create new branch thinkingSession.totalSteps++; const newStep: ThinkingStep = { id: thinkingSession.totalSteps, thought: `[Branch from Step ${branch_from_step}] ${thought}`, reasoning, category, timestamp }; thinkingSession.currentSteps.push(newStep); } // Add new step else { thinkingSession.totalSteps++; const newStep: ThinkingStep = { id: thinkingSession.totalSteps, thought, reasoning, category, timestamp }; thinkingSession.currentSteps.push(newStep); } // Handle completion if (complete_thinking) { thinkingSession.isComplete = true; thinkingSession.summary = `Sequential thinking completed with ${thinkingSession.currentSteps.length} steps.`; } // Update target if provided if (target_steps) { thinkingSession.metadata = { ...thinkingSession.metadata, target_steps }; } // Auto-save state await saveThinkingState(); // Generate progress report const progress = target_steps ? ` (${thinkingSession.currentSteps.length}/${target_steps})` : ''; const recentSteps = thinkingSession.currentSteps.slice(-3); let content = `🧠 **Sequential Thinking${progress}**\n\n`; if (revise_step !== undefined) { content += `📝 **Step ${revise_step} Revised**\n\n`; } else if (branch_from_step !== undefined) { content += `🌿 **New Branch from Step ${branch_from_step}**\n\n`; } else { content += `💭 **Step ${thinkingSession.totalSteps} Added**\n\n`; } content += "**Recent Thinking Chain:**\n"; recentSteps.forEach(step => { const categoryLabel = step.category ? ` [${step.category}]` : ''; content += `**${step.id}.${categoryLabel}** ${step.thought}\n`; if (step.reasoning) { content += ` *Reasoning: ${step.reasoning}*\n`; } content += '\n'; }); if (thinkingSession.isComplete) { content += `✅ **Thinking Complete**: ${thinkingSession.summary}\n\n`; } content += `*Continue with next thought or use 'get_thinking_summary' to review all steps*`; return { content: [{ type: "text", text: content }] }; } catch (error) { return { content: [{ type: "text", text: `❌ **Error in sequential thinking**: ${error instanceof Error ? error.message : String(error)}` }] }; } } );
- src/index-backup.ts:115-234 (handler)The core handler function that processes input parameters, manages thinking session state (add/revise/branch steps), persists state to JSON file, generates formatted progress reports, and handles errors.async ({ thought, reasoning, category, revise_step, branch_from_step, target_steps, complete_thinking }) => { try { const timestamp = new Date().toISOString(); // Handle revision of existing step if (revise_step !== undefined) { const stepIndex = thinkingSession.currentSteps.findIndex(step => step.id === revise_step); if (stepIndex !== -1) { thinkingSession.currentSteps[stepIndex] = { ...thinkingSession.currentSteps[stepIndex], thought, reasoning, category, timestamp }; } else { return { content: [{ type: "text", text: `❌ Step ${revise_step} not found. Available steps: ${thinkingSession.currentSteps.map(s => s.id).join(', ')}` }] }; } } // Handle branching from existing step else if (branch_from_step !== undefined) { const branchPoint = thinkingSession.currentSteps.find(step => step.id === branch_from_step); if (!branchPoint) { return { content: [{ type: "text", text: `❌ Cannot branch from step ${branch_from_step}. Step not found.` }] }; } // Create new branch thinkingSession.totalSteps++; const newStep: ThinkingStep = { id: thinkingSession.totalSteps, thought: `[Branch from Step ${branch_from_step}] ${thought}`, reasoning, category, timestamp }; thinkingSession.currentSteps.push(newStep); } // Add new step else { thinkingSession.totalSteps++; const newStep: ThinkingStep = { id: thinkingSession.totalSteps, thought, reasoning, category, timestamp }; thinkingSession.currentSteps.push(newStep); } // Handle completion if (complete_thinking) { thinkingSession.isComplete = true; thinkingSession.summary = `Sequential thinking completed with ${thinkingSession.currentSteps.length} steps.`; } // Update target if provided if (target_steps) { thinkingSession.metadata = { ...thinkingSession.metadata, target_steps }; } // Auto-save state await saveThinkingState(); // Generate progress report const progress = target_steps ? ` (${thinkingSession.currentSteps.length}/${target_steps})` : ''; const recentSteps = thinkingSession.currentSteps.slice(-3); let content = `🧠 **Sequential Thinking${progress}**\n\n`; if (revise_step !== undefined) { content += `📝 **Step ${revise_step} Revised**\n\n`; } else if (branch_from_step !== undefined) { content += `🌿 **New Branch from Step ${branch_from_step}**\n\n`; } else { content += `💭 **Step ${thinkingSession.totalSteps} Added**\n\n`; } content += "**Recent Thinking Chain:**\n"; recentSteps.forEach(step => { const categoryLabel = step.category ? ` [${step.category}]` : ''; content += `**${step.id}.${categoryLabel}** ${step.thought}\n`; if (step.reasoning) { content += ` *Reasoning: ${step.reasoning}*\n`; } content += '\n'; }); if (thinkingSession.isComplete) { content += `✅ **Thinking Complete**: ${thinkingSession.summary}\n\n`; } content += `*Continue with next thought or use 'get_thinking_summary' to review all steps*`; return { content: [{ type: "text", text: content }] }; } catch (error) { return { content: [{ type: "text", text: `❌ **Error in sequential thinking**: ${error instanceof Error ? error.message : String(error)}` }] }; } }
- src/index-backup.ts:107-113 (schema)Zod schema defining the input parameters for the sequential_thinking tool.thought: z.string().describe("Your current thought or analysis step"), reasoning: z.string().optional().describe("Optional: Explain why this thought follows from previous ones"), category: z.string().optional().describe("Optional: Categorize this thought (analysis, synthesis, evaluation, etc.)"), revise_step: z.number().optional().describe("Optional: Revise a previous step by its number"), branch_from_step: z.number().optional().describe("Optional: Create a new reasoning branch from a specific step number"), target_steps: z.number().optional().describe("Optional: Target number of thinking steps for this session"), complete_thinking: z.boolean().optional().describe("Optional: Mark the thinking session as complete")
- src/index-backup.ts:77-83 (helper)Helper functions for persisting and loading the thinking session state to/from a JSON file in the user's home directory.async function saveThinkingState() { try { await fs.writeFile(STORAGE_FILE, JSON.stringify(thinkingSession, null, 2)); } catch (error) { console.error("⚠️ Failed to save thinking state:", error); } }
- src/index-backup.ts:37-56 (helper)TypeScript interfaces defining the structure of ThinkingStep and ThinkingSession used throughout the tool implementation.interface ThinkingStep { id: number; thought: string; reasoning?: string; timestamp: string; category?: string; } interface ThinkingSession { currentSteps: ThinkingStep[]; totalSteps: number; isComplete: boolean; summary?: string; metadata?: Record<string, any>; pdfContext?: { filename?: string; content?: string; metadata?: Record<string, any>; }; }