Skip to main content
Glama
lin037

MCP Diagnostics

by lin037

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
NameRequiredDescriptionDefault
fileUriYes完整的文件URI,必须使用 file:///workspace/ 开头的格式。示例:file:///workspace/src/index.ts

Implementation Reference

  • 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,
      },
    },
  • 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;
    }
  • 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;
    }
Behavior3/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description carries the full burden of behavioral disclosure. It adds useful context about the URI format requirement and the warning symbol (⚠️), which hints at potential issues. However, it doesn't describe what diagnostic information is returned, error conditions, or performance characteristics.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is perfectly concise and well-structured: two sentences that efficiently convey the purpose, critical requirement, and alternative tool. Every sentence earns its place with no wasted words.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness3/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the tool's moderate complexity (single parameter but with specific format requirements), no annotations, and no output schema, the description is adequate but incomplete. It covers usage and parameter format well but lacks information about what diagnostic information is returned, which is important context for an agent.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 100%, so the schema already fully documents the fileUri parameter. The description reinforces the URI format requirement but doesn't add significant semantic meaning beyond what's in the schema. Baseline 3 is appropriate when schema does the heavy lifting.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the tool's purpose: '获取指定文件的诊断信息' (get diagnostic information for a specified file). It specifies the resource (file) and action (get diagnostics), but doesn't explicitly differentiate from sibling tools like getDiagnostics or getDiagnosticsForPath beyond mentioning the latter as an alternative for uncertain URI formats.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines5/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides explicit guidance on when to use this tool vs. alternatives: it specifies that this tool requires a full workspace URI format and recommends using getDiagnosticsForPath if unsure about the URI format. This clearly defines usage context and exclusions.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

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/lin037/mcp-diagnostics-trae'

If you have feedback or need assistance with the MCP directory API, please join our Discord server