Skip to main content
Glama

get_source

Retrieve source code from PHP files or specific line ranges to support debugging operations within Xdebug sessions.

Instructions

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

Input Schema

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

Input Schema (JSON Schema)

{ "$schema": "http://json-schema.org/draft-07/schema#", "additionalProperties": false, "properties": { "begin_line": { "description": "Starting line number", "type": "integer" }, "end_line": { "description": "Ending line number", "type": "integer" }, "file": { "description": "File path to get source from", "type": "string" }, "session_id": { "description": "Session ID", "type": "string" } }, "required": [ "file" ], "type": "object" }

Implementation Reference

  • MCP tool handler for 'get_source'. Resolves the debug session and retrieves source code using session.getSource, returning formatted JSON response or error.
    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 (required), begin_line/end_line (optional), session_id (optional).
    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 on the MCP server within registerInspectionTools.
    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), }), }, ], }; } } );
  • DebugSession.getSource method: sends DBGP 'source' command to retrieve source code from the debugger, handles base64 decoding if needed.
    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; }
  • High-level registration call to registerInspectionTools which includes the 'get_source' tool.
    registerInspectionTools(server, ctx.sessionManager);

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