kibi-mcp 0.3.3 → 0.4.0

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.
@@ -83,6 +83,35 @@ export const DIAGNOSTIC_TELEMETRY_SCHEMA = {
83
83
  },
84
84
  },
85
85
  };
86
+ // implements REQ-002
87
+ export function deriveDiagnosticFields(toolName, args, telemetry, result) {
88
+ const fields = {
89
+ telemetry_status: telemetry ? "provided" : "missing",
90
+ };
91
+ const structuredContent = result && typeof result === "object" && "structuredContent" in result
92
+ ? result.structuredContent
93
+ : undefined;
94
+ if (toolName === "kb_query" || toolName === "kb_search") {
95
+ const resultCount = Number(structuredContent?.count ?? 0);
96
+ fields.result_count = resultCount;
97
+ fields.zero_results = resultCount === 0;
98
+ fields.result_summary =
99
+ resultCount === 0 ? "0 results" : `${resultCount} results`;
100
+ }
101
+ if (toolName === "kb_check") {
102
+ const violationCount = Number(structuredContent?.count ?? 0);
103
+ fields.violation_count = violationCount;
104
+ fields.requested_rules = Array.isArray(args.rules) ? args.rules : [];
105
+ fields.result_summary =
106
+ violationCount === 0
107
+ ? "0 violations"
108
+ : `${violationCount} violations`;
109
+ }
110
+ if (!fields.result_summary) {
111
+ fields.result_summary = `${toolName} completed`;
112
+ }
113
+ return fields;
114
+ }
86
115
  /**
87
116
  * Extract business args and telemetry from tool call arguments.
88
117
  */
