mcp-reasoner
Enhance decision-making processes by applying advanced reasoning strategies like Beam Search and Monte Carlo Tree Search to systematically evaluate and optimize outcomes.
Instructions
Advanced reasoning tool with multiple strategies including Beam Search and Monte Carlo Tree Search
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| beamWidth | No | Number of top paths to maintain (n-sampling). Defaults to 3 if not specified | |
| nextThoughtNeeded | Yes | Whether another step is needed | |
| numSimulations | No | Number of MCTS simulations to run. Defaults to 50 if not specified | |
| strategyType | No | Reasoning strategy to use (beam_search or mcts) | |
| thought | Yes | Current reasoning step | |
| thoughtNumber | Yes | Current step number | |
| totalThoughts | Yes | Total expected steps |
Implementation Reference
- src/index.ts:106-171 (handler)MCP CallToolRequest handler specifically for the 'mcp-reasoner' tool. Validates input using processInput, calls reasoner.processThought, retrieves stats, and returns JSON response.server.setRequestHandler(CallToolRequestSchema, async (request) => { if (request.params.name !== "mcp-reasoner") { return { content: [{ type: "text", text: JSON.stringify({ error: "Unknown tool", success: false }) }], isError: true }; } try { // Process and validate input const step = processInput(request.params.arguments); // Process thought with selected strategy const response = await reasoner.processThought({ thought: step.thought, thoughtNumber: step.thoughtNumber, totalThoughts: step.totalThoughts, nextThoughtNeeded: step.nextThoughtNeeded, strategyType: step.strategyType, beamWidth: step.beamWidth, numSimulations: step.numSimulations }); // Get reasoning stats const stats = await reasoner.getStats(); // Return enhanced response const result = { thoughtNumber: step.thoughtNumber, totalThoughts: step.totalThoughts, nextThoughtNeeded: step.nextThoughtNeeded, thought: step.thought, nodeId: response.nodeId, score: response.score, strategyUsed: response.strategyUsed, stats: { totalNodes: stats.totalNodes, averageScore: stats.averageScore, maxDepth: stats.maxDepth, branchingFactor: stats.branchingFactor, strategyMetrics: stats.strategyMetrics } }; return { content: [{ type: "text", text: JSON.stringify(result) }] }; } catch (error) { return { content: [{ type: "text", text: JSON.stringify({ error: error instanceof Error ? error.message : String(error), success: false }) }], isError: true }; } });
- src/index.ts:57-103 (registration)Registers the 'mcp-reasoner' tool in response to ListToolsRequestSchema, including full input schema definition.server.setRequestHandler(ListToolsRequestSchema, async () => ({ tools: [{ name: "mcp-reasoner", description: "Advanced reasoning tool with multiple strategies including Beam Search and Monte Carlo Tree Search", inputSchema: { type: "object", properties: { thought: { type: "string", description: "Current reasoning step" }, thoughtNumber: { type: "integer", description: "Current step number", minimum: 1 }, totalThoughts: { type: "integer", description: "Total expected steps", minimum: 1 }, nextThoughtNeeded: { type: "boolean", description: "Whether another step is needed" }, strategyType: { type: "string", enum: Object.values(ReasoningStrategy), description: "Reasoning strategy to use (beam_search or mcts)" }, beamWidth: { type: "integer", description: "Number of top paths to maintain (n-sampling). Defaults to 3 if not specified", minimum: 1, maximum: 10 }, numSimulations: { type: "integer", description: "Number of MCTS simulations to run. Defaults to 50 if not specified", minimum: 1, maximum: 150 } }, required: ["thought", "thoughtNumber", "totalThoughts", "nextThoughtNeeded"] } }] }));
- src/reasoner.ts:43-76 (handler)Core handler logic in Reasoner class: selects or switches reasoning strategy based on input and delegates to strategy.processThought.public async processThought(request: ReasoningRequest): Promise<ReasoningResponse> { // Switch strategy if requested if (request.strategyType && this.strategies.has(request.strategyType as ReasoningStrategy)) { const strategyType = request.strategyType as ReasoningStrategy; // Create new strategy instance with appropriate parameters if (strategyType === ReasoningStrategy.BEAM_SEARCH) { this.currentStrategy = StrategyFactory.createStrategy( strategyType, this.stateManager, request.beamWidth ); } else { // All MCTS variants (base and experimental) use numSimulations this.currentStrategy = StrategyFactory.createStrategy( strategyType, this.stateManager, undefined, request.numSimulations ); } // Update strategy in map this.strategies.set(strategyType, this.currentStrategy); } // Process thought using current strategy const response = await this.currentStrategy.processThought(request); // Add strategy information to response return { ...response, strategyUsed: this.getCurrentStrategyName() }; }
- src/reasoner.ts:11-41 (helper)Reasoner constructor initializes StateManager and all available reasoning strategies (Beam Search, MCTS variants) using StrategyFactory.constructor() { this.stateManager = new StateManager(CONFIG.cacheSize); // Initialize available strategies this.strategies = new Map(); // Initialize base strategies this.strategies.set( ReasoningStrategy.BEAM_SEARCH, StrategyFactory.createStrategy(ReasoningStrategy.BEAM_SEARCH, this.stateManager, CONFIG.beamWidth) ); this.strategies.set( ReasoningStrategy.MCTS, StrategyFactory.createStrategy(ReasoningStrategy.MCTS, this.stateManager, undefined, CONFIG.numSimulations) ); // Initialize experimental MCTS strategies this.strategies.set( ReasoningStrategy.MCTS_002_ALPHA, StrategyFactory.createStrategy(ReasoningStrategy.MCTS_002_ALPHA, this.stateManager, undefined, CONFIG.numSimulations) ); this.strategies.set( ReasoningStrategy.MCTS_002_ALT_ALPHA, StrategyFactory.createStrategy(ReasoningStrategy.MCTS_002_ALT_ALPHA, this.stateManager, undefined, CONFIG.numSimulations) ); // Set default strategy const defaultStrategy = CONFIG.defaultStrategy as ReasoningStrategy; this.currentStrategy = this.strategies.get(defaultStrategy) || this.strategies.get(ReasoningStrategy.BEAM_SEARCH)!; }
- src/strategies/factory.ts:15-36 (helper)StrategyFactory creates specific strategy instances (BeamSearchStrategy, MonteCarloTreeSearchStrategy, etc.) based on ReasoningStrategy enum.export class StrategyFactory { static createStrategy( type: ReasoningStrategy, stateManager: StateManager, beamWidth?: number, numSimulations?: number ): BaseStrategy { switch (type) { case ReasoningStrategy.BEAM_SEARCH: return new BeamSearchStrategy(stateManager, beamWidth); case ReasoningStrategy.MCTS: return new MonteCarloTreeSearchStrategy(stateManager, numSimulations); case ReasoningStrategy.MCTS_002_ALPHA: return new MCTS002AlphaStrategy(stateManager, numSimulations); case ReasoningStrategy.MCTS_002_ALT_ALPHA: return new MCTS002AltAlphaStrategy(stateManager, numSimulations); default: throw new Error(`Unknown strategy type: ${type}`); } } }