akbo-cli 0.0.0-stage → 1.9.4
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.
- package/README.md +706 -2
- package/dist/catalog_store.d.ts +78 -0
- package/dist/catalog_store.js +375 -0
- package/dist/cleaner.d.ts +11 -0
- package/dist/cleaner.js +92 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +1596 -0
- package/dist/extractors/base.d.ts +35 -0
- package/dist/extractors/base.js +17 -0
- package/dist/extractors/custom_extractor.d.ts +55 -0
- package/dist/extractors/custom_extractor.js +198 -0
- package/dist/extractors/dotnet_extractor.d.ts +11 -0
- package/dist/extractors/dotnet_extractor.js +579 -0
- package/dist/extractors/golang_extractor.d.ts +11 -0
- package/dist/extractors/golang_extractor.js +73 -0
- package/dist/extractors/index.d.ts +14 -0
- package/dist/extractors/index.js +30 -0
- package/dist/extractors/jvm_extractor.d.ts +11 -0
- package/dist/extractors/jvm_extractor.js +319 -0
- package/dist/extractors/node_extractor.d.ts +11 -0
- package/dist/extractors/node_extractor.js +306 -0
- package/dist/extractors/proto_graphql_extractor.d.ts +11 -0
- package/dist/extractors/proto_graphql_extractor.js +125 -0
- package/dist/extractors/python_extractor.d.ts +11 -0
- package/dist/extractors/python_extractor.js +298 -0
- package/dist/extractors/rust_extractor.d.ts +11 -0
- package/dist/extractors/rust_extractor.js +66 -0
- package/dist/graph_builder.d.ts +87 -0
- package/dist/graph_builder.js +1498 -0
- package/dist/harvester.d.ts +38 -0
- package/dist/harvester.js +461 -0
- package/dist/hook_generator.d.ts +26 -0
- package/dist/hook_generator.js +203 -0
- package/dist/importers/skill_importer.d.ts +43 -0
- package/dist/importers/skill_importer.js +470 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.js +17 -0
- package/dist/indexer.d.ts +19 -0
- package/dist/indexer.js +108 -0
- package/dist/init_service.d.ts +37 -0
- package/dist/init_service.js +246 -0
- package/dist/mcp_server.d.ts +10 -0
- package/dist/mcp_server.js +632 -0
- package/dist/pool_resolver.d.ts +40 -0
- package/dist/pool_resolver.js +84 -0
- package/dist/provider.d.ts +135 -0
- package/dist/provider.js +880 -0
- package/dist/query_service.d.ts +33 -0
- package/dist/query_service.js +161 -0
- package/dist/schema_generator.d.ts +236 -0
- package/dist/schema_generator.js +264 -0
- package/dist/standards_loader.d.ts +7 -0
- package/dist/standards_loader.js +33 -0
- package/dist/types.d.ts +265 -0
- package/dist/types.js +105 -0
- package/dist/unit_synthesizer.d.ts +22 -0
- package/dist/unit_synthesizer.js +196 -0
- package/dist/validator.d.ts +13 -0
- package/dist/validator.js +152 -0
- package/dist/version.d.ts +6 -0
- package/dist/version.js +16 -0
- package/dist/watcher.d.ts +38 -0
- package/dist/watcher.js +132 -0
- package/package.json +56 -6
- package/schemas/index-schema.json +98 -0
- package/schemas/knowledge-unit.json +154 -0
|
@@ -0,0 +1,632 @@
|
|
|
1
|
+
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
|
|
2
|
+
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
|
|
3
|
+
import { z } from 'zod';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import fs from 'node:fs';
|
|
6
|
+
import { Harvester } from './harvester.js';
|
|
7
|
+
import { GraphBuilder } from './graph_builder.js';
|
|
8
|
+
import { KnowledgeValidator } from './validator.js';
|
|
9
|
+
import { QueryService } from './query_service.js';
|
|
10
|
+
import { SkillImporter } from './importers/skill_importer.js';
|
|
11
|
+
import { KnowledgePoolResolver } from './pool_resolver.js';
|
|
12
|
+
import { CatalogStore } from './catalog_store.js';
|
|
13
|
+
import { VERSION } from './version.js';
|
|
14
|
+
export function createMcpServer(repoRoot = process.cwd(), poolDirOrConfig) {
|
|
15
|
+
const root = path.resolve(repoRoot);
|
|
16
|
+
let config;
|
|
17
|
+
if (typeof poolDirOrConfig === 'string') {
|
|
18
|
+
config = { poolDir: poolDirOrConfig };
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
config = poolDirOrConfig || {};
|
|
22
|
+
}
|
|
23
|
+
if (config.poolDir) {
|
|
24
|
+
process.env.AKBO_POOL_DIR = config.poolDir;
|
|
25
|
+
}
|
|
26
|
+
// Parse disabled tools from config and environment variable
|
|
27
|
+
const envDisabled = process.env.AKBO_DISABLE_TOOLS
|
|
28
|
+
? process.env.AKBO_DISABLE_TOOLS.split(',').map(s => s.trim().toLowerCase())
|
|
29
|
+
: [];
|
|
30
|
+
const configuredDisabled = (config.disableTools || []).map(s => s.trim().toLowerCase());
|
|
31
|
+
const disabledTools = new Set([...configuredDisabled, ...envDisabled]);
|
|
32
|
+
const zeroTokenOnly = config.zeroTokenOnly ||
|
|
33
|
+
process.env.AKBO_ZERO_TOKEN_ONLY === '1' ||
|
|
34
|
+
process.env.AKBO_ZERO_TOKEN_ONLY === 'true';
|
|
35
|
+
if (zeroTokenOnly) {
|
|
36
|
+
disabledTools.add('akbo_query');
|
|
37
|
+
}
|
|
38
|
+
const disallowRootScan = config.disallowRootScan ||
|
|
39
|
+
process.env.AKBO_DISALLOW_ROOT_SCAN === '1' ||
|
|
40
|
+
process.env.AKBO_DISALLOW_ROOT_SCAN === 'true';
|
|
41
|
+
const envDisallowedDirs = process.env.AKBO_DISALLOW_DIRS
|
|
42
|
+
? process.env.AKBO_DISALLOW_DIRS.split(',').map(s => s.trim())
|
|
43
|
+
: [];
|
|
44
|
+
const disallowedDirs = [...(config.disallowedDirs || []), ...envDisallowedDirs];
|
|
45
|
+
function checkScanAllowed(targetDir) {
|
|
46
|
+
const normTarget = path.resolve(targetDir).replace(/\\/g, '/').toLowerCase();
|
|
47
|
+
const normRoot = path.resolve(root).replace(/\\/g, '/').toLowerCase();
|
|
48
|
+
if (disallowRootScan && normTarget === normRoot) {
|
|
49
|
+
return `Root scan is disallowed by --disallow-root-scan / AKBO_DISALLOW_ROOT_SCAN. Target directory cannot be the root repository: ${root}`;
|
|
50
|
+
}
|
|
51
|
+
if (disallowedDirs.length > 0) {
|
|
52
|
+
for (const d of disallowedDirs) {
|
|
53
|
+
const normD = path.resolve(d).replace(/\\/g, '/').toLowerCase();
|
|
54
|
+
if (normTarget === normD) {
|
|
55
|
+
return `Scanning directory "${targetDir}" is disallowed by configuration (AKBO_DISALLOW_DIRS).`;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return null;
|
|
60
|
+
}
|
|
61
|
+
const server = new McpServer({
|
|
62
|
+
name: 'akbo',
|
|
63
|
+
version: VERSION
|
|
64
|
+
});
|
|
65
|
+
// Tool 1: akbo_context
|
|
66
|
+
if (!disabledTools.has('akbo_context')) {
|
|
67
|
+
server.tool('akbo_context', 'Extract 0-token topological neighborhood (1-2 hops) & active invariants for an entity, class, file, or endpoint before code generation', {
|
|
68
|
+
target: z.string().describe('Target file, endpoint, class, validator, or message name (e.g. "InvoicesController.cs", "CreateInvoiceValidator")'),
|
|
69
|
+
hops: z.number().optional().default(2).describe('Maximum traversal hops (1 or 2, default: 2)'),
|
|
70
|
+
dir: z.string().optional().describe('Codebase directory (default: workspace root)'),
|
|
71
|
+
pool: z.string().optional().describe('External knowledge base pool directory')
|
|
72
|
+
}, async ({ target, hops = 2, dir, pool }) => {
|
|
73
|
+
const targetDir = path.resolve(dir || root);
|
|
74
|
+
const scanError = checkScanAllowed(targetDir);
|
|
75
|
+
if (scanError) {
|
|
76
|
+
return { content: [{ type: 'text', text: scanError }], isError: true };
|
|
77
|
+
}
|
|
78
|
+
const poolConfig = KnowledgePoolResolver.resolve(targetDir, { explicitPoolDir: pool });
|
|
79
|
+
const harvester = new Harvester(targetDir, undefined, poolConfig.poolDir);
|
|
80
|
+
const skeleton = await harvester.harvest({ useCache: true });
|
|
81
|
+
const graphBuilder = new GraphBuilder(targetDir, poolConfig.poolDir);
|
|
82
|
+
const graph = await graphBuilder.buildGraph(skeleton, undefined, { useCache: true });
|
|
83
|
+
const neighborhood = graphBuilder.extractNeighborhood(graph, target, hops);
|
|
84
|
+
if (!neighborhood) {
|
|
85
|
+
return {
|
|
86
|
+
content: [
|
|
87
|
+
{
|
|
88
|
+
type: 'text',
|
|
89
|
+
text: `No graph entity matching "${target}" was found in the codebase graph.`
|
|
90
|
+
}
|
|
91
|
+
]
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
return {
|
|
95
|
+
content: [
|
|
96
|
+
{
|
|
97
|
+
type: 'text',
|
|
98
|
+
text: neighborhood.formattedContext
|
|
99
|
+
}
|
|
100
|
+
]
|
|
101
|
+
};
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
// Tool 2: akbo_query
|
|
105
|
+
if (!disabledTools.has('akbo_query')) {
|
|
106
|
+
server.tool('akbo_query', 'Execute a GraphRAG query: Answer questions strictly grounded in the Codebase Knowledge Graph with cross-lingual seed resolution and zero hallucinations', {
|
|
107
|
+
prompt: z.string().describe('Architectural question or inquiry about the codebase'),
|
|
108
|
+
provider: z.enum(['gemini', 'openai', 'anthropic', 'deepseek', 'groq', 'ollama', 'agy', 'cli'])
|
|
109
|
+
.optional()
|
|
110
|
+
.describe('Execution provider (default: AKB_PROVIDER or "agy")'),
|
|
111
|
+
model: z.string().optional().describe('Model override (e.g. gemini-3.5-flash)'),
|
|
112
|
+
forceCli: z.boolean().optional().describe('Use system Gemini CLI instead of direct API call'),
|
|
113
|
+
dir: z.string().optional().describe('Codebase directory (default: workspace root)'),
|
|
114
|
+
pool: z.string().optional().describe('External knowledge base pool directory')
|
|
115
|
+
}, async ({ prompt, provider, model, forceCli, dir, pool }) => {
|
|
116
|
+
const targetDir = path.resolve(dir || root);
|
|
117
|
+
const scanError = checkScanAllowed(targetDir);
|
|
118
|
+
if (scanError) {
|
|
119
|
+
return { content: [{ type: 'text', text: scanError }], isError: true };
|
|
120
|
+
}
|
|
121
|
+
const poolConfig = KnowledgePoolResolver.resolve(targetDir, { explicitPoolDir: pool });
|
|
122
|
+
const chosenProvider = provider || process.env.AKB_PROVIDER || 'agy';
|
|
123
|
+
// Guard against recursive agent execution if running inside an Antigravity/agent session
|
|
124
|
+
if ((chosenProvider === 'agy' || chosenProvider === 'cli') && process.env.AKBO_NESTED_AGENT === '1') {
|
|
125
|
+
const harvester = new Harvester(targetDir, undefined, poolConfig.poolDir);
|
|
126
|
+
const skeleton = await harvester.harvest({ useCache: true });
|
|
127
|
+
const graphBuilder = new GraphBuilder(targetDir, poolConfig.poolDir);
|
|
128
|
+
const graph = await graphBuilder.buildGraph(skeleton, undefined, { useCache: true });
|
|
129
|
+
const neighborhood = graphBuilder.extractNeighborhood(graph, prompt);
|
|
130
|
+
return {
|
|
131
|
+
content: [
|
|
132
|
+
{
|
|
133
|
+
type: 'text',
|
|
134
|
+
text: JSON.stringify({
|
|
135
|
+
provider: 'direct_graph_slice (nested agent recursion prevented)',
|
|
136
|
+
answer: 'Nested autonomous agent detected (AKBO_NESTED_AGENT=1). Returning grounded architectural context slice directly to avoid recursive agent execution.',
|
|
137
|
+
context: neighborhood?.formattedContext || 'No direct entity match in CKG.',
|
|
138
|
+
keyEntities: neighborhood?.nodes.map(n => n.label) || [],
|
|
139
|
+
activeRules: (neighborhood?.activeInvariants || []).map(i => i.rule)
|
|
140
|
+
}, null, 2)
|
|
141
|
+
}
|
|
142
|
+
]
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
const queryService = new QueryService(targetDir, poolConfig.poolDir);
|
|
146
|
+
const result = await queryService.query(prompt, {
|
|
147
|
+
dir: targetDir,
|
|
148
|
+
poolDir: poolConfig.poolDir,
|
|
149
|
+
model,
|
|
150
|
+
forceCli,
|
|
151
|
+
providerName: chosenProvider
|
|
152
|
+
});
|
|
153
|
+
const responsePayload = {
|
|
154
|
+
provider: chosenProvider,
|
|
155
|
+
answer: result.answer,
|
|
156
|
+
keyEntities: result.keyEntities,
|
|
157
|
+
activeRules: result.activeRules
|
|
158
|
+
};
|
|
159
|
+
return {
|
|
160
|
+
content: [
|
|
161
|
+
{
|
|
162
|
+
type: 'text',
|
|
163
|
+
text: JSON.stringify(responsePayload, null, 2)
|
|
164
|
+
}
|
|
165
|
+
]
|
|
166
|
+
};
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
// Tool 3: akbo_harvest
|
|
170
|
+
if (!disabledTools.has('akbo_harvest')) {
|
|
171
|
+
server.tool('akbo_harvest', 'Deterministically scan codebase topology, manifests, schemas, endpoints, and validation rules (0 LLM tokens)', {
|
|
172
|
+
dir: z.string().optional().describe('Codebase directory (default: workspace root)'),
|
|
173
|
+
pool: z.string().optional().describe('External knowledge base pool directory'),
|
|
174
|
+
useCache: z.boolean().optional().default(true).describe('Use cached skeleton if codebase is unchanged'),
|
|
175
|
+
summaryOnly: z.boolean().optional().default(true).describe('Return concise summary of topology, manifests, schemas, and routes instead of full skeleton JSON (default: true)')
|
|
176
|
+
}, async ({ dir, pool, useCache = true, summaryOnly = true }) => {
|
|
177
|
+
const targetDir = path.resolve(dir || root);
|
|
178
|
+
const scanError = checkScanAllowed(targetDir);
|
|
179
|
+
if (scanError) {
|
|
180
|
+
return { content: [{ type: 'text', text: scanError }], isError: true };
|
|
181
|
+
}
|
|
182
|
+
const poolConfig = KnowledgePoolResolver.resolve(targetDir, { explicitPoolDir: pool });
|
|
183
|
+
const harvester = new Harvester(targetDir, undefined, poolConfig.poolDir);
|
|
184
|
+
const skeleton = await harvester.harvest({ useCache });
|
|
185
|
+
let outputText;
|
|
186
|
+
if (summaryOnly) {
|
|
187
|
+
const summary = {
|
|
188
|
+
targetDir,
|
|
189
|
+
topology: skeleton.topology,
|
|
190
|
+
manifestsCount: skeleton.manifests.length,
|
|
191
|
+
manifests: skeleton.manifests.map(m => ({ type: m.type, path: m.path, name: m.contentSummary?.name })),
|
|
192
|
+
schemasCount: skeleton.schemas.length,
|
|
193
|
+
schemas: skeleton.schemas.map(s => ({ type: s.type, file: s.file, tablesOrModels: s.tablesOrModels })),
|
|
194
|
+
routesCount: skeleton.routesAndCommands.length,
|
|
195
|
+
sampleRoutes: skeleton.routesAndCommands.slice(0, 20),
|
|
196
|
+
cacheFile: poolConfig.cacheFile
|
|
197
|
+
};
|
|
198
|
+
outputText = JSON.stringify(summary, null, 2);
|
|
199
|
+
}
|
|
200
|
+
else {
|
|
201
|
+
outputText = JSON.stringify(skeleton, null, 2);
|
|
202
|
+
}
|
|
203
|
+
return {
|
|
204
|
+
content: [
|
|
205
|
+
{
|
|
206
|
+
type: 'text',
|
|
207
|
+
text: outputText
|
|
208
|
+
}
|
|
209
|
+
]
|
|
210
|
+
};
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
// Tool 4: akbo_validate
|
|
214
|
+
if (!disabledTools.has('akbo_validate')) {
|
|
215
|
+
server.tool('akbo_validate', 'Validate knowledge/ directory against the AKB runtime schema and federation standards', {
|
|
216
|
+
dir: z.string().optional().describe('Codebase directory (default: workspace root)'),
|
|
217
|
+
pool: z.string().optional().describe('External knowledge base pool directory')
|
|
218
|
+
}, async ({ dir, pool }) => {
|
|
219
|
+
const targetDir = path.resolve(dir || root);
|
|
220
|
+
const poolConfig = KnowledgePoolResolver.resolve(targetDir, { explicitPoolDir: pool });
|
|
221
|
+
const validator = new KnowledgeValidator(targetDir, poolConfig.poolDir);
|
|
222
|
+
const result = await validator.validate();
|
|
223
|
+
return {
|
|
224
|
+
content: [
|
|
225
|
+
{
|
|
226
|
+
type: 'text',
|
|
227
|
+
text: JSON.stringify(result, null, 2)
|
|
228
|
+
}
|
|
229
|
+
]
|
|
230
|
+
};
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
// Tool 5: akbo_graph
|
|
234
|
+
if (!disabledTools.has('akbo_graph')) {
|
|
235
|
+
server.tool('akbo_graph', 'Deterministically build the Codebase Knowledge Graph (CKG) linking projects, endpoints, handlers, messages, schemas, validators, and invariants (0 tokens)', {
|
|
236
|
+
dir: z.string().optional().describe('Codebase directory (default: workspace root)'),
|
|
237
|
+
pool: z.string().optional().describe('External knowledge base pool directory'),
|
|
238
|
+
format: z.enum(['json', 'mermaid', 'markdown']).optional().default('json').describe('Export format: json, mermaid, markdown'),
|
|
239
|
+
clustered: z.boolean().optional().default(true).describe('Group nodes by domain subgraphs in Mermaid format (default: true)'),
|
|
240
|
+
nodeTypes: z.array(z.string()).optional().describe('Filter by node types (e.g. ["project", "endpoint", "handler"])'),
|
|
241
|
+
domain: z.string().optional().describe('Filter by architectural domain'),
|
|
242
|
+
maxNodes: z.number().optional().default(100).describe('Maximum nodes to return (default: 100)'),
|
|
243
|
+
useCache: z.boolean().optional().default(true).describe('Use cached skeleton if codebase is unchanged')
|
|
244
|
+
}, async ({ dir, pool, format = 'json', clustered = true, nodeTypes, domain, maxNodes = 100, useCache = true }) => {
|
|
245
|
+
const targetDir = path.resolve(dir || root);
|
|
246
|
+
const scanError = checkScanAllowed(targetDir);
|
|
247
|
+
if (scanError) {
|
|
248
|
+
return { content: [{ type: 'text', text: scanError }], isError: true };
|
|
249
|
+
}
|
|
250
|
+
const poolConfig = KnowledgePoolResolver.resolve(targetDir, { explicitPoolDir: pool });
|
|
251
|
+
const harvester = new Harvester(targetDir, undefined, poolConfig.poolDir);
|
|
252
|
+
const skeleton = await harvester.harvest({ useCache });
|
|
253
|
+
const graphBuilder = new GraphBuilder(targetDir, poolConfig.poolDir);
|
|
254
|
+
const graph = await graphBuilder.buildGraph(skeleton, undefined, { useCache });
|
|
255
|
+
let filteredNodes = graph.nodes;
|
|
256
|
+
if (nodeTypes && nodeTypes.length > 0) {
|
|
257
|
+
filteredNodes = filteredNodes.filter(n => nodeTypes.includes(n.type));
|
|
258
|
+
}
|
|
259
|
+
if (domain) {
|
|
260
|
+
const dLower = domain.toLowerCase();
|
|
261
|
+
filteredNodes = filteredNodes.filter(n => n.domain?.toLowerCase() === dLower);
|
|
262
|
+
}
|
|
263
|
+
const truncatedNodes = filteredNodes.slice(0, maxNodes);
|
|
264
|
+
const allowedNodeIds = new Set(truncatedNodes.map(n => n.id));
|
|
265
|
+
const filteredEdges = graph.edges.filter(e => allowedNodeIds.has(e.source) && allowedNodeIds.has(e.target));
|
|
266
|
+
const processedGraph = {
|
|
267
|
+
...graph,
|
|
268
|
+
nodes: truncatedNodes,
|
|
269
|
+
edges: filteredEdges
|
|
270
|
+
};
|
|
271
|
+
let output = '';
|
|
272
|
+
if (format === 'mermaid') {
|
|
273
|
+
output = graphBuilder.exportMermaid(processedGraph, { clustered });
|
|
274
|
+
}
|
|
275
|
+
else if (format === 'markdown') {
|
|
276
|
+
output = `# Codebase Knowledge Graph\n\nTotal Entities: ${processedGraph.nodes.length} (of ${graph.nodes.length}) | Relationships: ${processedGraph.edges.length}\n\n\`\`\`mermaid\n${graphBuilder.exportMermaid(processedGraph, { clustered })}\n\`\`\`\n`;
|
|
277
|
+
}
|
|
278
|
+
else {
|
|
279
|
+
output = JSON.stringify({
|
|
280
|
+
totalGraphNodes: graph.nodes.length,
|
|
281
|
+
returnedNodes: processedGraph.nodes.length,
|
|
282
|
+
nodes: processedGraph.nodes,
|
|
283
|
+
edges: processedGraph.edges
|
|
284
|
+
}, null, 2);
|
|
285
|
+
}
|
|
286
|
+
return {
|
|
287
|
+
content: [
|
|
288
|
+
{
|
|
289
|
+
type: 'text',
|
|
290
|
+
text: output
|
|
291
|
+
}
|
|
292
|
+
]
|
|
293
|
+
};
|
|
294
|
+
});
|
|
295
|
+
}
|
|
296
|
+
// Tool 6: akbo_list_invariants
|
|
297
|
+
if (!disabledTools.has('akbo_list_invariants')) {
|
|
298
|
+
server.tool('akbo_list_invariants', 'Retrieve architectural invariants and compliance rules from knowledge units, optionally filtered by target file, domain, or search query (0 LLM tokens)', {
|
|
299
|
+
file: z.string().optional().describe('Target file path to find applicable invariants for (matches applies_to patterns)'),
|
|
300
|
+
domain: z.string().optional().describe('Domain or category filter (e.g. "architecture", "development", "product", "decisions")'),
|
|
301
|
+
query: z.string().optional().describe('Substring or keyword search across rule, title, id, reason, do, dont'),
|
|
302
|
+
dir: z.string().optional().describe('Codebase directory (default: workspace root)'),
|
|
303
|
+
pool: z.string().optional().describe('External knowledge base pool directory')
|
|
304
|
+
}, async ({ file, domain, query, dir, pool }) => {
|
|
305
|
+
const targetDir = path.resolve(dir || root);
|
|
306
|
+
const poolConfig = KnowledgePoolResolver.resolve(targetDir, { explicitPoolDir: pool });
|
|
307
|
+
const graphBuilder = new GraphBuilder(targetDir, poolConfig.poolDir);
|
|
308
|
+
const units = graphBuilder.loadKnowledgeUnits();
|
|
309
|
+
let filtered = units;
|
|
310
|
+
if (domain) {
|
|
311
|
+
const dLower = domain.toLowerCase();
|
|
312
|
+
filtered = filtered.filter(u => (u.domain && u.domain.toLowerCase() === dLower) ||
|
|
313
|
+
(u.id && u.id.toLowerCase().startsWith(dLower)));
|
|
314
|
+
}
|
|
315
|
+
if (file) {
|
|
316
|
+
filtered = filtered.filter(u => graphBuilder.matchesAppliesTo(u, file));
|
|
317
|
+
}
|
|
318
|
+
if (query) {
|
|
319
|
+
const qLower = query.toLowerCase().trim();
|
|
320
|
+
filtered = filtered.filter(u => {
|
|
321
|
+
const matchId = u.id && u.id.toLowerCase().includes(qLower);
|
|
322
|
+
const matchTitle = u.title && u.title.toLowerCase().includes(qLower);
|
|
323
|
+
const matchRule = u.rule && u.rule.toLowerCase().includes(qLower);
|
|
324
|
+
const matchReason = u.reason && u.reason.toLowerCase().includes(qLower);
|
|
325
|
+
const matchDo = Array.isArray(u.do) && u.do.some(d => d.toLowerCase().includes(qLower));
|
|
326
|
+
const matchDont = Array.isArray(u.dont) && u.dont.some(d => d.toLowerCase().includes(qLower));
|
|
327
|
+
const matchApplies = Array.isArray(u.applies_to) && u.applies_to.some(a => a.toLowerCase().includes(qLower));
|
|
328
|
+
return matchId || matchTitle || matchRule || matchReason || matchDo || matchDont || matchApplies;
|
|
329
|
+
});
|
|
330
|
+
}
|
|
331
|
+
const output = filtered.map(u => ({
|
|
332
|
+
id: u.id,
|
|
333
|
+
type: u.type,
|
|
334
|
+
status: u.status,
|
|
335
|
+
title: u.title,
|
|
336
|
+
domain: u.domain || 'general',
|
|
337
|
+
rule: u.rule,
|
|
338
|
+
reason: u.reason,
|
|
339
|
+
applies_to: u.applies_to || [],
|
|
340
|
+
do: u.do || [],
|
|
341
|
+
dont: u.dont || [],
|
|
342
|
+
related_items: u.related_items || [],
|
|
343
|
+
authority: u.authority,
|
|
344
|
+
skill_file: u.skill_file,
|
|
345
|
+
reference_files: u.reference_files,
|
|
346
|
+
workflow_gates: u.workflow_gates
|
|
347
|
+
}));
|
|
348
|
+
return {
|
|
349
|
+
content: [
|
|
350
|
+
{
|
|
351
|
+
type: 'text',
|
|
352
|
+
text: JSON.stringify(output, null, 2)
|
|
353
|
+
}
|
|
354
|
+
]
|
|
355
|
+
};
|
|
356
|
+
});
|
|
357
|
+
}
|
|
358
|
+
// Tool 7: akbo_search (0-Token Entity & Symbol Search)
|
|
359
|
+
if (!disabledTools.has('akbo_search')) {
|
|
360
|
+
server.tool('akbo_search', 'Fast, zero-token in-memory entity search across symbols, endpoints, handlers, schemas, projects, and invariants in <5ms without calling an LLM', {
|
|
361
|
+
query: z.string().describe('Search query (class name, endpoint route, symbol, or file path)'),
|
|
362
|
+
type: z.enum(['all', 'project', 'endpoint', 'handler', 'schema', 'message', 'invariant']).optional().default('all'),
|
|
363
|
+
domain: z.string().optional().describe('Filter by architectural domain'),
|
|
364
|
+
limit: z.number().optional().default(20).describe('Maximum results to return (default 20)'),
|
|
365
|
+
dir: z.string().optional().describe('Codebase directory (default: workspace root)'),
|
|
366
|
+
pool: z.string().optional().describe('External knowledge base pool directory')
|
|
367
|
+
}, async ({ query, type = 'all', domain, limit = 20, dir, pool }) => {
|
|
368
|
+
const targetDir = path.resolve(dir || root);
|
|
369
|
+
const scanError = checkScanAllowed(targetDir);
|
|
370
|
+
if (scanError) {
|
|
371
|
+
return { content: [{ type: 'text', text: scanError }], isError: true };
|
|
372
|
+
}
|
|
373
|
+
const poolConfig = KnowledgePoolResolver.resolve(targetDir, { explicitPoolDir: pool });
|
|
374
|
+
const harvester = new Harvester(targetDir, undefined, poolConfig.poolDir);
|
|
375
|
+
const skeleton = await harvester.harvest({ useCache: true });
|
|
376
|
+
const graphBuilder = new GraphBuilder(targetDir, poolConfig.poolDir);
|
|
377
|
+
const graph = await graphBuilder.buildGraph(skeleton, undefined, { useCache: true });
|
|
378
|
+
const results = graphBuilder.searchNodes(graph, {
|
|
379
|
+
query,
|
|
380
|
+
type,
|
|
381
|
+
domain,
|
|
382
|
+
limit
|
|
383
|
+
});
|
|
384
|
+
return {
|
|
385
|
+
content: [
|
|
386
|
+
{
|
|
387
|
+
type: 'text',
|
|
388
|
+
text: JSON.stringify(results, null, 2)
|
|
389
|
+
}
|
|
390
|
+
]
|
|
391
|
+
};
|
|
392
|
+
});
|
|
393
|
+
}
|
|
394
|
+
// Tool 8: akbo_impact (Blast-Radius & Inbound Dependents)
|
|
395
|
+
if (!disabledTools.has('akbo_impact')) {
|
|
396
|
+
server.tool('akbo_impact', 'Calculate directional blast radius and inbound callers/dependents for a target symbol, endpoint, or project before editing code', {
|
|
397
|
+
target: z.string().describe('Target node ID, symbol name, project name, or file to evaluate impact for'),
|
|
398
|
+
maxDepth: z.number().optional().default(3).describe('Maximum traversal depth along inbound edges (default 3)'),
|
|
399
|
+
dir: z.string().optional().describe('Codebase directory (default: workspace root)'),
|
|
400
|
+
pool: z.string().optional().describe('External knowledge base pool directory')
|
|
401
|
+
}, async ({ target, maxDepth = 3, dir, pool }) => {
|
|
402
|
+
const targetDir = path.resolve(dir || root);
|
|
403
|
+
const scanError = checkScanAllowed(targetDir);
|
|
404
|
+
if (scanError) {
|
|
405
|
+
return { content: [{ type: 'text', text: scanError }], isError: true };
|
|
406
|
+
}
|
|
407
|
+
const poolConfig = KnowledgePoolResolver.resolve(targetDir, { explicitPoolDir: pool });
|
|
408
|
+
const harvester = new Harvester(targetDir, undefined, poolConfig.poolDir);
|
|
409
|
+
const skeleton = await harvester.harvest({ useCache: true });
|
|
410
|
+
const graphBuilder = new GraphBuilder(targetDir, poolConfig.poolDir);
|
|
411
|
+
const graph = await graphBuilder.buildGraph(skeleton, undefined, { useCache: true });
|
|
412
|
+
const impact = graphBuilder.calculateInboundImpact(graph, target, { maxDepth });
|
|
413
|
+
return {
|
|
414
|
+
content: [
|
|
415
|
+
{
|
|
416
|
+
type: 'text',
|
|
417
|
+
text: JSON.stringify(impact, null, 2)
|
|
418
|
+
}
|
|
419
|
+
]
|
|
420
|
+
};
|
|
421
|
+
});
|
|
422
|
+
}
|
|
423
|
+
// Tool 9: akbo_upsert_unit (Write or Update Knowledge Unit)
|
|
424
|
+
if (!disabledTools.has('akbo_upsert_unit')) {
|
|
425
|
+
server.tool('akbo_upsert_unit', 'Add or update knowledge standards or decisions in the external pool. Supports single unit or batch array, validates schema, writes YAML, and updates index.yaml', {
|
|
426
|
+
id: z.string().optional().describe('Unique rule ID (e.g. no-direct-db-in-controllers)'),
|
|
427
|
+
domain: z.string().optional().describe('Architectural domain (e.g. architecture, development, decisions)'),
|
|
428
|
+
title: z.string().optional().describe('Summary title of the standard'),
|
|
429
|
+
applies_to: z.array(z.string()).optional().describe('Glob patterns matching applicable files'),
|
|
430
|
+
rule: z.string().optional().describe('Rule directive statement'),
|
|
431
|
+
do: z.array(z.string()).optional().describe('Required or recommended practices'),
|
|
432
|
+
dont: z.array(z.string()).optional().describe('Forbidden practices'),
|
|
433
|
+
reason: z.string().optional().describe('Architectural reasoning or risk explanation'),
|
|
434
|
+
decision: z.string().optional().describe('Architecture decision statement (for architecture_decision)'),
|
|
435
|
+
context: z.string().optional().describe('Context and problem statement (for architecture_decision)'),
|
|
436
|
+
type: z.string().optional().describe('Unit type (development_standard, architecture_standard, architecture_decision, product_rule)'),
|
|
437
|
+
status: z.string().optional().describe('Approval status (approved, draft, deprecated)'),
|
|
438
|
+
severity: z.enum(['error', 'warning', 'info']).optional().default('warning'),
|
|
439
|
+
authority: z.object({ level: z.string(), confidence: z.string() }).optional().describe('Authority level (default: human_curated)'),
|
|
440
|
+
units: z.array(z.record(z.string(), z.any())).optional().describe('Array of unit objects for atomic batch upsert'),
|
|
441
|
+
validate: z.boolean().optional().default(true).describe('Run KnowledgeValidator after upserting (default: true)'),
|
|
442
|
+
dir: z.string().optional().describe('Codebase directory'),
|
|
443
|
+
pool: z.string().optional().describe('External knowledge base pool directory')
|
|
444
|
+
}, async (args) => {
|
|
445
|
+
const targetDir = path.resolve(args.dir || root);
|
|
446
|
+
let unitsToUpsert = [];
|
|
447
|
+
if (args.units && Array.isArray(args.units) && args.units.length > 0) {
|
|
448
|
+
unitsToUpsert = args.units;
|
|
449
|
+
}
|
|
450
|
+
else if (args.id) {
|
|
451
|
+
unitsToUpsert = [{
|
|
452
|
+
id: args.id,
|
|
453
|
+
type: args.type,
|
|
454
|
+
title: args.title,
|
|
455
|
+
domain: args.domain,
|
|
456
|
+
status: args.status,
|
|
457
|
+
authority: args.authority,
|
|
458
|
+
applies_to: args.applies_to,
|
|
459
|
+
rule: args.rule,
|
|
460
|
+
do: args.do,
|
|
461
|
+
dont: args.dont,
|
|
462
|
+
reason: args.reason,
|
|
463
|
+
decision: args.decision,
|
|
464
|
+
context: args.context
|
|
465
|
+
}];
|
|
466
|
+
}
|
|
467
|
+
else {
|
|
468
|
+
return {
|
|
469
|
+
content: [{ type: 'text', text: 'Either "id" or "units" must be provided to akbo_upsert_unit.' }],
|
|
470
|
+
isError: true
|
|
471
|
+
};
|
|
472
|
+
}
|
|
473
|
+
const result = await CatalogStore.upsertUnits(unitsToUpsert, {
|
|
474
|
+
targetDir,
|
|
475
|
+
poolDir: args.pool,
|
|
476
|
+
validate: args.validate !== false
|
|
477
|
+
});
|
|
478
|
+
if (!result.success) {
|
|
479
|
+
return {
|
|
480
|
+
content: [{ type: 'text', text: `Upsert failed: ${result.error}` }],
|
|
481
|
+
isError: true
|
|
482
|
+
};
|
|
483
|
+
}
|
|
484
|
+
return {
|
|
485
|
+
content: [
|
|
486
|
+
{
|
|
487
|
+
type: 'text',
|
|
488
|
+
text: JSON.stringify(result, null, 2)
|
|
489
|
+
}
|
|
490
|
+
]
|
|
491
|
+
};
|
|
492
|
+
});
|
|
493
|
+
}
|
|
494
|
+
// Tool 9b: akbo_upsert_units (Explicit Batch Upsert)
|
|
495
|
+
if (!disabledTools.has('akbo_upsert_units') && !disabledTools.has('akbo_upsert_unit')) {
|
|
496
|
+
server.tool('akbo_upsert_units', 'Batch upsert multiple knowledge units atomically into the knowledge pool and return validation report', {
|
|
497
|
+
units: z.array(z.record(z.string(), z.any())).describe('Array of knowledge unit objects to upsert'),
|
|
498
|
+
validate: z.boolean().optional().default(true).describe('Run KnowledgeValidator after upserting (default: true)'),
|
|
499
|
+
dir: z.string().optional().describe('Codebase directory'),
|
|
500
|
+
pool: z.string().optional().describe('External knowledge base pool directory')
|
|
501
|
+
}, async ({ units, validate = true, dir, pool }) => {
|
|
502
|
+
const targetDir = path.resolve(dir || root);
|
|
503
|
+
const result = await CatalogStore.upsertUnits(units, {
|
|
504
|
+
targetDir,
|
|
505
|
+
poolDir: pool,
|
|
506
|
+
validate
|
|
507
|
+
});
|
|
508
|
+
if (!result.success) {
|
|
509
|
+
return {
|
|
510
|
+
content: [{ type: 'text', text: `Batch upsert failed: ${result.error}` }],
|
|
511
|
+
isError: true
|
|
512
|
+
};
|
|
513
|
+
}
|
|
514
|
+
return {
|
|
515
|
+
content: [
|
|
516
|
+
{
|
|
517
|
+
type: 'text',
|
|
518
|
+
text: JSON.stringify(result, null, 2)
|
|
519
|
+
}
|
|
520
|
+
]
|
|
521
|
+
};
|
|
522
|
+
});
|
|
523
|
+
}
|
|
524
|
+
// Tool 9c: akbo_get_unit (Retrieve Unit by ID)
|
|
525
|
+
if (!disabledTools.has('akbo_get_unit')) {
|
|
526
|
+
server.tool('akbo_get_unit', 'Retrieve a knowledge unit YAML and metadata by its unique ID', {
|
|
527
|
+
id: z.string().min(1).describe('Unique rule ID to fetch'),
|
|
528
|
+
dir: z.string().optional().describe('Codebase directory'),
|
|
529
|
+
pool: z.string().optional().describe('External knowledge base pool directory')
|
|
530
|
+
}, async ({ id, dir, pool }) => {
|
|
531
|
+
const targetDir = path.resolve(dir || root);
|
|
532
|
+
const result = CatalogStore.getUnit(id, { targetDir, poolDir: pool });
|
|
533
|
+
if (!result) {
|
|
534
|
+
return {
|
|
535
|
+
content: [{ type: 'text', text: `Knowledge unit "${id}" not found.` }],
|
|
536
|
+
isError: true
|
|
537
|
+
};
|
|
538
|
+
}
|
|
539
|
+
return {
|
|
540
|
+
content: [
|
|
541
|
+
{
|
|
542
|
+
type: 'text',
|
|
543
|
+
text: JSON.stringify(result, null, 2)
|
|
544
|
+
}
|
|
545
|
+
]
|
|
546
|
+
};
|
|
547
|
+
});
|
|
548
|
+
}
|
|
549
|
+
// Tool 9d: akbo_delete_unit (Delete Unit by ID)
|
|
550
|
+
if (!disabledTools.has('akbo_delete_unit')) {
|
|
551
|
+
server.tool('akbo_delete_unit', 'Delete a knowledge unit from the pool, remove its index entry, and decrement counts', {
|
|
552
|
+
id: z.string().min(1).describe('Unique rule ID to delete'),
|
|
553
|
+
validate: z.boolean().optional().default(true).describe('Validate catalog after deletion (default: true)'),
|
|
554
|
+
dir: z.string().optional().describe('Codebase directory'),
|
|
555
|
+
pool: z.string().optional().describe('External knowledge base pool directory')
|
|
556
|
+
}, async ({ id, validate = true, dir, pool }) => {
|
|
557
|
+
const targetDir = path.resolve(dir || root);
|
|
558
|
+
const result = await CatalogStore.deleteUnit(id, { targetDir, poolDir: pool, validate });
|
|
559
|
+
if (!result.success) {
|
|
560
|
+
return {
|
|
561
|
+
content: [{ type: 'text', text: result.error || 'Failed to delete unit' }],
|
|
562
|
+
isError: true
|
|
563
|
+
};
|
|
564
|
+
}
|
|
565
|
+
return {
|
|
566
|
+
content: [
|
|
567
|
+
{
|
|
568
|
+
type: 'text',
|
|
569
|
+
text: JSON.stringify(result, null, 2)
|
|
570
|
+
}
|
|
571
|
+
]
|
|
572
|
+
};
|
|
573
|
+
});
|
|
574
|
+
}
|
|
575
|
+
// Tool 10: akbo_import_skills
|
|
576
|
+
if (!disabledTools.has('akbo_import_skills')) {
|
|
577
|
+
server.tool('akbo_import_skills', 'Import agent skills (.agents/skills/**/SKILL.md) into the central knowledge pool', {
|
|
578
|
+
sourceGlobs: z.array(z.string()).optional().describe('Custom globs to search for SKILL.md files'),
|
|
579
|
+
domain: z.string().optional().default('development').describe('Domain to assign to imported skills'),
|
|
580
|
+
dir: z.string().optional().describe('Codebase directory (default: workspace root)'),
|
|
581
|
+
pool: z.string().optional().describe('External knowledge base pool directory'),
|
|
582
|
+
force: z.boolean().optional().default(false).describe('Overwrite existing units')
|
|
583
|
+
}, async ({ sourceGlobs, domain = 'development', dir, pool, force = false }) => {
|
|
584
|
+
const targetDir = path.resolve(dir || root);
|
|
585
|
+
const poolConfig = KnowledgePoolResolver.resolve(targetDir, { explicitPoolDir: pool });
|
|
586
|
+
const importer = new SkillImporter(targetDir, poolConfig.poolDir);
|
|
587
|
+
const result = await importer.importSkills({
|
|
588
|
+
repoRoot: targetDir,
|
|
589
|
+
poolDir: poolConfig.poolDir,
|
|
590
|
+
sourceGlobs,
|
|
591
|
+
domain,
|
|
592
|
+
force
|
|
593
|
+
});
|
|
594
|
+
GraphBuilder.clearCache();
|
|
595
|
+
return {
|
|
596
|
+
content: [
|
|
597
|
+
{
|
|
598
|
+
type: 'text',
|
|
599
|
+
text: JSON.stringify(result, null, 2)
|
|
600
|
+
}
|
|
601
|
+
]
|
|
602
|
+
};
|
|
603
|
+
});
|
|
604
|
+
}
|
|
605
|
+
// Resource: akbo://knowledge/index
|
|
606
|
+
server.resource('catalog', 'akbo://knowledge/index', async (uri) => {
|
|
607
|
+
const poolConfig = KnowledgePoolResolver.resolve(root);
|
|
608
|
+
const indexPath = path.join(poolConfig.knowledgeDir, 'index.yaml');
|
|
609
|
+
const text = fs.existsSync(indexPath) ? fs.readFileSync(indexPath, 'utf-8') : '# No index.yaml found';
|
|
610
|
+
return {
|
|
611
|
+
contents: [
|
|
612
|
+
{
|
|
613
|
+
uri: uri.href,
|
|
614
|
+
text
|
|
615
|
+
}
|
|
616
|
+
]
|
|
617
|
+
};
|
|
618
|
+
});
|
|
619
|
+
return server;
|
|
620
|
+
}
|
|
621
|
+
export async function runMcpServer(repoRoot = process.cwd(), poolDirOrConfig, config) {
|
|
622
|
+
let resolvedConfig;
|
|
623
|
+
if (typeof poolDirOrConfig === 'string') {
|
|
624
|
+
resolvedConfig = { ...(config || {}), poolDir: poolDirOrConfig };
|
|
625
|
+
}
|
|
626
|
+
else {
|
|
627
|
+
resolvedConfig = poolDirOrConfig || config || {};
|
|
628
|
+
}
|
|
629
|
+
const server = createMcpServer(repoRoot, resolvedConfig);
|
|
630
|
+
const transport = new StdioServerTransport();
|
|
631
|
+
await server.connect(transport);
|
|
632
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
export interface PoolConfig {
|
|
2
|
+
/** The root directory of the code being scanned (e.g. C:/repos/EnterpriseApp/Services/Billing) */
|
|
3
|
+
targetDir: string;
|
|
4
|
+
/** The root directory where knowledge and caches reside (e.g. C:/AKBO/EnterprisePool) */
|
|
5
|
+
poolDir: string;
|
|
6
|
+
/** True if poolDir is external to targetDir */
|
|
7
|
+
isExternalPool: boolean;
|
|
8
|
+
/** Directory containing knowledge YAML files: poolDir/knowledge */
|
|
9
|
+
knowledgeDir: string;
|
|
10
|
+
/** Central standards file: poolDir/.akb/standards.yaml */
|
|
11
|
+
standardsFile: string;
|
|
12
|
+
/** Custom extractors file: poolDir/.akb/custom-extractors.yaml */
|
|
13
|
+
customExtractorsFile: string;
|
|
14
|
+
/** Directory where caches are stored: poolDir/.akb/caches */
|
|
15
|
+
cacheDir: string;
|
|
16
|
+
/** Specific cache file keyed by targetDir: poolDir/.akb/caches/<slug>-<hash>.json */
|
|
17
|
+
cacheFile: string;
|
|
18
|
+
}
|
|
19
|
+
export interface ResolveOptions {
|
|
20
|
+
explicitPoolDir?: string;
|
|
21
|
+
overrideEnv?: string;
|
|
22
|
+
}
|
|
23
|
+
export declare class KnowledgePoolResolver {
|
|
24
|
+
/**
|
|
25
|
+
* Resolves targetDir and poolDir using precedence:
|
|
26
|
+
* 1. Explicit argument (--pool / options.explicitPoolDir)
|
|
27
|
+
* 2. Environment variable AKBO_POOL_DIR
|
|
28
|
+
* 3. Upward directory walk from targetDir looking for knowledge/index.yaml, .akb/, or .git
|
|
29
|
+
* 4. Fallback: targetDir
|
|
30
|
+
*/
|
|
31
|
+
static resolve(targetDirInput?: string, options?: ResolveOptions): PoolConfig;
|
|
32
|
+
/**
|
|
33
|
+
* Traverses upward from startDir seeking an existing knowledge/ or .akb/ directory.
|
|
34
|
+
*/
|
|
35
|
+
static findNearestPool(startDir: string): string | null;
|
|
36
|
+
/**
|
|
37
|
+
* Helper to ensure cache directory exists before writing cache files.
|
|
38
|
+
*/
|
|
39
|
+
static ensureCacheDir(config: PoolConfig): void;
|
|
40
|
+
}
|