@yolk-sdk/emulators 0.1.0-canary.100

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 (338) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +2165 -0
  3. package/dist/anthropic.d.mts +269 -0
  4. package/dist/anthropic.d.mts.map +1 -0
  5. package/dist/anthropic.mjs +177 -0
  6. package/dist/anthropic.mjs.map +1 -0
  7. package/dist/chat-completions.d.mts +288 -0
  8. package/dist/chat-completions.d.mts.map +1 -0
  9. package/dist/chat-completions.mjs +451 -0
  10. package/dist/chat-completions.mjs.map +1 -0
  11. package/dist/codex.d.mts +265 -0
  12. package/dist/codex.d.mts.map +1 -0
  13. package/dist/codex.mjs +199 -0
  14. package/dist/codex.mjs.map +1 -0
  15. package/dist/dropbox/api.d.mts +63 -0
  16. package/dist/dropbox/api.d.mts.map +1 -0
  17. package/dist/dropbox/api.mjs +565 -0
  18. package/dist/dropbox/api.mjs.map +1 -0
  19. package/dist/dropbox/state.d.mts +136 -0
  20. package/dist/dropbox/state.d.mts.map +1 -0
  21. package/dist/dropbox/state.mjs +209 -0
  22. package/dist/dropbox/state.mjs.map +1 -0
  23. package/dist/dropbox.d.mts +79 -0
  24. package/dist/dropbox.d.mts.map +1 -0
  25. package/dist/dropbox.mjs +124 -0
  26. package/dist/dropbox.mjs.map +1 -0
  27. package/dist/email-fixtures.d.mts +36 -0
  28. package/dist/email-fixtures.d.mts.map +1 -0
  29. package/dist/email-fixtures.mjs +1080 -0
  30. package/dist/email-fixtures.mjs.map +1 -0
  31. package/dist/email.d.mts +160 -0
  32. package/dist/email.d.mts.map +1 -0
  33. package/dist/email.mjs +608 -0
  34. package/dist/email.mjs.map +1 -0
  35. package/dist/emulator-compose.d.mts +40 -0
  36. package/dist/emulator-compose.d.mts.map +1 -0
  37. package/dist/emulator-compose.mjs +91 -0
  38. package/dist/emulator-compose.mjs.map +1 -0
  39. package/dist/emulator-http.d.mts +53 -0
  40. package/dist/emulator-http.d.mts.map +1 -0
  41. package/dist/emulator-http.mjs +117 -0
  42. package/dist/emulator-http.mjs.map +1 -0
  43. package/dist/emulator-kernel.d.mts +176 -0
  44. package/dist/emulator-kernel.d.mts.map +1 -0
  45. package/dist/emulator-kernel.mjs +413 -0
  46. package/dist/emulator-kernel.mjs.map +1 -0
  47. package/dist/fixture-route.d.mts +129 -0
  48. package/dist/fixture-route.d.mts.map +1 -0
  49. package/dist/fixture-route.mjs +257 -0
  50. package/dist/fixture-route.mjs.map +1 -0
  51. package/dist/fortnox/api.d.mts +92 -0
  52. package/dist/fortnox/api.d.mts.map +1 -0
  53. package/dist/fortnox/api.mjs +750 -0
  54. package/dist/fortnox/api.mjs.map +1 -0
  55. package/dist/fortnox/state.d.mts +533 -0
  56. package/dist/fortnox/state.d.mts.map +1 -0
  57. package/dist/fortnox/state.mjs +612 -0
  58. package/dist/fortnox/state.mjs.map +1 -0
  59. package/dist/fortnox.d.mts +128 -0
  60. package/dist/fortnox.d.mts.map +1 -0
  61. package/dist/fortnox.mjs +403 -0
  62. package/dist/fortnox.mjs.map +1 -0
  63. package/dist/gateway-evaluate-recordings.d.mts +12 -0
  64. package/dist/gateway-evaluate-recordings.d.mts.map +1 -0
  65. package/dist/gateway-evaluate-recordings.mjs +128 -0
  66. package/dist/gateway-evaluate-recordings.mjs.map +1 -0
  67. package/dist/gateway.d.mts +272 -0
  68. package/dist/gateway.d.mts.map +1 -0
  69. package/dist/gateway.mjs +400 -0
  70. package/dist/gateway.mjs.map +1 -0
  71. package/dist/github/api.d.mts +63 -0
  72. package/dist/github/api.d.mts.map +1 -0
  73. package/dist/github/api.mjs +527 -0
  74. package/dist/github/api.mjs.map +1 -0
  75. package/dist/github/state.d.mts +272 -0
  76. package/dist/github/state.d.mts.map +1 -0
  77. package/dist/github/state.mjs +303 -0
  78. package/dist/github/state.mjs.map +1 -0
  79. package/dist/github.d.mts +77 -0
  80. package/dist/github.d.mts.map +1 -0
  81. package/dist/github.mjs +180 -0
  82. package/dist/github.mjs.map +1 -0
  83. package/dist/google/calendar.d.mts +10 -0
  84. package/dist/google/calendar.d.mts.map +1 -0
  85. package/dist/google/calendar.mjs +252 -0
  86. package/dist/google/calendar.mjs.map +1 -0
  87. package/dist/google/drive.d.mts +8 -0
  88. package/dist/google/drive.d.mts.map +1 -0
  89. package/dist/google/drive.mjs +252 -0
  90. package/dist/google/drive.mjs.map +1 -0
  91. package/dist/google/gmail.d.mts +10 -0
  92. package/dist/google/gmail.d.mts.map +1 -0
  93. package/dist/google/gmail.mjs +591 -0
  94. package/dist/google/gmail.mjs.map +1 -0
  95. package/dist/google/shared.d.mts +107 -0
  96. package/dist/google/shared.d.mts.map +1 -0
  97. package/dist/google/shared.mjs +146 -0
  98. package/dist/google/shared.mjs.map +1 -0
  99. package/dist/google/state.d.mts +385 -0
  100. package/dist/google/state.d.mts.map +1 -0
  101. package/dist/google/state.mjs +496 -0
  102. package/dist/google/state.mjs.map +1 -0
  103. package/dist/google.d.mts +75 -0
  104. package/dist/google.d.mts.map +1 -0
  105. package/dist/google.mjs +210 -0
  106. package/dist/google.mjs.map +1 -0
  107. package/dist/linkedin-search/api.d.mts +45 -0
  108. package/dist/linkedin-search/api.d.mts.map +1 -0
  109. package/dist/linkedin-search/api.mjs +181 -0
  110. package/dist/linkedin-search/api.mjs.map +1 -0
  111. package/dist/linkedin-search/state.d.mts +123 -0
  112. package/dist/linkedin-search/state.d.mts.map +1 -0
  113. package/dist/linkedin-search/state.mjs +261 -0
  114. package/dist/linkedin-search/state.mjs.map +1 -0
  115. package/dist/linkedin-search.d.mts +69 -0
  116. package/dist/linkedin-search.d.mts.map +1 -0
  117. package/dist/linkedin-search.mjs +158 -0
  118. package/dist/linkedin-search.mjs.map +1 -0
  119. package/dist/mcp/afloat-recordings.d.mts +8 -0
  120. package/dist/mcp/afloat-recordings.d.mts.map +1 -0
  121. package/dist/mcp/afloat-recordings.mjs +918 -0
  122. package/dist/mcp/afloat-recordings.mjs.map +1 -0
  123. package/dist/mcp/api.d.mts +45 -0
  124. package/dist/mcp/api.d.mts.map +1 -0
  125. package/dist/mcp/api.mjs +609 -0
  126. package/dist/mcp/api.mjs.map +1 -0
  127. package/dist/mcp/recordings.d.mts +40 -0
  128. package/dist/mcp/recordings.d.mts.map +1 -0
  129. package/dist/mcp/recordings.mjs +2390 -0
  130. package/dist/mcp/recordings.mjs.map +1 -0
  131. package/dist/mcp/state.d.mts +86 -0
  132. package/dist/mcp/state.d.mts.map +1 -0
  133. package/dist/mcp/state.mjs +98 -0
  134. package/dist/mcp/state.mjs.map +1 -0
  135. package/dist/mcp.d.mts +85 -0
  136. package/dist/mcp.d.mts.map +1 -0
  137. package/dist/mcp.mjs +264 -0
  138. package/dist/mcp.mjs.map +1 -0
  139. package/dist/messages.d.mts +183 -0
  140. package/dist/messages.d.mts.map +1 -0
  141. package/dist/messages.mjs +532 -0
  142. package/dist/messages.mjs.map +1 -0
  143. package/dist/microsoft/api.d.mts +47 -0
  144. package/dist/microsoft/api.d.mts.map +1 -0
  145. package/dist/microsoft/api.mjs +178 -0
  146. package/dist/microsoft/api.mjs.map +1 -0
  147. package/dist/microsoft/calendar.d.mts +30 -0
  148. package/dist/microsoft/calendar.d.mts.map +1 -0
  149. package/dist/microsoft/calendar.mjs +271 -0
  150. package/dist/microsoft/calendar.mjs.map +1 -0
  151. package/dist/microsoft/drive.d.mts +36 -0
  152. package/dist/microsoft/drive.d.mts.map +1 -0
  153. package/dist/microsoft/drive.mjs +298 -0
  154. package/dist/microsoft/drive.mjs.map +1 -0
  155. package/dist/microsoft/graph.d.mts +198 -0
  156. package/dist/microsoft/graph.d.mts.map +1 -0
  157. package/dist/microsoft/graph.mjs +264 -0
  158. package/dist/microsoft/graph.mjs.map +1 -0
  159. package/dist/microsoft/mail.d.mts +48 -0
  160. package/dist/microsoft/mail.d.mts.map +1 -0
  161. package/dist/microsoft/mail.mjs +488 -0
  162. package/dist/microsoft/mail.mjs.map +1 -0
  163. package/dist/microsoft/state.d.mts +480 -0
  164. package/dist/microsoft/state.d.mts.map +1 -0
  165. package/dist/microsoft/state.mjs +625 -0
  166. package/dist/microsoft/state.mjs.map +1 -0
  167. package/dist/microsoft.d.mts +168 -0
  168. package/dist/microsoft.d.mts.map +1 -0
  169. package/dist/microsoft.mjs +483 -0
  170. package/dist/microsoft.mjs.map +1 -0
  171. package/dist/node.d.mts +47 -0
  172. package/dist/node.d.mts.map +1 -0
  173. package/dist/node.mjs +178 -0
  174. package/dist/node.mjs.map +1 -0
  175. package/dist/notion/api.d.mts +51 -0
  176. package/dist/notion/api.d.mts.map +1 -0
  177. package/dist/notion/api.mjs +507 -0
  178. package/dist/notion/api.mjs.map +1 -0
  179. package/dist/notion/state.d.mts +329 -0
  180. package/dist/notion/state.d.mts.map +1 -0
  181. package/dist/notion/state.mjs +432 -0
  182. package/dist/notion/state.mjs.map +1 -0
  183. package/dist/notion.d.mts +72 -0
  184. package/dist/notion.d.mts.map +1 -0
  185. package/dist/notion.mjs +127 -0
  186. package/dist/notion.mjs.map +1 -0
  187. package/dist/openai.d.mts +165 -0
  188. package/dist/openai.d.mts.map +1 -0
  189. package/dist/openai.mjs +141 -0
  190. package/dist/openai.mjs.map +1 -0
  191. package/dist/opencode-recordings.d.mts +7 -0
  192. package/dist/opencode-recordings.d.mts.map +1 -0
  193. package/dist/opencode-recordings.mjs +222 -0
  194. package/dist/opencode-recordings.mjs.map +1 -0
  195. package/dist/opencode.d.mts +70 -0
  196. package/dist/opencode.d.mts.map +1 -0
  197. package/dist/opencode.mjs +217 -0
  198. package/dist/opencode.mjs.map +1 -0
  199. package/dist/r2-fixtures.d.mts +36 -0
  200. package/dist/r2-fixtures.d.mts.map +1 -0
  201. package/dist/r2-fixtures.mjs +245 -0
  202. package/dist/r2-fixtures.mjs.map +1 -0
  203. package/dist/r2-guard.d.mts +43 -0
  204. package/dist/r2-guard.d.mts.map +1 -0
  205. package/dist/r2-guard.mjs +216 -0
  206. package/dist/r2-guard.mjs.map +1 -0
  207. package/dist/r2.d.mts +166 -0
  208. package/dist/r2.d.mts.map +1 -0
  209. package/dist/r2.mjs +517 -0
  210. package/dist/r2.mjs.map +1 -0
  211. package/dist/responses.d.mts +231 -0
  212. package/dist/responses.d.mts.map +1 -0
  213. package/dist/responses.mjs +556 -0
  214. package/dist/responses.mjs.map +1 -0
  215. package/dist/route-evidence.d.mts +45 -0
  216. package/dist/route-evidence.d.mts.map +1 -0
  217. package/dist/route-evidence.mjs +56 -0
  218. package/dist/route-evidence.mjs.map +1 -0
  219. package/dist/router.d.mts +93 -0
  220. package/dist/router.d.mts.map +1 -0
  221. package/dist/router.mjs +259 -0
  222. package/dist/router.mjs.map +1 -0
  223. package/dist/stateful-core.d.mts +17 -0
  224. package/dist/stateful-core.d.mts.map +1 -0
  225. package/dist/stateful-core.mjs +41 -0
  226. package/dist/stateful-core.mjs.map +1 -0
  227. package/dist/stateful-emulator.d.mts +578 -0
  228. package/dist/stateful-emulator.d.mts.map +1 -0
  229. package/dist/stateful-emulator.mjs +1248 -0
  230. package/dist/stateful-emulator.mjs.map +1 -0
  231. package/dist/stateful-secrets.d.mts +84 -0
  232. package/dist/stateful-secrets.d.mts.map +1 -0
  233. package/dist/stateful-secrets.mjs +216 -0
  234. package/dist/stateful-secrets.mjs.map +1 -0
  235. package/dist/subscription-usage-recordings.d.mts +9 -0
  236. package/dist/subscription-usage-recordings.d.mts.map +1 -0
  237. package/dist/subscription-usage-recordings.mjs +53 -0
  238. package/dist/subscription-usage-recordings.mjs.map +1 -0
  239. package/dist/subscription-usage.d.mts +53 -0
  240. package/dist/subscription-usage.d.mts.map +1 -0
  241. package/dist/subscription-usage.mjs +21 -0
  242. package/dist/subscription-usage.mjs.map +1 -0
  243. package/dist/telegram/api.d.mts +34 -0
  244. package/dist/telegram/api.d.mts.map +1 -0
  245. package/dist/telegram/api.mjs +257 -0
  246. package/dist/telegram/api.mjs.map +1 -0
  247. package/dist/telegram/state.d.mts +128 -0
  248. package/dist/telegram/state.d.mts.map +1 -0
  249. package/dist/telegram/state.mjs +154 -0
  250. package/dist/telegram/state.mjs.map +1 -0
  251. package/dist/telegram.d.mts +62 -0
  252. package/dist/telegram.d.mts.map +1 -0
  253. package/dist/telegram.mjs +127 -0
  254. package/dist/telegram.mjs.map +1 -0
  255. package/dist/todoist/api.d.mts +55 -0
  256. package/dist/todoist/api.d.mts.map +1 -0
  257. package/dist/todoist/api.mjs +415 -0
  258. package/dist/todoist/api.mjs.map +1 -0
  259. package/dist/todoist/state.d.mts +275 -0
  260. package/dist/todoist/state.d.mts.map +1 -0
  261. package/dist/todoist/state.mjs +345 -0
  262. package/dist/todoist/state.mjs.map +1 -0
  263. package/dist/todoist.d.mts +68 -0
  264. package/dist/todoist.d.mts.map +1 -0
  265. package/dist/todoist.mjs +181 -0
  266. package/dist/todoist.mjs.map +1 -0
  267. package/dist/xai.d.mts +267 -0
  268. package/dist/xai.d.mts.map +1 -0
  269. package/dist/xai.mjs +252 -0
  270. package/dist/xai.mjs.map +1 -0
  271. package/package.json +157 -0
  272. package/src/anthropic.ts +289 -0
  273. package/src/chat-completions.ts +924 -0
  274. package/src/codex.ts +296 -0
  275. package/src/dropbox/api.ts +1084 -0
  276. package/src/dropbox/state.ts +364 -0
  277. package/src/dropbox.ts +203 -0
  278. package/src/email-fixtures.ts +1297 -0
  279. package/src/email.ts +1108 -0
  280. package/src/emulator-compose.ts +147 -0
  281. package/src/emulator-http.ts +184 -0
  282. package/src/emulator-kernel.ts +844 -0
  283. package/src/fixture-route.ts +561 -0
  284. package/src/fortnox/api.ts +1352 -0
  285. package/src/fortnox/state.ts +798 -0
  286. package/src/fortnox.ts +801 -0
  287. package/src/gateway-evaluate-recordings.ts +153 -0
  288. package/src/gateway.ts +530 -0
  289. package/src/github/api.ts +986 -0
  290. package/src/github/state.ts +439 -0
  291. package/src/github.ts +271 -0
  292. package/src/google/calendar.ts +486 -0
  293. package/src/google/drive.ts +471 -0
  294. package/src/google/gmail.ts +1011 -0
  295. package/src/google/shared.ts +321 -0
  296. package/src/google/state.ts +686 -0
  297. package/src/google.ts +298 -0
  298. package/src/linkedin-search/api.ts +363 -0
  299. package/src/linkedin-search/state.ts +373 -0
  300. package/src/linkedin-search.ts +241 -0
  301. package/src/mcp/afloat-recordings.ts +1040 -0
  302. package/src/mcp/api.ts +1105 -0
  303. package/src/mcp/recordings.ts +2665 -0
  304. package/src/mcp/state.ts +144 -0
  305. package/src/mcp.ts +364 -0
  306. package/src/messages.ts +844 -0
  307. package/src/microsoft/api.ts +426 -0
  308. package/src/microsoft/calendar.ts +491 -0
  309. package/src/microsoft/drive.ts +541 -0
  310. package/src/microsoft/graph.ts +550 -0
  311. package/src/microsoft/mail.ts +833 -0
  312. package/src/microsoft/state.ts +822 -0
  313. package/src/microsoft.ts +982 -0
  314. package/src/node.ts +293 -0
  315. package/src/notion/api.ts +976 -0
  316. package/src/notion/state.ts +512 -0
  317. package/src/notion.ts +210 -0
  318. package/src/openai.ts +198 -0
  319. package/src/opencode-recordings.ts +257 -0
  320. package/src/opencode.ts +271 -0
  321. package/src/r2-fixtures.ts +295 -0
  322. package/src/r2-guard.ts +324 -0
  323. package/src/r2.ts +890 -0
  324. package/src/responses.ts +901 -0
  325. package/src/route-evidence.ts +90 -0
  326. package/src/router.ts +490 -0
  327. package/src/stateful-core.ts +70 -0
  328. package/src/stateful-emulator.ts +2620 -0
  329. package/src/stateful-secrets.ts +299 -0
  330. package/src/subscription-usage-recordings.ts +77 -0
  331. package/src/subscription-usage.ts +68 -0
  332. package/src/telegram/api.ts +437 -0
  333. package/src/telegram/state.ts +229 -0
  334. package/src/telegram.ts +227 -0
  335. package/src/todoist/api.ts +736 -0
  336. package/src/todoist/state.ts +456 -0
  337. package/src/todoist.ts +316 -0
  338. package/src/xai.ts +341 -0
