@grafema/mcp 0.1.0-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,747 @@
1
+ /**
2
+ * MCP Tool Handlers
3
+ */
4
+ import { ensureAnalyzed } from './analysis.js';
5
+ import { getProjectPath, getAnalysisStatus, setIsAnalyzed, getOrCreateBackend, getGuaranteeManager, getGuaranteeAPI } from './state.js';
6
+ import { normalizeLimit, formatPaginationInfo, guardResponseSize, serializeBigInt, findSimilarTypes, textResult, errorResult, } from './utils.js';
7
+ import { isGuaranteeType } from '@grafema/core';
8
+ // === QUERY HANDLERS ===
9
+ export async function handleQueryGraph(args) {
10
+ const db = await ensureAnalyzed();
11
+ const { query, limit: requestedLimit, offset: requestedOffset, format } = args;
12
+ const explain = args.explain;
13
+ const limit = normalizeLimit(requestedLimit);
14
+ const offset = Math.max(0, requestedOffset || 0);
15
+ try {
16
+ // Check if backend supports Datalog queries
17
+ if (!('checkGuarantee' in db)) {
18
+ return errorResult('Backend does not support Datalog queries');
19
+ }
20
+ const checkFn = db.checkGuarantee;
21
+ const results = await checkFn(query);
22
+ const total = results.length;
23
+ if (total === 0) {
24
+ const nodeCounts = await db.countNodesByType();
25
+ const totalNodes = Object.values(nodeCounts).reduce((a, b) => a + b, 0);
26
+ const typeMatch = query.match(/node\([^,]+,\s*"([^"]+)"\)/);
27
+ const queriedType = typeMatch ? typeMatch[1] : null;
28
+ let hint = '';
29
+ if (queriedType && !nodeCounts[queriedType]) {
30
+ const availableTypes = Object.keys(nodeCounts);
31
+ const similar = findSimilarTypes(queriedType, availableTypes);
32
+ if (similar.length > 0) {
33
+ hint = `\nšŸ’” Did you mean: ${similar.join(', ')}?`;
34
+ }
35
+ else {
36
+ hint = `\nšŸ’” Available types: ${availableTypes.slice(0, 10).join(', ')}${availableTypes.length > 10 ? '...' : ''}`;
37
+ }
38
+ }
39
+ return textResult(`Query returned no results.${hint}\nšŸ“Š Graph: ${totalNodes.toLocaleString()} nodes`);
40
+ }
41
+ const paginatedResults = results.slice(offset, offset + limit);
42
+ const hasMore = offset + limit < total;
43
+ const enrichedResults = [];
44
+ for (const result of paginatedResults) {
45
+ const nodeId = result.bindings?.find((b) => b.name === 'X')?.value;
46
+ if (nodeId) {
47
+ const node = await db.getNode(nodeId);
48
+ if (node) {
49
+ enrichedResults.push({
50
+ ...node,
51
+ id: nodeId,
52
+ file: node.file,
53
+ line: node.line,
54
+ });
55
+ }
56
+ }
57
+ }
58
+ const paginationInfo = formatPaginationInfo({
59
+ limit,
60
+ offset,
61
+ returned: enrichedResults.length,
62
+ total,
63
+ hasMore,
64
+ });
65
+ const responseText = `Found ${total} result(s):${paginationInfo}\n\n${JSON.stringify(serializeBigInt(enrichedResults), null, 2)}`;
66
+ return textResult(guardResponseSize(responseText));
67
+ }
68
+ catch (error) {
69
+ return errorResult(error.message);
70
+ }
71
+ }
72
+ export async function handleFindCalls(args) {
73
+ const db = await ensureAnalyzed();
74
+ const { target: name, limit: requestedLimit, offset: requestedOffset } = args;
75
+ const className = args.className;
76
+ const limit = normalizeLimit(requestedLimit);
77
+ const offset = Math.max(0, requestedOffset || 0);
78
+ const calls = [];
79
+ let skipped = 0;
80
+ let totalMatched = 0;
81
+ for await (const node of db.queryNodes({ type: 'CALL' })) {
82
+ if (node.name !== name && node.method !== name)
83
+ continue;
84
+ if (className && node.object !== className)
85
+ continue;
86
+ totalMatched++;
87
+ if (skipped < offset) {
88
+ skipped++;
89
+ continue;
90
+ }
91
+ if (calls.length >= limit)
92
+ continue;
93
+ const callsEdges = await db.getOutgoingEdges(node.id, ['CALLS']);
94
+ const isResolved = callsEdges.length > 0;
95
+ let target = null;
96
+ if (isResolved) {
97
+ const targetNode = await db.getNode(callsEdges[0].dst);
98
+ target = targetNode
99
+ ? {
100
+ type: targetNode.type,
101
+ name: targetNode.name,
102
+ file: targetNode.file,
103
+ line: targetNode.line,
104
+ }
105
+ : null;
106
+ }
107
+ calls.push({
108
+ id: node.id,
109
+ name: node.name,
110
+ object: node.object,
111
+ file: node.file,
112
+ line: node.line,
113
+ resolved: isResolved,
114
+ target,
115
+ });
116
+ }
117
+ if (totalMatched === 0) {
118
+ return textResult(`No calls found for "${className ? className + '.' : ''}${name}"`);
119
+ }
120
+ const resolved = calls.filter((c) => c.resolved).length;
121
+ const unresolved = calls.length - resolved;
122
+ const hasMore = offset + calls.length < totalMatched;
123
+ const paginationInfo = formatPaginationInfo({
124
+ limit,
125
+ offset,
126
+ returned: calls.length,
127
+ total: totalMatched,
128
+ hasMore,
129
+ });
130
+ const responseText = `Found ${totalMatched} call(s) to "${className ? className + '.' : ''}${name}":${paginationInfo}\n` +
131
+ `- Resolved: ${resolved}\n` +
132
+ `- Unresolved: ${unresolved}\n\n` +
133
+ JSON.stringify(serializeBigInt(calls), null, 2);
134
+ return textResult(guardResponseSize(responseText));
135
+ }
136
+ export async function handleFindNodes(args) {
137
+ const db = await ensureAnalyzed();
138
+ const { type, name, file, limit: requestedLimit, offset: requestedOffset } = args;
139
+ const limit = normalizeLimit(requestedLimit);
140
+ const offset = Math.max(0, requestedOffset || 0);
141
+ const filter = {};
142
+ if (type)
143
+ filter.type = type;
144
+ if (name)
145
+ filter.name = name;
146
+ if (file)
147
+ filter.file = file;
148
+ const nodes = [];
149
+ let skipped = 0;
150
+ let totalMatched = 0;
151
+ for await (const node of db.queryNodes(filter)) {
152
+ totalMatched++;
153
+ if (skipped < offset) {
154
+ skipped++;
155
+ continue;
156
+ }
157
+ if (nodes.length < limit) {
158
+ nodes.push(node);
159
+ }
160
+ }
161
+ if (totalMatched === 0) {
162
+ return textResult('No nodes found matching criteria');
163
+ }
164
+ const hasMore = offset + nodes.length < totalMatched;
165
+ const paginationInfo = formatPaginationInfo({
166
+ limit,
167
+ offset,
168
+ returned: nodes.length,
169
+ total: totalMatched,
170
+ hasMore,
171
+ });
172
+ return textResult(`Found ${totalMatched} node(s):${paginationInfo}\n\n${JSON.stringify(serializeBigInt(nodes), null, 2)}`);
173
+ }
174
+ // === TRACE HANDLERS ===
175
+ export async function handleTraceAlias(args) {
176
+ const db = await ensureAnalyzed();
177
+ const { identifier: variableName, file } = args;
178
+ const projectPath = getProjectPath();
179
+ let varNode = null;
180
+ for await (const node of db.queryNodes({ type: 'VARIABLE' })) {
181
+ if (node.name === variableName && node.file?.includes(file || '')) {
182
+ varNode = node;
183
+ break;
184
+ }
185
+ }
186
+ if (!varNode) {
187
+ for await (const node of db.queryNodes({ type: 'CONSTANT' })) {
188
+ if (node.name === variableName && node.file?.includes(file || '')) {
189
+ varNode = node;
190
+ break;
191
+ }
192
+ }
193
+ }
194
+ if (!varNode) {
195
+ return errorResult(`Variable "${variableName}" not found in ${file || 'project'}`);
196
+ }
197
+ const chain = [];
198
+ const visited = new Set();
199
+ let current = varNode;
200
+ const MAX_DEPTH = 20;
201
+ while (current && chain.length < MAX_DEPTH) {
202
+ if (visited.has(current.id)) {
203
+ chain.push({ type: 'CYCLE_DETECTED', id: current.id });
204
+ break;
205
+ }
206
+ visited.add(current.id);
207
+ chain.push({
208
+ type: current.type,
209
+ name: current.name,
210
+ file: current.file,
211
+ line: current.line,
212
+ });
213
+ const edges = await db.getOutgoingEdges(current.id, ['ASSIGNED_FROM']);
214
+ if (edges.length === 0)
215
+ break;
216
+ current = await db.getNode(edges[0].dst);
217
+ }
218
+ return textResult(`Alias chain for "${variableName}" (${chain.length} steps):\n\n${JSON.stringify(serializeBigInt(chain), null, 2)}`);
219
+ }
220
+ export async function handleTraceDataFlow(args) {
221
+ const db = await ensureAnalyzed();
222
+ const { source, direction = 'forward', max_depth = 10 } = args;
223
+ // Find source node
224
+ let sourceNode = null;
225
+ // Try to find by ID first
226
+ sourceNode = await db.getNode(source);
227
+ // If not found, search by name
228
+ if (!sourceNode) {
229
+ for await (const node of db.queryNodes({ name: source })) {
230
+ sourceNode = node;
231
+ break;
232
+ }
233
+ }
234
+ if (!sourceNode) {
235
+ return errorResult(`Source "${source}" not found`);
236
+ }
237
+ const visited = new Set();
238
+ const paths = [];
239
+ async function trace(nodeId, depth, path) {
240
+ if (depth > max_depth || visited.has(nodeId))
241
+ return;
242
+ visited.add(nodeId);
243
+ const newPath = [...path, nodeId];
244
+ if (direction === 'forward' || direction === 'both') {
245
+ const outEdges = await db.getOutgoingEdges(nodeId, [
246
+ 'ASSIGNED_FROM',
247
+ 'DERIVES_FROM',
248
+ 'PASSES_ARGUMENT',
249
+ ]);
250
+ for (const edge of outEdges) {
251
+ await trace(edge.dst, depth + 1, newPath);
252
+ }
253
+ }
254
+ if (direction === 'backward' || direction === 'both') {
255
+ const inEdges = await db.getIncomingEdges(nodeId, [
256
+ 'ASSIGNED_FROM',
257
+ 'DERIVES_FROM',
258
+ 'PASSES_ARGUMENT',
259
+ ]);
260
+ for (const edge of inEdges) {
261
+ await trace(edge.src, depth + 1, newPath);
262
+ }
263
+ }
264
+ if (depth > 0) {
265
+ paths.push(newPath);
266
+ }
267
+ }
268
+ await trace(sourceNode.id, 0, []);
269
+ return textResult(`Data flow from "${source}" (${paths.length} paths):\n\n${JSON.stringify(paths, null, 2)}`);
270
+ }
271
+ export async function handleCheckInvariant(args) {
272
+ const db = await ensureAnalyzed();
273
+ const { rule, name: description } = args;
274
+ if (!('checkGuarantee' in db)) {
275
+ return errorResult('Backend does not support Datalog queries');
276
+ }
277
+ try {
278
+ const checkFn = db.checkGuarantee;
279
+ const violations = await checkFn(rule);
280
+ const total = violations.length;
281
+ if (total === 0) {
282
+ return textResult(`āœ… Invariant holds: ${description || 'No violations found'}`);
283
+ }
284
+ const enrichedViolations = [];
285
+ for (const v of violations.slice(0, 20)) {
286
+ const nodeId = v.bindings?.find((b) => b.name === 'X')?.value;
287
+ if (nodeId) {
288
+ const node = await db.getNode(nodeId);
289
+ if (node) {
290
+ enrichedViolations.push({
291
+ id: nodeId,
292
+ type: node.type,
293
+ name: node.name,
294
+ file: node.file,
295
+ line: node.line,
296
+ });
297
+ }
298
+ }
299
+ }
300
+ return textResult(`āŒ ${total} violation(s) found:\n\n${JSON.stringify(serializeBigInt(enrichedViolations), null, 2)}${total > 20 ? `\n\n... and ${total - 20} more` : ''}`);
301
+ }
302
+ catch (error) {
303
+ return errorResult(error.message);
304
+ }
305
+ }
306
+ // === ANALYSIS HANDLERS ===
307
+ export async function handleAnalyzeProject(args) {
308
+ const { service, force } = args;
309
+ if (force) {
310
+ setIsAnalyzed(false);
311
+ }
312
+ try {
313
+ await ensureAnalyzed(service || null);
314
+ const status = getAnalysisStatus();
315
+ return textResult(`āœ… Analysis complete!\n` +
316
+ `- Services discovered: ${status.servicesDiscovered}\n` +
317
+ `- Services analyzed: ${status.servicesAnalyzed}\n` +
318
+ `- Total time: ${status.timings.total || 'N/A'}s`);
319
+ }
320
+ catch (error) {
321
+ return errorResult(error.message);
322
+ }
323
+ }
324
+ export async function handleGetAnalysisStatus() {
325
+ const status = getAnalysisStatus();
326
+ return textResult(`Analysis Status:\n` +
327
+ `- Running: ${status.running}\n` +
328
+ `- Phase: ${status.phase || 'N/A'}\n` +
329
+ `- Message: ${status.message || 'N/A'}\n` +
330
+ `- Services discovered: ${status.servicesDiscovered}\n` +
331
+ `- Services analyzed: ${status.servicesAnalyzed}\n` +
332
+ (status.error ? `- Error: ${status.error}\n` : ''));
333
+ }
334
+ export async function handleGetStats() {
335
+ const db = await getOrCreateBackend();
336
+ const nodeCount = await db.nodeCount();
337
+ const edgeCount = await db.edgeCount();
338
+ const nodesByType = await db.countNodesByType();
339
+ const edgesByType = await db.countEdgesByType();
340
+ return textResult(`Graph Statistics:\n\n` +
341
+ `Total nodes: ${nodeCount.toLocaleString()}\n` +
342
+ `Total edges: ${edgeCount.toLocaleString()}\n\n` +
343
+ `Nodes by type:\n${JSON.stringify(nodesByType, null, 2)}\n\n` +
344
+ `Edges by type:\n${JSON.stringify(edgesByType, null, 2)}`);
345
+ }
346
+ export async function handleGetSchema(args) {
347
+ const db = await getOrCreateBackend();
348
+ const { type = 'all' } = args;
349
+ const nodesByType = await db.countNodesByType();
350
+ const edgesByType = await db.countEdgesByType();
351
+ let output = '';
352
+ if (type === 'nodes' || type === 'all') {
353
+ output += `Node Types (${Object.keys(nodesByType).length}):\n`;
354
+ for (const [t, count] of Object.entries(nodesByType)) {
355
+ output += ` - ${t}: ${count}\n`;
356
+ }
357
+ }
358
+ if (type === 'edges' || type === 'all') {
359
+ output += `\nEdge Types (${Object.keys(edgesByType).length}):\n`;
360
+ for (const [t, count] of Object.entries(edgesByType)) {
361
+ output += ` - ${t}: ${count}\n`;
362
+ }
363
+ }
364
+ return textResult(output);
365
+ }
366
+ // === GUARANTEE HANDLERS ===
367
+ /**
368
+ * Create a new guarantee (Datalog-based or contract-based)
369
+ */
370
+ export async function handleCreateGuarantee(args) {
371
+ await getOrCreateBackend(); // Ensure managers are initialized
372
+ const { name, rule, type, priority, status, owner, schema, condition, description, governs, severity } = args;
373
+ try {
374
+ // Determine if this is a contract-based guarantee
375
+ if (type && isGuaranteeType(type)) {
376
+ // Contract-based guarantee
377
+ const api = getGuaranteeAPI();
378
+ if (!api) {
379
+ return errorResult('GuaranteeAPI not initialized');
380
+ }
381
+ const guarantee = await api.createGuarantee({
382
+ type,
383
+ name,
384
+ priority,
385
+ status,
386
+ owner,
387
+ schema,
388
+ condition,
389
+ description,
390
+ governs,
391
+ });
392
+ return textResult(`āœ… Created contract-based guarantee: ${guarantee.id}\n` +
393
+ `Type: ${guarantee.type}\n` +
394
+ `Priority: ${guarantee.priority}\n` +
395
+ `Status: ${guarantee.status}` +
396
+ (guarantee.description ? `\nDescription: ${guarantee.description}` : ''));
397
+ }
398
+ else {
399
+ // Datalog-based guarantee
400
+ if (!rule) {
401
+ return errorResult('Datalog-based guarantee requires "rule" field');
402
+ }
403
+ const manager = getGuaranteeManager();
404
+ if (!manager) {
405
+ return errorResult('GuaranteeManager not initialized');
406
+ }
407
+ const guarantee = await manager.create({
408
+ id: name,
409
+ name,
410
+ rule,
411
+ severity: severity || 'warning',
412
+ governs: governs || ['**/*.js'],
413
+ });
414
+ return textResult(`āœ… Created Datalog-based guarantee: ${guarantee.id}\n` +
415
+ `Rule: ${guarantee.rule}\n` +
416
+ `Severity: ${guarantee.severity}`);
417
+ }
418
+ }
419
+ catch (error) {
420
+ return errorResult(`Failed to create guarantee: ${error.message}`);
421
+ }
422
+ }
423
+ /**
424
+ * List all guarantees (both Datalog-based and contract-based)
425
+ */
426
+ export async function handleListGuarantees() {
427
+ await getOrCreateBackend(); // Ensure managers are initialized
428
+ const results = [];
429
+ try {
430
+ // List Datalog-based guarantees
431
+ const manager = getGuaranteeManager();
432
+ if (manager) {
433
+ const datalogGuarantees = await manager.list();
434
+ if (datalogGuarantees.length > 0) {
435
+ results.push('## Datalog-based Guarantees\n');
436
+ for (const g of datalogGuarantees) {
437
+ results.push(`- **${g.id}** (${g.severity})`);
438
+ results.push(` Rule: ${g.rule.substring(0, 80)}${g.rule.length > 80 ? '...' : ''}`);
439
+ }
440
+ }
441
+ }
442
+ // List contract-based guarantees
443
+ const api = getGuaranteeAPI();
444
+ if (api) {
445
+ const contractGuarantees = await api.findGuarantees();
446
+ if (contractGuarantees.length > 0) {
447
+ if (results.length > 0)
448
+ results.push('\n');
449
+ results.push('## Contract-based Guarantees\n');
450
+ for (const g of contractGuarantees) {
451
+ results.push(`- **${g.id}** [${g.priority}] (${g.status})`);
452
+ if (g.description)
453
+ results.push(` ${g.description}`);
454
+ }
455
+ }
456
+ }
457
+ if (results.length === 0) {
458
+ return textResult('No guarantees defined yet.');
459
+ }
460
+ return textResult(results.join('\n'));
461
+ }
462
+ catch (error) {
463
+ return errorResult(`Failed to list guarantees: ${error.message}`);
464
+ }
465
+ }
466
+ /**
467
+ * Check guarantees (both Datalog-based and contract-based)
468
+ */
469
+ export async function handleCheckGuarantees(args) {
470
+ await getOrCreateBackend(); // Ensure managers are initialized
471
+ const { names } = args;
472
+ const results = [];
473
+ let totalPassed = 0;
474
+ let totalFailed = 0;
475
+ try {
476
+ const manager = getGuaranteeManager();
477
+ const api = getGuaranteeAPI();
478
+ if (names && names.length > 0) {
479
+ // Check specific guarantees
480
+ for (const name of names) {
481
+ // Try Datalog-based first
482
+ if (manager) {
483
+ try {
484
+ const result = await manager.check(name);
485
+ if (result.passed) {
486
+ totalPassed++;
487
+ results.push(`āœ… ${result.guaranteeId}: PASSED`);
488
+ }
489
+ else {
490
+ totalFailed++;
491
+ results.push(`āŒ ${result.guaranteeId}: FAILED (${result.violationCount} violations)`);
492
+ for (const v of result.violations.slice(0, 5)) {
493
+ results.push(` - ${v.file}:${v.line} (${v.type})`);
494
+ }
495
+ if (result.violationCount > 5) {
496
+ results.push(` ... and ${result.violationCount - 5} more`);
497
+ }
498
+ }
499
+ continue;
500
+ }
501
+ catch {
502
+ // Not a Datalog guarantee, try contract-based
503
+ }
504
+ }
505
+ // Try contract-based
506
+ if (api) {
507
+ try {
508
+ const result = await api.checkGuarantee(name);
509
+ if (result.passed) {
510
+ totalPassed++;
511
+ results.push(`āœ… ${result.id}: PASSED`);
512
+ }
513
+ else {
514
+ totalFailed++;
515
+ results.push(`āŒ ${result.id}: FAILED`);
516
+ for (const err of result.errors.slice(0, 5)) {
517
+ results.push(` - ${err}`);
518
+ }
519
+ }
520
+ }
521
+ catch {
522
+ results.push(`āš ļø ${name}: Not found`);
523
+ }
524
+ }
525
+ }
526
+ }
527
+ else {
528
+ // Check all guarantees
529
+ if (manager) {
530
+ const datalogResult = await manager.checkAll();
531
+ totalPassed += datalogResult.passed;
532
+ totalFailed += datalogResult.failed;
533
+ if (datalogResult.total > 0) {
534
+ results.push('## Datalog Guarantees\n');
535
+ for (const r of datalogResult.results) {
536
+ if (r.passed) {
537
+ results.push(`āœ… ${r.guaranteeId}: PASSED`);
538
+ }
539
+ else {
540
+ results.push(`āŒ ${r.guaranteeId}: FAILED (${r.violationCount} violations)`);
541
+ }
542
+ }
543
+ }
544
+ }
545
+ if (api) {
546
+ const contractResult = await api.checkAllGuarantees();
547
+ totalPassed += contractResult.passed;
548
+ totalFailed += contractResult.failed;
549
+ if (contractResult.total > 0) {
550
+ if (results.length > 0)
551
+ results.push('\n');
552
+ results.push('## Contract Guarantees\n');
553
+ for (const r of contractResult.results) {
554
+ if (r.passed) {
555
+ results.push(`āœ… ${r.id}: PASSED`);
556
+ }
557
+ else {
558
+ results.push(`āŒ ${r.id}: FAILED`);
559
+ }
560
+ }
561
+ }
562
+ }
563
+ }
564
+ if (results.length === 0) {
565
+ return textResult('No guarantees to check.');
566
+ }
567
+ const summary = `\n---\nTotal: ${totalPassed + totalFailed} | āœ… Passed: ${totalPassed} | āŒ Failed: ${totalFailed}`;
568
+ return textResult(results.join('\n') + summary);
569
+ }
570
+ catch (error) {
571
+ return errorResult(`Failed to check guarantees: ${error.message}`);
572
+ }
573
+ }
574
+ /**
575
+ * Delete a guarantee
576
+ */
577
+ export async function handleDeleteGuarantee(args) {
578
+ await getOrCreateBackend(); // Ensure managers are initialized
579
+ const { name } = args;
580
+ try {
581
+ // Try Datalog-based first
582
+ const manager = getGuaranteeManager();
583
+ if (manager) {
584
+ try {
585
+ await manager.delete(name);
586
+ return textResult(`āœ… Deleted Datalog guarantee: ${name}`);
587
+ }
588
+ catch {
589
+ // Not found in Datalog, try contract-based
590
+ }
591
+ }
592
+ // Try contract-based
593
+ const api = getGuaranteeAPI();
594
+ if (api) {
595
+ const deleted = await api.deleteGuarantee(name);
596
+ if (deleted) {
597
+ return textResult(`āœ… Deleted contract guarantee: ${name}`);
598
+ }
599
+ }
600
+ return errorResult(`Guarantee not found: ${name}`);
601
+ }
602
+ catch (error) {
603
+ return errorResult(`Failed to delete guarantee: ${error.message}`);
604
+ }
605
+ }
606
+ // === COVERAGE & DOCS ===
607
+ export async function handleGetCoverage(args) {
608
+ const db = await getOrCreateBackend();
609
+ const projectPath = getProjectPath();
610
+ const { path: targetPath = projectPath } = args;
611
+ const nodeCount = await db.nodeCount();
612
+ const moduleNodes = db.findByType ? await db.findByType('MODULE') : [];
613
+ return textResult(`Coverage for ${targetPath}:\n` +
614
+ `- Analyzed files: ${moduleNodes.length}\n` +
615
+ `- Total nodes: ${nodeCount}\n`);
616
+ }
617
+ export async function handleGetDocumentation(args) {
618
+ const { topic = 'overview' } = args;
619
+ const docs = {
620
+ overview: `
621
+ # Grafema Code Analysis
622
+
623
+ Grafema is a static code analyzer that builds a graph of your codebase.
624
+
625
+ ## Key Tools
626
+ - query_graph: Execute Datalog queries
627
+ - find_calls: Find function/method calls
628
+ - trace_alias: Trace variable aliases
629
+ - check_invariant: Verify code invariants
630
+
631
+ ## Quick Start
632
+ 1. Use get_stats to see graph size
633
+ 2. Use find_nodes to explore the codebase
634
+ 3. Use query_graph for complex queries
635
+ `,
636
+ queries: `
637
+ # Datalog Queries
638
+
639
+ ## Syntax
640
+ violation(X) :- node(X, "TYPE"), attr(X, "name", "value").
641
+
642
+ ## Available Predicates
643
+ - node(Id, Type) - match nodes
644
+ - edge(Src, Dst, Type) - match edges
645
+ - attr(Id, Name, Value) - match attributes
646
+ - \\+ - negation (not)
647
+
648
+ ## Examples
649
+ Find all functions:
650
+ violation(X) :- node(X, "FUNCTION").
651
+
652
+ Find unresolved calls:
653
+ violation(X) :- node(X, "CALL"), \\+ edge(X, _, "CALLS").
654
+ `,
655
+ types: `
656
+ # Node & Edge Types
657
+
658
+ ## Core Node Types
659
+ - MODULE, FUNCTION, CLASS, METHOD, VARIABLE
660
+ - CALL, IMPORT, EXPORT, PARAMETER
661
+
662
+ ## HTTP/Network
663
+ - http:route, http:request, db:query
664
+
665
+ ## Edge Types
666
+ - CONTAINS, CALLS, DEPENDS_ON
667
+ - ASSIGNED_FROM, INSTANCE_OF, PASSES_ARGUMENT
668
+ `,
669
+ guarantees: `
670
+ # Code Guarantees
671
+
672
+ Guarantees are persistent code invariants.
673
+
674
+ ## Create
675
+ Use create_guarantee with a name and Datalog rule.
676
+
677
+ ## Check
678
+ Use check_guarantees to verify all guarantees.
679
+
680
+ ## Example
681
+ Name: no-eval
682
+ Rule: violation(X) :- node(X, "CALL"), attr(X, "name", "eval").
683
+ `,
684
+ };
685
+ const content = docs[topic] || docs.overview;
686
+ return textResult(content.trim());
687
+ }
688
+ // === BUG REPORTING ===
689
+ export async function handleReportIssue(args) {
690
+ const { title, description, context, labels = ['bug'] } = args;
691
+ const githubToken = process.env.GITHUB_TOKEN;
692
+ const repo = 'Disentinel/grafema';
693
+ // Build issue body
694
+ const body = `## Description
695
+ ${description}
696
+
697
+ ${context ? `## Context\n\`\`\`\n${context}\n\`\`\`\n` : ''}
698
+ ## Environment
699
+ - Grafema version: 0.1.0-alpha.1
700
+ - Reported via: MCP tool
701
+
702
+ ---
703
+ *This issue was automatically created via Grafema MCP server.*`;
704
+ // Try GitHub API if token is available
705
+ if (githubToken) {
706
+ try {
707
+ const response = await fetch(`https://api.github.com/repos/${repo}/issues`, {
708
+ method: 'POST',
709
+ headers: {
710
+ 'Authorization': `token ${githubToken}`,
711
+ 'Content-Type': 'application/json',
712
+ 'Accept': 'application/vnd.github.v3+json',
713
+ },
714
+ body: JSON.stringify({
715
+ title,
716
+ body,
717
+ labels: labels.filter(l => ['bug', 'enhancement', 'documentation', 'question'].includes(l)),
718
+ }),
719
+ });
720
+ if (response.ok) {
721
+ const issue = await response.json();
722
+ return textResult(`āœ… Issue created successfully!\n\n` +
723
+ `**Issue #${issue.number}**: ${issue.html_url}\n\n` +
724
+ `Thank you for reporting this issue.`);
725
+ }
726
+ else {
727
+ const error = await response.text();
728
+ throw new Error(`GitHub API error: ${response.status} - ${error}`);
729
+ }
730
+ }
731
+ catch (error) {
732
+ // Fall through to manual template if API fails
733
+ console.error('[report_issue] GitHub API failed:', error);
734
+ }
735
+ }
736
+ // Fallback: return template for manual submission
737
+ const issueUrl = `https://github.com/${repo}/issues/new`;
738
+ const encodedTitle = encodeURIComponent(title);
739
+ const encodedBody = encodeURIComponent(body);
740
+ const encodedLabels = encodeURIComponent(labels.join(','));
741
+ const directUrl = `${issueUrl}?title=${encodedTitle}&body=${encodedBody}&labels=${encodedLabels}`;
742
+ return textResult(`āš ļø GITHUB_TOKEN not configured. Please create the issue manually:\n\n` +
743
+ `**Quick link** (may truncate long descriptions):\n${directUrl}\n\n` +
744
+ `**Or copy this template to** ${issueUrl}:\n\n` +
745
+ `---\n**Title:** ${title}\n\n${body}\n---\n\n` +
746
+ `To enable automatic issue creation, set GITHUB_TOKEN environment variable.`);
747
+ }