Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/extension/chatSessions/claude/common/mcpServers/ideMcpServer.ts
13406 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { createSdkMcpServer, McpServerConfig, tool } from '@anthropic-ai/claude-agent-sdk';
7
import { z } from 'zod';
8
import { ILanguageDiagnosticsService } from '../../../../../platform/languages/common/languageDiagnosticsService';
9
import { URI } from '../../../../../util/vs/base/common/uri';
10
import { DiagnosticSeverity } from '../../../../../vscodeTypes';
11
import { IClaudeMcpServerContributor, registerClaudeMcpServerContributor } from '../claudeMcpServerRegistry';
12
13
const severityToString: Record<DiagnosticSeverity, string> = {
14
[DiagnosticSeverity.Error]: 'error',
15
[DiagnosticSeverity.Warning]: 'warning',
16
[DiagnosticSeverity.Information]: 'information',
17
[DiagnosticSeverity.Hint]: 'hint',
18
};
19
20
export interface DiagnosticEntry {
21
readonly uri: string;
22
readonly filePath: string;
23
readonly diagnostics: readonly {
24
readonly message: string;
25
readonly severity: string;
26
readonly range: {
27
readonly start: { readonly line: number; readonly character: number };
28
readonly end: { readonly line: number; readonly character: number };
29
};
30
readonly source: string | undefined;
31
readonly code: string | number | undefined;
32
}[];
33
}
34
35
/**
36
* Fetches diagnostics from the language diagnostics service and formats them
37
* for the MCP tool response.
38
*/
39
export function getDiagnosticsHandler(
40
diagnosticsService: ILanguageDiagnosticsService,
41
args: { uri?: string }
42
): DiagnosticEntry[] {
43
let 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 } }[]][];
44
45
if (args.uri) {
46
let fileUri: URI;
47
try {
48
fileUri = URI.parse(args.uri);
49
} catch {
50
throw 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).`);
51
}
52
entries = [[fileUri, diagnosticsService.getDiagnostics(fileUri)]];
53
} else {
54
entries = diagnosticsService.getAllDiagnostics();
55
}
56
57
return entries
58
.map(([fileUri, fileDiagnostics]) => ({
59
uri: fileUri.toString(),
60
filePath: fileUri.fsPath,
61
diagnostics: fileDiagnostics.map(d => ({
62
message: d.message,
63
severity: severityToString[d.severity] ?? 'unknown',
64
range: {
65
start: { line: d.range.start.line, character: d.range.start.character },
66
end: { line: d.range.end.line, character: d.range.end.character },
67
},
68
source: d.source,
69
code: typeof d.code === 'object' ? d.code.value : d.code,
70
})),
71
}))
72
.filter(item => item.diagnostics.length > 0);
73
}
74
75
class IdeMcpServerContributor implements IClaudeMcpServerContributor {
76
77
constructor(
78
@ILanguageDiagnosticsService private readonly diagnosticsService: ILanguageDiagnosticsService,
79
) { }
80
81
async getMcpServers(): Promise<Record<string, McpServerConfig>> {
82
const diagnosticsService = this.diagnosticsService;
83
84
const getDiagnosticsTool = tool(
85
'getDiagnostics',
86
'Get language diagnostics from VS Code. Returns errors, warnings, information, and hints for files in the workspace.',
87
{
88
uri: z.string().optional().describe('Optional file URI to get diagnostics for. If not provided, gets diagnostics for all files.'),
89
},
90
async (args: { uri?: string }) => {
91
const result = getDiagnosticsHandler(diagnosticsService, args);
92
return {
93
content: [{
94
type: 'text' as const,
95
text: JSON.stringify(result, null, 2),
96
}],
97
};
98
}
99
);
100
101
const server = createSdkMcpServer({
102
name: 'ide',
103
version: '0.0.1',
104
tools: [getDiagnosticsTool],
105
});
106
107
return { ide: server };
108
}
109
}
110
111
registerClaudeMcpServerContributor(IdeMcpServerContributor);
112
113