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
| Name | Required | Description | Default |
|---|---|---|---|
| file | Yes | File path to get source from | |
| begin_line | No | Starting line number | |
| end_line | No | Ending line number | |
| session_id | No | Session 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
- src/tools/inspection.ts:461-523 (handler)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), }), }, ], }; } }
- src/tools/inspection.ts:456-460 (schema)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'), },
- src/tools/inspection.ts:452-524 (registration)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), }), }, ], }; } } );
- src/session/session.ts:457-482 (helper)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; }
- src/tools/index.ts:58-58 (registration)High-level registration call to registerInspectionTools which includes the 'get_source' tool.registerInspectionTools(server, ctx.sessionManager);