skybridge 0.0.0-dev.b27efff → 0.0.0-dev.b28ce95

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 (269) hide show
  1. package/README.md +123 -124
  2. package/dist/cli/build-helpers.d.ts +7 -0
  3. package/dist/cli/build-helpers.js +82 -0
  4. package/dist/cli/build-helpers.js.map +1 -0
  5. package/dist/cli/build-helpers.test.d.ts +1 -0
  6. package/dist/cli/build-helpers.test.js +64 -0
  7. package/dist/cli/build-helpers.test.js.map +1 -0
  8. package/dist/cli/detect-port.d.ts +2 -2
  9. package/dist/cli/detect-port.js +9 -20
  10. package/dist/cli/detect-port.js.map +1 -1
  11. package/dist/cli/header.js.map +1 -1
  12. package/dist/cli/resolve-views-dir.d.ts +1 -0
  13. package/dist/cli/resolve-views-dir.js +17 -0
  14. package/dist/cli/resolve-views-dir.js.map +1 -0
  15. package/dist/cli/run-command.js.map +1 -1
  16. package/dist/cli/telemetry.js.map +1 -1
  17. package/dist/cli/tunnel-control-server.d.ts +9 -0
  18. package/dist/cli/tunnel-control-server.js +31 -0
  19. package/dist/cli/tunnel-control-server.js.map +1 -0
  20. package/dist/cli/tunnel-control-server.test.d.ts +1 -0
  21. package/dist/cli/tunnel-control-server.test.js +39 -0
  22. package/dist/cli/tunnel-control-server.test.js.map +1 -0
  23. package/dist/cli/tunnel-handler.d.ts +3 -0
  24. package/dist/cli/tunnel-handler.js +48 -0
  25. package/dist/cli/tunnel-handler.js.map +1 -0
  26. package/dist/cli/tunnel-handler.test.d.ts +1 -0
  27. package/dist/cli/tunnel-handler.test.js +105 -0
  28. package/dist/cli/tunnel-handler.test.js.map +1 -0
  29. package/dist/cli/tunnel.d.ts +57 -0
  30. package/dist/cli/tunnel.js +154 -0
  31. package/dist/cli/tunnel.js.map +1 -0
  32. package/dist/cli/tunnel.test.d.ts +1 -0
  33. package/dist/cli/tunnel.test.js +190 -0
  34. package/dist/cli/tunnel.test.js.map +1 -0
  35. package/dist/cli/types.js.map +1 -1
  36. package/dist/cli/use-execute-steps.js.map +1 -1
  37. package/dist/cli/use-messages.js.map +1 -1
  38. package/dist/cli/use-nodemon.js +11 -2
  39. package/dist/cli/use-nodemon.js.map +1 -1
  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 +1 -1
  44. package/dist/cli/use-tunnel.js +102 -68
  45. package/dist/cli/use-tunnel.js.map +1 -1
  46. package/dist/cli/use-typescript-check.d.ts +1 -0
  47. package/dist/cli/use-typescript-check.js +42 -7
  48. package/dist/cli/use-typescript-check.js.map +1 -1
  49. package/dist/commands/build.d.ts +0 -1
  50. package/dist/commands/build.js +41 -18
  51. package/dist/commands/build.js.map +1 -1
  52. package/dist/commands/create.d.ts +9 -0
  53. package/dist/commands/create.js +30 -0
  54. package/dist/commands/create.js.map +1 -0
  55. package/dist/commands/dev.d.ts +1 -0
  56. package/dist/commands/dev.js +49 -2
  57. package/dist/commands/dev.js.map +1 -1
  58. package/dist/commands/start.js +7 -1
  59. package/dist/commands/start.js.map +1 -1
  60. package/dist/commands/telemetry/disable.js.map +1 -1
  61. package/dist/commands/telemetry/enable.js.map +1 -1
  62. package/dist/commands/telemetry/status.js.map +1 -1
  63. package/dist/server/asset-base-url-transform-plugin.d.ts +1 -0
  64. package/dist/server/asset-base-url-transform-plugin.js +17 -2
  65. package/dist/server/asset-base-url-transform-plugin.js.map +1 -1
  66. package/dist/server/asset-base-url-transform-plugin.test.js +80 -1
  67. package/dist/server/asset-base-url-transform-plugin.test.js.map +1 -1
  68. package/dist/server/auth.d.ts +20 -0
  69. package/dist/server/auth.js +28 -0
  70. package/dist/server/auth.js.map +1 -0
  71. package/dist/server/build-manifest.test.d.ts +1 -0
  72. package/dist/server/build-manifest.test.js +27 -0
  73. package/dist/server/build-manifest.test.js.map +1 -0
  74. package/dist/server/content-helpers.d.ts +40 -0
  75. package/dist/server/content-helpers.js +33 -0
  76. package/dist/server/content-helpers.js.map +1 -1
  77. package/dist/server/content-helpers.test.js.map +1 -1
  78. package/dist/server/express.d.ts +1 -5
  79. package/dist/server/express.js +31 -7
  80. package/dist/server/express.js.map +1 -1
  81. package/dist/server/express.test.js +271 -69
  82. package/dist/server/express.test.js.map +1 -1
  83. package/dist/server/file-ref.d.ts +28 -0
  84. package/dist/server/file-ref.js +27 -0
  85. package/dist/server/file-ref.js.map +1 -0
  86. package/dist/server/index.d.ts +4 -2
  87. package/dist/server/index.js +3 -1
  88. package/dist/server/index.js.map +1 -1
  89. package/dist/server/inferUtilityTypes.js.map +1 -1
  90. package/dist/server/metric.js.map +1 -1
  91. package/dist/server/middleware.d.ts +16 -3
  92. package/dist/server/middleware.js.map +1 -1
  93. package/dist/server/middleware.test-d.js.map +1 -1
  94. package/dist/server/middleware.test.js.map +1 -1
  95. package/dist/server/server.d.ts +235 -8
  96. package/dist/server/server.js +306 -74
  97. package/dist/server/server.js.map +1 -1
  98. package/dist/server/templateHelper.d.ts +0 -2
  99. package/dist/server/templateHelper.js +3 -22
  100. package/dist/server/templateHelper.js.map +1 -1
  101. package/dist/server/templates.generated.d.ts +4 -0
  102. package/dist/server/templates.generated.js +47 -0
  103. package/dist/server/templates.generated.js.map +1 -0
  104. package/dist/server/tunnel-proxy-router.d.ts +7 -0
  105. package/dist/server/tunnel-proxy-router.js +110 -0
  106. package/dist/server/tunnel-proxy-router.js.map +1 -0
  107. package/dist/server/tunnel-proxy-router.test.d.ts +1 -0
  108. package/dist/server/tunnel-proxy-router.test.js +229 -0
  109. package/dist/server/tunnel-proxy-router.test.js.map +1 -0
  110. package/dist/server/view-resource-resolution.test.d.ts +6 -0
  111. package/dist/server/view-resource-resolution.test.js +88 -0
  112. package/dist/server/view-resource-resolution.test.js.map +1 -0
  113. package/dist/server/viewsDevServer.js.map +1 -1
  114. package/dist/test/utils.js.map +1 -1
  115. package/dist/test/view.test.js +45 -0
  116. package/dist/test/view.test.js.map +1 -1
  117. package/dist/version.js +1 -3
  118. package/dist/version.js.map +1 -1
  119. package/dist/web/bridges/apps-sdk/adaptor.d.ts +7 -2
  120. package/dist/web/bridges/apps-sdk/adaptor.js +27 -3
  121. package/dist/web/bridges/apps-sdk/adaptor.js.map +1 -1
  122. package/dist/web/bridges/apps-sdk/bridge.d.ts +1 -0
  123. package/dist/web/bridges/apps-sdk/bridge.js +1 -0
  124. package/dist/web/bridges/apps-sdk/bridge.js.map +1 -1
  125. package/dist/web/bridges/apps-sdk/index.js.map +1 -1
  126. package/dist/web/bridges/apps-sdk/types.d.ts +8 -1
  127. package/dist/web/bridges/apps-sdk/types.js.map +1 -1
  128. package/dist/web/bridges/apps-sdk/use-apps-sdk-context.d.ts +11 -0
  129. package/dist/web/bridges/apps-sdk/use-apps-sdk-context.js +11 -0
  130. package/dist/web/bridges/apps-sdk/use-apps-sdk-context.js.map +1 -1
  131. package/dist/web/bridges/get-adaptor.d.ts +7 -0
  132. package/dist/web/bridges/get-adaptor.js +7 -0
  133. package/dist/web/bridges/get-adaptor.js.map +1 -1
  134. package/dist/web/bridges/index.js.map +1 -1
  135. package/dist/web/bridges/mcp-app/adaptor.d.ts +7 -2
  136. package/dist/web/bridges/mcp-app/adaptor.js +21 -6
  137. package/dist/web/bridges/mcp-app/adaptor.js.map +1 -1
  138. package/dist/web/bridges/mcp-app/bridge.d.ts +4 -2
  139. package/dist/web/bridges/mcp-app/bridge.js +23 -1
  140. package/dist/web/bridges/mcp-app/bridge.js.map +1 -1
  141. package/dist/web/bridges/mcp-app/index.js.map +1 -1
  142. package/dist/web/bridges/mcp-app/types.js.map +1 -1
  143. package/dist/web/bridges/mcp-app/use-mcp-app-context.d.ts +12 -0
  144. package/dist/web/bridges/mcp-app/use-mcp-app-context.js +12 -0
  145. package/dist/web/bridges/mcp-app/use-mcp-app-context.js.map +1 -1
  146. package/dist/web/bridges/mcp-app/use-mcp-app-context.test.js.map +1 -1
  147. package/dist/web/bridges/mcp-app/view-tools.test.d.ts +1 -0
  148. package/dist/web/bridges/mcp-app/view-tools.test.js +144 -0
  149. package/dist/web/bridges/mcp-app/view-tools.test.js.map +1 -0
  150. package/dist/web/bridges/types.d.ts +98 -3
  151. package/dist/web/bridges/types.js.map +1 -1
  152. package/dist/web/bridges/use-host-context.d.ts +5 -0
  153. package/dist/web/bridges/use-host-context.js +5 -0
  154. package/dist/web/bridges/use-host-context.js.map +1 -1
  155. package/dist/web/components/modal-provider.js.map +1 -1
  156. package/dist/web/create-store.d.ts +26 -0
  157. package/dist/web/create-store.js +26 -0
  158. package/dist/web/create-store.js.map +1 -1
  159. package/dist/web/create-store.test.js.map +1 -1
  160. package/dist/web/data-llm.d.ts +33 -0
  161. package/dist/web/data-llm.js +28 -0
  162. package/dist/web/data-llm.js.map +1 -1
  163. package/dist/web/data-llm.test.js.map +1 -1
  164. package/dist/web/generate-helpers.d.ts +2 -0
  165. package/dist/web/generate-helpers.js +2 -0
  166. package/dist/web/generate-helpers.js.map +1 -1
  167. package/dist/web/generate-helpers.test-d.js +4 -2
  168. package/dist/web/generate-helpers.test-d.js.map +1 -1
  169. package/dist/web/generate-helpers.test.js.map +1 -1
  170. package/dist/web/helpers/state.js.map +1 -1
  171. package/dist/web/helpers/state.test.js.map +1 -1
  172. package/dist/web/hooks/index.d.ts +4 -0
  173. package/dist/web/hooks/index.js +4 -0
  174. package/dist/web/hooks/index.js.map +1 -1
  175. package/dist/web/hooks/test/utils.d.ts +6 -2
  176. package/dist/web/hooks/test/utils.js +13 -2
  177. package/dist/web/hooks/test/utils.js.map +1 -1
  178. package/dist/web/hooks/use-call-tool.d.ts +45 -0
  179. package/dist/web/hooks/use-call-tool.js +28 -0
  180. package/dist/web/hooks/use-call-tool.js.map +1 -1
  181. package/dist/web/hooks/use-call-tool.test-d.js.map +1 -1
  182. package/dist/web/hooks/use-call-tool.test.js +27 -6
  183. package/dist/web/hooks/use-call-tool.test.js.map +1 -1
  184. package/dist/web/hooks/use-display-mode.d.ts +20 -0
  185. package/dist/web/hooks/use-display-mode.js +20 -0
  186. package/dist/web/hooks/use-display-mode.js.map +1 -1
  187. package/dist/web/hooks/use-display-mode.test-d.js.map +1 -1
  188. package/dist/web/hooks/use-display-mode.test.js.map +1 -1
  189. package/dist/web/hooks/use-download.d.ts +5 -0
  190. package/dist/web/hooks/use-download.js +8 -0
  191. package/dist/web/hooks/use-download.js.map +1 -0
  192. package/dist/web/hooks/use-download.test.d.ts +1 -0
  193. package/dist/web/hooks/use-download.test.js +95 -0
  194. package/dist/web/hooks/use-download.test.js.map +1 -0
  195. package/dist/web/hooks/use-files.d.ts +32 -0
  196. package/dist/web/hooks/use-files.js +32 -0
  197. package/dist/web/hooks/use-files.js.map +1 -1
  198. package/dist/web/hooks/use-files.test.js.map +1 -1
  199. package/dist/web/hooks/use-layout.d.ts +2 -0
  200. package/dist/web/hooks/use-layout.js +2 -0
  201. package/dist/web/hooks/use-layout.js.map +1 -1
  202. package/dist/web/hooks/use-layout.test.js.map +1 -1
  203. package/dist/web/hooks/use-open-external.d.ts +17 -0
  204. package/dist/web/hooks/use-open-external.js +16 -0
  205. package/dist/web/hooks/use-open-external.js.map +1 -1
  206. package/dist/web/hooks/use-open-external.test.js.map +1 -1
  207. package/dist/web/hooks/use-register-view-tool.d.ts +38 -0
  208. package/dist/web/hooks/use-register-view-tool.js +50 -0
  209. package/dist/web/hooks/use-register-view-tool.js.map +1 -0
  210. package/dist/web/hooks/use-request-close.d.ts +16 -0
  211. package/dist/web/hooks/use-request-close.js +21 -0
  212. package/dist/web/hooks/use-request-close.js.map +1 -0
  213. package/dist/web/hooks/use-request-close.test.d.ts +1 -0
  214. package/dist/web/hooks/use-request-close.test.js +52 -0
  215. package/dist/web/hooks/use-request-close.test.js.map +1 -0
  216. package/dist/web/hooks/use-request-modal.d.ts +16 -1
  217. package/dist/web/hooks/use-request-modal.js +16 -1
  218. package/dist/web/hooks/use-request-modal.js.map +1 -1
  219. package/dist/web/hooks/use-request-modal.test.js.map +1 -1
  220. package/dist/web/hooks/use-request-size.d.ts +20 -0
  221. package/dist/web/hooks/use-request-size.js +24 -0
  222. package/dist/web/hooks/use-request-size.js.map +1 -0
  223. package/dist/web/hooks/use-request-size.test.d.ts +1 -0
  224. package/dist/web/hooks/use-request-size.test.js +65 -0
  225. package/dist/web/hooks/use-request-size.test.js.map +1 -0
  226. package/dist/web/hooks/use-send-follow-up-message.d.ts +19 -1
  227. package/dist/web/hooks/use-send-follow-up-message.js +19 -2
  228. package/dist/web/hooks/use-send-follow-up-message.js.map +1 -1
  229. package/dist/web/hooks/use-set-open-in-app-url.d.ts +17 -0
  230. package/dist/web/hooks/use-set-open-in-app-url.js +17 -0
  231. package/dist/web/hooks/use-set-open-in-app-url.js.map +1 -1
  232. package/dist/web/hooks/use-set-open-in-app-url.test.js.map +1 -1
  233. package/dist/web/hooks/use-tool-info.d.ts +53 -2
  234. package/dist/web/hooks/use-tool-info.js +30 -7
  235. package/dist/web/hooks/use-tool-info.js.map +1 -1
  236. package/dist/web/hooks/use-tool-info.test-d.js +11 -29
  237. package/dist/web/hooks/use-tool-info.test-d.js.map +1 -1
  238. package/dist/web/hooks/use-tool-info.test.js +5 -5
  239. package/dist/web/hooks/use-tool-info.test.js.map +1 -1
  240. package/dist/web/hooks/use-user.d.ts +2 -0
  241. package/dist/web/hooks/use-user.js +2 -0
  242. package/dist/web/hooks/use-user.js.map +1 -1
  243. package/dist/web/hooks/use-user.test.js.map +1 -1
  244. package/dist/web/hooks/use-view-state.d.ts +21 -0
  245. package/dist/web/hooks/use-view-state.js.map +1 -1
  246. package/dist/web/hooks/use-view-state.test.js.map +1 -1
  247. package/dist/web/index.js.map +1 -1
  248. package/dist/web/mount-view.d.ts +19 -0
  249. package/dist/web/mount-view.js +19 -0
  250. package/dist/web/mount-view.js.map +1 -1
  251. package/dist/web/plugin/data-llm.test.js.map +1 -1
  252. package/dist/web/plugin/plugin.d.ts +28 -0
  253. package/dist/web/plugin/plugin.js +63 -15
  254. package/dist/web/plugin/plugin.js.map +1 -1
  255. package/dist/web/plugin/scan-views.d.ts +8 -0
  256. package/dist/web/plugin/scan-views.js +26 -8
  257. package/dist/web/plugin/scan-views.js.map +1 -1
  258. package/dist/web/plugin/scan-views.test.js +33 -1
  259. package/dist/web/plugin/scan-views.test.js.map +1 -1
  260. package/dist/web/plugin/transform-data-llm.js.map +1 -1
  261. package/dist/web/plugin/transform-data-llm.test.js.map +1 -1
  262. package/dist/web/plugin/validate-view.js.map +1 -1
  263. package/dist/web/plugin/validate-view.test.js.map +1 -1
  264. package/dist/web/proxy.js.map +1 -1
  265. package/dist/web/types.d.ts +4 -0
  266. package/dist/web/types.js.map +1 -1
  267. package/package.json +16 -8
  268. package/dist/server/templates/development.hbs +0 -12
  269. package/dist/server/templates/production.hbs +0 -6
