Path: blob/main/extensions/copilot/src/extension/chatSessions/claude/common/mcpServers/ideMcpServer.ts
13406 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { createSdkMcpServer, McpServerConfig, tool } from '@anthropic-ai/claude-agent-sdk';6import { z } from 'zod';7import { ILanguageDiagnosticsService } from '../../../../../platform/languages/common/languageDiagnosticsService';8import { URI } from '../../../../../util/vs/base/common/uri';9import { DiagnosticSeverity } from '../../../../../vscodeTypes';10import { IClaudeMcpServerContributor, registerClaudeMcpServerContributor } from '../claudeMcpServerRegistry';1112const severityToString: Record<DiagnosticSeverity, string> = {13[DiagnosticSeverity.Error]: 'error',14[DiagnosticSeverity.Warning]: 'warning',15[DiagnosticSeverity.Information]: 'information',16[DiagnosticSeverity.Hint]: 'hint',17};1819export interface DiagnosticEntry {20readonly uri: string;21readonly filePath: string;22readonly diagnostics: readonly {23readonly message: string;24readonly severity: string;25readonly range: {26readonly start: { readonly line: number; readonly character: number };27readonly end: { readonly line: number; readonly character: number };28};29readonly source: string | undefined;30readonly code: string | number | undefined;31}[];32}3334/**35* Fetches diagnostics from the language diagnostics service and formats them36* for the MCP tool response.37*/38export function getDiagnosticsHandler(39diagnosticsService: ILanguageDiagnosticsService,40args: { uri?: string }41): DiagnosticEntry[] {42let entries: [URI, readonly { message: string; severity: DiagnosticSeverity; range: { start: { line: number; character: number }; end: { line: number; character: number } }; source?: string; code?: string | number | { value: string | number; target: URI } }[]][];4344if (args.uri) {45let fileUri: URI;46try {47fileUri = URI.parse(args.uri);48} catch {49throw new Error(`Invalid URI: "${args.uri}". Expected an absolute path (e.g., /path/to/file.ts) or a URI with a scheme (e.g., file:///path/to/file.ts, untitled:Untitled-1).`);50}51entries = [[fileUri, diagnosticsService.getDiagnostics(fileUri)]];52} else {53entries = diagnosticsService.getAllDiagnostics();54}5556return entries57.map(([fileUri, fileDiagnostics]) => ({58uri: fileUri.toString(),59filePath: fileUri.fsPath,60diagnostics: fileDiagnostics.map(d => ({61message: d.message,62severity: severityToString[d.severity] ?? 'unknown',63range: {64start: { line: d.range.start.line, character: d.range.start.character },65end: { line: d.range.end.line, character: d.range.end.character },66},67source: d.source,68code: typeof d.code === 'object' ? d.code.value : d.code,69})),70}))71.filter(item => item.diagnostics.length > 0);72}7374class IdeMcpServerContributor implements IClaudeMcpServerContributor {7576constructor(77@ILanguageDiagnosticsService private readonly diagnosticsService: ILanguageDiagnosticsService,78) { }7980async getMcpServers(): Promise<Record<string, McpServerConfig>> {81const diagnosticsService = this.diagnosticsService;8283const getDiagnosticsTool = tool(84'getDiagnostics',85'Get language diagnostics from VS Code. Returns errors, warnings, information, and hints for files in the workspace.',86{87uri: z.string().optional().describe('Optional file URI to get diagnostics for. If not provided, gets diagnostics for all files.'),88},89async (args: { uri?: string }) => {90const result = getDiagnosticsHandler(diagnosticsService, args);91return {92content: [{93type: 'text' as const,94text: JSON.stringify(result, null, 2),95}],96};97}98);99100const server = createSdkMcpServer({101name: 'ide',102version: '0.0.1',103tools: [getDiagnosticsTool],104});105106return { ide: server };107}108}109110registerClaudeMcpServerContributor(IdeMcpServerContributor);111112113