mrplex 0.0.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 (319) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +284 -0
  3. package/dist/cli/config.d.ts +28 -0
  4. package/dist/cli/config.js +48 -0
  5. package/dist/cli/config.js.map +1 -0
  6. package/dist/cli/exit-codes.d.ts +13 -0
  7. package/dist/cli/exit-codes.js +39 -0
  8. package/dist/cli/exit-codes.js.map +1 -0
  9. package/dist/cli/format.d.ts +22 -0
  10. package/dist/cli/format.js +148 -0
  11. package/dist/cli/format.js.map +1 -0
  12. package/dist/cli/main.d.ts +16 -0
  13. package/dist/cli/main.js +1518 -0
  14. package/dist/cli/main.js.map +1 -0
  15. package/dist/client/kernel-client.d.ts +97 -0
  16. package/dist/client/kernel-client.js +14 -0
  17. package/dist/client/kernel-client.js.map +1 -0
  18. package/dist/client/local.d.ts +26 -0
  19. package/dist/client/local.js +103 -0
  20. package/dist/client/local.js.map +1 -0
  21. package/dist/client/remote-mcp.d.ts +35 -0
  22. package/dist/client/remote-mcp.js +174 -0
  23. package/dist/client/remote-mcp.js.map +1 -0
  24. package/dist/embed/backfill.d.ts +24 -0
  25. package/dist/embed/backfill.js +33 -0
  26. package/dist/embed/backfill.js.map +1 -0
  27. package/dist/embed/chunker.d.ts +31 -0
  28. package/dist/embed/chunker.js +121 -0
  29. package/dist/embed/chunker.js.map +1 -0
  30. package/dist/embed/cmd-hook.d.ts +21 -0
  31. package/dist/embed/cmd-hook.js +100 -0
  32. package/dist/embed/cmd-hook.js.map +1 -0
  33. package/dist/embed/config.d.ts +61 -0
  34. package/dist/embed/config.js +109 -0
  35. package/dist/embed/config.js.map +1 -0
  36. package/dist/embed/hook.d.ts +38 -0
  37. package/dist/embed/hook.js +56 -0
  38. package/dist/embed/hook.js.map +1 -0
  39. package/dist/embed/http-hook.d.ts +13 -0
  40. package/dist/embed/http-hook.js +39 -0
  41. package/dist/embed/http-hook.js.map +1 -0
  42. package/dist/embed/worker.d.ts +62 -0
  43. package/dist/embed/worker.js +228 -0
  44. package/dist/embed/worker.js.map +1 -0
  45. package/dist/fixtures/hwe/characters/albion-ever.md +12 -0
  46. package/dist/fixtures/hwe/characters/cent.md +11 -0
  47. package/dist/fixtures/hwe/concepts/cent-demiurge-resonance.md +10 -0
  48. package/dist/fixtures/hwe/concepts/phase-looper.md +10 -0
  49. package/dist/fixtures/hwe/entities/demiurge.md +11 -0
  50. package/dist/fixtures/hwe/entities/monad.md +11 -0
  51. package/dist/fixtures/hwe/moc/globocorp-network.md +19 -0
  52. package/dist/fixtures/hwe/organizations/globocorp.md +11 -0
  53. package/dist/fixtures/hwe/readme.md +24 -0
  54. package/dist/fixtures/starship/crew/aria-okonkwo.md +14 -0
  55. package/dist/fixtures/starship/crew/bexley-orr.md +14 -0
  56. package/dist/fixtures/starship/crew/dax-thorne.md +14 -0
  57. package/dist/fixtures/starship/crew/isolde-marsh.md +16 -0
  58. package/dist/fixtures/starship/crew/kestrel-vance.md +15 -0
  59. package/dist/fixtures/starship/crew/quill-vasquez.md +14 -0
  60. package/dist/fixtures/starship/crew/soren-halloway.md +16 -0
  61. package/dist/fixtures/starship/encounters/the-drift-choir.md +14 -0
  62. package/dist/fixtures/starship/encounters/the-hollow-signal.md +16 -0
  63. package/dist/fixtures/starship/encounters/the-wandering-buoy.md +14 -0
  64. package/dist/fixtures/starship/equipment/coolant-loop-b.md +13 -0
  65. package/dist/fixtures/starship/equipment/emergency-ration-bars.md +12 -0
  66. package/dist/fixtures/starship/equipment/long-range-sensors.md +13 -0
  67. package/dist/fixtures/starship/equipment/plasma-manifold-3.md +15 -0
  68. package/dist/fixtures/starship/equipment/shuttle-corvid.md +13 -0
  69. package/dist/fixtures/starship/logs/marsh-4419-1.md +13 -0
  70. package/dist/fixtures/starship/logs/okonkwo-4415-1.md +13 -0
  71. package/dist/fixtures/starship/logs/orr-4413-1.md +13 -0
  72. package/dist/fixtures/starship/logs/orr-4420-1.md +12 -0
  73. package/dist/fixtures/starship/logs/thorne-4413-2.md +13 -0
  74. package/dist/fixtures/starship/logs/vance-4419-2.md +13 -0
  75. package/dist/fixtures/starship/misc/the-galley-ficus.md +11 -0
  76. package/dist/fixtures/starship/missions/the-cinder-run.md +17 -0
  77. package/dist/fixtures/starship/missions/the-drift-choir.md +17 -0
  78. package/dist/fixtures/starship/missions/the-hollow-signal.md +17 -0
  79. package/dist/fixtures/starship/missions/the-silent-beacon.md +17 -0
  80. package/dist/fixtures/starship/moc/crew.md +19 -0
  81. package/dist/fixtures/starship/moc/encounters.md +14 -0
  82. package/dist/fixtures/starship/moc/missions.md +15 -0
  83. package/dist/fixtures/starship/readme.md +60 -0
  84. package/dist/index.d.ts +1 -0
  85. package/dist/index.js +3 -0
  86. package/dist/index.js.map +1 -0
  87. package/dist/kernel/auth/glob.d.ts +46 -0
  88. package/dist/kernel/auth/glob.js +112 -0
  89. package/dist/kernel/auth/glob.js.map +1 -0
  90. package/dist/kernel/auth/scope.d.ts +53 -0
  91. package/dist/kernel/auth/scope.js +83 -0
  92. package/dist/kernel/auth/scope.js.map +1 -0
  93. package/dist/kernel/casefold.d.ts +37 -0
  94. package/dist/kernel/casefold.js +44 -0
  95. package/dist/kernel/casefold.js.map +1 -0
  96. package/dist/kernel/constants.d.ts +21 -0
  97. package/dist/kernel/constants.js +22 -0
  98. package/dist/kernel/constants.js.map +1 -0
  99. package/dist/kernel/context.d.ts +70 -0
  100. package/dist/kernel/context.js +114 -0
  101. package/dist/kernel/context.js.map +1 -0
  102. package/dist/kernel/deletion.d.ts +50 -0
  103. package/dist/kernel/deletion.js +83 -0
  104. package/dist/kernel/deletion.js.map +1 -0
  105. package/dist/kernel/diff.d.ts +36 -0
  106. package/dist/kernel/diff.js +61 -0
  107. package/dist/kernel/diff.js.map +1 -0
  108. package/dist/kernel/errors.d.ts +37 -0
  109. package/dist/kernel/errors.js +55 -0
  110. package/dist/kernel/errors.js.map +1 -0
  111. package/dist/kernel/frontmatter-input.d.ts +32 -0
  112. package/dist/kernel/frontmatter-input.js +85 -0
  113. package/dist/kernel/frontmatter-input.js.map +1 -0
  114. package/dist/kernel/graph.d.ts +34 -0
  115. package/dist/kernel/graph.js +559 -0
  116. package/dist/kernel/graph.js.map +1 -0
  117. package/dist/kernel/history.d.ts +74 -0
  118. package/dist/kernel/history.js +143 -0
  119. package/dist/kernel/history.js.map +1 -0
  120. package/dist/kernel/kernel.d.ts +162 -0
  121. package/dist/kernel/kernel.js +675 -0
  122. package/dist/kernel/kernel.js.map +1 -0
  123. package/dist/kernel/path-config.d.ts +101 -0
  124. package/dist/kernel/path-config.js +171 -0
  125. package/dist/kernel/path-config.js.map +1 -0
  126. package/dist/kernel/query/ast.d.ts +24 -0
  127. package/dist/kernel/query/ast.js +52 -0
  128. package/dist/kernel/query/ast.js.map +1 -0
  129. package/dist/kernel/query/cel-parse.d.ts +37 -0
  130. package/dist/kernel/query/cel-parse.js +88 -0
  131. package/dist/kernel/query/cel-parse.js.map +1 -0
  132. package/dist/kernel/query/degrees.d.ts +31 -0
  133. package/dist/kernel/query/degrees.js +135 -0
  134. package/dist/kernel/query/degrees.js.map +1 -0
  135. package/dist/kernel/query/graph-ast.d.ts +51 -0
  136. package/dist/kernel/query/graph-ast.js +139 -0
  137. package/dist/kernel/query/graph-ast.js.map +1 -0
  138. package/dist/kernel/query/query.d.ts +56 -0
  139. package/dist/kernel/query/query.js +276 -0
  140. package/dist/kernel/query/query.js.map +1 -0
  141. package/dist/kernel/validation.d.ts +64 -0
  142. package/dist/kernel/validation.js +183 -0
  143. package/dist/kernel/validation.js.map +1 -0
  144. package/dist/kernel/version-id.d.ts +10 -0
  145. package/dist/kernel/version-id.js +21 -0
  146. package/dist/kernel/version-id.js.map +1 -0
  147. package/dist/kernel/wire.d.ts +148 -0
  148. package/dist/kernel/wire.js +6 -0
  149. package/dist/kernel/wire.js.map +1 -0
  150. package/dist/links/backfill.d.ts +28 -0
  151. package/dist/links/backfill.js +33 -0
  152. package/dist/links/backfill.js.map +1 -0
  153. package/dist/links/extract.d.ts +29 -0
  154. package/dist/links/extract.js +233 -0
  155. package/dist/links/extract.js.map +1 -0
  156. package/dist/links/link-config.d.ts +70 -0
  157. package/dist/links/link-config.js +107 -0
  158. package/dist/links/link-config.js.map +1 -0
  159. package/dist/links/maintain.d.ts +37 -0
  160. package/dist/links/maintain.js +91 -0
  161. package/dist/links/maintain.js.map +1 -0
  162. package/dist/links/repair.d.ts +46 -0
  163. package/dist/links/repair.js +96 -0
  164. package/dist/links/repair.js.map +1 -0
  165. package/dist/links/resolve.d.ts +41 -0
  166. package/dist/links/resolve.js +121 -0
  167. package/dist/links/resolve.js.map +1 -0
  168. package/dist/links/stale.d.ts +32 -0
  169. package/dist/links/stale.js +73 -0
  170. package/dist/links/stale.js.map +1 -0
  171. package/dist/markdown/content-hash.d.ts +34 -0
  172. package/dist/markdown/content-hash.js +46 -0
  173. package/dist/markdown/content-hash.js.map +1 -0
  174. package/dist/markdown/frontmatter.d.ts +69 -0
  175. package/dist/markdown/frontmatter.js +147 -0
  176. package/dist/markdown/frontmatter.js.map +1 -0
  177. package/dist/markdown/hash-backfill.d.ts +22 -0
  178. package/dist/markdown/hash-backfill.js +43 -0
  179. package/dist/markdown/hash-backfill.js.map +1 -0
  180. package/dist/mcp/query-syntax.d.ts +20 -0
  181. package/dist/mcp/query-syntax.js +194 -0
  182. package/dist/mcp/query-syntax.js.map +1 -0
  183. package/dist/mcp/render.d.ts +31 -0
  184. package/dist/mcp/render.js +110 -0
  185. package/dist/mcp/render.js.map +1 -0
  186. package/dist/mcp/server.d.ts +60 -0
  187. package/dist/mcp/server.js +170 -0
  188. package/dist/mcp/server.js.map +1 -0
  189. package/dist/mcp/tools.d.ts +93 -0
  190. package/dist/mcp/tools.js +1218 -0
  191. package/dist/mcp/tools.js.map +1 -0
  192. package/dist/rest/conditional.d.ts +37 -0
  193. package/dist/rest/conditional.js +58 -0
  194. package/dist/rest/conditional.js.map +1 -0
  195. package/dist/rest/negotiate.d.ts +33 -0
  196. package/dist/rest/negotiate.js +54 -0
  197. package/dist/rest/negotiate.js.map +1 -0
  198. package/dist/rest/routes.d.ts +37 -0
  199. package/dist/rest/routes.js +744 -0
  200. package/dist/rest/routes.js.map +1 -0
  201. package/dist/seed/fixture-seed.d.ts +35 -0
  202. package/dist/seed/fixture-seed.js +75 -0
  203. package/dist/seed/fixture-seed.js.map +1 -0
  204. package/dist/seed/starship-config.d.ts +3 -0
  205. package/dist/seed/starship-config.js +5 -0
  206. package/dist/seed/starship-config.js.map +1 -0
  207. package/dist/server/headers.d.ts +36 -0
  208. package/dist/server/headers.js +30 -0
  209. package/dist/server/headers.js.map +1 -0
  210. package/dist/server/http-error.d.ts +53 -0
  211. package/dist/server/http-error.js +104 -0
  212. package/dist/server/http-error.js.map +1 -0
  213. package/dist/server/serve.d.ts +56 -0
  214. package/dist/server/serve.js +135 -0
  215. package/dist/server/serve.js.map +1 -0
  216. package/dist/shell/audit.d.ts +32 -0
  217. package/dist/shell/audit.js +28 -0
  218. package/dist/shell/audit.js.map +1 -0
  219. package/dist/shell/guard.d.ts +50 -0
  220. package/dist/shell/guard.js +176 -0
  221. package/dist/shell/guard.js.map +1 -0
  222. package/dist/shell/keys.d.ts +36 -0
  223. package/dist/shell/keys.js +64 -0
  224. package/dist/shell/keys.js.map +1 -0
  225. package/dist/shell/login.d.ts +56 -0
  226. package/dist/shell/login.js +130 -0
  227. package/dist/shell/login.js.map +1 -0
  228. package/dist/shell/oidc.d.ts +62 -0
  229. package/dist/shell/oidc.js +102 -0
  230. package/dist/shell/oidc.js.map +1 -0
  231. package/dist/shell/policy.d.ts +100 -0
  232. package/dist/shell/policy.js +232 -0
  233. package/dist/shell/policy.js.map +1 -0
  234. package/dist/shell/proxy-policy.d.ts +34 -0
  235. package/dist/shell/proxy-policy.js +120 -0
  236. package/dist/shell/proxy-policy.js.map +1 -0
  237. package/dist/shell/proxy.d.ts +47 -0
  238. package/dist/shell/proxy.js +253 -0
  239. package/dist/shell/proxy.js.map +1 -0
  240. package/dist/shell/serve.d.ts +86 -0
  241. package/dist/shell/serve.js +0 -0
  242. package/dist/shell/serve.js.map +1 -0
  243. package/dist/shell/stdio.d.ts +55 -0
  244. package/dist/shell/stdio.js +63 -0
  245. package/dist/shell/stdio.js.map +1 -0
  246. package/dist/storage/registry.d.ts +20 -0
  247. package/dist/storage/registry.js +38 -0
  248. package/dist/storage/registry.js.map +1 -0
  249. package/dist/storage/search-plan.d.ts +93 -0
  250. package/dist/storage/search-plan.js +15 -0
  251. package/dist/storage/search-plan.js.map +1 -0
  252. package/dist/storage/types.d.ts +448 -0
  253. package/dist/storage/types.js +14 -0
  254. package/dist/storage/types.js.map +1 -0
  255. package/dist/storage/versions-since.d.ts +72 -0
  256. package/dist/storage/versions-since.js +92 -0
  257. package/dist/storage/versions-since.js.map +1 -0
  258. package/dist/storage-postgres/adapter.d.ts +17 -0
  259. package/dist/storage-postgres/adapter.js +782 -0
  260. package/dist/storage-postgres/adapter.js.map +1 -0
  261. package/dist/storage-postgres/compile-postgres.d.ts +22 -0
  262. package/dist/storage-postgres/compile-postgres.js +830 -0
  263. package/dist/storage-postgres/compile-postgres.js.map +1 -0
  264. package/dist/storage-postgres/errors.d.ts +34 -0
  265. package/dist/storage-postgres/errors.js +50 -0
  266. package/dist/storage-postgres/errors.js.map +1 -0
  267. package/dist/storage-postgres/migrations/0001_init.sql +113 -0
  268. package/dist/storage-postgres/migrations/0002_content_hash.sql +7 -0
  269. package/dist/storage-postgres/migrations/index.d.ts +22 -0
  270. package/dist/storage-postgres/migrations/index.js +70 -0
  271. package/dist/storage-postgres/migrations/index.js.map +1 -0
  272. package/dist/storage-sqlite/adapter.d.ts +2 -0
  273. package/dist/storage-sqlite/adapter.js +688 -0
  274. package/dist/storage-sqlite/adapter.js.map +1 -0
  275. package/dist/storage-sqlite/compile-filter.d.ts +41 -0
  276. package/dist/storage-sqlite/compile-filter.js +881 -0
  277. package/dist/storage-sqlite/compile-filter.js.map +1 -0
  278. package/dist/storage-sqlite/compile-sqlite.d.ts +19 -0
  279. package/dist/storage-sqlite/compile-sqlite.js +176 -0
  280. package/dist/storage-sqlite/compile-sqlite.js.map +1 -0
  281. package/dist/storage-sqlite/migrations/0001_init.sql +123 -0
  282. package/dist/storage-sqlite/migrations/0002_content_hash.sql +7 -0
  283. package/dist/storage-sqlite/migrations/index.d.ts +6 -0
  284. package/dist/storage-sqlite/migrations/index.js +41 -0
  285. package/dist/storage-sqlite/migrations/index.js.map +1 -0
  286. package/dist/storage-sqlite/vec.d.ts +40 -0
  287. package/dist/storage-sqlite/vec.js +62 -0
  288. package/dist/storage-sqlite/vec.js.map +1 -0
  289. package/dist/sync/cursor.d.ts +36 -0
  290. package/dist/sync/cursor.js +66 -0
  291. package/dist/sync/cursor.js.map +1 -0
  292. package/dist/sync/daemon.d.ts +40 -0
  293. package/dist/sync/daemon.js +202 -0
  294. package/dist/sync/daemon.js.map +1 -0
  295. package/dist/sync/feed.d.ts +38 -0
  296. package/dist/sync/feed.js +125 -0
  297. package/dist/sync/feed.js.map +1 -0
  298. package/dist/sync/fs-store.d.ts +8 -0
  299. package/dist/sync/fs-store.js +76 -0
  300. package/dist/sync/fs-store.js.map +1 -0
  301. package/dist/sync/intrinsics.d.ts +52 -0
  302. package/dist/sync/intrinsics.js +76 -0
  303. package/dist/sync/intrinsics.js.map +1 -0
  304. package/dist/sync/paths.d.ts +38 -0
  305. package/dist/sync/paths.js +56 -0
  306. package/dist/sync/paths.js.map +1 -0
  307. package/dist/sync/push.d.ts +48 -0
  308. package/dist/sync/push.js +328 -0
  309. package/dist/sync/push.js.map +1 -0
  310. package/dist/sync/reconcile.d.ts +54 -0
  311. package/dist/sync/reconcile.js +252 -0
  312. package/dist/sync/reconcile.js.map +1 -0
  313. package/dist/sync/run.d.ts +27 -0
  314. package/dist/sync/run.js +46 -0
  315. package/dist/sync/run.js.map +1 -0
  316. package/dist/version.d.ts +2 -0
  317. package/dist/version.js +5 -0
  318. package/dist/version.js.map +1 -0
  319. package/package.json +80 -0
