evaluate
Evaluate PHP expressions in debug sessions to inspect variables, call methods, or perform calculations during PHP debugging with Xdebug.
Instructions
Evaluate a PHP expression in the current context. Returns the result of the expression. Use for calculations, method calls, or inspecting computed values.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| expression | Yes | PHP expression to evaluate (e.g., '$x + $y', 'count($array)', '$user->getName()', 'array_keys($data)') | |
| stack_depth | No | Stack frame depth | |
| session_id | No | Session ID |
Implementation Reference
- src/tools/inspection.ts:376-450 (registration)MCP tool registration for 'evaluate', including Zod input schema and async handler that resolves debug session, evaluates PHP expression, formats result with formatProperty helper, and returns structured JSON response or error.server.tool( 'evaluate', "Evaluate a PHP expression in the current context. Returns the result of the expression. Use for calculations, method calls, or inspecting computed values.", { expression: z .string() .describe( "PHP expression to evaluate (e.g., '$x + $y', 'count($array)', '$user->getName()', 'array_keys($data)')" ), stack_depth: z.number().int().default(0).describe('Stack frame depth'), session_id: z.string().optional().describe('Session ID'), }, async ({ expression, stack_depth, session_id }) => { const session = sessionManager.resolveSession(session_id); if (!session) { return { content: [ { type: 'text', text: JSON.stringify({ error: 'No active debug session' }), }, ], }; } try { const result = await session.evaluate(expression, stack_depth); if (!result) { return { content: [ { type: 'text', text: JSON.stringify({ error: 'Evaluation returned no result', expression, }), }, ], }; } return { content: [ { type: 'text', text: JSON.stringify( { expression, result: formatProperty(result), }, null, 2 ), }, ], }; } catch (error) { return { content: [ { type: 'text', text: JSON.stringify({ error: 'Evaluation failed', expression, message: error instanceof Error ? error.message : String(error), }), }, ], }; } } );
- src/tools/inspection.ts:12-31 (handler)Helper function to recursively format DBGP Property objects into a readable JSON structure, limiting depth to 3 to prevent excessive nesting.function formatProperty(prop: Property, depth: number = 0): Record<string, unknown> { const result: Record<string, unknown> = { name: prop.name, type: prop.type, }; if (prop.classname) result.classname = prop.classname; if (prop.value !== undefined) result.value = prop.value; if (prop.numchildren !== undefined && prop.numchildren > 0) { result.numchildren = prop.numchildren; } if (prop.constant) result.constant = true; // Include nested properties if present and not too deep if (prop.properties && prop.properties.length > 0 && depth < 3) { result.children = prop.properties.map((p) => formatProperty(p, depth + 1)); } return result; }
- src/session/session.ts:438-453 (handler)Core DebugSession method implementing PHP expression evaluation via DBGP 'eval' command. Invoked by the MCP 'evaluate' tool handler.async evaluate( expression: string, stackDepth: number = 0 ): Promise<Property | null> { const response = await this.connection.sendCommand( 'eval', { d: stackDepth.toString() }, expression ); if (response.error) { throw new Error(`Evaluation error: ${response.error.message}`); } return this.connection.parseProperty(response); }