capture_request_context
Capture current HTTP request data including GET, POST, session, cookies, and headers for PHP debugging with Xdebug.
Instructions
Capture the current HTTP request context ($_GET, $_POST, $_SESSION, $_COOKIE, headers)
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| session_id | No | Session ID |
Input Schema (JSON Schema)
{
"$schema": "http://json-schema.org/draft-07/schema#",
"additionalProperties": false,
"properties": {
"session_id": {
"description": "Session ID",
"type": "string"
}
},
"type": "object"
}
Implementation Reference
- src/tools/advanced.ts:314-360 (handler)The inline handler function for the 'capture_request_context' tool. Resolves the debug session, captures the request context using RequestContextCapture instance, generates a summary, and returns formatted JSON response with superglobals and headers.async ({ session_id }) => { const session = ctx.sessionManager.resolveSession(session_id); if (!session) { return { content: [{ type: 'text', text: JSON.stringify({ error: 'No active session' }) }], }; } try { const context = await ctx.requestCapture.capture(session); const summary = ctx.requestCapture.getSummary(context); return { content: [ { type: 'text', text: JSON.stringify( { summary, headers: context.headers, get: context.get, post: context.post, cookies: context.cookie, session: context.session, requestBody: context.requestBody, }, null, 2 ), }, ], }; } catch (error) { return { content: [ { type: 'text', text: JSON.stringify({ error: 'Failed to capture request context', message: error instanceof Error ? error.message : String(error), }), }, ], }; } } );
- Core implementation of request context capture. Fetches PHP superglobals ($_GET, $_POST, $_SESSION, $_COOKIE, $_SERVER, $_FILES) using DBGP session.getVariable, extracts HTTP method/URI/headers from $_SERVER, and attempts to capture raw request body via php://input.async capture(session: DebugSession): Promise<RequestContext> { const context: RequestContext = { capturedAt: new Date(), get: {}, post: {}, session: {}, cookie: {}, server: {}, files: {}, headers: {}, }; // Capture superglobals const superglobals = [ { name: '$_GET', target: 'get' }, { name: '$_POST', target: 'post' }, { name: '$_SESSION', target: 'session' }, { name: '$_COOKIE', target: 'cookie' }, { name: '$_SERVER', target: 'server' }, { name: '$_FILES', target: 'files' }, ]; for (const { name, target } of superglobals) { try { const result = await session.getVariable(name, { contextId: 1 }); // 1 = superglobals if (result) { const obj = this.propertyToObject(result); if (target === 'get') context.get = obj; else if (target === 'post') context.post = obj; else if (target === 'session') context.session = obj; else if (target === 'cookie') context.cookie = obj; else if (target === 'server') context.server = obj; else if (target === 'files') context.files = obj; } } catch { logger.debug(`Failed to capture ${name}`); } } // Extract useful server info if (context.server) { const server = context.server as Record<string, string>; context.method = server['REQUEST_METHOD']; context.uri = server['REQUEST_URI']; // Extract headers from $_SERVER for (const [key, value] of Object.entries(server)) { if (key.startsWith('HTTP_')) { const headerName = key .slice(5) .toLowerCase() .replace(/_/g, '-') .replace(/\b\w/g, (c) => c.toUpperCase()); context.headers[headerName] = String(value); } } if (server['CONTENT_TYPE']) { context.headers['Content-Type'] = server['CONTENT_TYPE']; } if (server['CONTENT_LENGTH']) { context.headers['Content-Length'] = server['CONTENT_LENGTH']; } } // Try to capture raw request body try { const bodyResult = await session.evaluate('file_get_contents("php://input")'); if (bodyResult?.value) { context.requestBody = bodyResult.value; } } catch { // Request body may not be available } this.lastContext = context; return context; }
- src/session/request-context.ts:10-22 (schema)TypeScript interface defining the structure of the captured HTTP request context data.export interface RequestContext { capturedAt: Date; method?: string; uri?: string; get: Record<string, unknown>; post: Record<string, unknown>; session: Record<string, unknown>; cookie: Record<string, unknown>; server: Record<string, unknown>; files: Record<string, unknown>; headers: Record<string, string>; requestBody?: string; }
- src/tools/advanced.ts:308-360 (registration)MCP tool registration call that defines the tool name, description, input schema, and references the handler function.server.tool( 'capture_request_context', 'Capture the current HTTP request context ($_GET, $_POST, $_SESSION, $_COOKIE, headers)', { session_id: z.string().optional().describe('Session ID'), }, async ({ session_id }) => { const session = ctx.sessionManager.resolveSession(session_id); if (!session) { return { content: [{ type: 'text', text: JSON.stringify({ error: 'No active session' }) }], }; } try { const context = await ctx.requestCapture.capture(session); const summary = ctx.requestCapture.getSummary(context); return { content: [ { type: 'text', text: JSON.stringify( { summary, headers: context.headers, get: context.get, post: context.post, cookies: context.cookie, session: context.session, requestBody: context.requestBody, }, null, 2 ), }, ], }; } catch (error) { return { content: [ { type: 'text', text: JSON.stringify({ error: 'Failed to capture request context', message: error instanceof Error ? error.message : String(error), }), }, ], }; } } );
- Helper method that generates a concise summary of the captured request context, used in the tool response.getSummary(context: RequestContext): { method: string; uri: string; hasGet: boolean; hasPost: boolean; hasSession: boolean; hasCookies: boolean; hasFiles: boolean; headerCount: number; } { return { method: context.method || 'N/A', uri: context.uri || 'N/A', hasGet: Object.keys(context.get).length > 0, hasPost: Object.keys(context.post).length > 0, hasSession: Object.keys(context.session).length > 0, hasCookies: Object.keys(context.cookie).length > 0, hasFiles: Object.keys(context.files).length > 0, headerCount: Object.keys(context.headers).length, }; }