getDiagnosticsForFile
Retrieve diagnostic information for a specific file to identify errors, warnings, and hints for code analysis and improvement.
Instructions
获取指定文件的诊断信息。⚠️ 注意:需要使用完整的workspace URI格式,如 "file:///workspace/src/index.ts"。如果不确定URI格式,建议使用 getDiagnosticsForPath 工具。
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| fileUri | Yes | 完整的文件URI,必须使用 file:///workspace/ 开头的格式。示例:file:///workspace/src/index.ts |
Implementation Reference
- src/index.ts:151-181 (handler)Main MCP tool handler for 'getDiagnosticsForFile': validates input, fetches diagnostics via client, handles empty results, formats JSON response, and manages errors.private async handleGetDiagnosticsForFile(args: { fileUri: string }) { if (!args || !args.fileUri) { throw new McpError(ErrorCode.InvalidParams, '缺少必需参数: fileUri'); } try { const diagnostics = await this.diagnosticsClient.getDiagnosticsForFile(args.fileUri); if (!diagnostics || diagnostics.length === 0) { return { content: [ { type: 'text', text: '未找到指定 URI 的文件或该文件没有诊断信息。', }, ], }; } return { content: [ { type: 'text', text: JSON.stringify(diagnostics, null, 2), }, ], }; } catch (error) { throw new McpError( ErrorCode.InternalError, `[handleGetDiagnosticsForFile] ${error instanceof Error ? error.message : String(error)}` ); } }
- src/index.ts:56-69 (registration)Registration of the 'getDiagnosticsForFile' tool in the listTools response, including name, description, and input schema definition.name: 'getDiagnosticsForFile', description: '获取指定文件的诊断信息。⚠️ 注意:需要使用完整的workspace URI格式,如 "file:///workspace/src/index.ts"。如果不确定URI格式,建议使用 getDiagnosticsForPath 工具。', inputSchema: { type: 'object', properties: { fileUri: { type: 'string', description: '完整的文件URI,必须使用 file:///workspace/ 开头的格式。示例:file:///workspace/src/index.ts', }, }, required: ['fileUri'], additionalProperties: false, }, },
- src/vscode.ts:73-91 (helper)Core helper function in VSCodeDiagnosticsClient that implements file-specific diagnostics retrieval by fetching all diagnostics and filtering/matching by normalized URI.async getDiagnosticsForFile(fileUri: string): Promise<FileDiagnostic[]> { const allDiagnostics = await this.getDiagnostics(); // 标准化文件URI,支持多种输入格式 const normalizedInputUri = this.normalizeFileUri(fileUri); // 过滤匹配的文件 const matchedFiles = allDiagnostics.filter(d => { const normalizedDiagnosticUri = this.normalizeFileUri(d.uri); return normalizedDiagnosticUri === normalizedInputUri || d.uri === fileUri || d.uri.endsWith(fileUri) || normalizedDiagnosticUri.endsWith(normalizedInputUri); }); console.error(`查找文件诊断: 输入=${fileUri}, 标准化=${normalizedInputUri}, 找到=${matchedFiles.length}个匹配`); return matchedFiles; }
- src/vscode.ts:1-25 (schema)TypeScript interfaces defining the structure of diagnostics data: Diagnostic, FileDiagnostic, and DiagnosticSummary used by the tool./** * VS Code 诊断信息接口 */ export interface Diagnostic { range: { start: { line: number; character: number }; end: { line: number; character: number }; }; severity: number; // 1=Error, 2=Warning, 3=Info, 4=Hint source: string; message: string; code?: string | number; } export interface FileDiagnostic { uri: string; diagnostics: Diagnostic[]; } export interface DiagnosticSummary { totalFiles: number; errors: number; warnings: number; }