getDiagnosticsForFile
Retrieve diagnostic information (errors, warnings, hints) for a specific file using its full workspace URI in Trae IDE. Ensure accurate file identification for AI-driven code analysis and suggestions.
Instructions
获取指定文件的诊断信息。⚠️ 注意:需要使用完整的workspace URI格式,如 "file:///workspace/src/index.ts"。如果不确定URI格式,建议使用 getDiagnosticsForPath 工具。
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| fileUri | Yes | 完整的文件URI,必须使用 file:///workspace/ 开头的格式。示例:file:///workspace/src/index.ts |
Input Schema (JSON Schema)
{
"additionalProperties": false,
"properties": {
"fileUri": {
"description": "完整的文件URI,必须使用 file:///workspace/ 开头的格式。示例:file:///workspace/src/index.ts",
"type": "string"
}
},
"required": [
"fileUri"
],
"type": "object"
}
Implementation Reference
- src/index.ts:151-181 (handler)MCP tool handler for getDiagnosticsForFile: validates input, calls VSCodeDiagnosticsClient.getDiagnosticsForFile, handles empty results and errors, returns JSON-formatted diagnostics.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:58-68 (schema)Input schema defining the required 'fileUri' parameter for the getDiagnosticsForFile tool.inputSchema: { type: 'object', properties: { fileUri: { type: 'string', description: '完整的文件URI,必须使用 file:///workspace/ 开头的格式。示例:file:///workspace/src/index.ts', }, }, required: ['fileUri'], additionalProperties: false, },
- src/index.ts:55-69 (registration)Registration of the getDiagnosticsForFile tool in the ListToolsRequestSchema handler, including name, description, and schema.{ 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 normalizing URIs and filtering from all diagnostics.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:15-18 (schema)Type definition for FileDiagnostic, used as output structure for getDiagnosticsForFile.export interface FileDiagnostic { uri: string; diagnostics: Diagnostic[]; }