Skip to main content
Glama

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

NameRequiredDescriptionDefault
expressionYesPHP expression to evaluate (e.g., '$x + $y', 'count($array)', '$user->getName()', 'array_keys($data)')
stack_depthNoStack frame depth
session_idNoSession 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

  • 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), }), }, ], }; } }
  • 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'), },
  • 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), }), }, ], }; } } );
  • High-level registration call in tools index that invokes registerInspectionTools, thereby registering the 'evaluate' tool among inspection tools.
    registerInspectionTools(server, ctx.sessionManager);
  • 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; }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/kpanuragh/xdebug-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server