-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathvalidate.js
More file actions
276 lines (234 loc) · 7.97 KB
/
Copy pathvalidate.js
File metadata and controls
276 lines (234 loc) · 7.97 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#!/usr/bin/env node
/**
* Schema validation and conflict detection for MCP server definitions.
*
* Usage:
* node scripts/validate.js servers/foo.json servers/bar.json
* node scripts/validate.js --all
* node scripts/validate.js --check-conflicts
*/
const fs = require("fs");
const path = require("path");
const Ajv = require("ajv");
const addFormats = require("ajv-formats");
const { glob } = require("glob");
const ROOT = path.resolve(__dirname, "..");
const SCHEMA_PATH = path.join(ROOT, "schemas", "server-definition.schema.json");
const SERVERS_DIR = path.join(ROOT, "servers");
const CATEGORIES_PATH = path.join(ROOT, "categories.json");
// Fields that contributors must not set -- they are platform-managed
const PLATFORM_FIELDS = ["badges", "stats", "sponsored", "featured"];
/**
* Load the set of valid category IDs from categories.json. The bundler's
* server_categories table has a foreign key to categories.id, so any server
* that references an ID not in categories.json will silently fail to sync.
* We catch those at validate time so they never reach main.
*/
function loadValidCategoryIds() {
try {
const raw = fs.readFileSync(CATEGORIES_PATH, "utf-8");
const categories = JSON.parse(raw);
return new Set(categories.map((c) => c.id));
} catch (err) {
console.error(`Failed to load categories.json: ${err.message}`);
return null;
}
}
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
function loadJson(filePath) {
const raw = fs.readFileSync(filePath, "utf-8");
return JSON.parse(raw);
}
async function getAllServerFiles() {
const pattern = path.join(SERVERS_DIR, "*.json").replace(/\\/g, "/");
const files = await glob(pattern);
return files.map((f) => path.resolve(f));
}
function stripPlatformFields(data, filePath) {
const warnings = [];
for (const field of PLATFORM_FIELDS) {
if (data[field] !== undefined) {
warnings.push(
` WARNING: "${field}" is a platform-managed field and will be stripped from ${path.basename(filePath)}`
);
delete data[field];
}
}
// Also check nested _platform prefix fields
for (const key of Object.keys(data)) {
if (key.startsWith("_platform")) {
warnings.push(
` WARNING: "${key}" is a platform-managed field and will be stripped from ${path.basename(filePath)}`
);
delete data[key];
}
}
return warnings;
}
// ---------------------------------------------------------------------------
// Validate
// ---------------------------------------------------------------------------
async function validateFiles(files) {
const schema = loadJson(SCHEMA_PATH);
// Remove $schema draft identifier -- ajv v8 uses draft-07 by default and
// does not ship the 2020-12 meta-schema. The structural keywords we use
// are compatible, so we simply strip the $schema to avoid a lookup error.
delete schema.$schema;
const ajv = new Ajv({ allErrors: true, strict: false });
addFormats(ajv);
const validate = ajv.compile(schema);
const validCategoryIds = loadValidCategoryIds();
let hasErrors = false;
for (const filePath of files) {
const relative = path.relative(ROOT, filePath);
let data;
try {
data = loadJson(filePath);
} catch (err) {
console.error(`FAIL ${relative}`);
console.error(` Could not parse JSON: ${err.message}`);
hasErrors = true;
continue;
}
// Strip platform-managed fields and warn
const warnings = stripPlatformFields(data, filePath);
for (const w of warnings) {
console.warn(w);
}
const valid = validate(data);
const fileErrors = [];
if (!valid) {
for (const err of validate.errors) {
fileErrors.push(`${err.instancePath || "/"}: ${err.message}`);
}
}
// Enforce that every category reference exists in categories.json.
// The bundler's server_categories table has a FK constraint — a typo
// here silently breaks sync for this server.
if (validCategoryIds && Array.isArray(data.categories)) {
const unknown = data.categories.filter((id) => !validCategoryIds.has(id));
for (const id of unknown) {
fileErrors.push(
`/categories: unknown category "${id}" (not in categories.json; pick one of: ${Array.from(validCategoryIds).sort().join(", ")})`
);
}
}
if (fileErrors.length > 0) {
console.error(`FAIL ${relative}`);
for (const msg of fileErrors) {
console.error(` - ${msg}`);
}
hasErrors = true;
} else {
console.log(`PASS ${relative}`);
}
}
return hasErrors;
}
// ---------------------------------------------------------------------------
// Conflict detection
// ---------------------------------------------------------------------------
async function checkConflicts() {
const files = await getAllServerFiles();
if (files.length === 0) {
console.log("No server files found. Skipping conflict check.");
return false;
}
const ids = new Map(); // id -> file
const aliases = new Map(); // alias -> file
let hasConflicts = false;
for (const filePath of files) {
const relative = path.relative(ROOT, filePath);
let data;
try {
data = loadJson(filePath);
} catch {
// Skip files that cannot be parsed -- validate step will catch them
continue;
}
// Check duplicate IDs
if (data.id) {
if (ids.has(data.id)) {
console.error(
`CONFLICT Duplicate ID "${data.id}" in ${relative} (already defined in ${ids.get(data.id)})`
);
hasConflicts = true;
} else {
ids.set(data.id, relative);
}
// Also check if an ID collides with an existing alias
if (aliases.has(data.id)) {
console.error(
`CONFLICT ID "${data.id}" in ${relative} collides with alias in ${aliases.get(data.id)}`
);
hasConflicts = true;
}
}
// Check duplicate aliases
if (data.alias) {
if (aliases.has(data.alias)) {
console.error(
`CONFLICT Duplicate alias "${data.alias}" in ${relative} (already defined in ${aliases.get(data.alias)})`
);
hasConflicts = true;
} else {
aliases.set(data.alias, relative);
}
// Also check if alias collides with an existing ID
if (ids.has(data.alias)) {
console.error(
`CONFLICT Alias "${data.alias}" in ${relative} collides with ID in ${ids.get(data.alias)}`
);
hasConflicts = true;
}
}
}
if (!hasConflicts) {
console.log(`No conflicts found across ${files.length} server file(s).`);
}
return hasConflicts;
}
// ---------------------------------------------------------------------------
// Main
// ---------------------------------------------------------------------------
async function main() {
const args = process.argv.slice(2);
const doAll = args.includes("--all");
const doConflicts = args.includes("--check-conflicts");
const fileArgs = args.filter((a) => !a.startsWith("--"));
let exitCode = 0;
// Conflict-only mode
if (doConflicts && !doAll && fileArgs.length === 0) {
const conflicts = await checkConflicts();
if (conflicts) exitCode = 1;
process.exit(exitCode);
}
// Determine files to validate
let files = [];
if (doAll) {
files = await getAllServerFiles();
} else if (fileArgs.length > 0) {
files = fileArgs.map((f) => path.resolve(f));
}
if (files.length === 0 && !doConflicts) {
console.log("No files to validate. Provide file paths or use --all.");
process.exit(0);
}
// Validate
if (files.length > 0) {
const hasErrors = await validateFiles(files);
if (hasErrors) exitCode = 1;
}
// Optionally run conflict check alongside validation
if (doConflicts) {
const conflicts = await checkConflicts();
if (conflicts) exitCode = 1;
}
process.exit(exitCode);
}
main().catch((err) => {
console.error("Unexpected error:", err);
process.exit(1);
});