@@ -104,8 +104,13 @@ export const PROMPTS = [
104
104
  "",
105
105
  "Treat this server as a branch-aware knowledge graph interface for software traceability.",
106
106
  "",
107
- "The server exposes 4 public tools for KB operations:",
108
- "- `kb_query`: Read-only lookup of entities by type, ID, tags, or source file",
107
+ "The server exposes a curated public tool surface for KB operations:",
108
+ "- `kb_search`: Discovery across metadata and markdown body text",
109
+ "- `kb_query`: Exact lookup of entities by type, ID, tags, or source file",
110
+ "- `kb_status`: Branch, snapshot, and freshness inspection",
111
+ "- `kb_find_gaps`: Bulk missing/present relationship analysis",
112
+ "- `kb_coverage`: Curated coverage reporting",
113
+ "- `kb_graph`: Bounded graph traversal from seed IDs",
109
114
  "- `kb_upsert`: Create or update entities and their relationships",
110
115
  "- `kb_delete`: Remove entities by ID (with dependency safety checks)",
111
116
  "- `kb_check`: Validate KB integrity against configurable rules",
@@ -113,7 +118,7 @@ export const PROMPTS = [
113
118
  "Core modeling principles:",
114
119
  "- Encode requirements as linked facts: `req --constrains--> fact` plus `req --requires_property--> fact`.",
115
120
  "- Reuse canonical fact IDs across requirements; shared constrained facts make contradictions detectable.",
116
- "- Use `kb_query` first to confirm current state before any mutation.",
121
+ "- Use `kb_search` first for discovery, then `kb_query` for exact follow-up before any mutation.",
117
122
  "- Use `kb_upsert` and `kb_delete` only for intentional, traceable KB changes.",
118
123
  "- Run `kb_check` after meaningful mutations to catch integrity issues early.",
119
124
  "- Prefer explicit IDs and enum values to avoid invalid parameters.",
@@ -128,7 +133,7 @@ export const PROMPTS = [
128
133
  "",
129
134
  "Follow this sequence for reliable operation:",
130
135
  "",
131
- "1. **Query-first**: Always call `kb_query` to confirm current state before any mutation.",
136
+ "1. **Discover first**: Call `kb_search` for exploratory discovery, then `kb_query` to confirm exact current state before mutation.",
132
137
  "2. **Create-before-link**: Create endpoint entities with `kb_upsert` before linking them.",
133
138
  "3. **Validate intent**: If creating links, call `kb_query` for both endpoint IDs first to ensure they exist.",
134
139
  "4. **Model requirements as facts**: For new/updated reqs, create/reuse fact entities first, then express req semantics with `constrains` + `requires_property`.",
@@ -167,8 +172,8 @@ function registerDocResources() {
167
172
  "kibi-mcp is a stdio MCP server for querying and mutating the Kibi knowledge base.",
168
173
  "",
169
174
  "Scope:",
170
- "- Entity CRUD-like operations for KB records",
171
- "- Validation of KB integrity after changes",
175
+ "- Read-only discovery and reporting (`kb_search`, `kb_query`, `kb_status`, `kb_find_gaps`, `kb_coverage`, `kb_graph`)",
176
+ "- KB mutation and validation (`kb_upsert`, `kb_delete`, `kb_check`)",
172
177
  "- Automatic branch-local attachment for the active workspace",
173
178
  "",
174
179
  "Use this server when you need branch-local, machine-readable project memory.",
@@ -188,6 +193,11 @@ function registerDocResources() {
188
193
  const examples = [
189
194
  "# kibi-mcp Examples",
190
195
  "",
196
+ "## Discover before mutating",
197
+ '1. `kb_search` with `{ "query": "login flow" }` to discover related requirements, tests, and ADRs',
198
+ '2. `kb_query` with `{ "type": "req", "sourceFile": "src/auth/login.ts" }` for exact follow-up',
199
+ '3. `kb_status` with `{}` when branch attachment or freshness confidence matters',
200
+ "",
191
201
  "## Model requirements as reusable facts",
192
202
  '1. `kb_query` with `{ "type": "fact" }` to find existing fact IDs before creating new ones',
193
203
  "2. `kb_upsert` for the fact entity first (create-before-link)",
@@ -195,6 +205,11 @@ function registerDocResources() {
195
205
  "4. Reuse the same constrained fact ID across related requirements; vary property facts only when semantics differ",
196
206
  '5. `kb_check` with `{ "rules": ["required-fields","no-dangling-refs"] }` for targeted validation',
197
207
  "",
208
+ "## Find missing coverage",
209
+ '1. `kb_find_gaps` with `{ "type": "req", "missingRelationships": ["specified_by", "verified_by"] }` to find under-linked requirements',
210
+ '2. `kb_coverage` with `{ "by": "req", "includePassing": false }` to review evaluated coverage rows',
211
+ '3. `kb_graph` with `{ "seedIds": ["REQ-001"], "direction": "both", "depth": 2 }` to inspect neighboring entities',
212
+ "",
198
213
  "## Add a requirement and link it to a test",
199
214
  '1. `kb_query` with `{ "type": "test" }` to check for existing test IDs',
200
215
  '2. `kb_query` with `{ "id": "REQ-XXX" }` to verify the requirement exists',
@@ -17,11 +17,16 @@
17
17
  */
18
18
  import process from "node:process";
19
19
  import { z } from "zod";
20
- import { DIAGNOSTIC_MODE_ENABLED, appendUsageLogLine, extractToolCallPayload, } from "../diagnostics.js";
20
+ import { DIAGNOSTIC_MODE_ENABLED, appendUsageLogLine, deriveDiagnosticFields, extractToolCallPayload, } from "../diagnostics.js";
21
+ import { handleKbCoverage } from "../tools/coverage.js";
21
22
  import { TOOLS } from "../tools-config.js";
22
23
  import { handleKbCheck } from "../tools/check.js";
23
24
  import { handleKbDelete } from "../tools/delete.js";
25
+ import { handleKbFindGaps } from "../tools/find-gaps.js";
26
+ import { handleKbGraph } from "../tools/graph.js";
24
27
  import { handleKbQuery } from "../tools/query.js";
28
+ import { handleKbSearch } from "../tools/search.js";
29
+ import { handleKbStatus } from "../tools/status.js";
25
30
  import { handleKbUpsert } from "../tools/upsert.js";
26
31
  import { activeBranchName, ensureProlog, inFlightRequests, isShuttingDown, prologProcess, } from "./session.js";
27
32
  const ACTIVE_TOOLS = TOOLS;
@@ -164,6 +169,7 @@ function addTool(server, name, description, inputSchema, handler) {
164
169
  // Log usage in diagnostic mode
165
170
  if (DIAGNOSTIC_MODE_ENABLED) {
166
171
  const finishedAt = new Date();
172
+ const diagnosticFields = deriveDiagnosticFields(name, businessArgs, telemetry, result);
167
173
  appendUsageLogLine({
168
174
  timestamp: finishedAt.toISOString(),
169
175
  request_id: requestId,
@@ -176,6 +182,7 @@ function addTool(server, name, description, inputSchema, handler) {
176
182
  duration_ms: finishedAt.getTime() - startedAt.getTime(),
177
183
  prolog_pid: prologProcess?.getPid() ?? null,
178
184
  active_branch: activeBranchName,
185
+ ...diagnosticFields,
179
186
  });
180
187
  }
181
188
  return result;
@@ -218,6 +225,7 @@ function addTool(server, name, description, inputSchema, handler) {
218
225
  };
219
226
  server.registerTool(name, { description, inputSchema: jsonSchemaToZod(inputSchema) }, wrappedHandler);
220
227
  }
228
+ // implements REQ-002, REQ-013
221
229
  export function registerAllTools(server) {
222
230
  const toolDef = (name) => {
223
231
  const t = ACTIVE_TOOLS.find((t) => t.name === name);
@@ -229,6 +237,26 @@ export function registerAllTools(server) {
229
237
  const prolog = await ensureProlog();
230
238
  return handleKbQuery(prolog, args);
231
239
  });
240
+ addTool(server, "kb_search", toolDef("kb_search").description, toolDef("kb_search").inputSchema, async (args) => {
241
+ const prolog = await ensureProlog();
242
+ return handleKbSearch(prolog, args);
243
+ });
244
+ addTool(server, "kb_status", toolDef("kb_status").description, toolDef("kb_status").inputSchema, async (args) => {
245
+ const prolog = await ensureProlog();
246
+ return handleKbStatus(prolog, args);
247
+ });
248
+ addTool(server, "kb_find_gaps", toolDef("kb_find_gaps").description, toolDef("kb_find_gaps").inputSchema, async (args) => {
249
+ const prolog = await ensureProlog();
250
+ return handleKbFindGaps(prolog, args);
251
+ });
252
+ addTool(server, "kb_coverage", toolDef("kb_coverage").description, toolDef("kb_coverage").inputSchema, async (args) => {
253
+ const prolog = await ensureProlog();
254
+ return handleKbCoverage(prolog, args);
255
+ });
256
+ addTool(server, "kb_graph", toolDef("kb_graph").description, toolDef("kb_graph").inputSchema, async (args) => {
257
+ const prolog = await ensureProlog();
258
+ return handleKbGraph(prolog, args);
259
+ });
232
260
  addTool(server, "kb_upsert", toolDef("kb_upsert").description, toolDef("kb_upsert").inputSchema, async (args) => {
233
261
  const prolog = await ensureProlog();
234
262
  return handleKbUpsert(prolog, args);
@@ -64,6 +64,24 @@ function formatDiagnosticsForMcp(diagnostics) {
64
64
  suggestion: d.suggestion,
65
65
  }));
66
66
  }
67
+ function formatViolationText(violations) {
68
+ if (violations.length === 0) {
69
+ return "No violations found";
70
+ }
71
+ const details = violations.map((violation) => {
72
+ const parts = [
73
+ violation.rule,
74
+ violation.entityId,
75
+ violation.source ?? "unknown-source",
76
+ violation.description,
77
+ ];
78
+ if (violation.suggestion) {
79
+ parts.push(`Suggestion: ${violation.suggestion}`);
80
+ }
81
+ return `- ${parts.join(" | ")}`;
82
+ });
83
+ return `${violations.length} violations found\n${details.join("\n")}`;
84
+ }
67
85
  // implements REQ-002
68
86
  function loadChecksConfig(workspaceRoot) {
69
87
  const configPath = path.join(workspaceRoot, ".kb", "config.json");
@@ -151,9 +169,7 @@ export async function handleKbCheck(prolog, args) {
151
169
  file: v.source,
152
170
  suggestion: v.suggestion,
153
171
  }));
154
- const summary = aggregatedViolations.length === 0
155
- ? "No violations found"
156
- : `${aggregatedViolations.length} violations found`;
172
+ const summary = formatViolationText(aggregatedViolations);
157
173
  return {
158
174
  content: [
159
175
  {
@@ -0,0 +1,83 @@
1
+ import { existsSync } from "node:fs";
2
+ import { createRequire } from "node:module";
3
+ import path from "node:path";
4
+ import { PrologProcess } from "kibi-cli/prolog";
5
+ import { escapeAtomContent } from "kibi-cli/prolog/codec";
6
+ const require = createRequire(import.meta.url);
7
+ // implements REQ-002, REQ-013
8
+ export function resolveCorePlPath(fileName) {
9
+ const envKey = `KIBI_${fileName.replace(/\W/g, "_").toUpperCase()}_PATH`;
10
+ const override = process.env[envKey];
11
+ if (override && existsSync(override)) {
12
+ return override;
13
+ }
14
+ try {
15
+ const installedPath = require.resolve(`kibi-core/src/${fileName}`);
16
+ if (existsSync(installedPath)) {
17
+ return installedPath;
18
+ }
19
+ }
20
+ catch { }
21
+ const localPath = path.join(process.cwd(), "packages/core/src", fileName);
22
+ if (existsSync(localPath)) {
23
+ return localPath;
24
+ }
25
+ throw new Error(`Unable to resolve core module path for ${fileName}`);
26
+ }
27
+ // implements REQ-002, REQ-013
28
+ export async function runJsonModuleQuery(prolog, fileName, goal, errorLabel) {
29
+ const modulePath = escapeAtomContent(resolveCorePlPath(fileName).replace(/\\/g, "/"));
30
+ if (!(prolog instanceof PrologProcess)) {
31
+ const mockedResult = await prolog.query(`(use_module('${modulePath}'), ${goal})`);
32
+ if (!mockedResult.success) {
33
+ throw new Error(`${errorLabel} query failed: ${mockedResult.error || "Unknown error"}`);
34
+ }
35
+ const mockedJson = mockedResult.bindings.JsonString;
36
+ if (!mockedJson) {
37
+ throw new Error(`${errorLabel} query returned no JsonString binding`);
38
+ }
39
+ let mockedParsed = JSON.parse(mockedJson);
40
+ if (typeof mockedParsed === "string") {
41
+ mockedParsed = JSON.parse(mockedParsed);
42
+ }
43
+ return mockedParsed;
44
+ }
45
+ const oneShotCapable = prolog;
46
+ prolog.invalidateCache();
47
+ const result = oneShotCapable.useOneShotMode
48
+ ? await prolog.query(`(use_module('${modulePath}'), ${goal})`)
49
+ : await runInteractiveModuleQuery(prolog, modulePath, goal, errorLabel);
50
+ if (!result.success) {
51
+ throw new Error(`${errorLabel} query failed: ${result.error || "Unknown error"}`);
52
+ }
53
+ const rawJson = result.bindings.JsonString;
54
+ if (!rawJson) {
55
+ throw new Error(`${errorLabel} query returned no JsonString binding`);
56
+ }
57
+ let parsed = JSON.parse(rawJson);
58
+ if (typeof parsed === "string") {
59
+ parsed = JSON.parse(parsed);
60
+ }
61
+ return parsed;
62
+ }
63
+ async function runInteractiveModuleQuery(prolog, modulePath, goal, errorLabel) {
64
+ const loadResult = await prolog.query(`use_module('${modulePath}')`);
65
+ if (!loadResult.success) {
66
+ throw new Error(`${errorLabel} module load failed: ${loadResult.error || "Unknown error"}`);
67
+ }
68
+ return prolog.query(goal);
69
+ }
70
+ // implements REQ-002, REQ-013
71
+ export function toPrologAtom(value) {
72
+ if (!value) {
73
+ return "none";
74
+ }
75
+ return `'${escapeAtomContent(value)}'`;
76
+ }
77
+ // implements REQ-002, REQ-013
78
+ export function toPrologList(values) {
79
+ if (!values || values.length === 0) {
80
+ return "[]";
81
+ }
82
+ return `[${values.map((value) => `'${escapeAtomContent(value)}'`).join(",")}]`;
83
+ }
@@ -0,0 +1,28 @@
1
+ import { runJsonModuleQuery, toPrologList } from "./core-module.js";
2
+ // implements REQ-002, REQ-013
3
+ export async function handleKbCoverage(prolog, args) {
4
+ const by = args.by ?? "req";
5
+ const limit = args.limit ?? 100;
6
+ const offset = args.offset ?? 0;
7
+ const includePassing = args.includePassing ?? false;
8
+ const includeTransitive = args.includeTransitive ?? true;
9
+ try {
10
+ const payload = await runJsonModuleQuery(prolog, "discovery.pl", `discovery:coverage_report_json('${by}', ${toPrologList(args.tags)}, ${includePassing}, ${includeTransitive}, ${limit}, ${offset}, JsonString)`, "Coverage execution");
11
+ const summary = payload?.summary ?? {};
12
+ const fullyCovered = Number(summary.fullyCovered ?? 0);
13
+ const total = Number(summary.total ?? 0);
14
+ return {
15
+ content: [
16
+ {
17
+ type: "text",
18
+ text: `Coverage summary: ${fullyCovered} fully covered out of ${total}.`,
19
+ },
20
+ ],
21
+ structuredContent: payload,
22
+ };
23
+ }
24
+ catch (error) {
25
+ const message = error instanceof Error ? error.message : String(error);
26
+ throw new Error(`Coverage execution failed: ${message}`);
27
+ }
28
+ }
@@ -0,0 +1,110 @@
1
+ import { escapeAtomContent, parseEntityFromBinding, parseEntityFromList, parseListOfLists, } from "kibi-cli/prolog/codec";
2
+ export const VALID_ENTITY_TYPES = [
3
+ "req",
4
+ "scenario",
5
+ "test",
6
+ "adr",
7
+ "flag",
8
+ "event",
9
+ "symbol",
10
+ "fact",
11
+ ];
12
+ // implements REQ-002, REQ-013
13
+ export function validateEntityType(type) {
14
+ if (type && !VALID_ENTITY_TYPES.includes(type)) {
15
+ throw new Error(`Invalid type '${type}'. Valid types: ${VALID_ENTITY_TYPES.join(", ")}. Use a single type value, or omit this parameter to query all entities.`);
16
+ }
17
+ }
18
+ // implements REQ-002, REQ-013
19
+ export function buildEntityGoal(args) {
20
+ const { type, id, tags, sourceFile } = args;
21
+ if (sourceFile) {
22
+ const safeSource = escapeAtomContent(sourceFile);
23
+ if (type) {
24
+ const safeType = escapeAtomContent(type);
25
+ return `findall([Id,'${safeType}',Props], (kb_entities_by_source('${safeSource}', SourceIds), member(Id, SourceIds), kb_entity(Id, '${safeType}', Props)), Results)`;
26
+ }
27
+ return `findall([Id,Type,Props], (kb_entities_by_source('${safeSource}', SourceIds), member(Id, SourceIds), kb_entity(Id, Type, Props)), Results)`;
28
+ }
29
+ if (id && type) {
30
+ const safeId = escapeAtomContent(id);
31
+ const safeType = escapeAtomContent(type);
32
+ return `findall(['${safeId}','${safeType}',Props], kb_entity('${safeId}', '${safeType}', Props), Results)`;
33
+ }
34
+ if (id) {
35
+ const safeId = escapeAtomContent(id);
36
+ return `findall(['${safeId}',Type,Props], kb_entity('${safeId}', Type, Props), Results)`;
37
+ }
38
+ if (tags && tags.length > 0) {
39
+ if (type) {
40
+ const safeType = escapeAtomContent(type);
41
+ return `findall([Id,'${safeType}',Props], kb_entity(Id, '${safeType}', Props), Results)`;
42
+ }
43
+ return "findall([Id,Type,Props], kb_entity(Id, Type, Props), Results)";
44
+ }
45
+ if (type) {
46
+ const safeType = escapeAtomContent(type);
47
+ return `findall([Id,'${safeType}',Props], kb_entity(Id, '${safeType}', Props), Results)`;
48
+ }
49
+ return "findall([Id,Type,Props], kb_entity(Id, Type, Props), Results)";
50
+ }
51
+ // implements REQ-002, REQ-013
52
+ export async function loadEntities(prolog, args) {
53
+ validateEntityType(args.type);
54
+ const goal = buildEntityGoal(args);
55
+ const queryResult = await prolog.query(goal);
56
+ let results = [];
57
+ if (!queryResult.success) {
58
+ throw new Error(queryResult.error || "Query failed with unknown error");
59
+ }
60
+ if (queryResult.bindings.Results) {
61
+ const entitiesData = parseListOfLists(queryResult.bindings.Results);
62
+ for (const data of entitiesData) {
63
+ results.push(parseEntityFromList(data));
64
+ }
65
+ }
66
+ else if (queryResult.bindings.Result) {
67
+ results = [parseEntityFromBinding(queryResult.bindings.Result)];
68
+ }
69
+ if (args.tags && args.tags.length > 0) {
70
+ const requestedTags = args.tags;
71
+ results = dedupeEntities(results.filter((entity) => hasAnyTag(entity, requestedTags)));
72
+ }
73
+ return dedupeEntities(results);
74
+ }
75
+ // implements REQ-002
76
+ export function paginateResults(results, limit = 100, offset = 0) {
77
+ return results.slice(offset, offset + limit);
78
+ }
79
+ function hasAnyTag(entity, requestedTags) {
80
+ const expected = new Set(requestedTags.map(normalizeTagValue));
81
+ const rawTags = entity.tags;
82
+ if (!Array.isArray(rawTags) || rawTags.length === 0) {
83
+ return false;
84
+ }
85
+ for (const tag of rawTags) {
86
+ if (expected.has(normalizeTagValue(tag))) {
87
+ return true;
88
+ }
89
+ }
90
+ return false;
91
+ }
92
+ function normalizeTagValue(tag) {
93
+ return String(tag).trim();
94
+ }
95
+ // implements REQ-002
96
+ export function dedupeEntities(entities) {
97
+ const seen = new Set();
98
+ const deduped = [];
99
+ for (const entity of entities) {
100
+ const id = String(entity.id ?? "");
101
+ const type = String(entity.type ?? "");
102
+ const key = `${type}::${id}`;
103
+ if (seen.has(key)) {
104
+ continue;
105
+ }
106
+ seen.add(key);
107
+ deduped.push(entity);
108
+ }
109
+ return deduped;
110
+ }
@@ -0,0 +1,29 @@
1
+ import { runJsonModuleQuery, toPrologAtom, toPrologList } from "./core-module.js";
2
+ import { validateEntityType } from "./entity-query.js";
3
+ // implements REQ-002, REQ-013
4
+ export async function handleKbFindGaps(prolog, args) {
5
+ validateEntityType(args.type);
6
+ const limit = args.limit ?? 100;
7
+ const offset = args.offset ?? 0;
8
+ try {
9
+ const payload = await runJsonModuleQuery(prolog, "discovery.pl", `discovery:find_gaps_json(${toPrologAtom(args.type)}, ${toPrologList(args.missingRelationships)}, ${toPrologList(args.presentRelationships)}, ${toPrologList(args.tags)}, ${toPrologAtom(args.sourceFile)}, ${limit}, ${offset}, JsonString)`, "Find-gaps execution");
10
+ const rows = payload?.rows ?? [];
11
+ return {
12
+ content: [
13
+ {
14
+ type: "text",
15
+ text: rows.length === 0
16
+ ? "No matching gaps found."
17
+ : `Found ${payload?.count ?? rows.length} gap rows. Showing ${rows.length}: ${rows
18
+ .map((row) => row.id)
19
+ .join(", ")}`,
20
+ },
21
+ ],
22
+ structuredContent: payload,
23
+ };
24
+ }
25
+ catch (error) {
26
+ const message = error instanceof Error ? error.message : String(error);
27
+ throw new Error(`Find-gaps execution failed: ${message}`);
28
+ }
29
+ }
@@ -0,0 +1,27 @@
1
+ import { runJsonModuleQuery, toPrologList } from "./core-module.js";
2
+ // implements REQ-002, REQ-013
3
+ export async function handleKbGraph(prolog, args) {
4
+ const direction = args.direction ?? "outgoing";
5
+ const depth = args.depth ?? 1;
6
+ const maxNodes = args.maxNodes ?? 200;
7
+ const maxEdges = args.maxEdges ?? 500;
8
+ try {
9
+ const payload = await runJsonModuleQuery(prolog, "discovery.pl", `discovery:graph_expand_json(${toPrologList(args.seedIds)}, ${toPrologList(args.relationships)}, '${direction}', ${depth}, ${toPrologList(args.entityTypes)}, ${maxNodes}, ${maxEdges}, JsonString)`, "Graph execution");
10
+ const nodes = payload?.nodes ?? [];
11
+ return {
12
+ content: [
13
+ {
14
+ type: "text",
15
+ text: nodes.length === 0
16
+ ? "Graph traversal returned no nodes."
17
+ : `Graph traversal returned ${nodes.length} nodes and ${(payload?.edges ?? []).length} edges from ${args.seedIds.join(", ")}.`,
18
+ },
19
+ ],
20
+ structuredContent: payload,
21
+ };
22
+ }
23
+ catch (error) {
24
+ const message = error instanceof Error ? error.message : String(error);
25
+ throw new Error(`Graph execution failed: ${message}`);
26
+ }
27
+ }
@@ -1,14 +1,5 @@
1
- import { escapeAtomContent, parseEntityFromBinding, parseEntityFromList, parseListOfLists, } from "kibi-cli/prolog/codec";
2
- export const VALID_ENTITY_TYPES = [
3
- "req",
4
- "scenario",
5
- "test",
6
- "adr",
7
- "flag",
8
- "event",
9
- "symbol",
10
- "fact",
11
- ];
1
+ import { loadEntities, paginateResults, } from "./entity-query.js";
2
+ export { VALID_ENTITY_TYPES } from "./entity-query.js";
12
3
  /**
13
4
  * Handle kb.query tool calls
14
5
  * Reuses query logic from CLI command
@@ -16,73 +7,8 @@ export const VALID_ENTITY_TYPES = [
16
7
  export async function handleKbQuery(prolog, args) {
17
8
  const { type, id, tags, sourceFile, limit = 100, offset = 0 } = args;
18
9
  try {
19
- let results = [];
20
- // Validate type if provided
21
- if (type) {
22
- if (!VALID_ENTITY_TYPES.includes(type)) {
23
- throw new Error(`Invalid type '${type}'. Valid types: ${VALID_ENTITY_TYPES.join(", ")}. Use a single type value, or omit this parameter to query all entities.`);
24
- }
25
- }
26
- // Build Prolog query
27
- let goal;
28
- if (sourceFile) {
29
- const safeSource = escapeAtomContent(sourceFile);
30
- if (type) {
31
- const safeType = escapeAtomContent(type);
32
- goal = `findall([Id,'${safeType}',Props], (kb_entities_by_source('${safeSource}', SourceIds), member(Id, SourceIds), kb_entity(Id, '${safeType}', Props)), Results)`;
33
- }
34
- else {
35
- goal = `findall([Id,Type,Props], (kb_entities_by_source('${safeSource}', SourceIds), member(Id, SourceIds), kb_entity(Id, Type, Props)), Results)`;
36
- }
37
- }
38
- else if (id && type) {
39
- const safeId = escapeAtomContent(id);
40
- const safeType = escapeAtomContent(type);
41
- goal = `findall(['${safeId}','${safeType}',Props], kb_entity('${safeId}', '${safeType}', Props), Results)`;
42
- }
43
- else if (id) {
44
- const safeId = escapeAtomContent(id);
45
- goal = `findall(['${safeId}',Type,Props], kb_entity('${safeId}', Type, Props), Results)`;
46
- }
47
- else if (tags && tags.length > 0) {
48
- // JS-side fallback until REQ-mcp-tag-filtering-server-side is implemented.
49
- if (type) {
50
- const safeType = escapeAtomContent(type);
51
- goal = `findall([Id,'${safeType}',Props], kb_entity(Id, '${safeType}', Props), Results)`;
52
- }
53
- else {
54
- goal = "findall([Id,Type,Props], kb_entity(Id, Type, Props), Results)";
55
- }
56
- }
57
- else if (type) {
58
- const safeType = escapeAtomContent(type);
59
- goal = `findall([Id,'${safeType}',Props], kb_entity(Id, '${safeType}', Props), Results)`;
60
- }
61
- else {
62
- goal = "findall([Id,Type,Props], kb_entity(Id, Type, Props), Results)";
63
- }
64
- const queryResult = await prolog.query(goal);
65
- if (queryResult.success) {
66
- if (queryResult.bindings.Results) {
67
- const entitiesData = parseListOfLists(queryResult.bindings.Results);
68
- for (const data of entitiesData) {
69
- const entity = parseEntityFromList(data);
70
- results.push(entity);
71
- }
72
- }
73
- else if (queryResult.bindings.Result) {
74
- const entity = parseEntityFromBinding(queryResult.bindings.Result);
75
- results = [entity];
76
- }
77
- }
78
- else {
79
- throw new Error(queryResult.error || "Query failed with unknown error");
80
- }
81
- if (tags && tags.length > 0) {
82
- results = dedupeEntities(results.filter((entity) => hasAnyTag(entity, tags)));
83
- }
84
- // Apply pagination
85
- const paginated = results.slice(offset, offset + limit);
10
+ const results = await loadEntities(prolog, { type, id, tags, sourceFile });
11
+ const paginated = paginateResults(results, limit, offset);
86
12
  // Build human-readable text with entity IDs and titles
87
13
  let text;
88
14
  if (results.length === 0) {
@@ -118,34 +44,3 @@ export async function handleKbQuery(prolog, args) {
118
44
  throw new Error(`Query execution failed: ${message}`);
119
45
  }
120
46
  }
121
- function hasAnyTag(entity, requestedTags) {
122
- const expected = new Set(requestedTags.map(normalizeTagValue));
123
- const rawTags = entity.tags;
124
- if (!Array.isArray(rawTags) || rawTags.length === 0) {
125
- return false;
126
- }
127
- for (const tag of rawTags) {
128
- if (expected.has(normalizeTagValue(tag))) {
129
- return true;
130
- }
131
- }
132
- return false;
133
- }
134
- function normalizeTagValue(tag) {
135
- return String(tag).trim();
136
- }
137
- function dedupeEntities(entities) {
138
- const seen = new Set();
139
- const deduped = [];
140
- for (const entity of entities) {
141
- const id = String(entity.id ?? "");
142
- const type = String(entity.type ?? "");
143
- const key = `${type}::${id}`;
144
- if (seen.has(key)) {
145
- continue;
146
- }
147
- seen.add(key);
148
- deduped.push(entity);
149
- }
150
- return deduped;
151
- }
@@ -0,0 +1,37 @@
1
+ import { rankEntities } from "kibi-cli/search-ranking";
2
+ import { loadEntities, paginateResults, validateEntityType, } from "./entity-query.js";
3
+ import { resolveWorkspaceRoot } from "../workspace.js";
4
+ // implements REQ-mcp-search-discovery, REQ-002
5
+ export async function handleKbSearch(prolog, args) {
6
+ const { query, type, limit = 20, offset = 0 } = args;
7
+ const trimmedQuery = query.trim();
8
+ if (!trimmedQuery) {
9
+ throw new Error("Search execution failed: query must be a non-empty string");
10
+ }
11
+ validateEntityType(type);
12
+ try {
13
+ const workspaceRoot = resolveWorkspaceRoot();
14
+ const entities = await loadEntities(prolog, { type });
15
+ const matches = await rankEntities(entities, trimmedQuery, workspaceRoot);
16
+ const paginated = paginateResults(matches, limit, offset);
17
+ const text = matches.length === 0
18
+ ? `No search results for '${trimmedQuery}'.`
19
+ : `Found ${matches.length} search results for '${trimmedQuery}'. Showing ${paginated.length} (offset ${offset}, limit ${limit}): ${paginated
20
+ .map((match) => `${match.entity.id} [${match.reasons.join(", ")}]`)
21
+ .join(", ")}`;
22
+ return {
23
+ content: [{ type: "text", text }],
24
+ structuredContent: {
25
+ results: paginated,
26
+ count: matches.length,
27
+ },
28
+ };
29
+ }
30
+ catch (error) {
31
+ const message = error instanceof Error ? error.message : String(error);
32
+ if (message.startsWith("Search execution failed:")) {
33
+ throw error;
34
+ }
35
+ throw new Error(`Search execution failed: ${message}`);
36
+ }
37
+ }
@@ -0,0 +1,20 @@
1
+ import { runJsonModuleQuery } from "./core-module.js";
2
+ // implements REQ-002, REQ-013
3
+ export async function handleKbStatus(prolog, _args) {
4
+ try {
5
+ const payload = await runJsonModuleQuery(prolog, "status.pl", "status:kb_status_json(JsonString)", "Status execution");
6
+ return {
7
+ content: [
8
+ {
9
+ type: "text",
10
+ text: `Branch ${payload.branch} is ${payload.syncState} (snapshot ${payload.snapshotId}, dirty=${payload.dirty})`,
11
+ },
12
+ ],
13
+ structuredContent: payload,
14
+ };
15
+ }
16
+ catch (error) {
17
+ const message = error instanceof Error ? error.message : String(error);
18
+ throw new Error(`Status execution failed: ${message}`);
19
+ }
20
+ }
@@ -64,6 +64,172 @@ const BASE_TOOLS = [
64
64
  },
65
65
  },
66
66
  },
67
+ {
68
+ name: "kb_search",
69
+ description: "Search KB entities for discovery using metadata and markdown body text. Use for exploratory lookup before exact follow-up with kb_query. No mutation side effects.",
70
+ inputSchema: {
71
+ type: "object",
72
+ required: ["query"],
73
+ properties: {
74
+ query: {
75
+ type: "string",
76
+ description: "Free-text query for metadata and markdown body discovery. Example: 'OAuth login flow'.",
77
+ },
78
+ type: {
79
+ type: "string",
80
+ enum: [
81
+ "req",
82
+ "scenario",
83
+ "test",
84
+ "adr",
85
+ "flag",
86
+ "event",
87
+ "symbol",
88
+ "fact",
89
+ ],
90
+ description: "Optional entity type filter to narrow discovery. Example: 'req'.",
91
+ },
92
+ limit: {
93
+ type: "integer",
94
+ default: 20,
95
+ description: "Optional max rows to return after ranking. Default: 20.",
96
+ },
97
+ offset: {
98
+ type: "integer",
99
+ default: 0,
100
+ description: "Optional zero-based pagination offset. Default: 0.",
101
+ },
102
+ },
103
+ },
104
+ },
105
+ {
106
+ name: "kb_status",
107
+ description: "Report current branch, snapshot, and freshness metadata for the attached KB. Read-only status inspection with no mutation side effects.",
108
+ inputSchema: {
109
+ type: "object",
110
+ properties: {},
111
+ },
112
+ },
113
+ {
114
+ name: "kb_find_gaps",
115
+ description: "Run bulk missing/present relationship analysis over KB entities. Use for questions like which requirements lack scenarios or tests. No mutation side effects.",
116
+ inputSchema: {
117
+ type: "object",
118
+ properties: {
119
+ type: {
120
+ type: "string",
121
+ enum: [
122
+ "req",
123
+ "scenario",
124
+ "test",
125
+ "adr",
126
+ "flag",
127
+ "event",
128
+ "symbol",
129
+ "fact",
130
+ ],
131
+ },
132
+ missingRelationships: {
133
+ type: "array",
134
+ items: { type: "string" },
135
+ },
136
+ presentRelationships: {
137
+ type: "array",
138
+ items: { type: "string" },
139
+ },
140
+ tags: {
141
+ type: "array",
142
+ items: { type: "string" },
143
+ },
144
+ sourceFile: {
145
+ type: "string",
146
+ },
147
+ limit: {
148
+ type: "integer",
149
+ default: 100,
150
+ },
151
+ offset: {
152
+ type: "integer",
153
+ default: 0,
154
+ },
155
+ },
156
+ },
157
+ },
158
+ {
159
+ name: "kb_coverage",
160
+ description: "Generate curated coverage reports for requirements, symbols, or grouped types. Read-only reporting with no mutation side effects.",
161
+ inputSchema: {
162
+ type: "object",
163
+ properties: {
164
+ by: {
165
+ type: "string",
166
+ enum: ["req", "symbol", "type"],
167
+ default: "req",
168
+ },
169
+ tags: {
170
+ type: "array",
171
+ items: { type: "string" },
172
+ },
173
+ includePassing: {
174
+ type: "boolean",
175
+ default: false,
176
+ },
177
+ includeTransitive: {
178
+ type: "boolean",
179
+ default: true,
180
+ },
181
+ limit: {
182
+ type: "integer",
183
+ default: 100,
184
+ },
185
+ offset: {
186
+ type: "integer",
187
+ default: 0,
188
+ },
189
+ },
190
+ },
191
+ },
192
+ {
193
+ name: "kb_graph",
194
+ description: "Run bounded graph traversal from one or more seed IDs across curated relationship types. No mutation side effects.",
195
+ inputSchema: {
196
+ type: "object",
197
+ required: ["seedIds"],
198
+ properties: {
199
+ seedIds: {
200
+ type: "array",
201
+ items: { type: "string" },
202
+ },
203
+ relationships: {
204
+ type: "array",
205
+ items: { type: "string" },
206
+ },
207
+ direction: {
208
+ type: "string",
209
+ enum: ["outgoing", "incoming", "both"],
210
+ default: "outgoing",
211
+ },
212
+ depth: {
213
+ type: "integer",
214
+ default: 1,
215
+ minimum: 1,
216
+ maximum: 5,
217
+ },
218
+ entityTypes: {
219
+ type: "array",
220
+ items: { type: "string" },
221
+ },
222
+ maxNodes: {
223
+ type: "integer",
224
+ default: 200,
225
+ },
226
+ maxEdges: {
227
+ type: "integer",
228
+ default: 500,
229
+ },
230
+ },
231
+ },
232
+ },
67
233
  {
68
234
  name: "kb_upsert",
69
235
  description: "Create or update one entity and optional relationships. Use for KB mutations after validating intent. Use the `relationships` array for batch creation of multiple links in a single call (e.g., linking a requirement to multiple tests or facts). Prefer modeling requirements as reusable fact links (`constrains`, `requires_property`) so consistency and contradiction checks remain queryable. Relationship endpoints must already exist in KB. Do not use for read-only inspection. Side effects: writes KB, may refresh symbol coordinates.",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kibi-mcp",
3
- "version": "0.3.3",
3
+ "version": "0.4.0",
4
4
  "dependencies": {
5
5
  "@modelcontextprotocol/sdk": "^1.26.0",
6
6
  "ajv": "^8.18.0",
@@ -9,8 +9,8 @@
9
9
  "fast-glob": "^3.2.12",
10
10
  "gray-matter": "^4.0.3",
11
11
  "js-yaml": "^4.1.0",
12
- "kibi-cli": "^0.2.7",
13
- "kibi-core": "^0.1.10",
12
+ "kibi-cli": "^0.3.0",
13
+ "kibi-core": "^0.2.0",
14
14
  "mcpcat": "^0.1.12",
15
15
  "ts-morph": "^23.0.0",
16
16
  "zod": "^4.3.6"