|
| 1 | +import { McpServer, ResourceTemplate } from '@modelcontextprotocol/sdk/server/mcp.js'; |
| 2 | +import type { TopologyState } from './state.js'; |
| 3 | + |
| 4 | +export function registerResources(server: McpServer, state: TopologyState): void { |
| 5 | + // Resource 1: Full topology graph |
| 6 | + server.resource( |
| 7 | + 'graph', |
| 8 | + 'topology://graph', |
| 9 | + { description: 'Full topology graph with all nodes and edges (dependency + semantic)' }, |
| 10 | + async () => { |
| 11 | + const graph = await state.ensureGraph(); |
| 12 | + return { |
| 13 | + contents: [ |
| 14 | + { |
| 15 | + uri: 'topology://graph', |
| 16 | + mimeType: 'application/json', |
| 17 | + text: JSON.stringify(graph, null, 2), |
| 18 | + }, |
| 19 | + ], |
| 20 | + }; |
| 21 | + }, |
| 22 | + ); |
| 23 | + |
| 24 | + // Resource 2: Summary statistics |
| 25 | + server.resource( |
| 26 | + 'stats', |
| 27 | + 'topology://stats', |
| 28 | + { description: 'Summary statistics: node count, edge count, broken edges, semantic edges, languages' }, |
| 29 | + async () => { |
| 30 | + const graph = await state.ensureGraph(); |
| 31 | + |
| 32 | + const languageCounts: Record<string, number> = {}; |
| 33 | + for (const node of graph.nodes) { |
| 34 | + const lang = node.language ?? 'unknown'; |
| 35 | + languageCounts[lang] = (languageCounts[lang] ?? 0) + 1; |
| 36 | + } |
| 37 | + |
| 38 | + const stats = { |
| 39 | + nodeCount: graph.nodes.length, |
| 40 | + edgeCount: graph.edges.length, |
| 41 | + dependencyEdges: graph.edges.filter((e) => e.linkType !== 'semantic').length, |
| 42 | + semanticEdges: graph.edges.filter((e) => e.linkType === 'semantic').length, |
| 43 | + brokenEdges: graph.edges.filter((e) => e.isBroken).length, |
| 44 | + languages: languageCounts, |
| 45 | + timestamp: graph.timestamp, |
| 46 | + }; |
| 47 | + |
| 48 | + return { |
| 49 | + contents: [ |
| 50 | + { |
| 51 | + uri: 'topology://stats', |
| 52 | + mimeType: 'application/json', |
| 53 | + text: JSON.stringify(stats, null, 2), |
| 54 | + }, |
| 55 | + ], |
| 56 | + }; |
| 57 | + }, |
| 58 | + ); |
| 59 | + |
| 60 | + // Resource 3: Node detail by file path (ResourceTemplate) |
| 61 | + server.resource( |
| 62 | + 'node', |
| 63 | + new ResourceTemplate('topology://node/{filePath}', { list: undefined }), |
| 64 | + { description: 'Node details and related edges for a specific file (use URL-encoded path)' }, |
| 65 | + async (uri, params) => { |
| 66 | + const graph = await state.ensureGraph(); |
| 67 | + const filePath = decodeURIComponent(params.filePath as string); |
| 68 | + |
| 69 | + const node = graph.nodes.find((n) => n.id === filePath); |
| 70 | + if (!node) { |
| 71 | + return { |
| 72 | + contents: [ |
| 73 | + { |
| 74 | + uri: uri.href, |
| 75 | + mimeType: 'application/json', |
| 76 | + text: JSON.stringify({ error: `Node not found: ${filePath}` }), |
| 77 | + }, |
| 78 | + ], |
| 79 | + }; |
| 80 | + } |
| 81 | + |
| 82 | + const imports = graph.edges.filter( |
| 83 | + (e) => e.source === filePath && e.linkType !== 'semantic', |
| 84 | + ); |
| 85 | + const importedBy = graph.edges.filter( |
| 86 | + (e) => e.target === filePath && e.linkType !== 'semantic', |
| 87 | + ); |
| 88 | + const similar = graph.edges.filter( |
| 89 | + (e) => |
| 90 | + e.linkType === 'semantic' && |
| 91 | + (e.source === filePath || e.target === filePath), |
| 92 | + ); |
| 93 | + |
| 94 | + const detail = { |
| 95 | + node, |
| 96 | + imports: imports.map((e) => ({ target: e.target, isBroken: e.isBroken })), |
| 97 | + importedBy: importedBy.map((e) => ({ source: e.source, isBroken: e.isBroken })), |
| 98 | + similar: similar.map((e) => ({ |
| 99 | + file: e.source === filePath ? e.target : e.source, |
| 100 | + similarity: e.similarity, |
| 101 | + })), |
| 102 | + }; |
| 103 | + |
| 104 | + return { |
| 105 | + contents: [ |
| 106 | + { |
| 107 | + uri: uri.href, |
| 108 | + mimeType: 'application/json', |
| 109 | + text: JSON.stringify(detail, null, 2), |
| 110 | + }, |
| 111 | + ], |
| 112 | + }; |
| 113 | + }, |
| 114 | + ); |
| 115 | +} |
0 commit comments