skybridge 0.0.0-dev.fe23f20 → 0.0.0-dev.fe35f75

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 (293) hide show
  1. package/README.md +153 -0
  2. package/bin/run.js +0 -4
  3. package/dist/cli/detect-port.d.ts +18 -0
  4. package/dist/cli/detect-port.js +61 -0
  5. package/dist/cli/detect-port.js.map +1 -0
  6. package/dist/cli/header.js +1 -1
  7. package/dist/cli/header.js.map +1 -1
  8. package/dist/cli/run-command.js.map +1 -1
  9. package/dist/cli/telemetry.d.ts +7 -0
  10. package/dist/cli/telemetry.js +123 -0
  11. package/dist/cli/telemetry.js.map +1 -0
  12. package/dist/cli/tunnel-control-server.d.ts +9 -0
  13. package/dist/cli/tunnel-control-server.js +31 -0
  14. package/dist/cli/tunnel-control-server.js.map +1 -0
  15. package/dist/cli/tunnel-control-server.test.js +39 -0
  16. package/dist/cli/tunnel-control-server.test.js.map +1 -0
  17. package/dist/cli/tunnel-handler.d.ts +3 -0
  18. package/dist/cli/tunnel-handler.js +48 -0
  19. package/dist/cli/tunnel-handler.js.map +1 -0
  20. package/dist/cli/tunnel-handler.test.js +105 -0
  21. package/dist/cli/tunnel-handler.test.js.map +1 -0
  22. package/dist/cli/tunnel.d.ts +57 -0
  23. package/dist/cli/tunnel.js +154 -0
  24. package/dist/cli/tunnel.js.map +1 -0
  25. package/dist/cli/tunnel.test.d.ts +1 -0
  26. package/dist/cli/tunnel.test.js +190 -0
  27. package/dist/cli/tunnel.test.js.map +1 -0
  28. package/dist/cli/types.d.ts +5 -0
  29. package/dist/cli/types.js +2 -0
  30. package/dist/cli/types.js.map +1 -0
  31. package/dist/cli/use-execute-steps.d.ts +3 -2
  32. package/dist/cli/use-execute-steps.js +6 -1
  33. package/dist/cli/use-execute-steps.js.map +1 -1
  34. package/dist/cli/use-messages.d.ts +3 -0
  35. package/dist/cli/use-messages.js +11 -0
  36. package/dist/cli/use-messages.js.map +1 -0
  37. package/dist/cli/use-nodemon.d.ts +2 -0
  38. package/dist/cli/use-nodemon.js +73 -0
  39. package/dist/cli/use-nodemon.js.map +1 -0
  40. package/dist/cli/use-open-browser.d.ts +1 -0
  41. package/dist/cli/use-open-browser.js +44 -0
  42. package/dist/cli/use-open-browser.js.map +1 -0
  43. package/dist/cli/use-tunnel.d.ts +14 -0
  44. package/dist/cli/use-tunnel.js +131 -0
  45. package/dist/cli/use-tunnel.js.map +1 -0
  46. package/dist/cli/use-typescript-check.d.ts +9 -0
  47. package/dist/cli/use-typescript-check.js +94 -0
  48. package/dist/cli/use-typescript-check.js.map +1 -0
  49. package/dist/commands/build.js +64 -6
  50. package/dist/commands/build.js.map +1 -1
  51. package/dist/commands/dev.d.ts +6 -1
  52. package/dist/commands/dev.js +69 -9
  53. package/dist/commands/dev.js.map +1 -1
  54. package/dist/commands/start.d.ts +3 -1
  55. package/dist/commands/start.js +31 -15
  56. package/dist/commands/start.js.map +1 -1
  57. package/dist/commands/telemetry/disable.d.ts +5 -0
  58. package/dist/commands/telemetry/disable.js +14 -0
  59. package/dist/commands/telemetry/disable.js.map +1 -0
  60. package/dist/commands/telemetry/enable.d.ts +5 -0
  61. package/dist/commands/telemetry/enable.js +14 -0
  62. package/dist/commands/telemetry/enable.js.map +1 -0
  63. package/dist/commands/telemetry/status.d.ts +5 -0
  64. package/dist/commands/telemetry/status.js +14 -0
  65. package/dist/commands/telemetry/status.js.map +1 -0
  66. package/dist/server/asset-base-url-transform-plugin.d.ts +10 -0
  67. package/dist/server/asset-base-url-transform-plugin.js +33 -0
  68. package/dist/server/asset-base-url-transform-plugin.js.map +1 -0
  69. package/dist/server/asset-base-url-transform-plugin.test.d.ts +1 -0
  70. package/dist/server/asset-base-url-transform-plugin.test.js +84 -0
  71. package/dist/server/asset-base-url-transform-plugin.test.js.map +1 -0
  72. package/dist/server/content-helpers.d.ts +27 -0
  73. package/dist/server/content-helpers.js +46 -0
  74. package/dist/server/content-helpers.js.map +1 -0
  75. package/dist/server/content-helpers.test.d.ts +1 -0
  76. package/dist/server/content-helpers.test.js +70 -0
  77. package/dist/server/content-helpers.test.js.map +1 -0
  78. package/dist/server/express.d.ts +11 -0
  79. package/dist/server/express.js +101 -0
  80. package/dist/server/express.js.map +1 -0
  81. package/dist/server/express.test.d.ts +1 -0
  82. package/dist/server/express.test.js +430 -0
  83. package/dist/server/express.test.js.map +1 -0
  84. package/dist/server/index.d.ts +5 -3
  85. package/dist/server/index.js +3 -2
  86. package/dist/server/index.js.map +1 -1
  87. package/dist/server/inferUtilityTypes.d.ts +6 -6
  88. package/dist/server/inferUtilityTypes.js.map +1 -1
  89. package/dist/server/metric.d.ts +14 -0
  90. package/dist/server/metric.js +62 -0
  91. package/dist/server/metric.js.map +1 -0
  92. package/dist/server/middleware.d.ts +124 -0
  93. package/dist/server/middleware.js +93 -0
  94. package/dist/server/middleware.js.map +1 -0
  95. package/dist/server/middleware.test-d.d.ts +1 -0
  96. package/dist/server/middleware.test-d.js +75 -0
  97. package/dist/server/middleware.test-d.js.map +1 -0
  98. package/dist/server/middleware.test.d.ts +1 -0
  99. package/dist/server/middleware.test.js +493 -0
  100. package/dist/server/middleware.test.js.map +1 -0
  101. package/dist/server/server.d.ts +160 -63
  102. package/dist/server/server.js +393 -63
  103. package/dist/server/server.js.map +1 -1
  104. package/dist/server/templateHelper.d.ts +5 -7
  105. package/dist/server/templateHelper.js +3 -22
  106. package/dist/server/templateHelper.js.map +1 -1
  107. package/dist/server/templates.generated.d.ts +4 -0
  108. package/dist/server/templates.generated.js +47 -0
  109. package/dist/server/templates.generated.js.map +1 -0
  110. package/dist/server/tunnel-proxy-router.d.ts +7 -0
  111. package/dist/server/tunnel-proxy-router.js +110 -0
  112. package/dist/server/tunnel-proxy-router.js.map +1 -0
  113. package/dist/server/tunnel-proxy-router.test.d.ts +1 -0
  114. package/dist/server/tunnel-proxy-router.test.js +229 -0
  115. package/dist/server/tunnel-proxy-router.test.js.map +1 -0
  116. package/dist/server/viewsDevServer.d.ts +14 -0
  117. package/dist/server/viewsDevServer.js +45 -0
  118. package/dist/server/viewsDevServer.js.map +1 -0
  119. package/dist/test/utils.d.ts +13 -21
  120. package/dist/test/utils.js +42 -37
  121. package/dist/test/utils.js.map +1 -1
  122. package/dist/test/view.test.d.ts +1 -0
  123. package/dist/test/view.test.js +523 -0
  124. package/dist/test/view.test.js.map +1 -0
  125. package/dist/version.d.ts +1 -0
  126. package/dist/version.js +3 -0
  127. package/dist/version.js.map +1 -0
  128. package/dist/web/bridges/apps-sdk/adaptor.d.ts +18 -6
  129. package/dist/web/bridges/apps-sdk/adaptor.js +71 -8
  130. package/dist/web/bridges/apps-sdk/adaptor.js.map +1 -1
  131. package/dist/web/bridges/apps-sdk/bridge.d.ts +1 -1
  132. package/dist/web/bridges/apps-sdk/bridge.js.map +1 -1
  133. package/dist/web/bridges/apps-sdk/index.d.ts +1 -1
  134. package/dist/web/bridges/apps-sdk/index.js.map +1 -1
  135. package/dist/web/bridges/apps-sdk/types.d.ts +39 -27
  136. package/dist/web/bridges/apps-sdk/types.js.map +1 -1
  137. package/dist/web/bridges/apps-sdk/use-apps-sdk-context.js.map +1 -1
  138. package/dist/web/bridges/get-adaptor.js.map +1 -1
  139. package/dist/web/bridges/index.js.map +1 -1
  140. package/dist/web/bridges/mcp-app/adaptor.d.ts +39 -8
  141. package/dist/web/bridges/mcp-app/adaptor.js +182 -56
  142. package/dist/web/bridges/mcp-app/adaptor.js.map +1 -1
  143. package/dist/web/bridges/mcp-app/bridge.d.ts +13 -30
  144. package/dist/web/bridges/mcp-app/bridge.js +43 -196
  145. package/dist/web/bridges/mcp-app/bridge.js.map +1 -1
  146. package/dist/web/bridges/mcp-app/index.js.map +1 -1
  147. package/dist/web/bridges/mcp-app/types.js.map +1 -1
  148. package/dist/web/bridges/mcp-app/use-mcp-app-context.d.ts +5 -3
  149. package/dist/web/bridges/mcp-app/use-mcp-app-context.js +2 -2
  150. package/dist/web/bridges/mcp-app/use-mcp-app-context.js.map +1 -1
  151. package/dist/web/bridges/mcp-app/use-mcp-app-context.test.js +1 -41
  152. package/dist/web/bridges/mcp-app/use-mcp-app-context.test.js.map +1 -1
  153. package/dist/web/bridges/types.d.ts +55 -12
  154. package/dist/web/bridges/types.js.map +1 -1
  155. package/dist/web/bridges/use-host-context.js.map +1 -1
  156. package/dist/web/components/modal-provider.d.ts +4 -0
  157. package/dist/web/components/modal-provider.js +45 -0
  158. package/dist/web/components/modal-provider.js.map +1 -0
  159. package/dist/web/create-store.js +17 -3
  160. package/dist/web/create-store.js.map +1 -1
  161. package/dist/web/create-store.test.js +23 -20
  162. package/dist/web/create-store.test.js.map +1 -1
  163. package/dist/web/data-llm.d.ts +1 -1
  164. package/dist/web/data-llm.js +3 -3
  165. package/dist/web/data-llm.js.map +1 -1
  166. package/dist/web/data-llm.test.js +33 -30
  167. package/dist/web/data-llm.test.js.map +1 -1
  168. package/dist/web/generate-helpers.d.ts +20 -18
  169. package/dist/web/generate-helpers.js +20 -18
  170. package/dist/web/generate-helpers.js.map +1 -1
  171. package/dist/web/generate-helpers.test-d.js +26 -26
  172. package/dist/web/generate-helpers.test-d.js.map +1 -1
  173. package/dist/web/generate-helpers.test.js.map +1 -1
  174. package/dist/web/helpers/state.d.ts +2 -2
  175. package/dist/web/helpers/state.js +11 -11
  176. package/dist/web/helpers/state.js.map +1 -1
  177. package/dist/web/helpers/state.test.js +9 -9
  178. package/dist/web/helpers/state.test.js.map +1 -1
  179. package/dist/web/hooks/index.d.ts +5 -2
  180. package/dist/web/hooks/index.js +4 -1
  181. package/dist/web/hooks/index.js.map +1 -1
  182. package/dist/web/hooks/test/utils.js +4 -0
  183. package/dist/web/hooks/test/utils.js.map +1 -1
  184. package/dist/web/hooks/use-call-tool.js.map +1 -1
  185. package/dist/web/hooks/use-call-tool.test-d.js.map +1 -1
  186. package/dist/web/hooks/use-call-tool.test.js +0 -4
  187. package/dist/web/hooks/use-call-tool.test.js.map +1 -1
  188. package/dist/web/hooks/use-display-mode.d.ts +3 -3
  189. package/dist/web/hooks/use-display-mode.js.map +1 -1
  190. package/dist/web/hooks/use-display-mode.test-d.d.ts +1 -0
  191. package/dist/web/hooks/use-display-mode.test-d.js +8 -0
  192. package/dist/web/hooks/use-display-mode.test-d.js.map +1 -0
  193. package/dist/web/hooks/use-display-mode.test.js.map +1 -1
  194. package/dist/web/hooks/use-files.d.ts +3 -6
  195. package/dist/web/hooks/use-files.js +5 -2
  196. package/dist/web/hooks/use-files.js.map +1 -1
  197. package/dist/web/hooks/use-files.test.js +28 -3
  198. package/dist/web/hooks/use-files.test.js.map +1 -1
  199. package/dist/web/hooks/use-layout.d.ts +1 -1
  200. package/dist/web/hooks/use-layout.js.map +1 -1
  201. package/dist/web/hooks/use-layout.test.js +3 -3
  202. package/dist/web/hooks/use-layout.test.js.map +1 -1
  203. package/dist/web/hooks/use-open-external.d.ts +3 -1
  204. package/dist/web/hooks/use-open-external.js +1 -1
  205. package/dist/web/hooks/use-open-external.js.map +1 -1
  206. package/dist/web/hooks/use-open-external.test.js +26 -11
  207. package/dist/web/hooks/use-open-external.test.js.map +1 -1
  208. package/dist/web/hooks/use-request-close.d.ts +2 -0
  209. package/dist/web/hooks/use-request-close.js +8 -0
  210. package/dist/web/hooks/use-request-close.js.map +1 -0
  211. package/dist/web/hooks/use-request-close.test.d.ts +1 -0
  212. package/dist/web/hooks/use-request-close.test.js +52 -0
  213. package/dist/web/hooks/use-request-close.test.js.map +1 -0
  214. package/dist/web/hooks/use-request-modal.d.ts +3 -3
  215. package/dist/web/hooks/use-request-modal.js +10 -8
  216. package/dist/web/hooks/use-request-modal.js.map +1 -1
  217. package/dist/web/hooks/use-request-modal.test.js +5 -1
  218. package/dist/web/hooks/use-request-modal.test.js.map +1 -1
  219. package/dist/web/hooks/use-request-size.d.ts +3 -0
  220. package/dist/web/hooks/use-request-size.js +8 -0
  221. package/dist/web/hooks/use-request-size.js.map +1 -0
  222. package/dist/web/hooks/use-request-size.test.d.ts +1 -0
  223. package/dist/web/hooks/use-request-size.test.js +65 -0
  224. package/dist/web/hooks/use-request-size.test.js.map +1 -0
  225. package/dist/web/hooks/use-send-follow-up-message.d.ts +2 -1
  226. package/dist/web/hooks/use-send-follow-up-message.js +2 -2
  227. package/dist/web/hooks/use-send-follow-up-message.js.map +1 -1
  228. package/dist/web/hooks/use-set-open-in-app-url.d.ts +1 -0
  229. package/dist/web/hooks/use-set-open-in-app-url.js +8 -0
  230. package/dist/web/hooks/use-set-open-in-app-url.js.map +1 -0
  231. package/dist/web/hooks/use-set-open-in-app-url.test.d.ts +1 -0
  232. package/dist/web/hooks/use-set-open-in-app-url.test.js +43 -0
  233. package/dist/web/hooks/use-set-open-in-app-url.test.js.map +1 -0
  234. package/dist/web/hooks/use-tool-info.js.map +1 -1
  235. package/dist/web/hooks/use-tool-info.test-d.js.map +1 -1
  236. package/dist/web/hooks/use-tool-info.test.js +1 -1
  237. package/dist/web/hooks/use-tool-info.test.js.map +1 -1
  238. package/dist/web/hooks/use-user.js +18 -2
  239. package/dist/web/hooks/use-user.js.map +1 -1
  240. package/dist/web/hooks/use-user.test.js +29 -1
  241. package/dist/web/hooks/use-user.test.js.map +1 -1
  242. package/dist/web/hooks/use-view-state.d.ts +4 -0
  243. package/dist/web/hooks/use-view-state.js +32 -0
  244. package/dist/web/hooks/use-view-state.js.map +1 -0
  245. package/dist/web/hooks/use-view-state.test.d.ts +1 -0
  246. package/dist/web/hooks/use-view-state.test.js +177 -0
  247. package/dist/web/hooks/use-view-state.test.js.map +1 -0
  248. package/dist/web/index.d.ts +1 -2
  249. package/dist/web/index.js +1 -2
  250. package/dist/web/index.js.map +1 -1
  251. package/dist/web/mount-view.d.ts +1 -0
  252. package/dist/web/{mount-widget.js → mount-view.js} +11 -3
  253. package/dist/web/mount-view.js.map +1 -0
  254. package/dist/web/plugin/data-llm.test.js.map +1 -1
  255. package/dist/web/plugin/plugin.d.ts +4 -1
  256. package/dist/web/plugin/plugin.js +135 -18
  257. package/dist/web/plugin/plugin.js.map +1 -1
  258. package/dist/web/plugin/scan-views.d.ts +16 -0
  259. package/dist/web/plugin/scan-views.js +88 -0
  260. package/dist/web/plugin/scan-views.js.map +1 -0
  261. package/dist/web/plugin/scan-views.test.d.ts +1 -0
  262. package/dist/web/plugin/scan-views.test.js +99 -0
  263. package/dist/web/plugin/scan-views.test.js.map +1 -0
  264. package/dist/web/plugin/transform-data-llm.js +1 -1
  265. package/dist/web/plugin/transform-data-llm.js.map +1 -1
  266. package/dist/web/plugin/transform-data-llm.test.js.map +1 -1
  267. package/dist/web/plugin/validate-view.d.ts +1 -0
  268. package/dist/web/plugin/validate-view.js +9 -0
  269. package/dist/web/plugin/validate-view.js.map +1 -0
  270. package/dist/web/plugin/validate-view.test.d.ts +1 -0
  271. package/dist/web/plugin/validate-view.test.js +24 -0
  272. package/dist/web/plugin/validate-view.test.js.map +1 -0
  273. package/dist/web/proxy.js +0 -1
  274. package/dist/web/proxy.js.map +1 -1
  275. package/dist/web/types.js.map +1 -1
  276. package/package.json +51 -30
  277. package/tsconfig.base.json +33 -0
  278. package/dist/server/templates/development.hbs +0 -66
  279. package/dist/server/templates/production.hbs +0 -7
  280. package/dist/server/widgetsDevServer.d.ts +0 -12
  281. package/dist/server/widgetsDevServer.js +0 -47
  282. package/dist/server/widgetsDevServer.js.map +0 -1
  283. package/dist/test/widget.test.js +0 -255
  284. package/dist/test/widget.test.js.map +0 -1
  285. package/dist/web/hooks/use-widget-state.d.ts +0 -4
  286. package/dist/web/hooks/use-widget-state.js +0 -32
  287. package/dist/web/hooks/use-widget-state.js.map +0 -1
  288. package/dist/web/hooks/use-widget-state.test.js +0 -61
  289. package/dist/web/hooks/use-widget-state.test.js.map +0 -1
  290. package/dist/web/mount-widget.d.ts +0 -1
  291. package/dist/web/mount-widget.js.map +0 -1
  292. /package/dist/{test/widget.test.d.ts → cli/tunnel-control-server.test.d.ts} +0 -0
  293. /package/dist/{web/hooks/use-widget-state.test.d.ts → cli/tunnel-handler.test.d.ts} +0 -0
