Skip to main content
Glama

capture_request_context

Capture current HTTP request data including GET/POST parameters, session variables, cookies, and headers for debugging PHP applications with Xdebug.

Instructions

Capture the current HTTP request context ($_GET, $_POST, $_SESSION, $_COOKIE, headers)

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
session_idNoSession ID

Implementation Reference

  • Registration of the 'capture_request_context' tool using server.tool(), including inline handler and schema.
    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),
                }),
              },
            ],
          };
        }
      }
    );
  • The inline anonymous handler function that executes the tool logic: resolves session, captures context using RequestContextCapture, formats and returns JSON response.
    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),
              }),
            },
          ],
        };
      }
    }
  • Zod input schema for the tool, defining optional session_id parameter.
    {
      session_id: z.string().optional().describe('Session ID'),
    },
  • Core capture method in RequestContextCapture class that fetches PHP superglobals via debug session and constructs the request context object used by the tool handler.
    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;
    }
  • getSummary helper method that provides a concise summary of the captured context, included in the tool's 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,
      };
    }

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