@@ -5,6 +5,7 @@ import path from "node:path";
5
5
  import { Server as SdkServer, } from "@modelcontextprotocol/sdk/server/index.js";
6
6
  import { McpServer as McpServerBase } from "@modelcontextprotocol/sdk/server/mcp.js";
7
7
  import { mergeWith, union } from "es-toolkit";
8
+ import express, {} from "express";
8
9
  import { createApp } from "./express.js";
9
10
  import { createMiddlewareEntry } from "./metric.js";
10
11
  import { buildMiddlewareChain, getHandlerMaps } from "./middleware.js";
@@ -16,6 +17,21 @@ const mergeWithUnion = (target, source) => {
16
17
  }
17
18
  });
18
19
  };
20
+ /**
21
+ * Drop the query string from a `ui://` view URI, leaving the bare path. The
22
+ * `?v=` cache key is the only query we append, so a plain split is enough and
23
+ * sidesteps `URL` normalization quirks on the non-special `ui:` scheme.
24
+ */
25
+ function stripQuery(uri) {
26
+ const queryIndex = uri.indexOf("?");
27
+ return queryIndex === -1 ? uri : uri.slice(0, queryIndex);
28
+ }
29
+ /**
30
+ * Coerce a tool handler's return value into an MCP `content` array. Strings
31
+ * become a single `TextContent`; a single block is wrapped in an array;
32
+ * `undefined` produces `[]`. Mostly used internally — exported so consumers
33
+ * who build content lazily can apply the same normalization.
34
+ */
19
35
  export function normalizeContent(content) {
20
36
  if (content === undefined) {
21
37
  return [];
@@ -29,31 +45,106 @@ export function normalizeContent(content) {
29
45
  return [content];
30
46
  }
31
47
  const McpServerBaseOmitted = McpServerBase;
48
+ /**
49
+ * The Skybridge server. Extends the MCP SDK's `McpServer` with a typed tool
50
+ * registry, view resources, an embedded Express app, and protocol-level
51
+ * middleware. Construct it with the same `Implementation` info you would pass
52
+ * to the SDK, chain {@link McpServer.registerTool} calls to declare tools,
53
+ * then call {@link McpServer.run} to start the HTTP server.
54
+ *
55
+ * The `TTools` generic accumulates each registered tool's input/output/meta
56
+ * shape, so `typeof server` carries enough information for view-side helpers
57
+ * like {@link generateHelpers} to produce fully-typed hooks.
58
+ *
59
+ * @typeParam TTools - Accumulated tool registry. Filled in by `registerTool`
60
+ * chaining; you almost never set this manually.
61
+ *
62
+ * @example
63
+ * ```ts
64
+ * const server = new McpServer({ name: "my-app", version: "1.0.0" }, {})
65
+ * .registerTool({
66
+ * name: "search",
67
+ * inputSchema: { query: z.string() },
68
+ * view: { component: "search" },
69
+ * }, async ({ query }) => ({ content: `Results for ${query}` }));
70
+ *
71
+ * await server.run();
72
+ * export type AppType = typeof server;
73
+ * ```
74
+ *
75
+ * @see https://docs.skybridge.tech/api-reference/mcp-server
76
+ */
77
+ // Side channel populated by `dist/__entry.js` before user code is imported.
78
+ // Set at module scope rather than passed through the constructor because the
79
+ // wrapper has the manifest before the user's `new McpServer(...)` runs, and
80
+ // threading it through every call site (including user templates) is exactly
81
+ // the boilerplate this design is trying to hide.
82
+ let pendingBuildManifest = null;
83
+ /**
84
+ * Prime the build-time Vite manifest before user code constructs its
85
+ * `McpServer`. Called from the generated `dist/__entry.js`; not part of the
86
+ * user-facing API.
87
+ *
88
+ * @internal
89
+ */
90
+ export function __setBuildManifest(manifest) {
91
+ pendingBuildManifest = manifest;
92
+ }
32
93
  export class McpServer extends McpServerBaseOmitted {
94
+ /**
95
+ * The underlying Express app. Use this to extend the HTTP server with
96
+ * custom routes, middleware, or settings — e.g.
97
+ * `server.express.get("/health", ...)`.
98
+ *
99
+ * `express.json()` is pre-applied — tune it via the constructor's third
100
+ * argument, e.g. `new McpServer(info, {}, { json: { limit: "10mb" } })`.
101
+ * Register your handlers before `run()`;
102
+ * after `run()`, dev-mode middleware, the `/mcp` route, and the default
103
+ * error handler are appended in that order.
104
+ *
105
+ * Note: Alpic Cloud only routes traffic to `/mcp` — custom routes work
106
+ * locally and on self-hosted deployments.
107
+ */
33
108
  express;
34
- customMiddleware = [];
35
109
  customErrorMiddleware = [];
36
110
  mcpMiddlewareEntries = [];
37
111
  mcpMiddlewareApplied = false;
38
112
  claimedViews = new Map();
113
+ viewMetaBuilders = new Map();
114
+ /**
115
+ * Maps a view resource's query-less path to its canonical registered URI
116
+ * (the one carrying the `?v=` cache key). Lets `resources/read` resolve the
117
+ * underlying view no matter which version param the consumer sends, since
118
+ * the param is only a cache key, not part of the resource's identity.
119
+ */
120
+ viewUriByPath = new Map();
121
+ viteManifest = null;
39
122
  serverInfo;
40
123
  serverOptions;
41
- constructor(serverInfo, options) {
124
+ constructor(serverInfo, options, skybridgeOptions) {
42
125
  super(serverInfo, options);
43
126
  this.serverInfo = serverInfo;
44
127
  this.serverOptions = options;
128
+ this.express = express();
129
+ this.express.use(express.json(skybridgeOptions?.json));
130
+ // Pick up the manifest if `dist/__entry.js` primed it before importing
131
+ // user code. Consume-once: clear after the first construction so a
132
+ // subsequent test that doesn't prime can't inherit stale state.
133
+ // Explicit `setViteManifest` calls still win because they happen after
134
+ // construction.
135
+ if (pendingBuildManifest) {
136
+ this.setViteManifest(pendingBuildManifest);
137
+ pendingBuildManifest = null;
138
+ }
45
139
  }
46
140
  use(pathOrHandler, ...handlers) {
141
+ // Branching is load-bearing: Express's `app.use` overloads can't be
142
+ // resolved against a `string | RequestHandler` union, so we narrow.
47
143
  if (typeof pathOrHandler === "string") {
48
- this.customMiddleware.push({
49
- path: pathOrHandler,
50
- handlers,
51
- });
144
+ this.express.use(pathOrHandler, ...handlers);
52
145
  }
53
146
  else {
54
- this.customMiddleware.push({
55
- handlers: [pathOrHandler, ...handlers],
56
- });
147
+ this.express.use(pathOrHandler, ...handlers);
57
148
  }
58
149
  return this;
59
150
  }
@@ -97,10 +188,69 @@ export class McpServer extends McpServerBaseOmitted {
97
188
  return;
98
189
  }
99
190
  this.mcpMiddlewareApplied = true;
191
+ // Surface view-resource _meta on `resources/list` (per ext-apps spec:
192
+ // hosts/checkers read CSP & domain at list time before fetching content).
193
+ const viewListMetaEntry = {
194
+ filter: "resources/list",
195
+ handler: async (_req, extra, next) => {
196
+ const result = (await next());
197
+ for (const resource of result.resources) {
198
+ const builder = this.viewMetaBuilders.get(resource.uri);
199
+ if (!builder) {
200
+ continue;
201
+ }
202
+ const meta = builder(extra);
203
+ resource._meta = {
204
+ ...(resource._meta ?? {}),
205
+ ...meta,
206
+ };
207
+ }
208
+ return result;
209
+ },
210
+ };
211
+ // Resolve a view's `resources/read` by its query-less path so the
212
+ // underlying asset is served no matter the `?v=` value (stale cache key,
213
+ // no param, etc.). The version param is a cache-busting hint for external
214
+ // consumers; it must not gate resolution. We rewrite the lookup URI to the
215
+ // canonical registered one, then restore the requested URI on the response
216
+ // so the consumer-facing URI is never rewritten.
217
+ const viewReadResolveEntry = {
218
+ filter: "resources/read",
219
+ handler: async (req, _extra, next) => {
220
+ const requested = req.params.uri;
221
+ if (typeof requested !== "string") {
222
+ return next();
223
+ }
224
+ const path = stripQuery(requested);
225
+ const canonical = this.viewUriByPath.get(path);
226
+ if (!canonical) {
227
+ return next();
228
+ }
229
+ req.params.uri = canonical;
230
+ try {
231
+ const result = (await next());
232
+ for (const content of result.contents ?? []) {
233
+ if (typeof content.uri === "string" &&
234
+ stripQuery(content.uri) === path) {
235
+ content.uri = requested;
236
+ }
237
+ }
238
+ return result;
239
+ }
240
+ finally {
241
+ // Restore the shared request params so middleware outer to us never
242
+ // observes the rewritten lookup URI after next() unwinds.
243
+ req.params.uri = requested;
244
+ }
245
+ },
246
+ };
100
247
  const monitoringEntry = createMiddlewareEntry();
101
- const entries = monitoringEntry
102
- ? [monitoringEntry, ...this.mcpMiddlewareEntries]
103
- : this.mcpMiddlewareEntries;
248
+ const entries = [
249
+ ...(monitoringEntry ? [monitoringEntry] : []),
250
+ viewListMetaEntry,
251
+ viewReadResolveEntry,
252
+ ...this.mcpMiddlewareEntries,
253
+ ];
104
254
  if (entries.length === 0) {
105
255
  return;
106
256
  }
@@ -115,6 +265,14 @@ export class McpServer extends McpServerBaseOmitted {
115
265
  instrumentMap(requestHandlers, false);
116
266
  instrumentMap(notificationHandlers, true);
117
267
  }
268
+ /**
269
+ * Connect to an MCP transport (override of the SDK's `connect`). Use this
270
+ * when you're embedding Skybridge in a host that already manages its own
271
+ * transport (e.g. stdio for desktop apps); for HTTP, prefer {@link McpServer.run}
272
+ * which sets the transport up for you. Locks in any middleware registered
273
+ * via {@link McpServer.mcpMiddleware} — further calls to that method will
274
+ * throw afterwards.
275
+ */
118
276
  async connect(transport) {
119
277
  this.applyMcpMiddleware();
120
278
  return McpServerBase.prototype.connect.call(this, transport);
@@ -138,28 +296,71 @@ export class McpServer extends McpServerBaseOmitted {
138
296
  target._notificationHandlers = notificationHandlers;
139
297
  await fresh.connect(transport);
140
298
  }
299
+ /**
300
+ * Start the HTTP server. Listens on `process.env.__PORT` (default `3000`),
301
+ * mounts the `/mcp` route, applies any custom Express middleware registered
302
+ * via {@link McpServer.use} / {@link McpServer.useOnError}, and locks in
303
+ * any MCP middleware registered via {@link McpServer.mcpMiddleware}.
304
+ *
305
+ * On Cloudflare Workers / workerd, returns an object exposing `fetch` so
306
+ * the runtime can bridge incoming requests to the Node HTTP server. On
307
+ * Vercel (`VERCEL === "1"`), returns the Express app directly so the
308
+ * serverless function entry can call it as a `(req, res)` handler. On
309
+ * Node, returns `undefined` once listening.
310
+ */
141
311
  async run() {
142
312
  this.applyMcpMiddleware();
143
- const httpServer = http.createServer();
144
- if (!this.express) {
145
- this.express = await createApp({
313
+ if (process.env.VERCEL === "1") {
314
+ // createApp only reads httpServer inside its dev-only branch
315
+ // (viewsDevServer); under VERCEL=1 + NODE_ENV=production it's a
316
+ // bare object passed to satisfy the required parameter.
317
+ const httpServer = http.createServer();
318
+ await createApp({
146
319
  mcpServer: this,
147
320
  httpServer,
148
- customMiddleware: this.customMiddleware,
149
321
  errorMiddleware: this.customErrorMiddleware,
150
322
  });
323
+ return this.express;
151
324
  }
325
+ const httpServer = http.createServer();
326
+ await createApp({
327
+ mcpServer: this,
328
+ httpServer,
329
+ errorMiddleware: this.customErrorMiddleware,
330
+ });
152
331
  httpServer.on("request", this.express);
153
- return new Promise((resolve, reject) => {
332
+ const port = parseInt(process.env.__PORT ?? "3000", 10);
333
+ await new Promise((resolve, reject) => {
154
334
  httpServer.on("error", (error) => {
155
335
  console.error("Failed to start server:", error);
156
336
  reject(error);
157
337
  });
158
- const port = parseInt(process.env.__PORT ?? "3000", 10);
159
338
  httpServer.listen(port, () => {
160
339
  resolve();
161
340
  });
162
341
  });
342
+ // On workerd, bridge the Node http server to a Workers fetch handler.
343
+ // The specifier is held in a variable to sidestep tsc's module resolution
344
+ // (`cloudflare:node` only exists under wrangler/workerd).
345
+ if (typeof navigator !== "undefined" &&
346
+ navigator.userAgent === "Cloudflare-Workers") {
347
+ const cloudflareNode = "cloudflare:node";
348
+ const { httpServerHandler } = await import(cloudflareNode);
349
+ return httpServerHandler({ port });
350
+ }
351
+ const shutdown = () => {
352
+ // Drop both handlers so a second signal falls through to Node's default
353
+ // (force-quit on a second Ctrl+C while drain is hanging).
354
+ process.off("SIGTERM", shutdown);
355
+ process.off("SIGINT", shutdown);
356
+ httpServer.close(() => process.exit(0));
357
+ // Force exit if connections don't drain in time so the port is still
358
+ // released promptly (e.g. for nodemon restarts).
359
+ setTimeout(() => process.exit(0), 3000).unref();
360
+ };
361
+ process.on("SIGTERM", shutdown);
362
+ process.on("SIGINT", shutdown);
363
+ return undefined;
163
364
  }
164
365
  enforceOneToolPerView(component, toolName) {
165
366
  const existingTool = this.claimedViews.get(component);
@@ -168,6 +369,57 @@ export class McpServer extends McpServerBaseOmitted {
168
369
  }
169
370
  this.claimedViews.set(component, toolName);
170
371
  }
372
+ resolveViewRequestContext(extra) {
373
+ const isProduction = process.env.NODE_ENV === "production";
374
+ const headers = extra?.requestInfo?.headers || {};
375
+ const header = (key) => {
376
+ const val = headers[key];
377
+ return Array.isArray(val) ? val[0] : val;
378
+ };
379
+ const isClaude = header("user-agent") === "Claude-User";
380
+ let serverUrl;
381
+ const forwardedHost = header("x-forwarded-host");
382
+ const origin = header("origin");
383
+ const host = header("host");
384
+ if (forwardedHost) {
385
+ const proto = header("x-forwarded-proto") || "https";
386
+ serverUrl = `${proto}://${forwardedHost}`;
387
+ }
388
+ else if (origin) {
389
+ serverUrl = origin;
390
+ }
391
+ else if (host) {
392
+ const proto = ["127.0.0.1:", "localhost:"].some((p) => host.startsWith(p))
393
+ ? "http"
394
+ : "https";
395
+ serverUrl = `${proto}://${host}`;
396
+ }
397
+ else {
398
+ const devPort = process.env.__PORT || "3000";
399
+ serverUrl = `http://localhost:${devPort}`;
400
+ }
401
+ const connectDomains = [serverUrl];
402
+ if (!isProduction) {
403
+ const wsUrl = new URL(serverUrl);
404
+ wsUrl.protocol = wsUrl.protocol === "https:" ? "wss:" : "ws:";
405
+ connectDomains.push(wsUrl.origin);
406
+ }
407
+ let contentMetaOverrides = {};
408
+ if (isClaude) {
409
+ const pathname = extra?.requestInfo?.url?.pathname ?? "";
410
+ const rawUrl = header("x-alpic-forwarded-url") ?? `${serverUrl}${pathname}`;
411
+ // Strip a lone trailing slash so the hash matches the connector URL
412
+ // as registered with Claude (which has no trailing slash on bare origins).
413
+ const url = rawUrl.endsWith("/") ? rawUrl.slice(0, -1) : rawUrl;
414
+ const hash = crypto
415
+ .createHash("sha256")
416
+ .update(url)
417
+ .digest("hex")
418
+ .slice(0, 32);
419
+ contentMetaOverrides = { domain: `${hash}.claudemcpcontent.com` };
420
+ }
421
+ return { serverUrl, connectDomains, contentMetaOverrides };
422
+ }
171
423
  registerViewResources(toolName, view, toolMeta) {
172
424
  const hosts = view.hosts ?? ["apps-sdk", "mcp-app"];
173
425
  // Append a content-derived version param so hosts (e.g. ChatGPT) bust
@@ -272,40 +524,25 @@ export class McpServer extends McpServerBaseOmitted {
272
524
  });
273
525
  // @ts-expect-error - For backwards compatibility with Claude current implementation of the specs
274
526
  toolMeta["ui/resourceUri"] = viewResource.uri;
275
- toolMeta.ui = { resourceUri: viewResource.uri };
527
+ toolMeta.ui = { ...toolMeta.ui, resourceUri: viewResource.uri };
276
528
  }
277
529
  }
278
530
  registerViewResource({ name, viewResource, view, }) {
279
531
  const { hostType, uri: viewUri, mimeType, buildContentMeta } = viewResource;
532
+ const buildMeta = (extra) => {
533
+ const { serverUrl, connectDomains, contentMetaOverrides } = this.resolveViewRequestContext(extra);
534
+ return buildContentMeta({
535
+ resourceDomains: [serverUrl],
536
+ connectDomains,
537
+ domain: serverUrl,
538
+ baseUriDomains: [serverUrl],
539
+ }, contentMetaOverrides);
540
+ };
541
+ this.viewMetaBuilders.set(viewUri, buildMeta);
542
+ this.viewUriByPath.set(stripQuery(viewUri), viewUri);
280
543
  this.registerResource(name, viewUri, { description: view.description }, async (uri, extra) => {
281
544
  const isProduction = process.env.NODE_ENV === "production";
282
- const isClaude = extra?.requestInfo?.headers?.["user-agent"] === "Claude-User";
283
- const headers = extra?.requestInfo?.headers || {};
284
- const header = (key) => {
285
- const val = headers[key];
286
- return Array.isArray(val) ? val[0] : val;
287
- };
288
- let serverUrl;
289
- const forwardedHost = header("x-forwarded-host");
290
- const origin = header("origin");
291
- const host = header("host");
292
- if (forwardedHost) {
293
- const proto = header("x-forwarded-proto") || "https";
294
- serverUrl = `${proto}://${forwardedHost}`;
295
- }
296
- else if (origin) {
297
- serverUrl = origin;
298
- }
299
- else if (host) {
300
- const proto = ["127.0.0.1:", "localhost:"].some((p) => host.startsWith(p))
301
- ? "http"
302
- : "https";
303
- serverUrl = `${proto}://${host}`;
304
- }
305
- else {
306
- const devPort = process.env.__PORT || "3000";
307
- serverUrl = `http://localhost:${devPort}`;
308
- }
545
+ const { serverUrl } = this.resolveViewRequestContext(extra);
309
546
  const html = isProduction
310
547
  ? templateHelper.renderProduction({
311
548
  hostType,
@@ -318,35 +555,9 @@ export class McpServer extends McpServerBaseOmitted {
318
555
  serverUrl,
319
556
  viewName: view.component,
320
557
  });
321
- const connectDomains = [serverUrl];
322
- if (!isProduction) {
323
- const wsUrl = new URL(serverUrl);
324
- wsUrl.protocol = wsUrl.protocol === "https:" ? "wss:" : "ws:";
325
- connectDomains.push(wsUrl.origin);
326
- }
327
- let contentMetaOverrides = {};
328
- if (isClaude) {
329
- const pathname = extra?.requestInfo?.url?.pathname ?? "";
330
- const rawUrl = header("x-alpic-forwarded-url") ?? `${serverUrl}${pathname}`;
331
- // Strip a lone trailing slash so the hash matches the connector URL
332
- // as registered with Claude (which has no trailing slash on bare origins).
333
- const url = rawUrl.endsWith("/") ? rawUrl.slice(0, -1) : rawUrl;
334
- const hash = crypto
335
- .createHash("sha256")
336
- .update(url)
337
- .digest("hex")
338
- .slice(0, 32);
339
- contentMetaOverrides = { domain: `${hash}.claudemcpcontent.com` };
340
- }
341
- const contentMeta = buildContentMeta({
342
- resourceDomains: [serverUrl],
343
- connectDomains,
344
- domain: serverUrl,
345
- baseUriDomains: [serverUrl],
346
- }, contentMetaOverrides);
347
558
  return {
348
559
  contents: [
349
- { uri: uri.href, mimeType, text: html, _meta: contentMeta },
560
+ { uri: uri.href, mimeType, text: html, _meta: buildMeta(extra) },
350
561
  ],
351
562
  };
352
563
  });
@@ -399,7 +610,20 @@ export class McpServer extends McpServerBaseOmitted {
399
610
  const manifest = this.readManifest();
400
611
  return manifest[key]?.file;
401
612
  }
613
+ /**
614
+ * Inject the Vite manifest as a value rather than letting `readManifest()`
615
+ * load it from disk. Required for runtimes without a usable filesystem
616
+ * (Cloudflare Workers, etc.) — the user's `skybridge build` emits the
617
+ * manifest as a JS module which the entry imports and passes here.
618
+ */
619
+ setViteManifest(manifest) {
620
+ this.viteManifest = manifest;
621
+ return this;
622
+ }
402
623
  readManifest() {
624
+ if (this.viteManifest) {
625
+ return this.viteManifest;
626
+ }
403
627
  return JSON.parse(readFileSync(path.join(process.cwd(), "dist", "assets", ".vite", "manifest.json"), "utf-8"));
404
628
  }
405
629
  registerTool(...args) {
@@ -410,8 +634,16 @@ export class McpServer extends McpServerBaseOmitted {
410
634
  }
411
635
  const config = args[0];
412
636
  const cb = args[1];
413
- const { name, view, _meta: userToolMeta, ...toolFields } = config;
637
+ const { name, view, securitySchemes, _meta: userToolMeta, ...toolFields } = config;
414
638
  const toolMeta = { ...userToolMeta };
639
+ if (securitySchemes) {
640
+ // SEP-1488 puts `securitySchemes` at the top level of the tool
641
+ // descriptor, but the SDK's `registerTool` drops unknown top-level
642
+ // fields, so the canonical spot isn't reachable without intercepting
643
+ // `tools/list`. Use the `_meta` back-compat mirror documented in the
644
+ // Apps SDK reference until SEP-1488 lands in the spec.
645
+ toolMeta.securitySchemes = securitySchemes;
646
+ }
415
647
  if (view) {
416
648
  this.enforceOneToolPerView(view.component, name);
417
649
  this.registerViewResources(name, view, toolMeta);