@dikolab/kbdb 0.1.6 → 0.2.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.
Files changed (199) hide show
  1. package/README.md +30 -9
  2. package/dist/cli.cjs +181 -50
  3. package/dist/cli.cjs.map +3 -3
  4. package/dist/cli.mjs +181 -50
  5. package/dist/cli.mjs.map +3 -3
  6. package/dist/kbdb-worker.cjs +283 -29
  7. package/dist/kbdb-worker.cjs.map +4 -4
  8. package/dist/mod.cjs +13 -2
  9. package/dist/mod.cjs.map +2 -2
  10. package/dist/mod.d.ts +25 -0
  11. package/dist/mod.mjs +13 -2
  12. package/dist/mod.mjs.map +2 -2
  13. package/dist/src/cli.d.ts +8 -0
  14. package/dist/src/shared/cli/functions/format-output.function.d.ts +7 -7
  15. package/dist/src/shared/cli/functions/run-search.function.d.ts +25 -16
  16. package/dist/src/shared/cli/index.d.ts +1 -1
  17. package/dist/src/shared/cli/typings/cli-options.interface.d.ts +2 -0
  18. package/dist/src/shared/cli/typings/search-display.model.d.ts +25 -7
  19. package/dist/src/shared/mcp/functions/handle-recall.function.d.ts +29 -0
  20. package/dist/src/shared/mcp/typings/mcp-client.interface.d.ts +30 -11
  21. package/dist/src/shared/version/constants/version.constant.d.ts +1 -1
  22. package/dist/src/shared/wasm-codec/functions/decode-recall-result.function.d.ts +28 -0
  23. package/dist/src/shared/wasm-codec/functions/decode-scored-results.function.d.ts +14 -7
  24. package/dist/src/shared/wasm-codec/functions/encode-recall-params.function.d.ts +21 -0
  25. package/dist/src/shared/wasm-codec/functions/encode-search-params.function.d.ts +1 -1
  26. package/dist/src/shared/wasm-codec/index.d.ts +5 -2
  27. package/dist/src/shared/worker-client/classes/worker-client.class.d.ts +13 -3
  28. package/dist/src/shared/worker-client/index.d.ts +2 -1
  29. package/dist/src/shared/worker-client/typings/recall-result.model.d.ts +94 -0
  30. package/dist/src/shared/worker-client/typings/search-params.model.d.ts +2 -0
  31. package/dist/src/shared/worker-client/typings/search-result.model.d.ts +23 -7
  32. package/dist/src/shared/worker-daemon/classes/worker-daemon.class.d.ts +1 -0
  33. package/dist/src/worker.d.ts +7 -0
  34. package/dist/wasm/fs-database/kbdb_fs_database.d.ts +18 -0
  35. package/dist/wasm/fs-database/kbdb_fs_database.js +30 -0
  36. package/dist/wasm/fs-database/kbdb_fs_database_bg.wasm +0 -0
  37. package/dist/wasm/fs-database/kbdb_fs_database_bg.wasm.d.ts +2 -0
  38. package/dist/wasm/kb-worker/kbdb_worker.d.ts +38 -0
  39. package/dist/wasm/kb-worker/kbdb_worker.js +56 -0
  40. package/dist/wasm/kb-worker/kbdb_worker_bg.wasm +0 -0
  41. package/dist/wasm/kb-worker/kbdb_worker_bg.wasm.d.ts +3 -0
  42. package/dist/worker.mjs +283 -29
  43. package/dist/worker.mjs.map +4 -4
  44. package/docs/details/README.md +26 -0
  45. package/docs/details/cli.md +608 -0
  46. package/docs/details/library-api.md +406 -0
  47. package/docs/details/mcp-server.md +430 -0
  48. package/docs/details/search-and-ranking.md +457 -0
  49. package/docs/details/storage.md +529 -0
  50. package/docs/goals/agents.md +751 -0
  51. package/docs/goals/architecture.svg +198 -0
  52. package/docs/goals/cli.md +2308 -0
  53. package/docs/goals/content-composer.md +609 -0
  54. package/docs/goals/content-parser.md +279 -0
  55. package/docs/goals/database.md +1452 -0
  56. package/docs/goals/document.md +368 -0
  57. package/docs/goals/mcp.md +1467 -0
  58. package/docs/goals/overview.md +124 -0
  59. package/docs/goals/query-parser.md +271 -0
  60. package/docs/goals/query-result.md +693 -0
  61. package/docs/goals/skills.md +738 -0
  62. package/docs/goals/worker-client.md +944 -0
  63. package/docs/goals/worker-daemon.md +1447 -0
  64. package/docs/modules/overview.md +298 -0
  65. package/docs/modules/rust/fs-database/functions/document.md +48 -0
  66. package/docs/modules/rust/fs-database/functions/integrity.md +68 -0
  67. package/docs/modules/rust/fs-database/functions/io.md +87 -0
  68. package/docs/modules/rust/fs-database/functions/module.md +22 -0
  69. package/docs/modules/rust/fs-database/functions/query-exec.md +44 -0
  70. package/docs/modules/rust/fs-database/functions/section.md +59 -0
  71. package/docs/modules/rust/fs-database/indexes/btree.md +35 -0
  72. package/docs/modules/rust/fs-database/indexes/corpus.md +61 -0
  73. package/docs/modules/rust/fs-database/indexes/inverted.md +60 -0
  74. package/docs/modules/rust/fs-database/indexes/module.md +64 -0
  75. package/docs/modules/rust/fs-database/indexes/positional.md +39 -0
  76. package/docs/modules/rust/fs-database/module.md +149 -0
  77. package/docs/modules/rust/fs-database/types/catalog.md +53 -0
  78. package/docs/modules/rust/fs-database/types/module.md +12 -0
  79. package/docs/modules/rust/fs-database/types/section.md +101 -0
  80. package/docs/modules/rust/kb-worker/functions/compose.md +57 -0
  81. package/docs/modules/rust/kb-worker/functions/module.md +20 -0
  82. package/docs/modules/rust/kb-worker/functions/retrieve.md +194 -0
  83. package/docs/modules/rust/kb-worker/functions/search.md +173 -0
  84. package/docs/modules/rust/kb-worker/functions/write.md +172 -0
  85. package/docs/modules/rust/kb-worker/module.md +171 -0
  86. package/docs/modules/rust/kb-worker/types/cache.md +90 -0
  87. package/docs/modules/rust/kb-worker/types/context.md +26 -0
  88. package/docs/modules/rust/kb-worker/types/module.md +16 -0
  89. package/docs/modules/rust/kb-worker/types/status.md +29 -0
  90. package/docs/modules/rust/kb-worker/types/token.md +50 -0
  91. package/docs/modules/rust/overview.md +495 -0
  92. package/docs/modules/rust/query-parser/functions/extract.md +56 -0
  93. package/docs/modules/rust/query-parser/functions/module.md +16 -0
  94. package/docs/modules/rust/query-parser/functions/parse.md +33 -0
  95. package/docs/modules/rust/query-parser/functions/pipeline.md +103 -0
  96. package/docs/modules/rust/query-parser/functions/rank.md +85 -0
  97. package/docs/modules/rust/query-parser/module.md +136 -0
  98. package/docs/modules/rust/query-parser/types/cache.md +39 -0
  99. package/docs/modules/rust/query-parser/types/keyphrase.md +60 -0
  100. package/docs/modules/rust/query-parser/types/module.md +17 -0
  101. package/docs/modules/rust/query-parser/types/token.md +42 -0
  102. package/docs/modules/rust/shared/content-composer/functions.md +114 -0
  103. package/docs/modules/rust/shared/content-composer/module.md +147 -0
  104. package/docs/modules/rust/shared/content-composer/types.md +93 -0
  105. package/docs/modules/rust/shared/content-parser/functions.md +72 -0
  106. package/docs/modules/rust/shared/content-parser/module.md +99 -0
  107. package/docs/modules/rust/shared/content-parser/types.md +71 -0
  108. package/docs/modules/rust/shared/corpus/module.md +24 -0
  109. package/docs/modules/rust/shared/corpus/types.md +141 -0
  110. package/docs/modules/rust/shared/document/module.md +25 -0
  111. package/docs/modules/rust/shared/document/types.md +154 -0
  112. package/docs/modules/rust/shared/encode/module.md +22 -0
  113. package/docs/modules/rust/shared/encode/traits.md +304 -0
  114. package/docs/modules/rust/shared/error/module.md +28 -0
  115. package/docs/modules/rust/shared/error/types.md +302 -0
  116. package/docs/modules/rust/shared/kbid/module.md +24 -0
  117. package/docs/modules/rust/shared/kbid/types.md +147 -0
  118. package/docs/modules/rust/shared/memory/functions.md +209 -0
  119. package/docs/modules/rust/shared/memory/module.md +24 -0
  120. package/docs/modules/rust/shared/module.md +168 -0
  121. package/docs/modules/rust/shared/query/module.md +27 -0
  122. package/docs/modules/rust/shared/query/types.md +236 -0
  123. package/docs/modules/rust/shared/section/module.md +25 -0
  124. package/docs/modules/rust/shared/section/types.md +294 -0
  125. package/docs/modules/rust/shared/term/module.md +25 -0
  126. package/docs/modules/rust/shared/term/types.md +139 -0
  127. package/docs/modules/typescript/cli.md +129 -0
  128. package/docs/modules/typescript/kbdb-worker.md +135 -0
  129. package/docs/modules/typescript/overview.md +378 -0
  130. package/docs/modules/typescript/shared/cli/constants.md +23 -0
  131. package/docs/modules/typescript/shared/cli/functions.md +630 -0
  132. package/docs/modules/typescript/shared/cli/module.md +125 -0
  133. package/docs/modules/typescript/shared/cli/typings.md +172 -0
  134. package/docs/modules/typescript/shared/hash/functions.md +20 -0
  135. package/docs/modules/typescript/shared/hash/module.md +21 -0
  136. package/docs/modules/typescript/shared/mcp/classes.md +179 -0
  137. package/docs/modules/typescript/shared/mcp/functions.md +386 -0
  138. package/docs/modules/typescript/shared/mcp/module.md +160 -0
  139. package/docs/modules/typescript/shared/mcp/typings.md +522 -0
  140. package/docs/modules/typescript/shared/module.md +94 -0
  141. package/docs/modules/typescript/shared/platform/functions.md +42 -0
  142. package/docs/modules/typescript/shared/platform/module.md +25 -0
  143. package/docs/modules/typescript/shared/runtime/functions.md +26 -0
  144. package/docs/modules/typescript/shared/runtime/module.md +27 -0
  145. package/docs/modules/typescript/shared/runtime/typings.md +13 -0
  146. package/docs/modules/typescript/shared/version/module.md +27 -0
  147. package/docs/modules/typescript/shared/wasm-codec/functions.md +391 -0
  148. package/docs/modules/typescript/shared/wasm-codec/module.md +98 -0
  149. package/docs/modules/typescript/shared/worker-client/classes.md +264 -0
  150. package/docs/modules/typescript/shared/worker-client/functions.md +175 -0
  151. package/docs/modules/typescript/shared/worker-client/module.md +84 -0
  152. package/docs/modules/typescript/shared/worker-client/typings.md +508 -0
  153. package/docs/modules/typescript/shared/worker-daemon/classes.md +239 -0
  154. package/docs/modules/typescript/shared/worker-daemon/constants.md +37 -0
  155. package/docs/modules/typescript/shared/worker-daemon/functions.md +221 -0
  156. package/docs/modules/typescript/shared/worker-daemon/module.md +112 -0
  157. package/docs/modules/typescript/shared/worker-daemon/typings.md +126 -0
  158. package/docs/overview.md +188 -0
  159. package/docs/release-notes/0.1.0.md +189 -0
  160. package/docs/release-notes/0.1.1.md +46 -0
  161. package/docs/release-notes/0.1.2.md +38 -0
  162. package/docs/release-notes/0.1.3.md +42 -0
  163. package/docs/release-notes/0.2.0.md +147 -0
  164. package/docs/release-notes/README.md +9 -0
  165. package/mod.ts +26 -0
  166. package/package.json +8 -2
  167. package/src/cli.ts +9 -0
  168. package/src/shared/cli/functions/dispatch-command.function.ts +3 -3
  169. package/src/shared/cli/functions/format-output.function.ts +41 -34
  170. package/src/shared/cli/functions/parse-args.function.ts +7 -0
  171. package/src/shared/cli/functions/run-search.function.ts +53 -27
  172. package/src/shared/cli/index.ts +4 -1
  173. package/src/shared/cli/typings/cli-options.interface.ts +2 -0
  174. package/src/shared/cli/typings/search-display.model.ts +26 -7
  175. package/src/shared/mcp/functions/create-tool-definitions.function.ts +64 -4
  176. package/src/shared/mcp/functions/handle-recall.function.ts +66 -0
  177. package/src/shared/mcp/functions/handle-tool-call.function.ts +11 -1
  178. package/src/shared/mcp/typings/mcp-client.interface.ts +33 -15
  179. package/src/shared/version/constants/version.constant.ts +1 -1
  180. package/src/shared/wasm-codec/functions/decode-recall-result.function.ts +267 -0
  181. package/src/shared/wasm-codec/functions/decode-scored-results.function.ts +81 -27
  182. package/src/shared/wasm-codec/functions/encode-recall-params.function.ts +68 -0
  183. package/src/shared/wasm-codec/functions/encode-search-params.function.ts +4 -1
  184. package/src/shared/wasm-codec/index.ts +11 -3
  185. package/src/shared/worker-client/classes/worker-client.class.ts +24 -4
  186. package/src/shared/worker-client/index.ts +11 -1
  187. package/src/shared/worker-client/typings/recall-result.model.ts +98 -0
  188. package/src/shared/worker-client/typings/search-params.model.ts +2 -0
  189. package/src/shared/worker-client/typings/search-result.model.ts +24 -7
  190. package/src/shared/worker-daemon/classes/worker-daemon.class.ts +25 -4
  191. package/src/wasm/fs-database/kbdb_fs_database.d.ts +18 -0
  192. package/src/wasm/fs-database/kbdb_fs_database.js +30 -0
  193. package/src/wasm/fs-database/kbdb_fs_database_bg.wasm +0 -0
  194. package/src/wasm/fs-database/kbdb_fs_database_bg.wasm.d.ts +2 -0
  195. package/src/wasm/kb-worker/kbdb_worker.d.ts +38 -0
  196. package/src/wasm/kb-worker/kbdb_worker.js +56 -0
  197. package/src/wasm/kb-worker/kbdb_worker_bg.wasm +0 -0
  198. package/src/wasm/kb-worker/kbdb_worker_bg.wasm.d.ts +3 -0
  199. package/src/worker.ts +8 -0
