evaluate
Evaluate PHP expressions in your debugging session to calculate values, call methods, or inspect computed results during PHP application debugging.
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
| 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 |
Input Schema (JSON Schema)
{
"$schema": "http://json-schema.org/draft-07/schema#",
"additionalProperties": false,
"properties": {
"expression": {
"description": "PHP expression to evaluate (e.g., '$x + $y', 'count($array)', '$user->getName()', 'array_keys($data)')",
"type": "string"
},
"session_id": {
"description": "Session ID",
"type": "string"
},
"stack_depth": {
"default": 0,
"description": "Stack frame depth",
"type": "integer"
}
},
"required": [
"expression"
],
"type": "object"
}
Implementation Reference
- src/tools/inspection.ts:388-448 (handler)Handler function that executes the 'evaluate' tool: resolves the debug session, evaluates the PHP expression using session.evaluate(), formats the result, handles errors, and returns structured JSON response.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:380-387 (schema)Input schema using Zod for the 'evaluate' tool parameters: expression (required PHP code), stack_depth (optional, defaults to 0), session_id (optional).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'), },
- src/tools/inspection.ts:377-449 (registration)Direct registration of the 'evaluate' MCP tool using server.tool(), including name, description, input schema, and handler function.'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/index.ts:58-58 (registration)High-level registration call in tools index that invokes registerInspectionTools, thereby registering the 'evaluate' tool among inspection tools.registerInspectionTools(server, ctx.sessionManager);
- src/tools/inspection.ts:12-31 (helper)Utility function to recursively format debug Property objects for JSON output in tool responses, used by the evaluate handler.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; }