set_variable
Set variable values in PHP debugging sessions to modify program state during runtime analysis and testing.
Instructions
Set the value of a variable in the current scope
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| name | Yes | Variable name (e.g., $x, $user->name) | |
| value | Yes | New value as a PHP literal (e.g., 42, "hello", true, null) | |
| context_id | No | Context ID | |
| 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": {
"context_id": {
"default": 0,
"description": "Context ID",
"type": "integer"
},
"name": {
"description": "Variable name (e.g., $x, $user->name)",
"type": "string"
},
"session_id": {
"description": "Session ID",
"type": "string"
},
"stack_depth": {
"default": 0,
"description": "Stack frame depth",
"type": "integer"
},
"value": {
"description": "New value as a PHP literal (e.g., 42, \"hello\", true, null)",
"type": "string"
}
},
"required": [
"name",
"value"
],
"type": "object"
}
Implementation Reference
- src/tools/inspection.ts:314-373 (handler)MCP tool 'set_variable' handler: registers the tool, defines input schema with zod, and provides the execution logic that resolves the debug session and calls session.setVariable(name, value, options). Returns success status in MCP content format.server.tool( 'set_variable', 'Set the value of a variable in the current scope', { name: z.string().describe('Variable name (e.g., $x, $user->name)'), value: z.string().describe('New value as a PHP literal (e.g., 42, "hello", true, null)'), context_id: z.number().int().default(0).describe('Context ID'), stack_depth: z.number().int().default(0).describe('Stack frame depth'), session_id: z.string().optional().describe('Session ID'), }, async ({ name, value, context_id, 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 success = await session.setVariable(name, value, { contextId: context_id, stackDepth: stack_depth, }); return { content: [ { type: 'text', text: JSON.stringify({ success, name, value, message: success ? `Variable ${name} set to ${value}` : 'Failed to set variable', }), }, ], }; } catch (error) { return { content: [ { type: 'text', text: JSON.stringify({ error: 'Failed to set variable', message: error instanceof Error ? error.message : String(error), }), }, ], }; } } );
- src/session/session.ts:409-434 (helper)Core helper method in DebugSession that implements variable setting by sending the DBGP 'property_set' command with name, value, and optional context/stack options.async setVariable( name: string, value: string, options?: { contextId?: number; stackDepth?: number; type?: string; } ): Promise<boolean> { const args: Record<string, string> = { n: name, }; if (options?.contextId !== undefined) { args['c'] = options.contextId.toString(); } if (options?.stackDepth !== undefined) { args['d'] = options.stackDepth.toString(); } if (options?.type) { args['t'] = options.type; } const response = await this.connection.sendCommand('property_set', args, value); return response.success === true || !response.error; }
- src/tools/index.ts:58-58 (registration)Top-level registration call in tools index that invokes registerInspectionTools, which includes the set_variable tool registration.registerInspectionTools(server, ctx.sessionManager);