Skip to main content
Glama
lin037

MCP Diagnostics

by lin037

getDiagnosticsForPath

Retrieve diagnostic information for a specific file path, supporting flexible path formats including relative paths and filenames to identify code issues.

Instructions

🌟 推荐工具:根据文件路径获取诊断信息,支持灵活的路径匹配。可以使用相对路径、文件名等多种格式,比 getDiagnosticsForFile 更易用。

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
filePathYes文件路径,支持多种格式: - 相对路径:src/index.ts - 文件名:index.ts - 带目录的文件名:test/TestJava.java 示例:要查看 index.ts 文件的问题,使用 "src/index.ts" 或 "index.ts"

Implementation Reference

  • Core handler implementing the getDiagnosticsForPath tool logic: fetches all diagnostics and applies flexible path matching (exact, suffix, filename, contains) to filter relevant file diagnostics.
    async getDiagnosticsForPath(filePath: string): Promise<FileDiagnostic[]> {
      const allDiagnostics = await this.getDiagnostics();
      
      // 标准化输入路径,统一使用正斜杠进行逻辑处理
      const normalizedPath = filePath.replace(/\\/g, '/').toLowerCase();
      
      // 多种匹配策略
      const matchedFiles = allDiagnostics.filter(d => {
        const uri = d.uri.toLowerCase();
        const cleanUri = uri.replace(/^file:\/\/\//, '').replace(/^file:\/\//, '').replace(/\\/g, '/');
        
        // 1. 精确匹配 (处理转换后的路径)
        if (cleanUri === normalizedPath) {
          return true;
        }
        
        // 2. 路径结尾匹配
        if (cleanUri.endsWith('/' + normalizedPath) || cleanUri.endsWith(normalizedPath)) {
          return true;
        }
        
        // 3. 文件名匹配
        const fileName = normalizedPath.split('/').pop() || normalizedPath;
        if (uri.endsWith('/' + fileName)) {
          return true;
        }
        
        // 4. 包含路径匹配
        if (cleanUri.includes(normalizedPath)) {
          return true;
        }
        
        return false;
      });
      
      console.error(`路径匹配结果: 输入=${filePath}, 找到=${matchedFiles.length}个匹配`);
      
      return matchedFiles;
    }
  • MCP server handler for getDiagnosticsForPath tool: validates input, delegates to VSCodeDiagnosticsClient, handles empty results and errors, formats response as MCP content.
    private async handleGetDiagnosticsForPath(args: { filePath: string }) {
      if (!args || !args.filePath) {
        throw new McpError(ErrorCode.InvalidParams, '缺少必需参数: filePath');
      }
      try {
        const diagnostics = await this.diagnosticsClient.getDiagnosticsForPath(args.filePath);
        if (!diagnostics || diagnostics.length === 0) {
          return {
            content: [
              {
                type: 'text',
                text: '未找到匹配该路径的文件。',
              },
            ],
          };
        }
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(diagnostics, null, 2),
            },
          ],
        };
      } catch (error) {
        throw new McpError(
          ErrorCode.InternalError,
          `[handleGetDiagnosticsForPath] ${error instanceof Error ? error.message : String(error)}`
        );
      }
  • src/index.ts:70-84 (registration)
    Registers the getDiagnosticsForPath tool in the MCP listTools response, providing name, description, and input schema.
    {
      name: 'getDiagnosticsForPath',
      description: '🌟 推荐工具:根据文件路径获取诊断信息,支持灵活的路径匹配。可以使用相对路径、文件名等多种格式,比 getDiagnosticsForFile 更易用。',
      inputSchema: {
        type: 'object',
        properties: {
          filePath: {
            type: 'string',
            description: '文件路径,支持多种格式:\n- 相对路径:src/index.ts\n- 文件名:index.ts\n- 带目录的文件名:test/TestJava.java\n示例:要查看 index.ts 文件的问题,使用 "src/index.ts" 或 "index.ts"',
          },
        },
        required: ['filePath'],
        additionalProperties: false,
      },
    },
  • Defines the input schema for the getDiagnosticsForPath tool, specifying the required 'filePath' string parameter.
    inputSchema: {
      type: 'object',
      properties: {
        filePath: {
          type: 'string',
          description: '文件路径,支持多种格式:\n- 相对路径:src/index.ts\n- 文件名:index.ts\n- 带目录的文件名:test/TestJava.java\n示例:要查看 index.ts 文件的问题,使用 "src/index.ts" 或 "index.ts"',
        },
      },
      required: ['filePath'],
      additionalProperties: false,

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