-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmcp-server.js
More file actions
435 lines (384 loc) · 17.5 KB
/
mcp-server.js
File metadata and controls
435 lines (384 loc) · 17.5 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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
#!/usr/bin/env node
'use strict';
/**
* Knowledge-as-Code — MCP Server
* Zero-dependency Model Context Protocol server over JSON-RPC / stdio.
*
* Reads project.yml at startup and exposes entity data as MCP tools
* with dynamic names derived from the configured ontology.
*
* Usage: node mcp-server.js (called by MCP host via stdio)
*/
const fs = require('fs');
const path = require('path');
const { loadMappingIndex, parseTable } = require('./scripts/lib/data-loaders');
const { parseFrontmatter, parseYaml } = require('./scripts/lib/parsers');
const ROOT = __dirname;
// ---------------------------------------------------------------------------
// Shared helpers (from scripts/build.js)
// ---------------------------------------------------------------------------
function slugify(str) {
return String(str || '').toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-|-$/g, '');
}
function escapeHTML(str) {
return String(str || '').replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
}
function humanizeId(id) {
return String(id || '').replace(/-/g, ' ').replace(/\b\w/g, c => c.toUpperCase());
}
function findDataDir() {
const dirs = ['data/examples', 'data'];
for (const base of dirs) {
const fullBase = path.join(ROOT, base);
if (fs.existsSync(fullBase)) return fullBase;
}
return path.join(ROOT, 'data');
}
function loadDir(dir) {
if (!fs.existsSync(dir)) return [];
return fs.readdirSync(dir)
.filter(f => f.endsWith('.md') && !f.startsWith('_'))
.map(f => {
const content = fs.readFileSync(path.join(dir, f), 'utf-8');
const { frontmatter, body } = parseFrontmatter(content);
return { id: f.replace('.md', ''), ...frontmatter, _body: body };
});
}
function parseProvisionSection(section) {
const trimmed = section.trim();
const lines = trimmed.split('\n');
const nameMatch = lines[0].match(/^## (.+)/);
if (!nameMatch) return null;
const provision = { name: nameMatch[1] };
const propTableMatch = trimmed.match(/\| Property \| Value \|[\s\S]*?\n\n/);
if (propTableMatch) {
parseTable(propTableMatch[0]).forEach(p => {
provision[p.property.toLowerCase().replace(/\s+/g, '_')] = p.value;
});
}
const reqMatch = trimmed.match(/### Requirements\n\n([\s\S]*?)(?=\n###|\n---|\n## |$)/);
if (reqMatch) provision.requirements = parseTable(reqMatch[1]);
return provision;
}
function loadContainers(dir) {
if (!fs.existsSync(dir)) return [];
return fs.readdirSync(dir)
.filter(f => f.endsWith('.md') && !f.startsWith('_'))
.map(f => {
const content = fs.readFileSync(path.join(dir, f), 'utf-8');
const { frontmatter, body } = parseFrontmatter(content);
const id = f.replace('.md', '');
const timelineMatch = body.match(/## Timeline\n\n([\s\S]*?)(?=\n---|\n## )/);
const timeline = timelineMatch ? parseTable(timelineMatch[1]) : [];
const provisionSections = body.split(/\n---\n/).slice(1);
const provisions = provisionSections.map(parseProvisionSection).filter(Boolean);
return { id, ...frontmatter, timeline, provisions, _body: body };
});
}
// ---------------------------------------------------------------------------
// Load project data
// ---------------------------------------------------------------------------
const config = (() => {
const configPath = path.join(ROOT, 'project.yml');
if (!fs.existsSync(configPath)) {
process.stderr.write('Error: project.yml not found.\n');
process.exit(1);
}
return parseYaml(fs.readFileSync(configPath, 'utf-8'));
})();
const dataDir = findDataDir();
const primaryDir = path.join(dataDir, config.entities?.primary?.directory || 'primary');
const containerDir = path.join(dataDir, config.entities?.container?.directory || 'container');
const authorityDir = path.join(dataDir, config.entities?.authority?.directory || 'authority');
const primaries = loadDir(primaryDir);
const containers = loadContainers(containerDir);
const authorities = loadDir(authorityDir);
const mappingFile = config.mapping?.file || 'provisions/index.yml';
let mappingPath = path.join(dataDir, mappingFile);
if (!fs.existsSync(mappingPath)) mappingPath = path.join(dataDir, 'mapping', 'index.yml');
const mappings = loadMappingIndex(mappingPath);
// ---------------------------------------------------------------------------
// Tool name derivation
// ---------------------------------------------------------------------------
const primaryName = slugify(config.entities?.primary?.name || 'primary');
const primaryPlural = slugify(config.entities?.primary?.plural || 'primaries');
const containerName = slugify(config.entities?.container?.name || 'container');
const containerPlural = slugify(config.entities?.container?.plural || 'containers');
const authorityName = slugify(config.entities?.authority?.name || 'authority');
const authorityPlural = slugify(config.entities?.authority?.plural || 'authorities');
// ---------------------------------------------------------------------------
// Boundary helper
// ---------------------------------------------------------------------------
function boundary(message, suggestions, why) {
const b = { message, suggestions: suggestions || [] };
if (why) b.why = why;
return b;
}
// ---------------------------------------------------------------------------
// Tool definitions
// ---------------------------------------------------------------------------
function getToolDefinitions() {
const pName = config.entities?.primary?.name || 'Primary';
const pPlural = config.entities?.primary?.plural || 'Primaries';
const cName = config.entities?.container?.name || 'Container';
const cPlural = config.entities?.container?.plural || 'Containers';
const aName = config.entities?.authority?.name || 'Authority';
const aPlural = config.entities?.authority?.plural || 'Authorities';
return [
{
name: `list_${primaryPlural}`,
description: `List all ${pPlural.toLowerCase()} in the knowledge base. Returns id, title, and group for each.`,
inputSchema: { type: 'object', properties: {}, required: [] }
},
{
name: `get_${primaryName}`,
description: `Get a single ${pName.toLowerCase()} by its ID. Returns all frontmatter fields and body content.`,
inputSchema: {
type: 'object',
properties: { id: { type: 'string', description: `The ${pName.toLowerCase()} ID (filename without .md)` } },
required: ['id']
}
},
{
name: `list_${containerPlural}`,
description: `List all ${cPlural.toLowerCase()} in the knowledge base. Returns id, title, status, and authority.`,
inputSchema: { type: 'object', properties: {}, required: [] }
},
{
name: `get_${containerName}`,
description: `Get a single ${cName.toLowerCase()} by its ID. Returns frontmatter, timeline, and provisions.`,
inputSchema: {
type: 'object',
properties: { id: { type: 'string', description: `The ${cName.toLowerCase()} ID (filename without .md)` } },
required: ['id']
}
},
{
name: `list_${authorityPlural}`,
description: `List all ${aPlural.toLowerCase()} in the knowledge base.`,
inputSchema: { type: 'object', properties: {}, required: [] }
},
{
name: `get_${authorityName}`,
description: `Get a single ${aName.toLowerCase()} by its ID. Returns all frontmatter fields and body content.`,
inputSchema: {
type: 'object',
properties: { id: { type: 'string', description: `The ${aName.toLowerCase()} ID (filename without .md)` } },
required: ['id']
}
},
{
name: 'search',
description: 'Full-text search across all entities in the knowledge base. Searches titles, IDs, and body content.',
inputSchema: {
type: 'object',
properties: { query: { type: 'string', description: 'Search query (case-insensitive substring match)' } },
required: ['query']
}
},
{
name: 'get_matrix',
description: `Coverage matrix showing which ${pPlural.toLowerCase()} are addressed by which ${cPlural.toLowerCase()}.`,
inputSchema: { type: 'object', properties: {}, required: [] }
},
{
name: 'get_mappings',
description: `All mapping entries connecting ${cPlural.toLowerCase()} to ${pPlural.toLowerCase()} via ${config.entities?.secondary?.plural?.toLowerCase() || 'secondaries'}.`,
inputSchema: { type: 'object', properties: {}, required: [] }
}
];
}
// ---------------------------------------------------------------------------
// Tool dispatch
// ---------------------------------------------------------------------------
function handleToolCall(name, args) {
// --- list primaries ---
if (name === `list_${primaryPlural}`) {
const items = primaries.map(p => ({ id: p.id, title: p.title || humanizeId(p.id), group: p.group || '' }));
return { content: [{ type: 'text', text: JSON.stringify(items, null, 2) }] };
}
// --- get primary ---
if (name === `get_${primaryName}`) {
const entity = primaries.find(p => p.id === args.id);
if (!entity) {
return {
content: [{ type: 'text', text: JSON.stringify({
error: `${config.entities?.primary?.name || 'Primary'} not found: ${args.id}`,
boundary: boundary(
`No ${(config.entities?.primary?.name || 'primary').toLowerCase()} with id "${args.id}" exists.`,
[`Use list_${primaryPlural} to see available IDs`, 'Check spelling and use lowercase-hyphenated format'],
'IDs are derived from filenames in the data directory'
)
}, null, 2) }],
isError: true
};
}
const { _body, ...rest } = entity;
return { content: [{ type: 'text', text: JSON.stringify({ ...rest, body: _body }, null, 2) }] };
}
// --- list containers ---
if (name === `list_${containerPlural}`) {
const items = containers.map(c => ({ id: c.id, title: c.title || humanizeId(c.id), status: c.status || '', authority: c.authority || '' }));
return { content: [{ type: 'text', text: JSON.stringify(items, null, 2) }] };
}
// --- get container ---
if (name === `get_${containerName}`) {
const entity = containers.find(c => c.id === args.id);
if (!entity) {
return {
content: [{ type: 'text', text: JSON.stringify({
error: `${config.entities?.container?.name || 'Container'} not found: ${args.id}`,
boundary: boundary(
`No ${(config.entities?.container?.name || 'container').toLowerCase()} with id "${args.id}" exists.`,
[`Use list_${containerPlural} to see available IDs`, 'Check spelling and use lowercase-hyphenated format'],
'IDs are derived from filenames in the data directory'
)
}, null, 2) }],
isError: true
};
}
const { _body, ...rest } = entity;
return { content: [{ type: 'text', text: JSON.stringify({ ...rest, body: _body }, null, 2) }] };
}
// --- list authorities ---
if (name === `list_${authorityPlural}`) {
const items = authorities.map(a => ({ id: a.id, title: a.title || humanizeId(a.id), type: a.type || '' }));
return { content: [{ type: 'text', text: JSON.stringify(items, null, 2) }] };
}
// --- get authority ---
if (name === `get_${authorityName}`) {
const entity = authorities.find(a => a.id === args.id);
if (!entity) {
return {
content: [{ type: 'text', text: JSON.stringify({
error: `${config.entities?.authority?.name || 'Authority'} not found: ${args.id}`,
boundary: boundary(
`No ${(config.entities?.authority?.name || 'authority').toLowerCase()} with id "${args.id}" exists.`,
[`Use list_${authorityPlural} to see available IDs`, 'Check spelling and use lowercase-hyphenated format'],
'IDs are derived from filenames in the data directory'
)
}, null, 2) }],
isError: true
};
}
const { _body, ...rest } = entity;
return { content: [{ type: 'text', text: JSON.stringify({ ...rest, body: _body }, null, 2) }] };
}
// --- search ---
if (name === 'search') {
const q = (args.query || '').toLowerCase();
if (!q) {
return {
content: [{ type: 'text', text: JSON.stringify({
error: 'Empty search query',
boundary: boundary('A non-empty query string is required.', ['Provide a keyword or phrase to search for'])
}, null, 2) }],
isError: true
};
}
const results = [];
const searchEntity = (entity, type) => {
const haystack = [entity.id, entity.title || '', entity._body || ''].join(' ').toLowerCase();
if (haystack.includes(q)) {
results.push({ type, id: entity.id, title: entity.title || humanizeId(entity.id) });
}
};
primaries.forEach(e => searchEntity(e, config.entities?.primary?.name || 'primary'));
containers.forEach(e => searchEntity(e, config.entities?.container?.name || 'container'));
authorities.forEach(e => searchEntity(e, config.entities?.authority?.name || 'authority'));
return { content: [{ type: 'text', text: JSON.stringify({ query: args.query, count: results.length, results }, null, 2) }] };
}
// --- get_matrix ---
if (name === 'get_matrix') {
const matrix = {};
for (const c of containers) {
matrix[c.id] = {};
for (const p of primaries) matrix[c.id][p.id] = false;
}
for (const m of mappings) {
const cId = m.regulation || m.container || m.framework;
if (cId && matrix[cId]) {
for (const obl of (m.obligations || [])) {
if (matrix[cId][obl] !== undefined) matrix[cId][obl] = true;
}
}
}
return { content: [{ type: 'text', text: JSON.stringify(matrix, null, 2) }] };
}
// --- get_mappings ---
if (name === 'get_mappings') {
return { content: [{ type: 'text', text: JSON.stringify(mappings, null, 2) }] };
}
// --- unknown tool ---
return {
content: [{ type: 'text', text: JSON.stringify({
error: `Unknown tool: ${name}`,
boundary: boundary(
`The tool "${name}" does not exist on this server.`,
['Use tools/list to see available tools'],
'Tool names are derived from entity names in project.yml'
)
}, null, 2) }],
isError: true
};
}
// ---------------------------------------------------------------------------
// JSON-RPC / MCP protocol
// ---------------------------------------------------------------------------
function makeResponse(id, result) {
return JSON.stringify({ jsonrpc: '2.0', id, result });
}
function makeError(id, code, message) {
return JSON.stringify({ jsonrpc: '2.0', id, error: { code, message } });
}
function handleMessage(msg) {
const { id, method, params } = msg;
if (method === 'initialize') {
return makeResponse(id, {
protocolVersion: '2024-11-05',
capabilities: { tools: {} },
serverInfo: {
name: config.name || 'Knowledge Base MCP',
version: '1.0.0'
}
});
}
if (method === 'notifications/initialized') {
return null; // notification, no response
}
if (method === 'tools/list') {
return makeResponse(id, { tools: getToolDefinitions() });
}
if (method === 'tools/call') {
const toolName = params?.name;
const toolArgs = params?.arguments || {};
const result = handleToolCall(toolName, toolArgs);
return makeResponse(id, result);
}
return makeError(id, -32601, `Method not found: ${method}`);
}
// ---------------------------------------------------------------------------
// Stdio transport
// ---------------------------------------------------------------------------
let buffer = '';
process.stdin.setEncoding('utf-8');
process.stdin.on('data', chunk => {
buffer += chunk;
let newlineIdx;
while ((newlineIdx = buffer.indexOf('\n')) !== -1) {
const line = buffer.slice(0, newlineIdx).trim();
buffer = buffer.slice(newlineIdx + 1);
if (!line) continue;
try {
const msg = JSON.parse(line);
const response = handleMessage(msg);
if (response) {
process.stdout.write(response + '\n');
}
} catch (err) {
const errResp = makeError(null, -32700, 'Parse error: ' + err.message);
process.stdout.write(errResp + '\n');
}
}
});
process.stdin.on('end', () => process.exit(0));