@@ -0,0 +1,60 @@
1
+ /**
2
+ * MCP surface — design §6.2, noauth plan §4.
3
+ *
4
+ * Protocol-true MCP via `@modelcontextprotocol/sdk`. Streamable HTTP runs
5
+ * **stateless**: no server-side session store. There is no per-request auth —
6
+ * mrplex trusts its caller (design §8). Each request's CallContext is read from
7
+ * the `X-Mrplex-*` headers, which a fronting shell injects (a proxy can set
8
+ * headers but not rewrite tool arguments — so headers are the injection path).
9
+ *
10
+ * STDIO binds one launch-time CallContext for the whole session (no per-request
11
+ * header channel); `--author` / `--scope` launch flags pin it (noauth plan §4).
12
+ *
13
+ * We use the lower-level `Server` class (not `McpServer`) so tool schemas
14
+ * can be plain JSON Schema — no Zod runtime dependency (tools.ts).
15
+ */
16
+ import type { IncomingMessage, ServerResponse } from "node:http";
17
+ import { Server as McpLowLevelServer } from "@modelcontextprotocol/sdk/server/index.js";
18
+ import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
19
+ import type { CallContext } from "../kernel/context.js";
20
+ import type { Kernel } from "../kernel/kernel.js";
21
+ import { type ContextForRequest, type KernelForRequest } from "../server/headers.js";
22
+ import type { Storage } from "../storage/types.js";
23
+ export type McpConfig = {
24
+ kernel: Kernel;
25
+ storage: Storage;
26
+ /**
27
+ * How each request becomes a CallContext. Defaults to reading the
28
+ * `X-Mrplex-*` headers; an authenticating shell substitutes an
29
+ * entitlement-derived context here.
30
+ */
31
+ contextForRequest?: ContextForRequest;
32
+ /**
33
+ * How each request obtains its Kernel. Defaults to the shared `kernel`; an
34
+ * authenticating shell returns a per-principal guarded kernel (and may throw
35
+ * to reject the request before the SDK parses the frame).
36
+ */
37
+ kernelForRequest?: KernelForRequest;
38
+ };
39
+ export type McpMount = {
40
+ handle: (req: IncomingMessage, res: ServerResponse) => Promise<void>;
41
+ close: () => Promise<void>;
42
+ };
43
+ /**
44
+ * Mount an MCP surface as a Streamable-HTTP request handler.
45
+ *
46
+ * We create a fresh Server + StreamableHTTPServerTransport pair PER REQUEST.
47
+ * That's the SDK's supported "stateless" mode: session-less, so state
48
+ * cannot accumulate between calls, and multi-instance deployments (§7.1)
49
+ * work without a shared session store.
50
+ */
51
+ export declare function mountMcpStreamableHttp(config: McpConfig): Promise<McpMount>;
52
+ export type StdioMount = {
53
+ server: McpLowLevelServer;
54
+ transport: StdioServerTransport;
55
+ close: () => Promise<void>;
56
+ };
57
+ export declare function startMcpStdio(config: {
58
+ kernel: Kernel;
59
+ context?: CallContext;
60
+ }): Promise<StdioMount>;
@@ -0,0 +1,170 @@
1
+ /**
2
+ * MCP surface — design §6.2, noauth plan §4.
3
+ *
4
+ * Protocol-true MCP via `@modelcontextprotocol/sdk`. Streamable HTTP runs
5
+ * **stateless**: no server-side session store. There is no per-request auth —
6
+ * mrplex trusts its caller (design §8). Each request's CallContext is read from
7
+ * the `X-Mrplex-*` headers, which a fronting shell injects (a proxy can set
8
+ * headers but not rewrite tool arguments — so headers are the injection path).
9
+ *
10
+ * STDIO binds one launch-time CallContext for the whole session (no per-request
11
+ * header channel); `--author` / `--scope` launch flags pin it (noauth plan §4).
12
+ *
13
+ * We use the lower-level `Server` class (not `McpServer`) so tool schemas
14
+ * can be plain JSON Schema — no Zod runtime dependency (tools.ts).
15
+ */
16
+ import { Server as McpLowLevelServer } from "@modelcontextprotocol/sdk/server/index.js";
17
+ import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
18
+ import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
19
+ import { CallToolRequestSchema, ListToolsRequestSchema } from "@modelcontextprotocol/sdk/types.js";
20
+ import { KernelError } from "../kernel/errors.js";
21
+ import { VERSION } from "../version.js";
22
+ import { contextFromHeaders, } from "../server/headers.js";
23
+ import { httpErrorForThrowable } from "../server/http-error.js";
24
+ import { TOOL_REGISTRY, toolByName } from "./tools.js";
25
+ /**
26
+ * Mount an MCP surface as a Streamable-HTTP request handler.
27
+ *
28
+ * We create a fresh Server + StreamableHTTPServerTransport pair PER REQUEST.
29
+ * That's the SDK's supported "stateless" mode: session-less, so state
30
+ * cannot accumulate between calls, and multi-instance deployments (§7.1)
31
+ * work without a shared session store.
32
+ */
33
+ export async function mountMcpStreamableHttp(config) {
34
+ const { kernel } = config;
35
+ const contextForRequest = config.contextForRequest ?? contextFromHeaders;
36
+ const kernelForRequest = config.kernelForRequest ?? (() => kernel);
37
+ async function handle(req, res) {
38
+ // Resolve the per-request kernel and CallContext before the SDK parses the
39
+ // frame. Either step may throw: the shell's `kernelForRequest` rejects an
40
+ // unauthenticated caller (HttpResponseError → its own status), and a
41
+ // malformed X-Mrplex-Scope is a KernelError "filter_invalid" → 400. A bad
42
+ // credential or claim is a loud error, never a silent full-access fallback.
43
+ let reqKernel;
44
+ let ctx;
45
+ try {
46
+ reqKernel = await kernelForRequest(req);
47
+ ctx = await contextForRequest(req);
48
+ }
49
+ catch (err) {
50
+ const { status, body } = httpErrorForThrowable(err);
51
+ // A generic 500 body from a non-kernel throwable is opaque here; keep the
52
+ // MCP-friendly filter_invalid shape for the malformed-input case.
53
+ const payload = err instanceof KernelError ? { code: err.code, data: err.data } : body;
54
+ res.statusCode = err instanceof KernelError ? 400 : status;
55
+ res.setHeader("Content-Type", "application/json");
56
+ res.end(JSON.stringify(payload));
57
+ return;
58
+ }
59
+ const server = buildMcpServer(reqKernel, () => ctx);
60
+ const transport = new StreamableHTTPServerTransport({
61
+ sessionIdGenerator: undefined, // stateless per m3-plan decision 3
62
+ enableJsonResponse: true,
63
+ });
64
+ res.on("close", () => {
65
+ // Best-effort — SDK also cleans up on its own.
66
+ transport.close().catch(() => { });
67
+ server.close().catch(() => { });
68
+ });
69
+ await server.connect(transport);
70
+ await transport.handleRequest(req, res);
71
+ }
72
+ async function close() {
73
+ // Per-request instances handle their own close; nothing global to tear down.
74
+ }
75
+ return { handle, close };
76
+ }
77
+ /**
78
+ * Server-level instructions surfaced to clients at `initialize`. Cross-tool
79
+ * conventions live here; per-tool details belong in tool descriptions
80
+ * (tools.ts) and the `query_syntax` reference. Clients vary in whether they
81
+ * show these to the model — don't put anything here that isn't ALSO
82
+ * discoverable through a tool description or an error message.
83
+ */
84
+ const SERVER_INSTRUCTIONS = `mrplex is a queryable, versioned store for Markdown documents with YAML frontmatter, organized into repos.
85
+
86
+ Conventions:
87
+ - Reads (docs_get / docs_get_version) return frontmatter_raw with server-injected \`$version: <version_id>\` then \`$content_hash: <sha256>\` lines (unless raw: true). Pass that frontmatter_raw back to docs_put unchanged and prev_version_id may be omitted — the embedded $version supplies it. \`$\`-prefixed frontmatter keys are server-owned and are stripped from writes.
88
+ - Writes use optimistic concurrency: docs_put / docs_delete need the previous version id; a \`stale_prev\` error means someone else wrote first — re-read (docs_get) and retry against the new version.
89
+ - When writing frontmatter, provide exactly one of \`frontmatter\` (JSON object) or \`frontmatter_raw\` (verbatim YAML).
90
+ - \`query\` searches current documents and returns lean projected hits, not full documents. Default \`select\` is ["$path"] — each hit is only \`{ "$path": "…" }\` (no body, no frontmatter, no version id). Pass \`select\` to project more (\`$body\`, \`$repo\`, \`$version_id\`, \`$content_hash\`, \`$semantic_score\`, frontmatter keys, …). \`semantic\` activates embedding-based retrieval; add \`$semantic_score\` to \`select\` for cosine similarity (1 = identical). Call \`docs_get\` (one path) or \`docs_get_many\` (batch) to recover whole documents. The filter is CEL with \`$\`-prefixed intrinsics ($path, $updated_at, $body, $content_hash) and link-graph predicates ($in, $has, $backlinks(), $links()). Call the \`query_syntax\` tool for the full language reference before writing a non-trivial filter.
91
+ - Tool failures return an in-band error (isError: true) with a JSON object { code, data } in the text content: e.g. filter_invalid (bad query — data.reason explains, data.hint says what to consult), stale_prev (concurrency conflict), doc_not_found, semantic_unavailable (no embedding hook configured). Codes are stable; reasons are prose. Error results carry no structuredContent (tool outputSchema describes success shapes only).`;
92
+ /**
93
+ * Build a low-level MCP Server with tools/list + tools/call handlers wired
94
+ * up. The `getContext` closure supplies the session's CallContext at call time.
95
+ */
96
+ function buildMcpServer(kernel, getContext) {
97
+ const server = new McpLowLevelServer({ name: "mrplex", version: VERSION }, { capabilities: { tools: {} }, instructions: SERVER_INSTRUCTIONS });
98
+ server.setRequestHandler(ListToolsRequestSchema, async () => ({
99
+ tools: TOOL_REGISTRY.map((t) => ({
100
+ name: t.name,
101
+ description: t.description,
102
+ inputSchema: t.inputSchema,
103
+ ...(t.outputSchema !== undefined && { outputSchema: t.outputSchema }),
104
+ })),
105
+ }));
106
+ server.setRequestHandler(CallToolRequestSchema, async (req) => {
107
+ const name = req.params.name;
108
+ const args = (req.params.arguments ?? {});
109
+ const tool = toolByName(name);
110
+ if (!tool) {
111
+ // Use filter_invalid (a real KernelErrorCode) rather than a fabricated
112
+ // "internal_error" — clients that rehydrate KernelError from the
113
+ // in-band payload should always see codes from the stable catalog.
114
+ return toolError({
115
+ code: "filter_invalid",
116
+ data: { reason: `unknown tool: ${name}` },
117
+ });
118
+ }
119
+ try {
120
+ const result = await tool.handler(kernel, getContext(), args);
121
+ return {
122
+ content: [{ type: "text", text: result.text }],
123
+ structuredContent: result.structured,
124
+ };
125
+ }
126
+ catch (err) {
127
+ if (err instanceof KernelError) {
128
+ return toolError({ code: err.code, data: err.data });
129
+ }
130
+ // Non-kernel throwable — surface as filter_invalid too, since a real
131
+ // internal server error would 500 at the transport, not in-band.
132
+ return toolError({
133
+ code: "filter_invalid",
134
+ data: { reason: err.message ?? String(err) },
135
+ });
136
+ }
137
+ });
138
+ return server;
139
+ }
140
+ /**
141
+ * Package a KernelError as an in-band tool error per §6.2 / m3-plan
142
+ * decision. `isError: true` with the JSON `{ code, data }` payload in the
143
+ * text content — and deliberately NO structuredContent: tools declare
144
+ * `outputSchema` for their success shapes, and spec-conformant clients
145
+ * (the SDK included) validate any structuredContent present against it
146
+ * even on error results. Error consumers parse the text channel
147
+ * (client/remote-mcp.ts does exactly that).
148
+ */
149
+ function toolError(payload) {
150
+ const text = JSON.stringify(payload);
151
+ return {
152
+ isError: true,
153
+ content: [{ type: "text", text }],
154
+ };
155
+ }
156
+ export async function startMcpStdio(config) {
157
+ const ctx = config.context ?? {};
158
+ const server = buildMcpServer(config.kernel, () => ctx);
159
+ const transport = new StdioServerTransport();
160
+ await server.connect(transport);
161
+ return {
162
+ server,
163
+ transport,
164
+ close: async () => {
165
+ await transport.close().catch(() => { });
166
+ await server.close().catch(() => { });
167
+ },
168
+ };
169
+ }
170
+ //# sourceMappingURL=server.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"server.js","sourceRoot":"","sources":["../../src/mcp/server.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAGH,OAAO,EAAE,MAAM,IAAI,iBAAiB,EAAE,MAAM,2CAA2C,CAAC;AACxF,OAAO,EAAE,oBAAoB,EAAE,MAAM,2CAA2C,CAAC;AACjF,OAAO,EAAE,6BAA6B,EAAE,MAAM,oDAAoD,CAAC;AACnG,OAAO,EAAE,qBAAqB,EAAE,sBAAsB,EAAE,MAAM,oCAAoC,CAAC;AAEnG,OAAO,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAElD,OAAO,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AACxC,OAAO,EAGL,kBAAkB,GACnB,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAAE,qBAAqB,EAAE,MAAM,yBAAyB,CAAC;AAEhE,OAAO,EAAE,aAAa,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAwBvD;;;;;;;GAOG;AACH,MAAM,CAAC,KAAK,UAAU,sBAAsB,CAAC,MAAiB;IAC5D,MAAM,EAAE,MAAM,EAAE,GAAG,MAAM,CAAC;IAC1B,MAAM,iBAAiB,GAAG,MAAM,CAAC,iBAAiB,IAAI,kBAAkB,CAAC;IACzE,MAAM,gBAAgB,GAAG,MAAM,CAAC,gBAAgB,IAAI,CAAC,GAAG,EAAE,CAAC,MAAM,CAAC,CAAC;IAEnE,KAAK,UAAU,MAAM,CAAC,GAAoB,EAAE,GAAmB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E,IAAI;QACb,OAAO,EAAE,CAAC,EAAE,IAAI,EAAE,MAAe,EAAE,IAAI,EAAE,CAAC;KAC3C,CAAC;AACJ,CAAC;AAcD,MAAM,CAAC,KAAK,UAAU,aAAa,CAAC,MAGnC;IACC,MAAM,GAAG,GAAG,MAAM,CAAC,OAAO,IAAI,EAAE,CAAC;IACjC,MAAM,MAAM,GAAG,cAAc,CAAC,MAAM,CAAC,MAAM,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC,CAAC;IACxD,MAAM,SAAS,GAAG,IAAI,oBAAoB,EAAE,CAAC;IAC7C,MAAM,MAAM,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;IAChC,OAAO;QACL,MAAM;QACN,SAAS;QACT,KAAK,EAAE,KAAK,IAAI,EAAE;YAChB,MAAM,SAAS,CAAC,KAAK,EAAE,CAAC,KAAK,CAAC,GAAG,EAAE,GAAE,CAAC,CAAC,CAAC;YACxC,MAAM,MAAM,CAAC,KAAK,EAAE,CAAC,KAAK,CAAC,GAAG,EAAE,GAAE,CAAC,CAAC,CAAC;QACvC,CAAC;KACF,CAAC;AACJ,CAAC"}
@@ -0,0 +1,93 @@
1
+ /**
2
+ * MCP tool registry — design §6.2.
3
+ *
4
+ * Tools mirror the kernel one-to-one. Each entry names the tool, describes it,
5
+ * carries a JSON Schema for the input, and delegates to a kernel call with the
6
+ * session's CallContext. No-auth (noauth plan): the user and token tools are
7
+ * gone; write tools gain an optional `author` and the query tool an optional
8
+ * `scope` — both overridden by the X-Mrplex-* request headers when present.
9
+ *
10
+ * Results shape:
11
+ * • On success: { structured, text } — structured is the wire type,
12
+ * text is a compact human rendering (render.ts).
13
+ * • On kernel error: throws; the SDK wiring in server.ts converts to an
14
+ * in-band tool error ({ code, data }) per §6.2.
15
+ */
16
+ import { type CallContext } from "../kernel/context.js";
17
+ import type { Kernel } from "../kernel/kernel.js";
18
+ /**
19
+ * A tool's structured payload is always a JSON object — MCP's
20
+ * `structuredContent` field is a record per the spec (and the SDK enforces
21
+ * it). List-shaped results are wrapped in `{ items: [...] }`; single-object
22
+ * results pass through.
23
+ */
24
+ export type ToolResult = {
25
+ structured: Record<string, unknown>;
26
+ text: string;
27
+ };
28
+ /**
29
+ * The session context carries the header-injected author/scope (see
30
+ * mcp/server.ts). Handlers merge in tool-arg author/scope only where the
31
+ * header left a gap — headers beat tool args (noauth plan decision 8).
32
+ */
33
+ export type ToolHandler = (kernel: Kernel, ctx: CallContext, args: Record<string, unknown>) => ToolResult | Promise<ToolResult>;
34
+ export type ToolEntry = {
35
+ name: string;
36
+ description: string;
37
+ inputSchema: JsonSchema;
38
+ /**
39
+ * Success-result shape of `structuredContent` (MCP `outputSchema`).
40
+ * In-band tool errors carry no structuredContent (server.ts toolError),
41
+ * so this describes success results only — per spec, error results are
42
+ * exempt from output-schema conformance.
43
+ */
44
+ outputSchema?: JsonSchema;
45
+ handler: ToolHandler;
46
+ };
47
+ type JsonSchema = {
48
+ type: "object";
49
+ description?: string;
50
+ properties: Record<string, JsonSchemaProp>;
51
+ required?: string[];
52
+ additionalProperties?: false;
53
+ };
54
+ type JsonSchemaProp = {
55
+ type: "string";
56
+ description?: string;
57
+ enum?: string[];
58
+ } | {
59
+ type: "number";
60
+ description?: string;
61
+ } | {
62
+ type: "integer";
63
+ description?: string;
64
+ minimum?: number;
65
+ } | {
66
+ type: "boolean";
67
+ description?: string;
68
+ } | {
69
+ type: "null";
70
+ description?: string;
71
+ } | {
72
+ type: "array";
73
+ description?: string;
74
+ items: JsonSchemaProp;
75
+ } | {
76
+ type: "object";
77
+ description?: string;
78
+ properties?: Record<string, JsonSchemaProp>;
79
+ required?: string[];
80
+ additionalProperties?: boolean | JsonSchemaProp;
81
+ } | {
82
+ oneOf: JsonSchemaProp[];
83
+ description?: string;
84
+ } | {
85
+ anyOf: JsonSchemaProp[];
86
+ description?: string;
87
+ } | {
88
+ description?: string;
89
+ };
90
+ export declare const TOOL_REGISTRY: ToolEntry[];
91
+ /** Convenient lookup by name. */
92
+ export declare function toolByName(name: string): ToolEntry | undefined;
93
+ export {};