@@ -0,0 +1,70 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import { audio, embeddedResource, image, resourceLink, text, } from "./content-helpers.js";
3
+ describe("text", () => {
4
+ it("returns a TextContent without annotations when none given", () => {
5
+ expect(text("hello")).toEqual({ type: "text", text: "hello" });
6
+ });
7
+ it("includes annotations when provided", () => {
8
+ expect(text("hi", { priority: 1 })).toEqual({
9
+ type: "text",
10
+ text: "hi",
11
+ annotations: { priority: 1 },
12
+ });
13
+ });
14
+ });
15
+ describe("image", () => {
16
+ it("base64-encodes Uint8Array data", () => {
17
+ const bytes = new Uint8Array([104, 105]);
18
+ expect(image(bytes, "image/png")).toEqual({
19
+ type: "image",
20
+ data: "aGk=",
21
+ mimeType: "image/png",
22
+ });
23
+ });
24
+ it("passes string data through unchanged (caller is responsible for base64)", () => {
25
+ expect(image("YWxyZWFkeS1iNjQ=", "image/png")).toEqual({
26
+ type: "image",
27
+ data: "YWxyZWFkeS1iNjQ=",
28
+ mimeType: "image/png",
29
+ });
30
+ });
31
+ });
32
+ describe("audio", () => {
33
+ it("base64-encodes Uint8Array data", () => {
34
+ const bytes = new Uint8Array([104, 105]);
35
+ expect(audio(bytes, "audio/mpeg")).toMatchObject({
36
+ type: "audio",
37
+ data: "aGk=",
38
+ mimeType: "audio/mpeg",
39
+ });
40
+ });
41
+ });
42
+ describe("embeddedResource", () => {
43
+ it("wraps a text resource with a type tag", () => {
44
+ expect(embeddedResource({
45
+ uri: "file:///a.txt",
46
+ mimeType: "text/plain",
47
+ text: "x",
48
+ })).toEqual({
49
+ type: "resource",
50
+ resource: { uri: "file:///a.txt", mimeType: "text/plain", text: "x" },
51
+ });
52
+ });
53
+ it("wraps a blob resource with a type tag", () => {
54
+ expect(embeddedResource({ uri: "file:///a.bin", blob: "YmFy" })).toEqual({
55
+ type: "resource",
56
+ resource: { uri: "file:///a.bin", blob: "YmFy" },
57
+ });
58
+ });
59
+ });
60
+ describe("resourceLink", () => {
61
+ it("spreads link fields alongside the type tag", () => {
62
+ expect(resourceLink({ uri: "file:///a", name: "a", title: "A" })).toEqual({
63
+ type: "resource_link",
64
+ uri: "file:///a",
65
+ name: "a",
66
+ title: "A",
67
+ });
68
+ });
69
+ });
70
+ //# sourceMappingURL=content-helpers.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"content-helpers.test.js","sourceRoot":"","sources":["../../src/server/content-helpers.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,QAAQ,CAAC;AAC9C,OAAO,EACL,KAAK,EACL,gBAAgB,EAChB,KAAK,EACL,YAAY,EACZ,IAAI,GACL,MAAM,sBAAsB,CAAC;AAE9B,QAAQ,CAAC,MAAM,EAAE,GAAG,EAAE;IACpB,EAAE,CAAC,2DAA2D,EAAE,GAAG,EAAE;QACnE,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,OAAO,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC,CAAC;IACjE,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,oCAAoC,EAAE,GAAG,EAAE;QAC5C,MAAM,CAAC,IAAI,CAAC,IAAI,EAAE,EAAE,QAAQ,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC;YAC1C,IAAI,EAAE,MAAM;YACZ,IAAI,EAAE,IAAI;YACV,WAAW,EAAE,EAAE,QAAQ,EAAE,CAAC,EAAE;SAC7B,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC;AAEH,QAAQ,CAAC,OAAO,EAAE,GAAG,EAAE;IACrB,EAAE,CAAC,gCAAgC,EAAE,GAAG,EAAE;QACxC,MAAM,KAAK,GAAG,IAAI,UAAU,CAAC,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC,CAAC;QACzC,MAAM,CAAC,KAAK,CAAC,KAAK,EAAE,WAAW,CAAC,CAAC,CAAC,OAAO,C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,CAAC,EAAE,GAAG,EAAE,eAAe,EAAE,IAAI,EAAE,MAAM,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC;YACvE,IAAI,EAAE,UAAU;YAChB,QAAQ,EAAE,EAAE,GAAG,EAAE,eAAe,EAAE,IAAI,EAAE,MAAM,EAAE;SACjD,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC;AAEH,QAAQ,CAAC,cAAc,EAAE,GAAG,EAAE;IAC5B,EAAE,CAAC,4CAA4C,EAAE,GAAG,EAAE;QACpD,MAAM,CAAC,YAAY,CAAC,EAAE,GAAG,EAAE,WAAW,EAAE,IAAI,EAAE,GAAG,EAAE,KAAK,EAAE,GAAG,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC;YACxE,IAAI,EAAE,eAAe;YACrB,GAAG,EAAE,WAAW;YAChB,IAAI,EAAE,GAAG;YACT,KAAK,EAAE,GAAG;SACX,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC","sourcesContent":["import { describe, expect, it } from \"vitest\";\nimport {\n audio,\n embeddedResource,\n image,\n resourceLink,\n text,\n} from \"./content-helpers.js\";\n\ndescribe(\"text\", () => {\n it(\"returns a TextContent without annotations when none given\", () => {\n expect(text(\"hello\")).toEqual({ type: \"text\", text: \"hello\" });\n });\n\n it(\"includes annotations when provided\", () => {\n expect(text(\"hi\", { priority: 1 })).toEqual({\n type: \"text\",\n text: \"hi\",\n annotations: { priority: 1 },\n });\n });\n});\n\ndescribe(\"image\", () => {\n it(\"base64-encodes Uint8Array data\", () => {\n const bytes = new Uint8Array([104, 105]);\n expect(image(bytes, \"image/png\")).toEqual({\n type: \"image\",\n data: \"aGk=\",\n mimeType: \"image/png\",\n });\n });\n\n it(\"passes string data through unchanged (caller is responsible for base64)\", () => {\n expect(image(\"YWxyZWFkeS1iNjQ=\", \"image/png\")).toEqual({\n type: \"image\",\n data: \"YWxyZWFkeS1iNjQ=\",\n mimeType: \"image/png\",\n });\n });\n});\n\ndescribe(\"audio\", () => {\n it(\"base64-encodes Uint8Array data\", () => {\n const bytes = new Uint8Array([104, 105]);\n expect(audio(bytes, \"audio/mpeg\")).toMatchObject({\n type: \"audio\",\n data: \"aGk=\",\n mimeType: \"audio/mpeg\",\n });\n });\n});\n\ndescribe(\"embeddedResource\", () => {\n it(\"wraps a text resource with a type tag\", () => {\n expect(\n embeddedResource({\n uri: \"file:///a.txt\",\n mimeType: \"text/plain\",\n text: \"x\",\n }),\n ).toEqual({\n type: \"resource\",\n resource: { uri: \"file:///a.txt\", mimeType: \"text/plain\", text: \"x\" },\n });\n });\n\n it(\"wraps a blob resource with a type tag\", () => {\n expect(embeddedResource({ uri: \"file:///a.bin\", blob: \"YmFy\" })).toEqual({\n type: \"resource\",\n resource: { uri: \"file:///a.bin\", blob: \"YmFy\" },\n });\n });\n});\n\ndescribe(\"resourceLink\", () => {\n it(\"spreads link fields alongside the type tag\", () => {\n expect(resourceLink({ uri: \"file:///a\", name: \"a\", title: \"A\" })).toEqual({\n type: \"resource_link\",\n uri: \"file:///a\",\n name: \"a\",\n title: \"A\",\n });\n });\n});\n"]}
@@ -0,0 +1,11 @@
1
+ import type http from "node:http";
2
+ import express from "express";
3
+ import type { McpServer } from "./server.js";
4
+ export declare function createApp({ mcpServer, httpServer, errorMiddleware, }: {
5
+ mcpServer: McpServer;
6
+ httpServer: http.Server;
7
+ errorMiddleware?: {
8
+ path?: string;
9
+ handlers: express.ErrorRequestHandler[];
10
+ }[];
11
+ }): Promise<express.Express>;
@@ -0,0 +1,101 @@
1
+ import path from "node:path";
2
+ import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
3
+ import cors from "cors";
4
+ import express from "express";
5
+ function parseControlPort(raw) {
6
+ if (raw === undefined) {
7
+ return null;
8
+ }
9
+ const n = Number.parseInt(raw, 10);
10
+ if (!Number.isFinite(n) || n <= 0 || n >= 65536) {
11
+ return null;
12
+ }
13
+ return n;
14
+ }
15
+ function applyMiddlewares(app, middlewares) {
16
+ for (const middleware of middlewares) {
17
+ if (middleware.path) {
18
+ app.use(middleware.path, ...middleware.handlers);
19
+ }
20
+ else {
21
+ app.use(...middleware.handlers);
22
+ }
23
+ }
24
+ }
25
+ function defaultErrorHandler(err, _req, res, _next) {
26
+ console.error("Error handling MCP request:", err);
27
+ if (!res.headersSent) {
28
+ res.status(500).json({
29
+ jsonrpc: "2.0",
30
+ error: { code: -32603, message: "Internal server error" },
31
+ id: null,
32
+ });
33
+ }
34
+ }
35
+ export async function createApp({ mcpServer, httpServer, errorMiddleware = [], }) {
36
+ const app = mcpServer.express;
37
+ // Read `process.env.NODE_ENV` inline: wrangler/esbuild only substitute the literal expression,
38
+ // so a local const would defeat dead-code elimination of the dev-only imports below.
39
+ if (process.env.NODE_ENV !== "production") {
40
+ const { devtoolsStaticServer } = await import("@skybridge/devtools");
41
+ app.use(await devtoolsStaticServer());
42
+ const { viewsDevServer } = await import("./viewsDevServer.js");
43
+ app.use(await viewsDevServer(httpServer));
44
+ const controlPort = parseControlPort(process.env.__TUNNEL_CONTROL_PORT);
45
+ if (controlPort !== null) {
46
+ const { createTunnelProxyRouter } = await import("./tunnel-proxy-router.js");
47
+ app.use(createTunnelProxyRouter(controlPort));
48
+ }
49
+ else if (process.env.__TUNNEL_CONTROL_PORT !== undefined) {
50
+ console.warn(`Ignoring invalid __TUNNEL_CONTROL_PORT=${process.env.__TUNNEL_CONTROL_PORT}`);
51
+ }
52
+ }
53
+ else {
54
+ const assetsPath = path.join(process.cwd(), "dist", "assets");
55
+ app.use("/assets", cors());
56
+ app.use("/assets", express.static(assetsPath));
57
+ }
58
+ app.use("/mcp", mcpMiddleware(mcpServer));
59
+ applyMiddlewares(app, errorMiddleware);
60
+ app.use("/mcp", defaultErrorHandler);
61
+ return app;
62
+ }
63
+ const mcpMiddleware = (server) => {
64
+ return async (req, res, next) => {
65
+ if (req.method !== "POST") {
66
+ res.writeHead(405).end(JSON.stringify({
67
+ jsonrpc: "2.0",
68
+ error: {
69
+ code: -32000,
70
+ message: "Method not allowed.",
71
+ },
72
+ id: null,
73
+ }));
74
+ return;
75
+ }
76
+ try {
77
+ const transport = new StreamableHTTPServerTransport({
78
+ sessionIdGenerator: undefined,
79
+ // Respond with a single JSON body instead of SSE. Skybridge's stateless
80
+ // transport never streams server-initiated messages, so SSE adds no
81
+ // capability — and on workerd specifically, `cloudflare:node`'s http
82
+ // bridge silently drops chunked writes that happen after the request
83
+ // handler awaits, which manifests as a 200 with empty body for any
84
+ // async tools/call.
85
+ enableJsonResponse: true,
86
+ });
87
+ res.on("close", () => {
88
+ transport.close();
89
+ });
90
+ await server.connectStatelessTransport(transport);
91
+ // Express strips the mount path from req.url (e.g. "/mcp" becomes "/").
92
+ // Restore it so the SDK builds the correct requestInfo.url.
93
+ req.url = req.originalUrl;
94
+ await transport.handleRequest(req, res, req.body);
95
+ }
96
+ catch (error) {
97
+ next(error);
98
+ }
99
+ };
100
+ };
101
+ //# sourceMappingURL=express.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"express.js","sourceRoot":"","sources":["../../src/server/express.ts"],"names":[],"mappings":"AACA,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EAAE,6BAA6B,EAAE,MAAM,oDAAoD,CAAC;AACnG,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,OAAO,MAAM,SAAS,CAAC;AAG9B,SAAS,gBAAgB,CAAC,GAAuB;IAC/C,IAAI,GAAG,KAAK,SAAS,EAAE,CAAC;QACtB,OAAO,IAAI,CAAC;IACd,CAAC;IACD,MAAM,CAAC,GAAG,MAAM,CAAC,QAAQ,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC;IACnC,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,KAAK,EAAE,CAAC;QAChD,OAAO,IAAI,CAAC;IACd,CAAC;IACD,OAAO,CAAC,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YAC1B,GAAG,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC,GAAG,CACpB,IAAI,CAAC,SAAS,CAAC;gBACb,OAAO,EAAE,KAAK;gBACd,KAAK,EAAE;oBACL,IAAI,EAAE,CAAC,KAAK;oBACZ,OAAO,EAAE,qBAAqB;iBAC/B;gBACD,EAAE,EAAE,IAAI;aACT,CAAC,CACH,CAAC;YACF,OAAO;QACT,CAAC;QAED,IAAI,CAAC;YACH,MAAM,SAAS,GAAG,IAAI,6BAA6B,CAAC;gBAClD,kBAAkB,EAAE,SAAS;gBAC7B,wEAAwE;gBACxE,oEAAoE;gBACpE,qEAAqE;gBACrE,qEAAqE;gBACrE,mEAAmE;gBACnE,oBAAoB;gBACpB,kBAAkB,EAAE,IAAI;aACzB,CAAC,CAAC;YAEH,GAAG,CAAC,EAAE,CAAC,OAAO,EAAE,GAAG,EAAE;gBACnB,SAAS,CAAC,KAAK,EAAE,CAAC;YACpB,CAAC,CAAC,CAAC;YAEH,MAAM,MAAM,CAAC,yBAAyB,CAAC,SAAS,CAAC,CAAC;YAClD,wEAAwE;YACxE,4DAA4D;YAC5D,GAAG,CAAC,GAAG,GAAG,GAAG,CAAC,WAAW,CAAC;YAC1B,MAAM,SAAS,CAAC,aAAa,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,IAAI,CAAC,CAAC;QACpD,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,IAAI,CAAC,KAAK,CAAC,CAAC;QACd,CAAC;IACH,CAAC,CAAC;AACJ,CAAC,CAAC","sourcesContent":["import type http from \"node:http\";\nimport path from \"node:path\";\nimport { StreamableHTTPServerTransport } from \"@modelcontextprotocol/sdk/server/streamableHttp.js\";\nimport cors from \"cors\";\nimport express from \"express\";\nimport type { McpServer } from \"./server.js\";\n\nfunction parseControlPort(raw: string | undefined): number | null {\n if (raw === undefined) {\n return null;\n }\n const n = Number.parseInt(raw, 10);\n if (!Number.isFinite(n) || n <= 0 || n >= 65536) {\n return null;\n }\n return n;\n}\n\nfunction applyMiddlewares(\n app: express.Express,\n middlewares: Array<{\n path?: string;\n handlers: express.ErrorRequestHandler[];\n }>,\n): void {\n for (const middleware of middlewares) {\n if (middleware.path) {\n app.use(middleware.path, ...middleware.handlers);\n } else {\n app.use(...middleware.handlers);\n }\n }\n}\n\nfunction defaultErrorHandler(\n err: unknown,\n _req: express.Request,\n res: express.Response,\n _next: express.NextFunction,\n) {\n console.error(\"Error handling MCP request:\", err);\n if (!res.headersSent) {\n res.status(500).json({\n jsonrpc: \"2.0\",\n error: { code: -32603, message: \"Internal server error\" },\n id: null,\n });\n }\n}\n\nexport async function createApp({\n mcpServer,\n httpServer,\n errorMiddleware = [],\n}: {\n mcpServer: McpServer;\n httpServer: http.Server;\n errorMiddleware?: {\n path?: string;\n handlers: express.ErrorRequestHandler[];\n }[];\n}): Promise<express.Express> {\n const app = mcpServer.express;\n\n // Read `process.env.NODE_ENV` inline: wrangler/esbuild only substitute the literal expression,\n // so a local const would defeat dead-code elimination of the dev-only imports below.\n if (process.env.NODE_ENV !== \"production\") {\n const { devtoolsStaticServer } = await import(\"@skybridge/devtools\");\n app.use(await devtoolsStaticServer());\n const { viewsDevServer } = await import(\"./viewsDevServer.js\");\n app.use(await viewsDevServer(httpServer));\n\n const controlPort = parseControlPort(process.env.__TUNNEL_CONTROL_PORT);\n if (controlPort !== null) {\n const { createTunnelProxyRouter } = await import(\n \"./tunnel-proxy-router.js\"\n );\n app.use(createTunnelProxyRouter(controlPort));\n } else if (process.env.__TUNNEL_CONTROL_PORT !== undefined) {\n console.warn(\n `Ignoring invalid __TUNNEL_CONTROL_PORT=${process.env.__TUNNEL_CONTROL_PORT}`,\n );\n }\n } else {\n const assetsPath = path.join(process.cwd(), \"dist\", \"assets\");\n\n app.use(\"/assets\", cors());\n app.use(\"/assets\", express.static(assetsPath));\n }\n\n app.use(\"/mcp\", mcpMiddleware(mcpServer));\n\n applyMiddlewares(app, errorMiddleware);\n\n app.use(\"/mcp\", defaultErrorHandler);\n\n return app;\n}\n\nconst mcpMiddleware = (server: McpServer): express.RequestHandler => {\n return async (\n req: express.Request,\n res: express.Response,\n next: express.NextFunction,\n ) => {\n if (req.method !== \"POST\") {\n res.writeHead(405).end(\n JSON.stringify({\n jsonrpc: \"2.0\",\n error: {\n code: -32000,\n message: \"Method not allowed.\",\n },\n id: null,\n }),\n );\n return;\n }\n\n try {\n const transport = new StreamableHTTPServerTransport({\n sessionIdGenerator: undefined,\n // Respond with a single JSON body instead of SSE. Skybridge's stateless\n // transport never streams server-initiated messages, so SSE adds no\n // capability — and on workerd specifically, `cloudflare:node`'s http\n // bridge silently drops chunked writes that happen after the request\n // handler awaits, which manifests as a 200 with empty body for any\n // async tools/call.\n enableJsonResponse: true,\n });\n\n res.on(\"close\", () => {\n transport.close();\n });\n\n await server.connectStatelessTransport(transport);\n // Express strips the mount path from req.url (e.g. \"/mcp\" becomes \"/\").\n // Restore it so the SDK builds the correct requestInfo.url.\n req.url = req.originalUrl;\n await transport.handleRequest(req, res, req.body);\n } catch (error) {\n next(error);\n }\n };\n};\n"]}
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,430 @@
1
+ import http from "node:http";
2
+ import { afterEach, describe, expect, it, vi } from "vitest";
3
+ import { McpServer } from "./server.js";
4
+ vi.mock("@skybridge/devtools", () => ({
5
+ devtoolsStaticServer: () => ((_req, _res, next) => next()),
6
+ }));
7
+ vi.mock("./viewsDevServer.js", () => ({
8
+ viewsDevServer: (_httpServer) => ((_req, _res, next) => next()),
9
+ }));
10
+ async function listen(app) {
11
+ const server = http.createServer(app);
12
+ await new Promise((resolve) => server.listen(0, resolve));
13
+ const port = server.address().port;
14
+ return { port, server };
15
+ }
16
+ let openServer;
17
+ afterEach(() => openServer?.close());
18
+ async function postMcp(port) {
19
+ return fetch(`http://localhost:${port}/mcp`, {
20
+ method: "POST",
21
+ headers: { "Content-Type": "application/json" },
22
+ body: JSON.stringify({ jsonrpc: "2.0", method: "initialize", id: 1 }),
23
+ });
24
+ }
25
+ async function postApi(port) {
26
+ return fetch(`http://localhost:${port}/api/test`, { method: "POST" });
27
+ }
28
+ describe("McpServer.express", () => {
29
+ it("exposes a ready Express app immediately after construction", () => {
30
+ const server = new McpServer({ name: "t", version: "0.0.0" });
31
+ expect(server.express).toBeDefined();
32
+ expect(typeof server.express.use).toBe("function");
33
+ expect(typeof server.express.get).toBe("function");
34
+ });
35
+ it("server.express.get registers a route reachable alongside /mcp", async () => {
36
+ const { createApp } = await import("./express.js");
37
+ const server = new McpServer({ name: "t", version: "0.0.0" });
38
+ server.express.get("/health", (_req, res) => {
39
+ res.json({ status: "ok" });
40
+ });
41
+ const httpServer = http.createServer();
42
+ await createApp({ mcpServer: server, httpServer });
43
+ const { port, server: listening } = await listen(server.express);
44
+ openServer = listening;
45
+ const health = await fetch(`http://localhost:${port}/health`);
46
+ expect(health.status).toBe(200);
47
+ expect(await health.json()).toEqual({ status: "ok" });
48
+ // /mcp still works (POST returns 200/4xx, not 404)
49
+ const mcp = await postMcp(port);
50
+ expect(mcp.status).not.toBe(404);
51
+ });
52
+ it("server.use and server.express.use produce the same registration order", async () => {
53
+ const { createApp } = await import("./express.js");
54
+ const callsA = [];
55
+ const callsB = [];
56
+ const buildServer = () => new McpServer({ name: "t", version: "0.0.0" });
57
+ const sA = buildServer();
58
+ sA.use((_req, _res, next) => {
59
+ callsA.push("first");
60
+ next();
61
+ });
62
+ sA.express.use((_req, _res, next) => {
63
+ callsA.push("second");
64
+ next();
65
+ });
66
+ const sB = buildServer();
67
+ sB.express.use((_req, _res, next) => {
68
+ callsB.push("first");
69
+ next();
70
+ });
71
+ sB.use((_req, _res, next) => {
72
+ callsB.push("second");
73
+ next();
74
+ });
75
+ for (const s of [sA, sB]) {
76
+ s.express.get("/probe", (_req, res) => res.json({ ok: true }));
77
+ const httpServer = http.createServer();
78
+ await createApp({ mcpServer: s, httpServer });
79
+ const { port, server: listening } = await listen(s.express);
80
+ openServer = listening;
81
+ await fetch(`http://localhost:${port}/probe`);
82
+ listening.close();
83
+ }
84
+ expect(callsA).toEqual(["first", "second"]);
85
+ expect(callsB).toEqual(["first", "second"]);
86
+ });
87
+ it("useOnError still wraps thrown /mcp errors after the route is mounted", async () => {
88
+ const { createApp } = await import("./express.js");
89
+ const server = new McpServer({ name: "t", version: "0.0.0" });
90
+ // Register the error handler BEFORE createApp — useOnError should still
91
+ // apply it after /mcp, so /mcp errors hit it.
92
+ const seen = [];
93
+ server.useOnError((_err, _req, res, _next) => {
94
+ seen.push("useOnError");
95
+ res.status(503).json({ from: "useOnError" });
96
+ });
97
+ // Force the /mcp handler to throw so the error pipeline runs.
98
+ vi.spyOn(server, "connectStatelessTransport").mockRejectedValue(new Error("boom"));
99
+ const httpServer = http.createServer();
100
+ await createApp({
101
+ mcpServer: server,
102
+ httpServer,
103
+ // Mirror what run() does: forward the McpServer's useOnError handlers
104
+ // to createApp so they get applied after /mcp.
105
+ // biome-ignore lint/complexity/useLiteralKeys: test mirrors run() internals to verify useOnError ordering
106
+ errorMiddleware: server["customErrorMiddleware"],
107
+ });
108
+ const { port, server: listening } = await listen(server.express);
109
+ openServer = listening;
110
+ const res = await postMcp(port);
111
+ expect(seen).toEqual(["useOnError"]);
112
+ expect(res.status).toBe(503);
113
+ expect(await res.json()).toEqual({ from: "useOnError" });
114
+ });
115
+ });
116
+ describe("createApp", () => {
117
+ it("runs global custom middleware before the /mcp handler", async () => {
118
+ const { createApp } = await import("./express.js");
119
+ const calls = [];
120
+ const mw = (_req, _res, next) => {
121
+ calls.push("custom");
122
+ next();
123
+ };
124
+ const server = new McpServer({ name: "t", version: "0.0.0" });
125
+ server.use(mw);
126
+ const httpServer = http.createServer();
127
+ const app = await createApp({ mcpServer: server, httpServer });
128
+ const { port, server: httpListening } = await listen(app);
129
+ openServer = httpListening;
130
+ await postMcp(port);
131
+ expect(calls).toEqual(["custom"]);
132
+ });
133
+ it("runs path-scoped middleware on /mcp", async () => {
134
+ const { createApp } = await import("./express.js");
135
+ const calls = [];
136
+ const mw = (_req, _res, next) => {
137
+ calls.push("auth");
138
+ next();
139
+ };
140
+ const server = new McpServer({ name: "t", version: "0.0.0" });
141
+ server.use("/mcp", mw);
142
+ const httpServer = http.createServer();
143
+ const app = await createApp({ mcpServer: server, httpServer });
144
+ const { port, server: httpListening } = await listen(app);
145
+ openServer = httpListening;
146
+ await postMcp(port);
147
+ expect(calls).toEqual(["auth"]);
148
+ });
149
+ it("allows middleware to short-circuit with 401", async () => {
150
+ const { createApp } = await import("./express.js");
151
+ const calls = [];
152
+ const reject = (_req, res) => {
153
+ calls.push("reject");
154
+ res.status(401).json({ error: "Unauthorized" });
155
+ };
156
+ const server = new McpServer({ name: "t", version: "0.0.0" });
157
+ server.use("/mcp", reject);
158
+ const httpServer = http.createServer();
159
+ const app = await createApp({ mcpServer: server, httpServer });
160
+ const { port, server: httpListening } = await listen(app);
161
+ openServer = httpListening;
162
+ const res = await postMcp(port);
163
+ expect(calls).toEqual(["reject"]);
164
+ expect(res.status).toBe(401);
165
+ expect(await res.json()).toEqual({ error: "Unauthorized" });
166
+ });
167
+ it("runs multiple global middleware in registration order", async () => {
168
+ const { createApp } = await import("./express.js");
169
+ const calls = [];
170
+ const mwA = (_req, _res, next) => {
171
+ calls.push("A");
172
+ next();
173
+ };
174
+ const mwB = (_req, _res, next) => {
175
+ calls.push("B");
176
+ next();
177
+ };
178
+ const server = new McpServer({ name: "t", version: "0.0.0" });
179
+ server.use(mwA);
180
+ server.use(mwB);
181
+ const httpServer = http.createServer();
182
+ const app = await createApp({ mcpServer: server, httpServer });
183
+ const { port, server: httpListening } = await listen(app);
184
+ openServer = httpListening;
185
+ await postMcp(port);
186
+ expect(calls).toEqual(["A", "B"]);
187
+ });
188
+ it("path-scoped middleware does not run on non-matching paths", async () => {
189
+ const { createApp } = await import("./express.js");
190
+ const calls = [];
191
+ const apiMw = (_req, _res, next) => {
192
+ calls.push("api");
193
+ next();
194
+ };
195
+ const server = new McpServer({ name: "t", version: "0.0.0" });
196
+ server.use("/api", apiMw);
197
+ const httpServer = http.createServer();
198
+ const app = await createApp({ mcpServer: server, httpServer });
199
+ const { port, server: httpListening } = await listen(app);
200
+ openServer = httpListening;
201
+ // Hit /mcp — the /api middleware should NOT fire
202
+ await postMcp(port);
203
+ expect(calls).toEqual([]);
204
+ });
205
+ it("supports Express Router via custom middleware", async () => {
206
+ const { createApp } = await import("./express.js");
207
+ const { Router } = await import("express");
208
+ const router = Router();
209
+ router.get("/health", (_req, res) => {
210
+ res.json({ status: "ok" });
211
+ });
212
+ const server = new McpServer({ name: "t", version: "0.0.0" });
213
+ server.use(router);
214
+ const httpServer = http.createServer();
215
+ const app = await createApp({ mcpServer: server, httpServer });
216
+ const { port, server: httpListening } = await listen(app);
217
+ openServer = httpListening;
218
+ const res = await fetch(`http://localhost:${port}/health`);
219
+ expect(res.status).toBe(200);
220
+ expect(await res.json()).toEqual({ status: "ok" });
221
+ });
222
+ it("supports path-prefixed Router", async () => {
223
+ const { createApp } = await import("./express.js");
224
+ const { Router } = await import("express");
225
+ const router = Router();
226
+ router.get("/data", (_req, res) => {
227
+ res.json({ value: 42 });
228
+ });
229
+ const server = new McpServer({ name: "t", version: "0.0.0" });
230
+ server.use("/api", router);
231
+ const httpServer = http.createServer();
232
+ const app = await createApp({ mcpServer: server, httpServer });
233
+ const { port, server: httpListening } = await listen(app);
234
+ openServer = httpListening;
235
+ const res = await fetch(`http://localhost:${port}/api/data`);
236
+ expect(res.status).toBe(200);
237
+ expect(await res.json()).toEqual({ value: 42 });
238
+ });
239
+ it("server survives middleware errors without crashing", async () => {
240
+ const { createApp } = await import("./express.js");
241
+ const throwing = () => {
242
+ throw new Error("boom");
243
+ };
244
+ const server = new McpServer({ name: "t", version: "0.0.0" });
245
+ server.use("/explode", throwing);
246
+ const httpServer = http.createServer();
247
+ const app = await createApp({ mcpServer: server, httpServer });
248
+ const { port, server: httpListening } = await listen(app);
249
+ openServer = httpListening;
250
+ const res = await fetch(`http://localhost:${port}/explode`);
251
+ expect(res.status).toBe(500);
252
+ // Server process did not crash — it still accepts connections
253
+ const followUp = await fetch(`http://localhost:${port}/explode`);
254
+ expect(followUp.status).toBe(500);
255
+ });
256
+ it("returns 500 JSON-RPC error when the MCP handler throws and no error middleware is registered", async () => {
257
+ const { createApp } = await import("./express.js");
258
+ const consoleSpy = vi.spyOn(console, "error").mockImplementation(() => { });
259
+ const mcpServer = new McpServer({ name: "t", version: "0.0.0" });
260
+ // Force the express-level error path: make connectStatelessTransport
261
+ // reject so the request handler hits its try/catch and calls next(error),
262
+ // which lands in the default /mcp error handler.
263
+ vi.spyOn(mcpServer, "connectStatelessTransport").mockRejectedValue(new Error("boom"));
264
+ const httpServer = http.createServer();
265
+ const app = await createApp({ mcpServer, httpServer });
266
+ const { port, server: httpListening } = await listen(app);
267
+ openServer = httpListening;
268
+ const res = await postMcp(port);
269
+ expect(res.status).toBe(500);
270
+ expect(await res.json()).toEqual({
271
+ jsonrpc: "2.0",
272
+ error: { code: -32603, message: "Internal server error" },
273
+ id: null,
274
+ });
275
+ expect(consoleSpy).toHaveBeenCalledWith("Error handling MCP request:", expect.any(Error));
276
+ consoleSpy.mockRestore();
277
+ });
278
+ it("invokes a custom error handler when the MCP handler throws", async () => {
279
+ const { createApp } = await import("./express.js");
280
+ const calls = [];
281
+ const errorHandler = (_err, _req, res, _next) => {
282
+ calls.push("error-handler");
283
+ res.status(503).json({ custom: true });
284
+ };
285
+ const mcpServer = new McpServer({ name: "t", version: "0.0.0" });
286
+ vi.spyOn(mcpServer, "connectStatelessTransport").mockRejectedValue(new Error("boom"));
287
+ const httpServer = http.createServer();
288
+ const app = await createApp({
289
+ mcpServer,
290
+ httpServer,
291
+ errorMiddleware: [{ handlers: [errorHandler] }],
292
+ });
293
+ const { port, server: httpListening } = await listen(app);
294
+ openServer = httpListening;
295
+ const res = await postMcp(port);
296
+ expect(calls).toEqual(["error-handler"]);
297
+ expect(res.status).toBe(503);
298
+ expect(await res.json()).toEqual({ custom: true });
299
+ });
300
+ it("invokes a path-scoped error handler only for matching routes", async () => {
301
+ const { createApp } = await import("./express.js");
302
+ const calls = [];
303
+ const mcpErrorHandler = (_err, _req, res, _next) => {
304
+ calls.push("mcp-error-handler");
305
+ res.status(503).json({ from: "mcp-error-handler" });
306
+ };
307
+ const throwingApiRoute = (_req, _res, next) => {
308
+ next(new Error("api error"));
309
+ };
310
+ const mcpServer = new McpServer({ name: "t", version: "0.0.0" });
311
+ vi.spyOn(mcpServer, "connectStatelessTransport").mockRejectedValue(new Error("boom"));
312
+ mcpServer.use("/api/test", throwingApiRoute);
313
+ const httpServer = http.createServer();
314
+ const app = await createApp({
315
+ mcpServer,
316
+ httpServer,
317
+ errorMiddleware: [{ path: "/mcp", handlers: [mcpErrorHandler] }],
318
+ });
319
+ const { port, server: httpListening } = await listen(app);
320
+ openServer = httpListening;
321
+ const mcpRes = await postMcp(port);
322
+ expect(calls).toEqual(["mcp-error-handler"]);
323
+ expect(mcpRes.status).toBe(503);
324
+ expect(await mcpRes.json()).toEqual({ from: "mcp-error-handler" });
325
+ const consoleSpy = vi.spyOn(console, "error").mockImplementation(() => { });
326
+ const apiRes = await postApi(port);
327
+ expect(calls).toEqual(["mcp-error-handler"]);
328
+ expect(apiRes.status).toBe(500);
329
+ consoleSpy.mockRestore();
330
+ });
331
+ it("handles concurrent /mcp requests without 'Already connected to a transport'", async () => {
332
+ const { createApp } = await import("./express.js");
333
+ const mcpServer = new McpServer({
334
+ name: "concurrent-test",
335
+ version: "0.0.0",
336
+ });
337
+ // Slow tool: keeps the underlying transport bound long enough to overlap
338
+ // with concurrent requests, exposing the shared-McpServer race.
339
+ mcpServer.registerTool({ name: "slow", description: "slow" }, async () => {
340
+ await new Promise((r) => setTimeout(r, 50));
341
+ return { content: [{ type: "text", text: "done" }] };
342
+ });
343
+ const consoleSpy = vi.spyOn(console, "error").mockImplementation(() => { });
344
+ const httpServer = http.createServer();
345
+ const app = await createApp({ mcpServer, httpServer });
346
+ const { port, server } = await listen(app);
347
+ openServer = server;
348
+ const callBody = (id) => JSON.stringify({
349
+ jsonrpc: "2.0",
350
+ method: "tools/call",
351
+ id,
352
+ params: { name: "slow", arguments: {} },
353
+ });
354
+ const N = 10;
355
+ const responses = await Promise.all(Array.from({ length: N }, (_, i) => fetch(`http://localhost:${port}/mcp`, {
356
+ method: "POST",
357
+ headers: {
358
+ "Content-Type": "application/json",
359
+ Accept: "application/json, text/event-stream",
360
+ },
361
+ body: callBody(i + 1),
362
+ })));
363
+ expect(responses.map((r) => r.status)).toEqual(Array(N).fill(200));
364
+ expect(consoleSpy).not.toHaveBeenCalledWith("Error handling MCP request:", expect.any(Error));
365
+ consoleSpy.mockRestore();
366
+ });
367
+ });
368
+ describe("createApp tunnel routes", () => {
369
+ it("proxies POST /__skybridge/tunnel to the cli control server in dev mode", async () => {
370
+ // Stand up a fake control listener that returns a known JSON body.
371
+ const control = http.createServer((_req, res) => {
372
+ res.writeHead(200, { "Content-Type": "application/json" });
373
+ res.end('{"status":"idle"}');
374
+ });
375
+ await new Promise((resolve) => control.listen(0, "127.0.0.1", resolve));
376
+ const controlAddr = control.address();
377
+ if (typeof controlAddr === "string" || controlAddr === null) {
378
+ control.close();
379
+ throw new Error("control server has no address");
380
+ }
381
+ const controlPort = controlAddr.port;
382
+ const prev = process.env.__TUNNEL_CONTROL_PORT;
383
+ process.env.__TUNNEL_CONTROL_PORT = String(controlPort);
384
+ try {
385
+ const { createApp } = await import("./express.js");
386
+ const mcpServer = new McpServer({ name: "t", version: "0.0.0" });
387
+ const httpServer = http.createServer();
388
+ const app = await createApp({ mcpServer, httpServer });
389
+ const { port, server } = await listen(app);
390
+ openServer = server;
391
+ const res = await fetch(`http://localhost:${port}/__skybridge/tunnel`, {
392
+ method: "POST",
393
+ });
394
+ expect(res.status).toBe(200);
395
+ expect(await res.json()).toEqual({ status: "idle" });
396
+ }
397
+ finally {
398
+ if (prev === undefined) {
399
+ delete process.env.__TUNNEL_CONTROL_PORT;
400
+ }
401
+ else {
402
+ process.env.__TUNNEL_CONTROL_PORT = prev;
403
+ }
404
+ await new Promise((resolve) => control.close(() => resolve()));
405
+ }
406
+ });
407
+ it("does not expose /__skybridge/tunnel in production mode", async () => {
408
+ const prevEnv = process.env.NODE_ENV;
409
+ process.env.NODE_ENV = "production";
410
+ try {
411
+ vi.resetModules();
412
+ const { createApp } = await import("./express.js");
413
+ const { McpServer: ReloadedMcpServer } = await import("./server.js");
414
+ const mcpServer = new ReloadedMcpServer({ name: "t", version: "0.0.0" });
415
+ const httpServer = http.createServer();
416
+ const app = await createApp({ mcpServer, httpServer });
417
+ const { port, server } = await listen(app);
418
+ openServer = server;
419
+ const res = await fetch(`http://localhost:${port}/__skybridge/tunnel`, {
420
+ method: "POST",
421
+ });
422
+ expect(res.status).toBe(404);
423
+ }
424
+ finally {
425
+ process.env.NODE_ENV = prevEnv;
426
+ vi.resetModules();
427
+ }
428
+ });
429
+ });
430
+ //# sourceMappingURL=express.test.js.map