evaluate_expression
Evaluate expressions within the Node.js debugging context to inspect variables, test code logic, and analyze program state during runtime.
Instructions
Evaluate an expression in the current debug context
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| expression | Yes | Expression to evaluate |
Implementation Reference
- src/index.ts:650-721 (handler)The core handler function for the 'evaluate_expression' tool. It evaluates a JavaScript expression in the debugged Node.js process using the Chrome DevTools Protocol (CDP). Prefers evaluating on the current call frame if paused, otherwise uses runtime evaluation. Handles errors and formats the result as text.private async evaluateExpression(args: { expression: string }) { if (!this.debugSession.connected || !this.debugSession.client) { return { content: [ { type: "text", text: "No active debug session. Please attach debugger first.", }, ], isError: true, }; } try { const { Runtime, Debugger } = this.debugSession.client; let result; // If we're paused and have a call stack, evaluate in the current call frame if (this.debugSession.isPaused && this.debugSession.callStack && this.debugSession.callStack.length > 0) { const currentFrame = this.debugSession.callStack[0]; result = await Debugger.evaluateOnCallFrame({ callFrameId: currentFrame.callFrameId, expression: args.expression, returnByValue: true }); } else { // Otherwise, evaluate in the runtime context result = await Runtime.evaluate({ expression: args.expression, contextId: this.debugSession.currentExecutionContext, includeCommandLineAPI: true, returnByValue: true }); } if (result.exceptionDetails) { return { content: [ { type: "text", text: `Exception: ${result.exceptionDetails.exception?.description || 'Unknown error'}`, }, ], isError: true, }; } const value = result.result.value !== undefined ? result.result.value : result.result.description || '[Object]'; return { content: [ { type: "text", text: `${args.expression} = ${JSON.stringify(value, null, 2)}`, }, ], }; } catch (error) { return { content: [ { type: "text", text: `Error evaluating expression: ${error}`, }, ], isError: true, }; } }
- src/index.ts:227-236 (schema)The JSON schema definition for the 'evaluate_expression' tool, registered in the ListTools response. Defines the required 'expression' string parameter.name: "evaluate_expression", description: "Evaluate an expression in the current debug context", inputSchema: { type: "object", properties: { expression: { type: "string", description: "Expression to evaluate" } }, required: ["expression"], }, },
- src/index.ts:265-267 (registration)Tool registration/dispatch in the main CallToolRequestSchema switch statement. Maps the tool name to the evaluateExpression handler method.case "evaluate_expression": return await this.evaluateExpression(args as { expression: string });