forked from lirantal/ls-mcp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmcp-config-linter-service.ts
More file actions
86 lines (72 loc) · 2 KB
/
mcp-config-linter-service.ts
File metadata and controls
86 lines (72 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import fs from 'node:fs/promises'
import { parse } from 'jsonc-parser'
export class MCPConfigLinterService {
private filePath: string
private fileContents: string | null = null
private parsed: boolean = false
private valid: boolean = false
private fileContentsData: any | null = null
constructor (filePath: string) {
this.filePath = filePath
}
async parseFile (): Promise<void> {
if (this.parsed) {
return
}
this.parsed = true
const fileContent = await this.getFileContent()
// @TODO this should also support YAML files and other formats
try {
this.fileContentsData = JSON.parse(fileContent)
if (typeof this.fileContentsData === 'object') {
this.valid = true
return
}
} catch (e) {
// ignore
}
try {
this.fileContentsData = parse(fileContent)
if (typeof this.fileContentsData === 'object') {
this.valid = true
}
} catch (error) {
// ignore
}
}
async isValidSyntax (): Promise<boolean> {
try {
await this.parseFile()
return this.valid
} catch (error) {
return false
}
}
async countMCPServers (): Promise<number> {
const mcpServers = await this.getMCPServers()
return Object.keys(mcpServers).length
}
async getMCPServers (): Promise<Record<string, object>> {
await this.parseFile()
// VS Code uses `servers`
if (this.fileContentsData?.servers) {
return this.fileContentsData.servers
}
// VS Code global settings.json file uses `mcp` -> `servers`
if (this.fileContentsData?.mcp?.servers) {
return this.fileContentsData.mcp.servers
}
// Claude and Cursor use the `mcpServers` key
if (this.fileContentsData?.mcpServers) {
return this.fileContentsData.mcpServers
}
return {}
}
async getFileContent (): Promise<string> {
if (this.fileContents) {
return this.fileContents
}
this.fileContents = await fs.readFile(this.filePath, 'utf-8')
return this.fileContents
}
}