@@ -0,0 +1,386 @@
1
+ # mcp/functions -- MCP Server Functions
2
+
3
+ *Factory, tool dispatch, resource dispatch, and
4
+ definition builders.*
5
+
6
+ ## Source
7
+
8
+ `src/shared/mcp/functions/`
9
+
10
+ ## Functions
11
+
12
+ ### `createMcpServer`
13
+
14
+ **File:** `create-mcp-server.function.ts`
15
+
16
+ Factory function. Creates and returns a configured
17
+ `McpServer` bound to the given `McpWorkerClient`.
18
+
19
+ ```ts
20
+ function createMcpServer(
21
+ client: McpWorkerClient,
22
+ ): McpServer;
23
+ ```
24
+
25
+ ### `createToolDefinitions`
26
+
27
+ **File:** `create-tool-definitions.function.ts`
28
+
29
+ Returns an array of `McpToolDefinition` objects
30
+ describing the MCP tools the server exposes. Each
31
+ definition includes `name`, `description`,
32
+ `inputSchema` (JSON Schema), and `annotations`
33
+ (`McpToolAnnotations`).
34
+
35
+ ```ts
36
+ function createToolDefinitions(): McpToolDefinition[];
37
+ ```
38
+
39
+ **Tools defined (18 total):**
40
+
41
+ | Name | Required Params | Optional Params |
42
+ |----------------|----------------------------|--------------------------------------|
43
+ | `search` | `query` | `mode`, `limit`, `content`, `offset` |
44
+ | `learn` | `content` | `type`, `docid`, `title`, `description` |
45
+ | `unlearn` | `kbid` | -- |
46
+ | `retrieve` | `kbid` or `docid` | -- |
47
+ | `recall` | `kbid` or `kbids` | `depth` |
48
+ | `status` | -- | -- |
49
+ | `content` | `ids` | -- |
50
+ | `check` | -- | -- |
51
+ | `gc` | -- | -- |
52
+ | `rebuild` | -- | -- |
53
+ | `skill-learn` | `name`, `description`, `body` | `arguments` |
54
+ | `skill-list` | -- | -- |
55
+ | `skill-get` | `name` or `kbid` | -- |
56
+ | `skill-delete` | `kbid` | -- |
57
+ | `agent-create` | `name`, `description`, `persona`, `skills` | -- |
58
+ | `agent-list` | -- | -- |
59
+ | `agent-get` | `docid` | -- |
60
+ | `agent-delete` | `docid` | -- |
61
+
62
+ ### `createResourceTemplates`
63
+
64
+ **File:** `create-resource-templates.function.ts`
65
+
66
+ Returns an array of `McpResourceTemplate` objects
67
+ describing the URI templates for MCP resources. These
68
+ are returned by `resources/templates/list`.
69
+
70
+ ```ts
71
+ function createResourceTemplates(): McpResourceTemplate[];
72
+ ```
73
+
74
+ **Templates defined:**
75
+
76
+ | URI Template | MIME Type |
77
+ |-------------------------------|---------------------|
78
+ | `kbdb://section/{kb_id}` | `text/plain` |
79
+ | `kbdb://document/{doc_id}` | `application/json` |
80
+
81
+ ### `handleToolCall`
82
+
83
+ **File:** `handle-tool-call.function.ts`
84
+
85
+ Single dispatcher for all MCP `tools/call` requests.
86
+ Routes by tool name to the appropriate
87
+ `McpWorkerClient` method.
88
+
89
+ ```ts
90
+ async function handleToolCall(
91
+ client: McpWorkerClient,
92
+ name: string,
93
+ args: Record<string, unknown>,
94
+ ): Promise<McpToolResult>;
95
+ ```
96
+
97
+ Routing:
98
+
99
+ | Tool Name | Client Method / Handler |
100
+ |----------------|--------------------------------------|
101
+ | `search` | `client.search(params)` |
102
+ | `learn` | `client.addSection(params)` |
103
+ | `unlearn` | `client.removeSection(kbid)` |
104
+ | `retrieve` | `client.readSections(kbids)` or |
105
+ | | `client.retrieveDocument(docid)` |
106
+ | `recall` | `handleRecall(params, client)` |
107
+ | `status` | `client.dbStatus()` |
108
+ | `content` | `client.content(ids)` |
109
+ | `check` | `client.integrityCheck()` |
110
+ | `gc` | `client.gc()` |
111
+ | `rebuild` | `client.rebuildIndexes()` |
112
+ | `skill-learn` | `handleSkillLearn(args, client)` |
113
+ | `skill-list` | `handleSkillList(client)` |
114
+ | `skill-get` | `handleSkillGet(args, client)` |
115
+ | `skill-delete` | `handleSkillDelete(args, client)` |
116
+ | `agent-create` | `handleAgentCreate(args, client)` |
117
+ | `agent-list` | `handleAgentList(client)` |
118
+ | `agent-get` | `handleAgentGet(args, client)` |
119
+ | `agent-delete` | `handleAgentDelete(args, client)` |
120
+
121
+ Tool execution errors (client throws) are caught and
122
+ returned as `{ content: [...], isError: true }`. They
123
+ are **not** returned as JSON-RPC error responses.
124
+ Unknown tool names also return `isError: true`.
125
+
126
+ ### `handleResourceRead`
127
+
128
+ **File:** `handle-resource-read.function.ts`
129
+
130
+ Handles MCP `resources/read` requests. Parses the
131
+ URI to determine whether it is a section or document
132
+ request, then calls the appropriate client method.
133
+
134
+ ```ts
135
+ async function handleResourceRead(
136
+ client: McpWorkerClient,
137
+ uri: string,
138
+ ): Promise<McpResourceResult>;
139
+ ```
140
+
141
+ Throws on invalid or unrecognised URIs. The caller
142
+ (`McpServer`) catches the error and returns a
143
+ `RESOURCE_NOT_FOUND` (-32002) JSON-RPC error
144
+ response.
145
+
146
+ ### `createPromptDefinitions`
147
+
148
+ **File:** `create-prompt-definitions.function.ts`
149
+
150
+ Maps an array of skill sections and agent documents
151
+ to `McpPrompt[]` for the `prompts/list` response.
152
+ Skill prompts use the skill name directly; agent
153
+ prompts use `agent:<name>` as the prompt name.
154
+
155
+ ```ts
156
+ function createPromptDefinitions(
157
+ sections: Section[],
158
+ ): McpPrompt[];
159
+ ```
160
+
161
+ ### `handlePromptGet`
162
+
163
+ **File:** `handle-prompt-get.function.ts`
164
+
165
+ Fetches a skill or agent by prompt name and renders
166
+ it as `McpPromptMessage[]`. For skills: substitutes
167
+ argument values into the template body and returns
168
+ a single user message. For agents: returns an
169
+ assistant message (persona) followed by a user
170
+ message listing available skills.
171
+
172
+ ```ts
173
+ async function handlePromptGet(
174
+ name: string,
175
+ args: Record<string, string>,
176
+ client: McpWorkerClient,
177
+ ): Promise<McpPromptMessage[]>;
178
+ ```
179
+
180
+ ### `handleCompletion`
181
+
182
+ **File:** `handle-completion.function.ts`
183
+
184
+ Provides autocomplete suggestions for prompt names
185
+ and resource URIs. Dispatches based on `ref.type`:
186
+ `ref/prompt` returns matching skill or agent names;
187
+ `ref/resource` returns matching kbid or docid values
188
+ from known URI patterns.
189
+
190
+ ```ts
191
+ async function handleCompletion(
192
+ ref: McpCompletionRef,
193
+ argument: Record<string, string> | undefined,
194
+ client: McpWorkerClient,
195
+ ): Promise<McpCompletionResult>;
196
+ ```
197
+
198
+ ### `parseSkillContent`
199
+
200
+ **File:** `parse-skill-content.function.ts`
201
+
202
+ Parses a JSON string stored as a skill section's
203
+ content into a typed `SkillDefinition`. Throws when
204
+ the JSON is malformed or missing required fields.
205
+
206
+ ```ts
207
+ function parseSkillContent(
208
+ json: string,
209
+ ): SkillDefinition;
210
+ ```
211
+
212
+ ### `serializeSkillContent`
213
+
214
+ **File:** `serialize-skill-content.function.ts`
215
+
216
+ Serializes a `SkillDefinition` to a JSON string for
217
+ storage as a section content body.
218
+
219
+ ```ts
220
+ function serializeSkillContent(
221
+ skill: SkillDefinition,
222
+ ): string;
223
+ ```
224
+
225
+ ### `handleSkillLearn`
226
+
227
+ **File:** `handle-skill-learn.function.ts`
228
+
229
+ Handles the `skill-learn` MCP tool call. Validates
230
+ parameters, serializes the skill definition to JSON,
231
+ and calls `client.addSection` with
232
+ `sectionType: 'skill'`.
233
+
234
+ ```ts
235
+ async function handleSkillLearn(
236
+ params: Record<string, unknown>,
237
+ client: McpWorkerClient,
238
+ ): Promise<McpToolResult>;
239
+ ```
240
+
241
+ ### `handleSkillList`
242
+
243
+ **File:** `handle-skill-list.function.ts`
244
+
245
+ Handles the `skill-list` MCP tool call. Calls
246
+ `client.listByType('skill')` and formats the results.
247
+
248
+ ```ts
249
+ async function handleSkillList(
250
+ client: McpWorkerClient,
251
+ ): Promise<McpToolResult>;
252
+ ```
253
+
254
+ ### `handleSkillGet`
255
+
256
+ **File:** `handle-skill-get.function.ts`
257
+
258
+ Handles the `skill-get` MCP tool call. Looks up the
259
+ skill by `name` (via search) or by `kbid` (via
260
+ `client.readSections`), parses the content, and
261
+ returns the full `SkillDefinition`.
262
+
263
+ ```ts
264
+ async function handleSkillGet(
265
+ params: Record<string, unknown>,
266
+ client: McpWorkerClient,
267
+ ): Promise<McpToolResult>;
268
+ ```
269
+
270
+ ### `handleSkillDelete`
271
+
272
+ **File:** `handle-skill-delete.function.ts`
273
+
274
+ Handles the `skill-delete` MCP tool call. Calls
275
+ `client.removeSection(kbid)`.
276
+
277
+ ```ts
278
+ async function handleSkillDelete(
279
+ params: Record<string, unknown>,
280
+ client: McpWorkerClient,
281
+ ): Promise<McpToolResult>;
282
+ ```
283
+
284
+ ### `handleAgentCreate`
285
+
286
+ **File:** `handle-agent-create.function.ts`
287
+
288
+ Handles the `agent-create` MCP tool call. Validates
289
+ skill kbids, builds the agent document manifest, and
290
+ stores it.
291
+
292
+ ```ts
293
+ async function handleAgentCreate(
294
+ params: Record<string, unknown>,
295
+ client: McpWorkerClient,
296
+ ): Promise<McpToolResult>;
297
+ ```
298
+
299
+ ### `handleAgentList`
300
+
301
+ **File:** `handle-agent-list.function.ts`
302
+
303
+ Handles the `agent-list` MCP tool call. Calls
304
+ `client.listDocumentsByType('agent')` and formats
305
+ the results.
306
+
307
+ ```ts
308
+ async function handleAgentList(
309
+ client: McpWorkerClient,
310
+ ): Promise<McpToolResult>;
311
+ ```
312
+
313
+ ### `handleAgentGet`
314
+
315
+ **File:** `handle-agent-get.function.ts`
316
+
317
+ Handles the `agent-get` MCP tool call. Retrieves
318
+ the agent document by `docid` and resolves each
319
+ referenced skill kbid into a `SkillDefinition`.
320
+ Missing skills appear as `null` name entries.
321
+
322
+ ```ts
323
+ async function handleAgentGet(
324
+ params: Record<string, unknown>,
325
+ client: McpWorkerClient,
326
+ ): Promise<McpToolResult>;
327
+ ```
328
+
329
+ ### `handleAgentDelete`
330
+
331
+ **File:** `handle-agent-delete.function.ts`
332
+
333
+ Handles the `agent-delete` MCP tool call. Removes
334
+ the agent document manifest. Referenced skill
335
+ sections are not deleted.
336
+
337
+ ```ts
338
+ async function handleAgentDelete(
339
+ params: Record<string, unknown>,
340
+ client: McpWorkerClient,
341
+ ): Promise<McpToolResult>;
342
+ ```
343
+
344
+ ### `handleRecall`
345
+
346
+ **File:** `handle-recall.function.ts`
347
+
348
+ Handles the `recall` MCP tool call. Validates that
349
+ at least one of `kbid` or `kbids` is provided,
350
+ clamps `depth` to 0--3, calls
351
+ `client.recall(params)`, and formats the result
352
+ as a JSON text content item.
353
+
354
+ ```ts
355
+ async function handleRecall(
356
+ params: Record<string, unknown>,
357
+ client: McpWorkerClient,
358
+ ): Promise<McpToolResult>;
359
+ ```
360
+
361
+ ## Testing
362
+
363
+ Each function has a `.spec.ts` at the module root:
364
+
365
+ ```
366
+ src/shared/mcp/
367
+ ├── create-mcp-server.spec.ts
368
+ ├── create-tool-definitions.spec.ts
369
+ ├── create-prompt-definitions.spec.ts
370
+ ├── create-resource-templates.spec.ts
371
+ ├── handle-tool-call.spec.ts
372
+ ├── handle-resource-read.spec.ts
373
+ ├── handle-prompt-get.spec.ts
374
+ ├── handle-completion.spec.ts
375
+ ├── parse-skill-content.spec.ts
376
+ ├── serialize-skill-content.spec.ts
377
+ ├── handle-skill-learn.spec.ts
378
+ ├── handle-skill-list.spec.ts
379
+ ├── handle-skill-get.spec.ts
380
+ ├── handle-skill-delete.spec.ts
381
+ ├── handle-agent-create.spec.ts
382
+ ├── handle-agent-list.spec.ts
383
+ ├── handle-agent-get.spec.ts
384
+ ├── handle-agent-delete.spec.ts
385
+ └── mcp-server.spec.ts
386
+ ```
@@ -0,0 +1,160 @@
1
+ # mcp -- MCP Server Module
2
+
3
+ *Long-lived MCP server over stdio. Routes tool and
4
+ resource requests to the worker daemon via the
5
+ worker-client module.*
6
+
7
+ ## Source
8
+
9
+ `src/shared/mcp/`
10
+
11
+ ## Responsibility
12
+
13
+ Starts an MCP server that listens on stdin and writes
14
+ responses to stdout using JSON-RPC 2.0 (protocol
15
+ version **2025-11-25**). On startup it creates a
16
+ `WorkerClient` to connect to the worker daemon. All
17
+ incoming MCP tool calls and resource reads are routed
18
+ through that client. The MCP module does **not**
19
+ perform keyword extraction, indexing, ranking, or
20
+ database I/O itself.
21
+
22
+ See [MCP](../../../../goals/mcp.md) for the full
23
+ behavioral specification.
24
+
25
+ ## Directory Layout
26
+
27
+ ```
28
+ src/shared/mcp/
29
+ ├── index.ts
30
+ ├── classes/
31
+ │ └── mcp-server.class.ts
32
+ ├── constants/
33
+ │ └── mcp-error-codes.constant.ts
34
+ ├── functions/
35
+ │ ├── create-mcp-server.function.ts
36
+ │ ├── create-prompt-definitions.function.ts
37
+ │ ├── create-resource-templates.function.ts
38
+ │ ├── create-tool-definitions.function.ts
39
+ │ ├── handle-agent-create.function.ts
40
+ │ ├── handle-agent-delete.function.ts
41
+ │ ├── handle-agent-get.function.ts
42
+ │ ├── handle-agent-list.function.ts
43
+ │ ├── handle-completion.function.ts
44
+ │ ├── handle-prompt-get.function.ts
45
+ │ ├── handle-recall.function.ts
46
+ │ ├── handle-resource-read.function.ts
47
+ │ ├── handle-skill-delete.function.ts
48
+ │ ├── handle-skill-get.function.ts
49
+ │ ├── handle-skill-learn.function.ts
50
+ │ ├── handle-skill-list.function.ts
51
+ │ ├── handle-tool-call.function.ts
52
+ │ ├── parse-skill-content.function.ts
53
+ │ └── serialize-skill-content.function.ts
54
+ ├── typings/
55
+ │ ├── mcp-client.interface.ts
56
+ │ ├── mcp-types.interface.ts
57
+ │ └── remove-section-result.model.ts
58
+ ├── tests/
59
+ │ └── mcp-protocol.spec.ts
60
+ └── *.spec.ts
61
+ ```
62
+
63
+ ## Exports
64
+
65
+ - **[classes.md](classes.md)** -- `McpServer`.
66
+ - **[functions.md](functions.md)** --
67
+ `createMcpServer`, `createToolDefinitions`,
68
+ `createPromptDefinitions`, `createResourceTemplates`,
69
+ `handleToolCall`, `handleResourceRead`,
70
+ `handlePromptGet`, `handleCompletion`,
71
+ `parseSkillContent`, `serializeSkillContent`,
72
+ `handleSkillLearn`, `handleSkillList`,
73
+ `handleSkillGet`, `handleSkillDelete`,
74
+ `handleAgentCreate`, `handleAgentList`,
75
+ `handleAgentGet`, `handleAgentDelete`,
76
+ `handleRecall`.
77
+ - **[typings.md](typings.md)** -- `McpWorkerClient`,
78
+ `McpRequest`, `McpResponse`, `McpNotification`,
79
+ `McpToolDefinition`, `McpResourceTemplate`,
80
+ `McpServerInfo`, `McpCapabilities`, `McpToolResult`,
81
+ `McpResourceResult`, `RemoveSectionResult`,
82
+ `McpPrompt`, `McpPromptArgument`, `McpPromptMessage`,
83
+ `McpPromptMessageContent`, `McpToolAnnotations`,
84
+ `McpCompletionRef`, `McpCompletionResult`,
85
+ `McpLogLevel`, `McpLogMessage`, `SkillDefinition`,
86
+ `SkillArgument`, `AgentDefinition`.
87
+ - **Error code constants**: `PARSE_ERROR`,
88
+ `METHOD_NOT_FOUND`, `INVALID_PARAMS`,
89
+ `INTERNAL_ERROR`, `RESOURCE_NOT_FOUND`.
90
+
91
+ ## Execution Flow
92
+
93
+ ```
94
+ cli.ts (entry point, MCP mode)
95
+
96
+ ├─ detect runtime (Deno / Node)
97
+ ├─ resolve db path (--db flag or KBDB_DIR env)
98
+ ├─ createWorkerClient({ targetDir })
99
+ ├─ createMcpServer(client)
100
+ │ └─ returns McpServer
101
+ ├─ server.listen()
102
+ │ ├─ opens stdio transport
103
+ │ ├─ reads JSON-RPC 2.0 requests from stdin
104
+ │ ├─ pre-init guard: rejects all methods except
105
+ │ │ initialize and ping until
106
+ │ │ notifications/initialized received
107
+ │ ├─ routes protocol messages:
108
+ │ │ ├─ initialize → version 2025-11-25
109
+ │ │ ├─ ping → {}
110
+ │ │ ├─ notifications/initialized → sets flag
111
+ │ │ └─ notifications/cancelled → ignored
112
+ │ ├─ routes tool calls via handleToolCall:
113
+ │ │ ├─ search → client.search(params)
114
+ │ │ ├─ learn → client.addSection(params)
115
+ │ │ ├─ unlearn → client.removeSection(kbid)
116
+ │ │ ├─ retrieve → client.readSections(kbids)
117
+ │ │ │ or client.retrieveDocument(docid)
118
+ │ │ ├─ recall → handleRecall(params, client)
119
+ │ │ ├─ status → client.dbStatus()
120
+ │ │ ├─ skill-learn → handleSkillLearn(params, client)
121
+ │ │ ├─ skill-list → handleSkillList(client)
122
+ │ │ ├─ skill-get → handleSkillGet(params, client)
123
+ │ │ ├─ skill-delete → handleSkillDelete(params, client)
124
+ │ │ ├─ agent-create → handleAgentCreate(params, client)
125
+ │ │ ├─ agent-list → handleAgentList(client)
126
+ │ │ ├─ agent-get → handleAgentGet(params, client)
127
+ │ │ └─ agent-delete → handleAgentDelete(params, client)
128
+ │ ├─ routes prompt requests:
129
+ │ │ ├─ prompts/list → createPromptDefinitions(sections)
130
+ │ │ └─ prompts/get → handlePromptGet(name, args, client)
131
+ │ ├─ routes completion requests:
132
+ │ │ └─ completion/complete → handleCompletion(ref, arg, client)
133
+ │ ├─ routes resource requests:
134
+ │ │ ├─ resources/list → { resources: [] }
135
+ │ │ ├─ resources/templates/list → URI templates
136
+ │ │ ├─ resources/subscribe → add URI to subscriptions
137
+ │ │ ├─ resources/unsubscribe → remove URI from subscriptions
138
+ │ │ └─ resources/read → handleResourceRead
139
+ │ │ ├─ kbdb://section/{kbid}
140
+ │ │ └─ kbdb://document/{docid}
141
+ │ ├─ routes logging and roots:
142
+ │ │ ├─ logging/setLevel → stores logLevel
143
+ │ │ └─ roots/list → returns .kbdb URI
144
+ │ └─ writes JSON-RPC 2.0 responses to stdout
145
+ └─ on stdin close:
146
+ └─ client.disconnect()
147
+ ```
148
+
149
+ ## Transport
150
+
151
+ The MCP server uses **stdio** transport:
152
+
153
+ - Reads JSON-RPC 2.0 requests from **stdin**.
154
+ - Writes JSON-RPC 2.0 responses to **stdout**.
155
+ - Diagnostic messages go to **stderr**.
156
+
157
+ The AI agent host (Claude Desktop, Claude Code, or
158
+ any MCP-compatible client) spawns `kbdb mcp` as a
159
+ child process and communicates over its stdin/stdout
160
+ streams.