get_diagnostics
Retrieve detailed diagnostic information for a document, including error and warning analysis, by providing the language identifier, file path, content, and project root for accurate code assessment.
Instructions
Get diagnostic information for a document
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| content | Yes | The current content of the file | |
| filePath | Yes | Absolute or relative path to the source file | |
| languageId | Yes | The language identifier (e.g., "typescript", "javascript") | |
| projectRoot | Yes | Important: Root directory of the project for resolving imports and node_modules where the tsconfig.json or jsconfig.json is located |
Implementation Reference
- src/index.ts:546-615 (handler)The handler function for the 'get_diagnostics' tool. It initializes an LSP server for the given language, constructs a TextDocumentItem from the provided file path and content, sets up a listener for diagnostics notifications, opens the document to trigger diagnostics, and resolves with the diagnostics or a timeout message.private async handleGetDiagnostics(args: any): Promise<any> { const { languageId, filePath, content, projectRoot } = args; console.log(`[handleGetDiagnostics] Processing request for ${languageId}`); const server = await this.getOrCreateServer(languageId, projectRoot); const actualRoot = server.workspaceRoot; const absolutePath = isAbsolute(filePath) ? filePath : join(actualRoot, filePath); const uri = `file://${absolutePath}`; // Ensure directory exists const fileDir = dirname(absolutePath); if (!existsSync(fileDir)) { mkdirSync(fileDir, { recursive: true }); } const textDocument: TextDocumentItem = { uri, languageId, version: 1, text: content, }; console.log(`[handleGetDiagnostics] Setting up diagnostics listener for ${uri}`); return new Promise((resolve) => { const listeners = this.diagnosticsListeners.get(uri) || []; const listener = (params: PublishDiagnosticsParams) => { console.log(`[handleGetDiagnostics] Received diagnostics for ${uri}:`, params); resolve({ content: [ { type: 'text', text: JSON.stringify(params.diagnostics, null, 2), }, ], }); // Remove listener after receiving diagnostics const index = listeners.indexOf(listener); if (index !== -1) { listeners.splice(index, 1); } }; listeners.push(listener); this.diagnosticsListeners.set(uri, listeners); // Send document to trigger diagnostics console.log(`[handleGetDiagnostics] Sending document to server:`, textDocument); server.connection.sendNotification('textDocument/didOpen', { textDocument, } as DidOpenTextDocumentParams); // Set timeout setTimeout(() => { console.log(`[handleGetDiagnostics] Timeout reached for ${uri}`); const index = listeners.indexOf(listener); if (index !== -1) { listeners.splice(index, 1); resolve({ content: [ { type: 'text', text: 'No diagnostics received within timeout', }, ], }); } }, 2000); }); }
- src/index.ts:360-384 (registration)Registers the 'get_diagnostics' tool in the MCP server tools list, including its name, description, and input schema.name: 'get_diagnostics', description: 'Get diagnostic information for a document', inputSchema: { type: 'object', properties: { languageId: { type: 'string', description: 'The language identifier (e.g., "typescript", "javascript")' }, filePath: { type: 'string', description: 'Absolute or relative path to the source file' }, content: { type: 'string', description: 'The current content of the file' }, projectRoot: { type: 'string', description: 'Important: Root directory of the project for resolving imports and node_modules where the tsconfig.json or jsconfig.json is located' }, }, required: ['languageId', 'filePath', 'content', 'projectRoot'], }, },
- src/index.ts:362-383 (schema)Defines the input schema for the 'get_diagnostics' tool, specifying required parameters: languageId, filePath, content, projectRoot.inputSchema: { type: 'object', properties: { languageId: { type: 'string', description: 'The language identifier (e.g., "typescript", "javascript")' }, filePath: { type: 'string', description: 'Absolute or relative path to the source file' }, content: { type: 'string', description: 'The current content of the file' }, projectRoot: { type: 'string', description: 'Important: Root directory of the project for resolving imports and node_modules where the tsconfig.json or jsconfig.json is located' }, }, required: ['languageId', 'filePath', 'content', 'projectRoot'], },