Skip to main content
Glama

get_source

Retrieve PHP source code from files or specific line ranges for debugging analysis with Xdebug.

Instructions

Get the source code of a file or a specific line range

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
fileYesFile path to get source from
begin_lineNoStarting line number
end_lineNoEnding line number
session_idNoSession ID

Implementation Reference

  • The MCP tool handler for 'get_source'. Resolves the debug session, calls session.getSource to retrieve the source code, handles errors, and returns the result as MCP text content.
    async ({ file, begin_line, end_line, session_id }) => {
      const session = sessionManager.resolveSession(session_id);
    
      if (!session) {
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify({ error: 'No active debug session' }),
            },
          ],
        };
      }
    
      try {
        const source = await session.getSource(file, begin_line, end_line);
    
        if (source === null) {
          return {
            content: [
              {
                type: 'text',
                text: JSON.stringify({
                  error: 'Failed to get source',
                  file,
                  message: 'File not found or not accessible',
                }),
              },
            ],
          };
        }
    
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(
                {
                  file,
                  beginLine: begin_line,
                  endLine: end_line,
                  source,
                },
                null,
                2
              ),
            },
          ],
        };
      } catch (error) {
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify({
                error: 'Failed to get source',
                message: error instanceof Error ? error.message : String(error),
              }),
            },
          ],
        };
      }
    }
  • Zod input schema defining parameters for the get_source tool: file path and optional line range.
    {
      file: z.string().describe('File path to get source from'),
      begin_line: z.number().int().optional().describe('Starting line number'),
      end_line: z.number().int().optional().describe('Ending line number'),
      session_id: z.string().optional().describe('Session ID'),
    },
  • Registration of the 'get_source' tool using server.tool(), including name, description, schema, and handler within the registerInspectionTools function.
    server.tool(
      'get_source',
      'Get the source code of a file or a specific line range',
      {
        file: z.string().describe('File path to get source from'),
        begin_line: z.number().int().optional().describe('Starting line number'),
        end_line: z.number().int().optional().describe('Ending line number'),
        session_id: z.string().optional().describe('Session ID'),
      },
      async ({ file, begin_line, end_line, session_id }) => {
        const session = sessionManager.resolveSession(session_id);
    
        if (!session) {
          return {
            content: [
              {
                type: 'text',
                text: JSON.stringify({ error: 'No active debug session' }),
              },
            ],
          };
        }
    
        try {
          const source = await session.getSource(file, begin_line, end_line);
    
          if (source === null) {
            return {
              content: [
                {
                  type: 'text',
                  text: JSON.stringify({
                    error: 'Failed to get source',
                    file,
                    message: 'File not found or not accessible',
                  }),
                },
              ],
            };
          }
    
          return {
            content: [
              {
                type: 'text',
                text: JSON.stringify(
                  {
                    file,
                    beginLine: begin_line,
                    endLine: end_line,
                    source,
                  },
                  null,
                  2
                ),
              },
            ],
          };
        } catch (error) {
          return {
            content: [
              {
                type: 'text',
                text: JSON.stringify({
                  error: 'Failed to get source',
                  message: error instanceof Error ? error.message : String(error),
                }),
              },
            ],
          };
        }
      }
    );
  • Core helper method in DebugSession that sends the DBGP 'source' command to retrieve file source code, handles base64 decoding if needed, and returns the source string.
    async getSource(
      fileUri: string,
      beginLine?: number,
      endLine?: number
    ): Promise<string | null> {
      const args: Record<string, string> = {
        f: this.normalizeFileUri(fileUri),
      };
    
      if (beginLine !== undefined) args['b'] = beginLine.toString();
      if (endLine !== undefined) args['e'] = endLine.toString();
    
      const response = await this.connection.sendCommand('source', args);
    
      if (response.error) return null;
    
      const data = response.data as Record<string, string>;
      const encoding = data['@_encoding'];
      const content = data['#text'] || '';
    
      if (encoding === 'base64' && content) {
        return Buffer.from(content, 'base64').toString('utf8');
      }
    
      return content;
    }

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