@@ -0,0 +1,45 @@
1
+ import { StatefulRoute } from "../stateful-emulator.mjs";
2
+ import { McpRecordedFixture } from "./recordings.mjs";
3
+ import { McpEmulatorState, McpRuntime } from "./state.mjs";
4
+ import * as Schema from "effect/Schema";
5
+
6
+ //#region src/mcp/api.d.ts
7
+ /** Drill knobs (tests only): each makes the emulator disagree with exactly one case's claim. */
8
+ type McpEmulatorDrills = {
9
+ /** `mcp.negotiation.era` (modern): the discover answer also carries a JSON-RPC error. */readonly discoverCarriesErrorResponse?: boolean; /** `mcp.modern.stateless`: the modern discover result lacks `resultType`. */
10
+ readonly discoverWithoutResultType?: boolean; /** `mcp.legacy.session`: minted session ids hold a space (not visible ASCII). */
11
+ readonly sessionIdNotVisibleAscii?: boolean; /** `mcp.transport.response-encoding` (modern): the discover answer holds its response twice. */
12
+ readonly discoverAnsweredTwice?: boolean; /** `mcp.tools.list`: the listing marks the write tool `readOnlyHint: true`. */
13
+ readonly writeToolMarkedReadOnly?: boolean; /** `mcp.tools.call-read`: the read call answers `isError: true`. */
14
+ readonly readCallAnswersToolError?: boolean; /** `mcp.tools.call-tool-error`: invalid arguments answer a JSON-RPC error. */
15
+ readonly invalidCallAnswersRpcError?: boolean; /** `mcp.errors.unknown-tool`: the absent tool answers a tool result. */
16
+ readonly absentCallAnswersResult?: boolean; /** `mcp.auth.rejected`: the 401 carries no `WWW-Authenticate` challenge. */
17
+ readonly unauthorizedWithoutChallenge?: boolean;
18
+ };
19
+ declare const mcpEmulatorDrillKnobs: ReadonlyArray<keyof McpEmulatorDrills>;
20
+ type McpApiEnv = {
21
+ readonly drills: Readonly<Record<keyof McpEmulatorDrills, boolean>>; /** The digest of the reserved invalid credential on the synthetic origin. */
22
+ readonly reservedDigest: string; /** The digest of the reserved invalid Afloat credential's remainder on the Afloat origin. */
23
+ readonly afloatReservedDigest: string; /** Runtime data the commits advance (never in the state). */
24
+ readonly runtime: McpRuntime;
25
+ };
26
+ type McpRoute = StatefulRoute<McpEmulatorState, McpApiEnv>;
27
+ type RpcId = string | number;
28
+ /**
29
+ * Where a copied recording is not in the canonical form id substitution relies on: every JSON
30
+ * answer body, and every SSE `data:` payload, must equal `JSON.stringify(JSON.parse(text))`, and
31
+ * every SSE data line must start with `data: `. `makeMcpEmulator` throws on any problem, so a
32
+ * recording copied in another form (escapes such as `\u00e9` or `\/`, numbers such as `1.0`,
33
+ * spacing) fails loudly instead of answering bytes the substitution would rewrite.
34
+ */
35
+ declare const mcpRecordingProblems: (fixtures: ReadonlyArray<McpRecordedFixture>) => ReadonlyArray<string>;
36
+ /**
37
+ * A JSON-RPC request id of the emulated form: an integer from 0 to 2^53 - 1, or 1 to 64 printable
38
+ * ASCII characters.
39
+ */
40
+ declare const isMcpEmulatedRequestId: (id: Schema.Json | undefined) => id is RpcId;
41
+ /** The route table: one wire route per recorded HTTP shape, each with its manifest variants. */
42
+ declare const mcpApiRoutes: ReadonlyArray<McpRoute>;
43
+ //#endregion
44
+ export { McpApiEnv, McpEmulatorDrills, isMcpEmulatedRequestId, mcpApiRoutes, mcpEmulatorDrillKnobs, mcpRecordingProblems };
45
+ //# sourceMappingURL=api.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"api.d.mts","names":[],"sources":["../../src/mcp/api.ts"],"mappings":";;;;;;;KAyDY,iBAAA;EAAA,kGAED,4BAAA;WAEA,yBAAA,YAFA;EAAA,SAIA,wBAAA;EAAA,SAEA,qBAAA,YAEA;EAAA,SAAA,uBAAA,YAIA;EAAA,SAFA,wBAAA,YAMA;EAAA,SAJA,0BAAA,YAI4B;EAAA,SAF5B,uBAAA,YAeV;EAAA,SAbU,4BAAA;AAAA;AAAA,cAGE,qBAAA,EAAuB,aAAa,OAAO,iBAAA;AAAA,KAY5C,SAAA;EAAA,SACD,MAAA,EAAQ,QAAA,CAAS,MAAA,OAAa,iBAAA;WAE9B,cAAA,UAFiB;EAAA,SAIjB,oBAAA,UAES;EAAA,SAAT,OAAA,EAAS,UAAA;AAAA;AAAA,KAGf,QAAA,GAAW,aAAA,CAAc,gBAAA,EAAkB,SAAA;AAAA,KAE3C,KAAA;;;;;;;;cA6OQ,oBAAA,GACX,QAAA,EAAU,aAAA,CAAc,kBAAA,MACvB,aAAA;AAnPF;;;;AAAA,cAuUY,sBAAA,GAA0B,EAAA,EAAI,MAAA,CAAO,IAAA,iBAAmB,EAAA,IAAM,KAEf;;cAgqB/C,YAAA,EAAc,aAAa,CAAC,QAAA"}
@@ -0,0 +1,609 @@
1
+ import { isJsonObject, isNotEmulated, notEmulated, parseJsonText, streamedCommit } from "../stateful-emulator.mjs";
2
+ import { mcpEmulatorAfloatFixtures } from "./afloat-recordings.mjs";
3
+ import { mcpEmulatorFixtures } from "./recordings.mjs";
4
+ import { mcpEmulatorAfloatOrigin, mcpEmulatorAfloatPath, mcpEmulatorLegacyPath, mcpEmulatorModernPath, mcpEmulatorOrigin, mcpEmulatorSessionPrefix } from "./state.mjs";
5
+ import { Predicate } from "effect";
6
+ //#region src/mcp/api.ts
7
+ /**
8
+ * MCP emulator route table (internal): one wire route per recorded HTTP shape (`POST /modern/mcp`,
9
+ * `POST /legacy/mcp`, `GET /legacy/mcp` on the synthetic origin, and `POST /mcp` on the Afloat
10
+ * origin), each answering only the JSON-RPC methods its profile's fixtures record, as manifest
11
+ * variants (`RPC <origin><path>#<method>`, plus the `GET` row).
12
+ *
13
+ * A request is admitted only when it equals a recorded request (`src/mcp/recordings.ts`) within
14
+ * the latitude: the MCP headers (`accept`, `content-type`, `mcp-method`, `mcp-protocol-version`,
15
+ * `mcp-name`, `last-event-id`) equal the recorded values or are absent where the recording has
16
+ * none; `mcp-session-id` is present exactly where recorded; the body has exactly the recorded
17
+ * keys; the id is any JSON-RPC id of the accepted form where one is recorded; and `params` equal
18
+ * the recorded ones except for JSON key order and the `_meta` client info's name and version
19
+ * (and, on the second page of the paged listing, the cursor, checked by issuance). Everything
20
+ * else is refused with a constant reason. The answer is the recorded one with only the request id
21
+ * substituted where the recording carries the recorded request id (the top-level `id` of a JSON
22
+ * answer, or of the SSE response event's payload), the session id (minted, or the request's) in
23
+ * the `mcp-session-id` header, and the cursor of the current generation in `nextCursor`.
24
+ *
25
+ * @experimental
26
+ */
27
+ const mcpEmulatorDrillKnobs = [
28
+ "discoverCarriesErrorResponse",
29
+ "discoverWithoutResultType",
30
+ "sessionIdNotVisibleAscii",
31
+ "discoverAnsweredTwice",
32
+ "writeToolMarkedReadOnly",
33
+ "readCallAnswersToolError",
34
+ "invalidCallAnswersRpcError",
35
+ "absentCallAnswersResult",
36
+ "unauthorizedWithoutChallenge"
37
+ ];
38
+ const fieldOf = (value, key) => isJsonObject(value) ? value[key] : void 0;
39
+ /** Every copied fixture: the synthetic profiles', then the Afloat profile's. */
40
+ const allFixtures = [...mcpEmulatorFixtures, ...mcpEmulatorAfloatFixtures];
41
+ const exchangeOf = (fixtureId, index) => {
42
+ const exchange = allFixtures.find((fixture) => fixture.id === fixtureId)?.exchanges.at(index);
43
+ if (exchange === void 0) throw new Error(`the MCP recordings have no exchange ${index} in ${fixtureId}`);
44
+ return exchange;
45
+ };
46
+ const modern = (caseId) => `${caseId}.modern.synthetic`;
47
+ const legacy = (caseId) => `${caseId}.legacy.synthetic`;
48
+ const afloat = (caseId) => `${caseId}.afloat.synthetic`;
49
+ const recorded = {
50
+ modernDiscover: exchangeOf(modern("mcp.negotiation.era"), 0),
51
+ modernRejected: exchangeOf(modern("mcp.auth.rejected"), 0),
52
+ modernList: exchangeOf(modern("mcp.negotiation.era"), 1),
53
+ modernFirstPage: exchangeOf(modern("mcp.tools.list"), 1),
54
+ modernSecondPage: exchangeOf(modern("mcp.tools.list"), 2),
55
+ modernCallRead: exchangeOf(modern("mcp.tools.call-read"), -1),
56
+ modernCallToolError: exchangeOf(modern("mcp.tools.call-tool-error"), -1),
57
+ modernCallAbsent: exchangeOf(modern("mcp.errors.unknown-tool"), -1),
58
+ legacyDiscover: exchangeOf(legacy("mcp.negotiation.era"), 0),
59
+ legacyRejected: exchangeOf(legacy("mcp.auth.rejected"), 0),
60
+ legacyInitialize: exchangeOf(legacy("mcp.negotiation.era"), 1),
61
+ legacyInitialized: exchangeOf(legacy("mcp.negotiation.era"), 2),
62
+ legacyGet: exchangeOf(legacy("mcp.negotiation.era"), 3),
63
+ legacyList: exchangeOf(legacy("mcp.negotiation.era"), 4),
64
+ legacyCallRead: exchangeOf(legacy("mcp.tools.call-read"), -1),
65
+ legacyCallToolError: exchangeOf(legacy("mcp.tools.call-tool-error"), -1),
66
+ legacyCallAbsent: exchangeOf(legacy("mcp.errors.unknown-tool"), -1),
67
+ afloatDiscover: exchangeOf(afloat("mcp.negotiation.era"), 0),
68
+ afloatRejected: exchangeOf(afloat("mcp.auth.rejected"), 0),
69
+ afloatList: exchangeOf(afloat("mcp.negotiation.era"), 1),
70
+ afloatCallRead: exchangeOf(afloat("mcp.tools.call-read"), -1),
71
+ afloatCallToolError: exchangeOf(afloat("mcp.tools.call-tool-error"), -1),
72
+ afloatCallAbsent: exchangeOf(afloat("mcp.errors.unknown-tool"), -1)
73
+ };
74
+ /** The cursor the paged listing records between its two pages. */
75
+ const recordedCursor = (() => {
76
+ const cursor = fieldOf(fieldOf(recorded.modernSecondPage.request.body, "params"), "cursor");
77
+ if (!Predicate.isString(cursor)) throw new Error("the paged MCP listing records no cursor");
78
+ return cursor;
79
+ })();
80
+ const casesSending = (origin, path, sends) => [...new Set(allFixtures.flatMap((fixture) => fixture.endpoint === `${origin}${path}` && fixture.exchanges.some((exchange) => sends(exchange.request)) ? [fixture.caseId] : []))];
81
+ const rpcMethodOf = (request) => {
82
+ const method = fieldOf(request.body, "method");
83
+ return Predicate.isString(method) ? method : void 0;
84
+ };
85
+ const rpcRow = (origin, path, method) => ({
86
+ method: "RPC",
87
+ path: `${origin}${path}#${method}`,
88
+ kind: "connector",
89
+ write: false,
90
+ caseIds: casesSending(origin, path, (request) => request.method === "POST" && rpcMethodOf(request) === method),
91
+ evidence: "unverified"
92
+ });
93
+ const modernMethods = [
94
+ "server/discover",
95
+ "tools/list",
96
+ "tools/call"
97
+ ];
98
+ const legacyMethods = [
99
+ "server/discover",
100
+ "initialize",
101
+ "notifications/initialized",
102
+ "tools/list",
103
+ "tools/call"
104
+ ];
105
+ const modernRows = modernMethods.map((method) => rpcRow(mcpEmulatorOrigin, mcpEmulatorModernPath, method));
106
+ const legacyRows = legacyMethods.map((method) => rpcRow(mcpEmulatorOrigin, mcpEmulatorLegacyPath, method));
107
+ /** The Afloat profile records the modern methods only (no paging, no session). */
108
+ const afloatRows = modernMethods.map((method) => rpcRow(mcpEmulatorAfloatOrigin, mcpEmulatorAfloatPath, method));
109
+ const getRow = {
110
+ method: "GET",
111
+ path: `${mcpEmulatorOrigin}${mcpEmulatorLegacyPath}`,
112
+ kind: "connector",
113
+ write: false,
114
+ caseIds: casesSending(mcpEmulatorOrigin, mcpEmulatorLegacyPath, (request) => request.method === "GET"),
115
+ evidence: "unverified"
116
+ };
117
+ const rowOf = (path, method, origin = mcpEmulatorOrigin) => `${origin}${path}#${method}`;
118
+ const isEventStream = (headers) => headers["content-type"] === "text/event-stream";
119
+ /** A recorded answer as status, headers, and chunks (a JSON body is one chunk; none is none). */
120
+ const answerOf = (response) => ({
121
+ status: response.status,
122
+ headers: response.headers,
123
+ chunks: "chunks" in response ? response.chunks : response.body === "" ? [] : [response.body]
124
+ });
125
+ const isResponseTo = (message, id) => isJsonObject(message) && message["id"] === id && ("result" in message || "error" in message);
126
+ const dataPrefix = "data: ";
127
+ /**
128
+ * `answer` with the JSON-RPC response to `recordedId` (the top-level message of a JSON answer, or
129
+ * the payload of the SSE event that carries it) replaced by `change` of it; every other chunk,
130
+ * line, and event (notifications, `id:` lines) stays byte for byte.
131
+ */
132
+ const mapResponse = (answer, recordedId, change) => {
133
+ const eventStream = isEventStream(answer.headers);
134
+ const chunks = answer.chunks.map((chunk) => {
135
+ if (!eventStream) {
136
+ const message = parseJsonText(chunk);
137
+ return isResponseTo(message, recordedId) ? JSON.stringify(change(message)) : chunk;
138
+ }
139
+ return chunk.split("\n").map((line) => {
140
+ if (!line.startsWith(dataPrefix)) return line;
141
+ const message = parseJsonText(line.slice(6));
142
+ return isResponseTo(message, recordedId) ? `${dataPrefix}${JSON.stringify(change(message))}` : line;
143
+ }).join("\n");
144
+ });
145
+ return {
146
+ ...answer,
147
+ chunks
148
+ };
149
+ };
150
+ const recordedIdOf = (exchange) => {
151
+ const id = fieldOf(exchange.request.body, "id");
152
+ return Predicate.isString(id) || Predicate.isNumber(id) ? id : void 0;
153
+ };
154
+ /** The recorded answer of `exchange` with the request's id where it carries the recorded id. */
155
+ const answerWithId = (exchange, id) => {
156
+ const answer = answerOf(exchange.response);
157
+ const recordedId = recordedIdOf(exchange);
158
+ if (recordedId === void 0 || id === void 0 || id === recordedId) return answer;
159
+ return mapResponse(answer, recordedId, (message) => ({
160
+ ...message,
161
+ id
162
+ }));
163
+ };
164
+ /** `answer` with its `mcp-session-id` header (recorded on SSE answers) set to `session`. */
165
+ const withSession = (answer, session) => answer.headers["mcp-session-id"] === void 0 ? answer : {
166
+ ...answer,
167
+ headers: {
168
+ ...answer.headers,
169
+ "mcp-session-id": session
170
+ }
171
+ };
172
+ /** The response message of an answer to `id` with `change` applied to its `result`. */
173
+ const mapResult = (change) => (message) => {
174
+ const result = message["result"];
175
+ return isJsonObject(result) ? {
176
+ ...message,
177
+ result: change(result)
178
+ } : message;
179
+ };
180
+ /**
181
+ * Where a copied recording is not in the canonical form id substitution relies on: every JSON
182
+ * answer body, and every SSE `data:` payload, must equal `JSON.stringify(JSON.parse(text))`, and
183
+ * every SSE data line must start with `data: `. `makeMcpEmulator` throws on any problem, so a
184
+ * recording copied in another form (escapes such as `\u00e9` or `\/`, numbers such as `1.0`,
185
+ * spacing) fails loudly instead of answering bytes the substitution would rewrite.
186
+ */
187
+ const mcpRecordingProblems = (fixtures) => fixtures.flatMap((fixture) => fixture.exchanges.flatMap(({ response }, index) => {
188
+ const where = `${fixture.id} exchange ${index}`;
189
+ const canonical = (text) => {
190
+ const parsed = parseJsonText(text);
191
+ return parsed !== void 0 && JSON.stringify(parsed) === text;
192
+ };
193
+ if (!("chunks" in response)) return response.body === "" || canonical(response.body) ? [] : [`${where}: the JSON body is not canonical`];
194
+ return response.chunks.flatMap((chunk, at) => chunk.split("\n").flatMap((line) => !line.startsWith("data") || line.startsWith(dataPrefix) && canonical(line.slice(6)) ? [] : [`${where} chunk ${at}: an SSE data line is not canonical`]));
195
+ }));
196
+ /** The MCP headers compared with the recording (absent where the recording has none). */
197
+ const comparedHeaders = [
198
+ "accept",
199
+ "content-type",
200
+ "last-event-id",
201
+ "mcp-method",
202
+ "mcp-name",
203
+ "mcp-protocol-version"
204
+ ];
205
+ const sessionHeader = "mcp-session-id";
206
+ /** The `mcp-*` headers a recording carries; any other (such as `mcp-param-*`) is refused. */
207
+ const recordedMcpHeaders = new Set([
208
+ "mcp-method",
209
+ "mcp-name",
210
+ "mcp-protocol-version",
211
+ sessionHeader
212
+ ]);
213
+ /** Refuse an `mcp-*` header no recording carries (fail closed when the names are unknown). */
214
+ const unrecordedMcpHeader = (request) => request.headerNames === void 0 || request.headerNames.some((name) => name.startsWith("mcp-") && !recordedMcpHeaders.has(name)) ? notEmulated("an mcp-* header no recording carries is not emulated") : void 0;
215
+ const headerProblem = (request, recordedRequest, names) => {
216
+ for (const name of names) if (request.header(name) !== recordedRequest.headers[name]) return notEmulated(`the ${name} header must be the recorded value, or absent where none is`);
217
+ if (request.header(sessionHeader) === void 0 !== (recordedRequest.headers[sessionHeader] === void 0)) return notEmulated("mcp-session-id must be sent exactly where the recording sends one");
218
+ };
219
+ /**
220
+ * A JSON-RPC request id of the emulated form: an integer from 0 to 2^53 - 1, or 1 to 64 printable
221
+ * ASCII characters.
222
+ */
223
+ const isMcpEmulatedRequestId = (id) => Predicate.isNumber(id) && Number.isSafeInteger(id) && id >= 0 || Predicate.isString(id) && /^[\x20-\x7E]{1,64}$/.test(id);
224
+ /** Structural JSON equality, ignoring object key order. */
225
+ const jsonEqual = (left, right) => {
226
+ if (Array.isArray(left)) return Array.isArray(right) && left.length === right.length && left.every((item, index) => {
227
+ const other = right[index];
228
+ return other !== void 0 && jsonEqual(item, other);
229
+ });
230
+ if (isJsonObject(left)) {
231
+ if (!isJsonObject(right)) return false;
232
+ const keys = Object.keys(left);
233
+ return keys.length === Object.keys(right).length && keys.every((key) => {
234
+ const value = left[key];
235
+ const other = right[key];
236
+ return Object.hasOwn(right, key) && value !== void 0 && other !== void 0 && jsonEqual(value, other);
237
+ });
238
+ }
239
+ return left === right;
240
+ };
241
+ const clientInfoKey = "io.modelcontextprotocol/clientInfo";
242
+ const isClientInfo = (value) => isJsonObject(value) && Object.keys(value).length === 2 && Predicate.isString(value["name"]) && value["name"].length > 0 && Predicate.isString(value["version"]) && value["version"].length > 0;
243
+ /**
244
+ * The request params normalised for comparison with `recordedParams`: the `_meta` client info
245
+ * replaced by the recorded one (when it is any non-empty name and version), and with `cursorFree`
246
+ * the cursor replaced by the recorded one (when it is a string; checked by issuance at plan).
247
+ */
248
+ const normalisedParams = (params, recordedParams, cursorFree) => {
249
+ if (!isJsonObject(params)) return notEmulated("params must be the recorded JSON object");
250
+ const meta = params["_meta"];
251
+ const recordedMeta = recordedParams["_meta"];
252
+ let normalised = params;
253
+ if (isJsonObject(recordedMeta) && recordedMeta[clientInfoKey] !== void 0) {
254
+ if (!isJsonObject(meta) || !isClientInfo(meta[clientInfoKey])) return notEmulated("the _meta client info must be a non-empty name and version only");
255
+ normalised = {
256
+ ...normalised,
257
+ _meta: {
258
+ ...meta,
259
+ [clientInfoKey]: recordedMeta[clientInfoKey]
260
+ }
261
+ };
262
+ }
263
+ if (cursorFree) {
264
+ if (!Predicate.isString(params["cursor"])) return notEmulated("the second page must send the cursor the first page issued");
265
+ normalised = {
266
+ ...normalised,
267
+ cursor: recordedCursor
268
+ };
269
+ }
270
+ return normalised;
271
+ };
272
+ /**
273
+ * The first candidate whose recorded request the JSON-RPC POST equals within the latitude, or
274
+ * not emulated. Every candidate records the same method; candidates differ only in their params
275
+ * and `mcp-name` (the recorded tool calls).
276
+ */
277
+ const matchRpc = (request, candidates, options = {}) => {
278
+ const [first] = candidates;
279
+ if (first === void 0) return notEmulated("this JSON-RPC method is not emulated here");
280
+ const firstRequest = recorded[first].request;
281
+ const body = request.json;
282
+ const recordedBody = firstRequest.body;
283
+ if (!isJsonObject(recordedBody)) return notEmulated("this JSON-RPC method is not emulated here");
284
+ const common = unrecordedMcpHeader(request) ?? headerProblem(request, firstRequest, comparedHeaders.filter((name) => name !== "mcp-name"));
285
+ if (common !== void 0) return common;
286
+ if (!isJsonObject(body)) return notEmulated("JSON-RPC batches and non-object bodies are not emulated");
287
+ const keys = Object.keys(body);
288
+ const recordedKeys = Object.keys(recordedBody);
289
+ if (keys.length !== recordedKeys.length || keys.some((key) => !recordedKeys.includes(key))) return notEmulated("the JSON-RPC message must have exactly the recorded members");
290
+ if (body["jsonrpc"] !== "2.0") return notEmulated("jsonrpc must be \"2.0\"");
291
+ const id = body["id"];
292
+ if ("id" in recordedBody && !isMcpEmulatedRequestId(id)) return notEmulated("the request id must be an integer from 0 to 2^53 - 1 or 1 to 64 printable ASCII characters");
293
+ const cursor = options.cursorFree === true ? fieldOf(body["params"], "cursor") : void 0;
294
+ for (const key of candidates) {
295
+ const candidate = recorded[key].request;
296
+ const candidateBody = candidate.body;
297
+ if (!isJsonObject(candidateBody)) continue;
298
+ if (headerProblem(request, candidate, ["mcp-name"]) !== void 0) continue;
299
+ const recordedParams = candidateBody["params"];
300
+ if (recordedParams !== void 0) {
301
+ const params = isJsonObject(recordedParams) ? normalisedParams(body["params"], recordedParams, options.cursorFree === true) : notEmulated("params must be the recorded value");
302
+ if (isNotEmulated(params)) return params;
303
+ if (!jsonEqual(params, recordedParams)) continue;
304
+ }
305
+ return {
306
+ key,
307
+ id: isMcpEmulatedRequestId(id) ? id : void 0,
308
+ cursor: Predicate.isString(cursor) ? cursor : void 0
309
+ };
310
+ }
311
+ return notEmulated("params other than the recorded ones are not emulated");
312
+ };
313
+ /** Refuse any query parameter (no fixture records one). */
314
+ const noQuery = (request) => request.rawQuery === void 0 || request.rawQuery === "" ? void 0 : notEmulated("query parameters are not emulated");
315
+ const isReserved = (request, env) => request.bearerDigest === env.reservedDigest;
316
+ const isAfloatReserved = (request, env) => request.bearerDigest === env.afloatReservedDigest;
317
+ const reservedOnlyOnProbe = "the reserved invalid credential is answered only on the era probe";
318
+ const admission = (variant, admitted, plan) => ({
319
+ variant,
320
+ plan: (state, context) => plan(state, admitted, context.env)
321
+ });
322
+ /** A commit that answers `answer` and writes nothing. */
323
+ const answerOnly = (answer) => streamedCommit(answer);
324
+ const sessionOf = (state, admitted, phase) => state.sessions.find((session) => session.id === admitted.session && session.phase === phase);
325
+ const unknownSession = (phase) => notEmulated(phase === "ready" ? "a request on no ready session of this emulator is not emulated" : "notifications/initialized on no initializing session of this emulator is not emulated");
326
+ /** The JSON-RPC error the era drill adds to the discover answer (another id: the SDK skips it). */
327
+ const drillErrorResponse = JSON.stringify({
328
+ jsonrpc: "2.0",
329
+ id: "yolk-emu-drill",
330
+ error: {
331
+ code: -32603,
332
+ message: "Synthetic drill error."
333
+ }
334
+ });
335
+ /** The listed tools that write, which the `writeToolMarkedReadOnly` drill marks read-only. */
336
+ const writeToolNames = new Set(["create_synthetic_note", "create-receipt-upload"]);
337
+ /** Drill transforms of an answer, by the recorded exchange it answers. */
338
+ const drilled = (key, answer, id, env) => {
339
+ const drills = env.drills;
340
+ switch (key) {
341
+ case "modernDiscover":
342
+ case "afloatDiscover": {
343
+ let next = answer;
344
+ if (drills.discoverWithoutResultType) next = mapResponse(next, id, mapResult(({ resultType: _resultType, ...result }) => result));
345
+ if (drills.discoverCarriesErrorResponse) next = {
346
+ ...next,
347
+ chunks: next.chunks.map((chunk) => `[${drillErrorResponse},${chunk}]`)
348
+ };
349
+ if (drills.discoverAnsweredTwice) next = {
350
+ ...next,
351
+ headers: {
352
+ ...next.headers,
353
+ "content-type": "text/event-stream"
354
+ },
355
+ chunks: next.chunks.flatMap((chunk) => [`event: message\ndata: ${chunk}\n\n`, `event: message\ndata: ${chunk}\n\n`])
356
+ };
357
+ return next;
358
+ }
359
+ case "modernList":
360
+ case "modernFirstPage":
361
+ case "modernSecondPage":
362
+ case "legacyList":
363
+ case "afloatList": return drills.writeToolMarkedReadOnly ? mapResponse(answer, id, mapResult((result) => {
364
+ const tools = result["tools"];
365
+ return Array.isArray(tools) ? {
366
+ ...result,
367
+ tools: tools.map((tool) => isJsonObject(tool) && writeToolNames.has(String(tool["name"])) ? {
368
+ ...tool,
369
+ annotations: { readOnlyHint: true }
370
+ } : tool)
371
+ } : result;
372
+ })) : answer;
373
+ case "modernCallRead":
374
+ case "legacyCallRead":
375
+ case "afloatCallRead": return drills.readCallAnswersToolError ? mapResponse(answer, id, mapResult((result) => ({
376
+ ...result,
377
+ isError: true
378
+ }))) : answer;
379
+ case "modernCallToolError":
380
+ case "legacyCallToolError":
381
+ case "afloatCallToolError": return drills.invalidCallAnswersRpcError ? mapResponse(answer, id, () => ({
382
+ jsonrpc: "2.0",
383
+ id,
384
+ error: {
385
+ code: -32602,
386
+ message: "Synthetic drill: invalid arguments."
387
+ }
388
+ })) : answer;
389
+ case "modernCallAbsent":
390
+ case "legacyCallAbsent":
391
+ case "afloatCallAbsent": {
392
+ if (!drills.absentCallAnswersResult) return answer;
393
+ const result = {
394
+ content: [{
395
+ type: "text",
396
+ text: "Synthetic drill result."
397
+ }],
398
+ isError: true
399
+ };
400
+ return mapResponse({
401
+ ...answer,
402
+ status: 200
403
+ }, id, () => ({
404
+ jsonrpc: "2.0",
405
+ id,
406
+ result: key === "legacyCallAbsent" ? result : {
407
+ ...result,
408
+ resultType: "complete"
409
+ }
410
+ }));
411
+ }
412
+ case "modernRejected":
413
+ case "legacyRejected":
414
+ case "afloatRejected": {
415
+ if (!drills.unauthorizedWithoutChallenge) return answer;
416
+ const { "www-authenticate": _challenge, ...headers } = answer.headers;
417
+ return {
418
+ ...answer,
419
+ headers
420
+ };
421
+ }
422
+ default: return answer;
423
+ }
424
+ };
425
+ /** The id the answer's response carries: the request's (substituted), else the recorded one. */
426
+ const responseIdOf = (key, id) => id ?? recordedIdOf(recorded[key]);
427
+ /** The recorded answer for `key`, with the request id substituted and any drill applied. */
428
+ const recordedAnswer = (key, id, env) => {
429
+ const answer = answerWithId(recorded[key], id);
430
+ const responseId = responseIdOf(key, id);
431
+ return responseId === void 0 ? answer : drilled(key, answer, responseId, env);
432
+ };
433
+ /** The cursor value issued in the current generation (the recorded one in its first). */
434
+ const cursorFor = (runtime) => (runtime.cursorFirstGeneration ?? runtime.generation) === runtime.generation ? recordedCursor : `${recordedCursor}.g${runtime.generation}`;
435
+ const planModernList = (state, admitted, env) => {
436
+ if (state.modernListing === "one-page") return answerOnly(recordedAnswer("modernList", admitted.id, env));
437
+ const cursor = cursorFor(env.runtime);
438
+ const recordedPage = recordedAnswer("modernFirstPage", admitted.id, env);
439
+ const responseId = responseIdOf("modernFirstPage", admitted.id);
440
+ return streamedCommit(cursor === recordedCursor || responseId === void 0 ? recordedPage : mapResponse(recordedPage, responseId, mapResult((result) => ({
441
+ ...result,
442
+ nextCursor: cursor
443
+ }))), () => {
444
+ env.runtime.cursorFirstGeneration ??= env.runtime.generation;
445
+ env.runtime.issuedCursor = cursor;
446
+ }, [cursor]);
447
+ };
448
+ const planModernSecondPage = (state, admitted, env) => state.modernListing === "two-pages" && env.runtime.issuedCursor !== void 0 && admitted.cursor === env.runtime.issuedCursor ? answerOnly(recordedAnswer("modernSecondPage", admitted.id, env)) : notEmulated("a cursor this emulator did not issue since the last reset or seed is not emulated");
449
+ const planInitialize = (state, admitted, env) => {
450
+ const number = env.runtime.nextSession;
451
+ if (state.sessions.length >= 256 || !Number.isSafeInteger(number + 1)) return notEmulated("the emulator holds as many sessions as it takes");
452
+ const session = env.drills.sessionIdNotVisibleAscii ? `yolk-emu-session ${number}` : `${mcpEmulatorSessionPrefix}${number}`;
453
+ return streamedCommit(withSession(recordedAnswer("legacyInitialize", admitted.id, env), session), () => {
454
+ env.runtime.nextSession = number + 1;
455
+ state.sessions = [...state.sessions, {
456
+ id: session,
457
+ phase: "initializing"
458
+ }];
459
+ }, [session]);
460
+ };
461
+ const planInitialized = (state, admitted, env) => {
462
+ const session = sessionOf(state, admitted, "initializing");
463
+ if (session === void 0) return unknownSession("initializing");
464
+ return streamedCommit(recordedAnswer("legacyInitialized", void 0, env), () => {
465
+ state.sessions = state.sessions.map((held) => held.id === session.id ? {
466
+ id: held.id,
467
+ phase: "ready"
468
+ } : held);
469
+ });
470
+ };
471
+ /** A legacy answer on a ready session: the recorded answer with the session's id. */
472
+ const planOnSession = (key) => (state, admitted, env) => {
473
+ const session = sessionOf(state, admitted, "ready");
474
+ return session === void 0 ? unknownSession("ready") : answerOnly(withSession(recordedAnswer(key, admitted.id, env), session.id));
475
+ };
476
+ const planAnswer = (key) => (_state, admitted, env) => answerOnly(recordedAnswer(key, admitted.id, env));
477
+ const routeEvidenceOf = (method, path, variants, origin = mcpEmulatorOrigin) => ({
478
+ method,
479
+ path,
480
+ kind: "connector",
481
+ write: false,
482
+ caseIds: [...new Set(variants.flatMap((variant) => variant.caseIds))],
483
+ evidence: "unverified",
484
+ origin,
485
+ variants
486
+ });
487
+ const admitted = (matched, request) => ({
488
+ ...matched,
489
+ session: request.header(sessionHeader)
490
+ });
491
+ /** The route table: one wire route per recorded HTTP shape, each with its manifest variants. */
492
+ const mcpApiRoutes = [
493
+ {
494
+ ...routeEvidenceOf("POST", mcpEmulatorModernPath, modernRows),
495
+ body: "json",
496
+ admit: (request, env) => {
497
+ const query = noQuery(request);
498
+ if (query !== void 0) return query;
499
+ const method = fieldOf(request.json, "method");
500
+ const variant = (name) => rowOf(mcpEmulatorModernPath, name);
501
+ if (method === "server/discover") {
502
+ const matched = matchRpc(request, ["modernDiscover"]);
503
+ if (isNotEmulated(matched)) return matched;
504
+ return isReserved(request, env) ? admission(variant(method), admitted(matched, request), planAnswer("modernRejected")) : admission(variant(method), admitted(matched, request), planAnswer("modernDiscover"));
505
+ }
506
+ if (isReserved(request, env)) return notEmulated(reservedOnlyOnProbe);
507
+ if (method === "tools/list") {
508
+ const paged = fieldOf(fieldOf(request.json, "params"), "cursor") !== void 0;
509
+ const matched = paged ? matchRpc(request, ["modernSecondPage"], { cursorFree: true }) : matchRpc(request, ["modernList"]);
510
+ if (isNotEmulated(matched)) return matched;
511
+ return admission(variant(method), admitted(matched, request), paged ? planModernSecondPage : planModernList);
512
+ }
513
+ if (method === "tools/call") {
514
+ const matched = matchRpc(request, [
515
+ "modernCallRead",
516
+ "modernCallToolError",
517
+ "modernCallAbsent"
518
+ ]);
519
+ if (isNotEmulated(matched)) return matched;
520
+ return admission(variant(method), admitted(matched, request), planAnswer(matched.key));
521
+ }
522
+ return notEmulated("this JSON-RPC method is not emulated on the modern profile");
523
+ }
524
+ },
525
+ {
526
+ ...routeEvidenceOf("POST", mcpEmulatorLegacyPath, legacyRows),
527
+ body: "json",
528
+ admit: (request, env) => {
529
+ const query = noQuery(request);
530
+ if (query !== void 0) return query;
531
+ const method = fieldOf(request.json, "method");
532
+ const variant = (name) => rowOf(mcpEmulatorLegacyPath, name);
533
+ if (method === "server/discover") {
534
+ const matched = matchRpc(request, ["legacyDiscover"]);
535
+ if (isNotEmulated(matched)) return matched;
536
+ return isReserved(request, env) ? admission(variant(method), admitted(matched, request), planAnswer("legacyRejected")) : admission(variant(method), admitted(matched, request), planAnswer("legacyDiscover"));
537
+ }
538
+ if (isReserved(request, env)) return notEmulated(reservedOnlyOnProbe);
539
+ const single = (key, name, plan) => {
540
+ const matched = matchRpc(request, [key]);
541
+ return isNotEmulated(matched) ? matched : admission(variant(name), admitted(matched, request), plan);
542
+ };
543
+ if (method === "initialize") return single("legacyInitialize", method, planInitialize);
544
+ if (method === "notifications/initialized") return single("legacyInitialized", method, planInitialized);
545
+ if (method === "tools/list") return single("legacyList", method, planOnSession("legacyList"));
546
+ if (method === "tools/call") {
547
+ const matched = matchRpc(request, [
548
+ "legacyCallRead",
549
+ "legacyCallToolError",
550
+ "legacyCallAbsent"
551
+ ]);
552
+ if (isNotEmulated(matched)) return matched;
553
+ return admission(variant(method), admitted(matched, request), planOnSession(matched.key));
554
+ }
555
+ return notEmulated("this JSON-RPC method is not emulated on the legacy profile");
556
+ }
557
+ },
558
+ {
559
+ ...routeEvidenceOf("GET", mcpEmulatorLegacyPath, [getRow]),
560
+ body: "none",
561
+ admit: (request, env) => {
562
+ const query = noQuery(request);
563
+ if (query !== void 0) return query;
564
+ if (isReserved(request, env)) return notEmulated(reservedOnlyOnProbe);
565
+ const problem = unrecordedMcpHeader(request) ?? headerProblem(request, recorded.legacyGet.request, comparedHeaders);
566
+ if (problem !== void 0) return problem;
567
+ return admission(getRow.path, {
568
+ key: "legacyGet",
569
+ id: void 0,
570
+ cursor: void 0,
571
+ session: request.header(sessionHeader)
572
+ }, planOnSession("legacyGet"));
573
+ }
574
+ },
575
+ {
576
+ ...routeEvidenceOf("POST", mcpEmulatorAfloatPath, afloatRows, mcpEmulatorAfloatOrigin),
577
+ body: "json",
578
+ admit: (request, env) => {
579
+ const query = noQuery(request);
580
+ if (query !== void 0) return query;
581
+ const method = fieldOf(request.json, "method");
582
+ const variant = (name) => rowOf(mcpEmulatorAfloatPath, name, mcpEmulatorAfloatOrigin);
583
+ if (method === "server/discover") {
584
+ const matched = matchRpc(request, ["afloatDiscover"]);
585
+ if (isNotEmulated(matched)) return matched;
586
+ return isAfloatReserved(request, env) ? admission(variant(method), admitted(matched, request), planAnswer("afloatRejected")) : admission(variant(method), admitted(matched, request), planAnswer("afloatDiscover"));
587
+ }
588
+ if (isAfloatReserved(request, env)) return notEmulated(reservedOnlyOnProbe);
589
+ if (method === "tools/list") {
590
+ const matched = matchRpc(request, ["afloatList"]);
591
+ return isNotEmulated(matched) ? matched : admission(variant(method), admitted(matched, request), planAnswer("afloatList"));
592
+ }
593
+ if (method === "tools/call") {
594
+ const matched = matchRpc(request, [
595
+ "afloatCallRead",
596
+ "afloatCallToolError",
597
+ "afloatCallAbsent"
598
+ ]);
599
+ if (isNotEmulated(matched)) return matched;
600
+ return admission(variant(method), admitted(matched, request), planAnswer(matched.key));
601
+ }
602
+ return notEmulated("this JSON-RPC method is not emulated on the Afloat profile");
603
+ }
604
+ }
605
+ ];
606
+ //#endregion
607
+ export { isMcpEmulatedRequestId, mcpApiRoutes, mcpEmulatorDrillKnobs, mcpRecordingProblems };
608
+
609
+ //# sourceMappingURL=api.mjs.map