skybridge 0.0.0-dev.ef94389 → 0.0.0-dev.efaf459
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.
- package/README.md +123 -120
- package/dist/cli/build-helpers.d.ts +7 -0
- package/dist/cli/build-helpers.js +82 -0
- package/dist/cli/build-helpers.js.map +1 -0
- package/dist/cli/build-helpers.test.js +64 -0
- package/dist/cli/build-helpers.test.js.map +1 -0
- package/dist/cli/detect-port.d.ts +2 -2
- package/dist/cli/detect-port.js +9 -20
- package/dist/cli/detect-port.js.map +1 -1
- package/dist/cli/header.js +1 -1
- package/dist/cli/header.js.map +1 -1
- package/dist/cli/resolve-views-dir.d.ts +1 -0
- package/dist/cli/resolve-views-dir.js +17 -0
- package/dist/cli/resolve-views-dir.js.map +1 -0
- package/dist/cli/run-command.js.map +1 -1
- package/dist/cli/telemetry.js.map +1 -1
- package/dist/cli/tunnel-control-server.d.ts +9 -0
- package/dist/cli/tunnel-control-server.js +31 -0
- package/dist/cli/tunnel-control-server.js.map +1 -0
- package/dist/cli/tunnel-control-server.test.js +39 -0
- package/dist/cli/tunnel-control-server.test.js.map +1 -0
- package/dist/cli/tunnel-handler.d.ts +3 -0
- package/dist/cli/tunnel-handler.js +48 -0
- package/dist/cli/tunnel-handler.js.map +1 -0
- package/dist/cli/tunnel-handler.test.js +105 -0
- package/dist/cli/tunnel-handler.test.js.map +1 -0
- package/dist/cli/tunnel.d.ts +57 -0
- package/dist/cli/tunnel.js +154 -0
- package/dist/cli/tunnel.js.map +1 -0
- package/dist/cli/tunnel.test.d.ts +1 -0
- package/dist/cli/tunnel.test.js +190 -0
- package/dist/cli/tunnel.test.js.map +1 -0
- package/dist/cli/types.d.ts +5 -0
- package/dist/cli/types.js +2 -0
- package/dist/cli/types.js.map +1 -0
- package/dist/cli/use-execute-steps.js.map +1 -1
- package/dist/cli/use-messages.d.ts +3 -0
- package/dist/cli/use-messages.js +11 -0
- package/dist/cli/use-messages.js.map +1 -0
- package/dist/cli/use-nodemon.d.ts +2 -7
- package/dist/cli/use-nodemon.js +18 -21
- package/dist/cli/use-nodemon.js.map +1 -1
- package/dist/cli/use-open-browser.d.ts +1 -0
- package/dist/cli/use-open-browser.js +44 -0
- package/dist/cli/use-open-browser.js.map +1 -0
- package/dist/cli/use-open-tunnel-browser.d.ts +6 -0
- package/dist/cli/use-open-tunnel-browser.js +19 -0
- package/dist/cli/use-open-tunnel-browser.js.map +1 -0
- package/dist/cli/use-tunnel.d.ts +14 -0
- package/dist/cli/use-tunnel.js +131 -0
- package/dist/cli/use-tunnel.js.map +1 -0
- package/dist/cli/use-typescript-check.d.ts +1 -0
- package/dist/cli/use-typescript-check.js +42 -7
- package/dist/cli/use-typescript-check.js.map +1 -1
- package/dist/commands/build.d.ts +0 -1
- package/dist/commands/build.js +50 -15
- package/dist/commands/build.js.map +1 -1
- package/dist/commands/create.d.ts +9 -0
- package/dist/commands/create.js +30 -0
- package/dist/commands/create.js.map +1 -0
- package/dist/commands/dev.d.ts +3 -1
- package/dist/commands/dev.js +64 -8
- package/dist/commands/dev.js.map +1 -1
- package/dist/commands/start.js +13 -10
- package/dist/commands/start.js.map +1 -1
- package/dist/commands/telemetry/disable.js.map +1 -1
- package/dist/commands/telemetry/enable.js.map +1 -1
- package/dist/commands/telemetry/status.js.map +1 -1
- package/dist/server/asset-base-url-transform-plugin.d.ts +6 -6
- package/dist/server/asset-base-url-transform-plugin.js +25 -11
- package/dist/server/asset-base-url-transform-plugin.js.map +1 -1
- package/dist/server/asset-base-url-transform-plugin.test.js +92 -14
- package/dist/server/asset-base-url-transform-plugin.test.js.map +1 -1
- package/dist/server/auth.d.ts +20 -0
- package/dist/server/auth.js +28 -0
- package/dist/server/auth.js.map +1 -0
- package/dist/server/build-manifest.test.d.ts +1 -0
- package/dist/server/build-manifest.test.js +27 -0
- package/dist/server/build-manifest.test.js.map +1 -0
- package/dist/server/content-helpers.d.ts +67 -0
- package/dist/server/content-helpers.js +79 -0
- package/dist/server/content-helpers.js.map +1 -0
- package/dist/server/content-helpers.test.d.ts +1 -0
- package/dist/server/content-helpers.test.js +70 -0
- package/dist/server/content-helpers.test.js.map +1 -0
- package/dist/server/express.d.ts +7 -5
- package/dist/server/express.js +53 -26
- package/dist/server/express.js.map +1 -1
- package/dist/server/express.test.js +411 -25
- package/dist/server/express.test.js.map +1 -1
- package/dist/server/file-ref.d.ts +28 -0
- package/dist/server/file-ref.js +27 -0
- package/dist/server/file-ref.js.map +1 -0
- package/dist/server/index.d.ts +6 -2
- package/dist/server/index.js +5 -1
- package/dist/server/index.js.map +1 -1
- package/dist/server/inferUtilityTypes.d.ts +6 -6
- package/dist/server/inferUtilityTypes.js.map +1 -1
- package/dist/server/metric.d.ts +14 -0
- package/dist/server/metric.js +62 -0
- package/dist/server/metric.js.map +1 -0
- package/dist/server/middleware.d.ts +16 -3
- package/dist/server/middleware.js.map +1 -1
- package/dist/server/middleware.test-d.js.map +1 -1
- package/dist/server/middleware.test.js +115 -5
- package/dist/server/middleware.test.js.map +1 -1
- package/dist/server/server.d.ts +302 -74
- package/dist/server/server.js +445 -120
- package/dist/server/server.js.map +1 -1
- package/dist/server/templateHelper.d.ts +5 -6
- package/dist/server/templateHelper.js +1 -2
- package/dist/server/templateHelper.js.map +1 -1
- package/dist/server/templates.generated.d.ts +4 -0
- package/dist/server/templates.generated.js +47 -0
- package/dist/server/templates.generated.js.map +1 -0
- package/dist/server/tunnel-proxy-router.d.ts +7 -0
- package/dist/server/tunnel-proxy-router.js +110 -0
- package/dist/server/tunnel-proxy-router.js.map +1 -0
- package/dist/server/tunnel-proxy-router.test.d.ts +1 -0
- package/dist/server/tunnel-proxy-router.test.js +229 -0
- package/dist/server/tunnel-proxy-router.test.js.map +1 -0
- package/dist/server/viewsDevServer.d.ts +14 -0
- package/dist/server/viewsDevServer.js +45 -0
- package/dist/server/viewsDevServer.js.map +1 -0
- package/dist/test/utils.d.ts +13 -21
- package/dist/test/utils.js +42 -37
- package/dist/test/utils.js.map +1 -1
- package/dist/test/view.test.d.ts +1 -0
- package/dist/test/view.test.js +568 -0
- package/dist/test/view.test.js.map +1 -0
- package/dist/version.d.ts +1 -0
- package/dist/version.js +3 -0
- package/dist/version.js.map +1 -0
- package/dist/web/bridges/apps-sdk/adaptor.d.ts +11 -4
- package/dist/web/bridges/apps-sdk/adaptor.js +59 -17
- package/dist/web/bridges/apps-sdk/adaptor.js.map +1 -1
- package/dist/web/bridges/apps-sdk/bridge.d.ts +1 -0
- package/dist/web/bridges/apps-sdk/bridge.js +1 -0
- package/dist/web/bridges/apps-sdk/bridge.js.map +1 -1
- package/dist/web/bridges/apps-sdk/index.js.map +1 -1
- package/dist/web/bridges/apps-sdk/types.d.ts +18 -6
- package/dist/web/bridges/apps-sdk/types.js.map +1 -1
- package/dist/web/bridges/apps-sdk/use-apps-sdk-context.d.ts +11 -0
- package/dist/web/bridges/apps-sdk/use-apps-sdk-context.js +11 -0
- package/dist/web/bridges/apps-sdk/use-apps-sdk-context.js.map +1 -1
- package/dist/web/bridges/get-adaptor.d.ts +7 -0
- package/dist/web/bridges/get-adaptor.js +7 -0
- package/dist/web/bridges/get-adaptor.js.map +1 -1
- package/dist/web/bridges/index.js.map +1 -1
- package/dist/web/bridges/mcp-app/adaptor.d.ts +21 -6
- package/dist/web/bridges/mcp-app/adaptor.js +127 -28
- package/dist/web/bridges/mcp-app/adaptor.js.map +1 -1
- package/dist/web/bridges/mcp-app/bridge.d.ts +4 -2
- package/dist/web/bridges/mcp-app/bridge.js +23 -1
- package/dist/web/bridges/mcp-app/bridge.js.map +1 -1
- package/dist/web/bridges/mcp-app/index.js.map +1 -1
- package/dist/web/bridges/mcp-app/types.js.map +1 -1
- package/dist/web/bridges/mcp-app/use-mcp-app-context.d.ts +12 -0
- package/dist/web/bridges/mcp-app/use-mcp-app-context.js +12 -0
- package/dist/web/bridges/mcp-app/use-mcp-app-context.js.map +1 -1
- package/dist/web/bridges/mcp-app/use-mcp-app-context.test.js.map +1 -1
- package/dist/web/bridges/mcp-app/view-tools.test.d.ts +1 -0
- package/dist/web/bridges/mcp-app/view-tools.test.js +144 -0
- package/dist/web/bridges/mcp-app/view-tools.test.js.map +1 -0
- package/dist/web/bridges/types.d.ts +113 -11
- package/dist/web/bridges/types.js.map +1 -1
- package/dist/web/bridges/use-host-context.d.ts +5 -0
- package/dist/web/bridges/use-host-context.js +5 -0
- package/dist/web/bridges/use-host-context.js.map +1 -1
- package/dist/web/components/modal-provider.js +3 -5
- package/dist/web/components/modal-provider.js.map +1 -1
- package/dist/web/create-store.d.ts +26 -0
- package/dist/web/create-store.js +43 -3
- package/dist/web/create-store.js.map +1 -1
- package/dist/web/create-store.test.js +14 -16
- package/dist/web/create-store.test.js.map +1 -1
- package/dist/web/data-llm.d.ts +34 -1
- package/dist/web/data-llm.js +31 -3
- package/dist/web/data-llm.js.map +1 -1
- package/dist/web/data-llm.test.js +22 -22
- package/dist/web/data-llm.test.js.map +1 -1
- package/dist/web/generate-helpers.d.ts +22 -18
- package/dist/web/generate-helpers.js +22 -18
- package/dist/web/generate-helpers.js.map +1 -1
- package/dist/web/generate-helpers.test-d.js +30 -28
- package/dist/web/generate-helpers.test-d.js.map +1 -1
- package/dist/web/generate-helpers.test.js.map +1 -1
- package/dist/web/helpers/state.d.ts +2 -2
- package/dist/web/helpers/state.js +11 -11
- package/dist/web/helpers/state.js.map +1 -1
- package/dist/web/helpers/state.test.js +9 -9
- package/dist/web/helpers/state.test.js.map +1 -1
- package/dist/web/hooks/index.d.ts +5 -1
- package/dist/web/hooks/index.js +5 -1
- package/dist/web/hooks/index.js.map +1 -1
- package/dist/web/hooks/test/utils.d.ts +6 -2
- package/dist/web/hooks/test/utils.js +13 -2
- package/dist/web/hooks/test/utils.js.map +1 -1
- package/dist/web/hooks/use-call-tool.d.ts +45 -0
- package/dist/web/hooks/use-call-tool.js +28 -0
- package/dist/web/hooks/use-call-tool.js.map +1 -1
- package/dist/web/hooks/use-call-tool.test-d.js.map +1 -1
- package/dist/web/hooks/use-call-tool.test.js +27 -6
- package/dist/web/hooks/use-call-tool.test.js.map +1 -1
- package/dist/web/hooks/use-display-mode.d.ts +20 -0
- package/dist/web/hooks/use-display-mode.js +20 -0
- package/dist/web/hooks/use-display-mode.js.map +1 -1
- package/dist/web/hooks/use-display-mode.test-d.js.map +1 -1
- package/dist/web/hooks/use-display-mode.test.js.map +1 -1
- package/dist/web/hooks/use-download.d.ts +5 -0
- package/dist/web/hooks/use-download.js +8 -0
- package/dist/web/hooks/use-download.js.map +1 -0
- package/dist/web/hooks/use-download.test.d.ts +1 -0
- package/dist/web/hooks/use-download.test.js +95 -0
- package/dist/web/hooks/use-download.test.js.map +1 -0
- package/dist/web/hooks/use-files.d.ts +34 -1
- package/dist/web/hooks/use-files.js +33 -0
- package/dist/web/hooks/use-files.js.map +1 -1
- package/dist/web/hooks/use-files.test.js +22 -2
- package/dist/web/hooks/use-files.test.js.map +1 -1
- package/dist/web/hooks/use-layout.d.ts +2 -0
- package/dist/web/hooks/use-layout.js +2 -0
- package/dist/web/hooks/use-layout.js.map +1 -1
- package/dist/web/hooks/use-layout.test.js.map +1 -1
- package/dist/web/hooks/use-open-external.d.ts +17 -0
- package/dist/web/hooks/use-open-external.js +16 -0
- package/dist/web/hooks/use-open-external.js.map +1 -1
- package/dist/web/hooks/use-open-external.test.js.map +1 -1
- package/dist/web/hooks/use-register-view-tool.d.ts +38 -0
- package/dist/web/hooks/use-register-view-tool.js +50 -0
- package/dist/web/hooks/use-register-view-tool.js.map +1 -0
- package/dist/web/hooks/use-request-close.d.ts +16 -0
- package/dist/web/hooks/use-request-close.js +21 -0
- package/dist/web/hooks/use-request-close.js.map +1 -0
- package/dist/web/hooks/use-request-close.test.d.ts +1 -0
- package/dist/web/hooks/use-request-close.test.js +52 -0
- package/dist/web/hooks/use-request-close.test.js.map +1 -0
- package/dist/web/hooks/use-request-modal.d.ts +16 -1
- package/dist/web/hooks/use-request-modal.js +19 -4
- package/dist/web/hooks/use-request-modal.js.map +1 -1
- package/dist/web/hooks/use-request-modal.test.js +5 -1
- package/dist/web/hooks/use-request-modal.test.js.map +1 -1
- package/dist/web/hooks/use-request-size.d.ts +20 -0
- package/dist/web/hooks/use-request-size.js +24 -0
- package/dist/web/hooks/use-request-size.js.map +1 -0
- package/dist/web/hooks/use-request-size.test.d.ts +1 -0
- package/dist/web/hooks/use-request-size.test.js +65 -0
- package/dist/web/hooks/use-request-size.test.js.map +1 -0
- package/dist/web/hooks/use-send-follow-up-message.d.ts +19 -1
- package/dist/web/hooks/use-send-follow-up-message.js +19 -2
- package/dist/web/hooks/use-send-follow-up-message.js.map +1 -1
- package/dist/web/hooks/use-set-open-in-app-url.d.ts +17 -0
- package/dist/web/hooks/use-set-open-in-app-url.js +17 -0
- package/dist/web/hooks/use-set-open-in-app-url.js.map +1 -1
- package/dist/web/hooks/use-set-open-in-app-url.test.js.map +1 -1
- package/dist/web/hooks/use-tool-info.d.ts +53 -2
- package/dist/web/hooks/use-tool-info.js +30 -7
- package/dist/web/hooks/use-tool-info.js.map +1 -1
- package/dist/web/hooks/use-tool-info.test-d.js +11 -29
- package/dist/web/hooks/use-tool-info.test-d.js.map +1 -1
- package/dist/web/hooks/use-tool-info.test.js +5 -5
- package/dist/web/hooks/use-tool-info.test.js.map +1 -1
- package/dist/web/hooks/use-user.d.ts +2 -0
- package/dist/web/hooks/use-user.js +20 -2
- package/dist/web/hooks/use-user.js.map +1 -1
- package/dist/web/hooks/use-user.test.js +28 -0
- package/dist/web/hooks/use-user.test.js.map +1 -1
- package/dist/web/hooks/use-view-state.d.ts +25 -0
- package/dist/web/hooks/use-view-state.js +32 -0
- package/dist/web/hooks/use-view-state.js.map +1 -0
- package/dist/web/hooks/use-view-state.test.d.ts +1 -0
- package/dist/web/hooks/use-view-state.test.js +177 -0
- package/dist/web/hooks/use-view-state.test.js.map +1 -0
- package/dist/web/index.d.ts +1 -2
- package/dist/web/index.js +1 -2
- package/dist/web/index.js.map +1 -1
- package/dist/web/mount-view.d.ts +20 -0
- package/dist/web/{mount-widget.js → mount-view.js} +21 -2
- package/dist/web/mount-view.js.map +1 -0
- package/dist/web/plugin/data-llm.test.js.map +1 -1
- package/dist/web/plugin/plugin.d.ts +32 -1
- package/dist/web/plugin/plugin.js +160 -25
- package/dist/web/plugin/plugin.js.map +1 -1
- package/dist/web/plugin/scan-views.d.ts +16 -0
- package/dist/web/plugin/scan-views.js +88 -0
- package/dist/web/plugin/scan-views.js.map +1 -0
- package/dist/web/plugin/scan-views.test.d.ts +1 -0
- package/dist/web/plugin/scan-views.test.js +99 -0
- package/dist/web/plugin/scan-views.test.js.map +1 -0
- package/dist/web/plugin/transform-data-llm.js +1 -1
- package/dist/web/plugin/transform-data-llm.js.map +1 -1
- package/dist/web/plugin/transform-data-llm.test.js.map +1 -1
- package/dist/web/plugin/validate-view.d.ts +1 -0
- package/dist/web/plugin/validate-view.js +9 -0
- package/dist/web/plugin/validate-view.js.map +1 -0
- package/dist/web/plugin/validate-view.test.d.ts +1 -0
- package/dist/web/plugin/validate-view.test.js +24 -0
- package/dist/web/plugin/validate-view.test.js.map +1 -0
- package/dist/web/proxy.js.map +1 -1
- package/dist/web/types.d.ts +4 -0
- package/dist/web/types.js.map +1 -1
- package/package.json +33 -17
- package/tsconfig.base.json +5 -0
- package/dist/server/const.d.ts +0 -1
- package/dist/server/const.js +0 -2
- package/dist/server/const.js.map +0 -1
- package/dist/server/templates/development.js +0 -27
- package/dist/server/templates/production.js +0 -23
- package/dist/server/widgetsDevServer.d.ts +0 -12
- package/dist/server/widgetsDevServer.js +0 -63
- package/dist/server/widgetsDevServer.js.map +0 -1
- package/dist/test/widget.test.js +0 -261
- package/dist/test/widget.test.js.map +0 -1
- package/dist/web/hooks/use-widget-state.d.ts +0 -4
- package/dist/web/hooks/use-widget-state.js +0 -32
- package/dist/web/hooks/use-widget-state.js.map +0 -1
- package/dist/web/hooks/use-widget-state.test.js +0 -64
- package/dist/web/hooks/use-widget-state.test.js.map +0 -1
- package/dist/web/mount-widget.d.ts +0 -1
- package/dist/web/mount-widget.js.map +0 -1
- package/dist/web/plugin/validate-widget.d.ts +0 -5
- package/dist/web/plugin/validate-widget.js +0 -27
- package/dist/web/plugin/validate-widget.js.map +0 -1
- package/dist/web/plugin/validate-widget.test.js +0 -42
- package/dist/web/plugin/validate-widget.test.js.map +0 -1
- /package/dist/{test/widget.test.d.ts → cli/build-helpers.test.d.ts} +0 -0
- /package/dist/{web/hooks/use-widget-state.test.d.ts → cli/tunnel-control-server.test.d.ts} +0 -0
- /package/dist/{web/plugin/validate-widget.test.d.ts → cli/tunnel-handler.test.d.ts} +0 -0
package/dist/server/server.js
CHANGED
|
@@ -2,10 +2,12 @@ import crypto from "node:crypto";
|
|
|
2
2
|
import { readFileSync } from "node:fs";
|
|
3
3
|
import http from "node:http";
|
|
4
4
|
import path from "node:path";
|
|
5
|
-
import {
|
|
5
|
+
import { Server as SdkServer, } from "@modelcontextprotocol/sdk/server/index.js";
|
|
6
|
+
import { McpServer as McpServerBase } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
6
7
|
import { mergeWith, union } from "es-toolkit";
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
8
|
+
import express, {} from "express";
|
|
9
|
+
import { createApp } from "./express.js";
|
|
10
|
+
import { createMiddlewareEntry } from "./metric.js";
|
|
9
11
|
import { buildMiddlewareChain, getHandlerMaps } from "./middleware.js";
|
|
10
12
|
import { templateHelper } from "./templateHelper.js";
|
|
11
13
|
const mergeWithUnion = (target, source) => {
|
|
@@ -15,28 +17,128 @@ const mergeWithUnion = (target, source) => {
|
|
|
15
17
|
}
|
|
16
18
|
});
|
|
17
19
|
};
|
|
18
|
-
|
|
20
|
+
/**
|
|
21
|
+
* Coerce a tool handler's return value into an MCP `content` array. Strings
|
|
22
|
+
* become a single `TextContent`; a single block is wrapped in an array;
|
|
23
|
+
* `undefined` produces `[]`. Mostly used internally — exported so consumers
|
|
24
|
+
* who build content lazily can apply the same normalization.
|
|
25
|
+
*/
|
|
26
|
+
export function normalizeContent(content) {
|
|
27
|
+
if (content === undefined) {
|
|
28
|
+
return [];
|
|
29
|
+
}
|
|
30
|
+
if (typeof content === "string") {
|
|
31
|
+
return [{ type: "text", text: content }];
|
|
32
|
+
}
|
|
33
|
+
if (Array.isArray(content)) {
|
|
34
|
+
return content;
|
|
35
|
+
}
|
|
36
|
+
return [content];
|
|
37
|
+
}
|
|
38
|
+
const McpServerBaseOmitted = McpServerBase;
|
|
39
|
+
/**
|
|
40
|
+
* The Skybridge server. Extends the MCP SDK's `McpServer` with a typed tool
|
|
41
|
+
* registry, view resources, an embedded Express app, and protocol-level
|
|
42
|
+
* middleware. Construct it with the same `Implementation` info you would pass
|
|
43
|
+
* to the SDK, chain {@link McpServer.registerTool} calls to declare tools,
|
|
44
|
+
* then call {@link McpServer.run} to start the HTTP server.
|
|
45
|
+
*
|
|
46
|
+
* The `TTools` generic accumulates each registered tool's input/output/meta
|
|
47
|
+
* shape, so `typeof server` carries enough information for view-side helpers
|
|
48
|
+
* like {@link generateHelpers} to produce fully-typed hooks.
|
|
49
|
+
*
|
|
50
|
+
* @typeParam TTools - Accumulated tool registry. Filled in by `registerTool`
|
|
51
|
+
* chaining; you almost never set this manually.
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* ```ts
|
|
55
|
+
* const server = new McpServer({ name: "my-app", version: "1.0.0" }, {})
|
|
56
|
+
* .registerTool({
|
|
57
|
+
* name: "search",
|
|
58
|
+
* inputSchema: { query: z.string() },
|
|
59
|
+
* view: { component: "search" },
|
|
60
|
+
* }, async ({ query }) => ({ content: `Results for ${query}` }));
|
|
61
|
+
*
|
|
62
|
+
* await server.run();
|
|
63
|
+
* export type AppType = typeof server;
|
|
64
|
+
* ```
|
|
65
|
+
*
|
|
66
|
+
* @see https://docs.skybridge.tech/api-reference/mcp-server
|
|
67
|
+
*/
|
|
68
|
+
// Side channel populated by `dist/__entry.js` before user code is imported.
|
|
69
|
+
// Set at module scope rather than passed through the constructor because the
|
|
70
|
+
// wrapper has the manifest before the user's `new McpServer(...)` runs, and
|
|
71
|
+
// threading it through every call site (including user templates) is exactly
|
|
72
|
+
// the boilerplate this design is trying to hide.
|
|
73
|
+
let pendingBuildManifest = null;
|
|
74
|
+
/**
|
|
75
|
+
* Prime the build-time Vite manifest before user code constructs its
|
|
76
|
+
* `McpServer`. Called from the generated `dist/__entry.js`; not part of the
|
|
77
|
+
* user-facing API.
|
|
78
|
+
*
|
|
79
|
+
* @internal
|
|
80
|
+
*/
|
|
81
|
+
export function __setBuildManifest(manifest) {
|
|
82
|
+
pendingBuildManifest = manifest;
|
|
83
|
+
}
|
|
84
|
+
export class McpServer extends McpServerBaseOmitted {
|
|
85
|
+
/**
|
|
86
|
+
* The underlying Express app. Use this to extend the HTTP server with
|
|
87
|
+
* custom routes, middleware, or settings — e.g.
|
|
88
|
+
* `server.express.get("/health", ...)`.
|
|
89
|
+
*
|
|
90
|
+
* `express.json()` is pre-applied. Register your handlers before `run()`;
|
|
91
|
+
* after `run()`, dev-mode middleware, the `/mcp` route, and the default
|
|
92
|
+
* error handler are appended in that order.
|
|
93
|
+
*
|
|
94
|
+
* Note: Alpic Cloud only routes traffic to `/mcp` — custom routes work
|
|
95
|
+
* locally and on self-hosted deployments.
|
|
96
|
+
*/
|
|
19
97
|
express;
|
|
20
|
-
|
|
98
|
+
customErrorMiddleware = [];
|
|
21
99
|
mcpMiddlewareEntries = [];
|
|
22
100
|
mcpMiddlewareApplied = false;
|
|
101
|
+
claimedViews = new Map();
|
|
102
|
+
viewMetaBuilders = new Map();
|
|
23
103
|
viteManifest = null;
|
|
104
|
+
serverInfo;
|
|
105
|
+
serverOptions;
|
|
106
|
+
constructor(serverInfo, options) {
|
|
107
|
+
super(serverInfo, options);
|
|
108
|
+
this.serverInfo = serverInfo;
|
|
109
|
+
this.serverOptions = options;
|
|
110
|
+
this.express = express();
|
|
111
|
+
this.express.use(express.json());
|
|
112
|
+
// Pick up the manifest if `dist/__entry.js` primed it before importing
|
|
113
|
+
// user code. Consume-once: clear after the first construction so a
|
|
114
|
+
// subsequent test that doesn't prime can't inherit stale state.
|
|
115
|
+
// Explicit `setViteManifest` calls still win because they happen after
|
|
116
|
+
// construction.
|
|
117
|
+
if (pendingBuildManifest) {
|
|
118
|
+
this.setViteManifest(pendingBuildManifest);
|
|
119
|
+
pendingBuildManifest = null;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
24
122
|
use(pathOrHandler, ...handlers) {
|
|
123
|
+
// Branching is load-bearing: Express's `app.use` overloads can't be
|
|
124
|
+
// resolved against a `string | RequestHandler` union, so we narrow.
|
|
25
125
|
if (typeof pathOrHandler === "string") {
|
|
26
|
-
this.
|
|
27
|
-
path: pathOrHandler,
|
|
28
|
-
handlers,
|
|
29
|
-
});
|
|
126
|
+
this.express.use(pathOrHandler, ...handlers);
|
|
30
127
|
}
|
|
31
128
|
else {
|
|
32
|
-
this.
|
|
33
|
-
handlers: [pathOrHandler, ...handlers],
|
|
34
|
-
});
|
|
129
|
+
this.express.use(pathOrHandler, ...handlers);
|
|
35
130
|
}
|
|
36
131
|
return this;
|
|
37
132
|
}
|
|
38
|
-
|
|
39
|
-
|
|
133
|
+
useOnError(pathOrHandler, ...handlers) {
|
|
134
|
+
if (typeof pathOrHandler === "string") {
|
|
135
|
+
this.customErrorMiddleware.push({ path: pathOrHandler, handlers });
|
|
136
|
+
}
|
|
137
|
+
else {
|
|
138
|
+
this.customErrorMiddleware.push({
|
|
139
|
+
handlers: [pathOrHandler, ...handlers],
|
|
140
|
+
});
|
|
141
|
+
}
|
|
40
142
|
return this;
|
|
41
143
|
}
|
|
42
144
|
mcpMiddleware(filterOrHandler,
|
|
@@ -68,12 +170,36 @@ export class McpServer extends McpServerBase {
|
|
|
68
170
|
return;
|
|
69
171
|
}
|
|
70
172
|
this.mcpMiddlewareApplied = true;
|
|
71
|
-
|
|
173
|
+
// Surface view-resource _meta on `resources/list` (per ext-apps spec:
|
|
174
|
+
// hosts/checkers read CSP & domain at list time before fetching content).
|
|
175
|
+
const viewListMetaEntry = {
|
|
176
|
+
filter: "resources/list",
|
|
177
|
+
handler: async (_req, extra, next) => {
|
|
178
|
+
const result = (await next());
|
|
179
|
+
for (const resource of result.resources) {
|
|
180
|
+
const builder = this.viewMetaBuilders.get(resource.uri);
|
|
181
|
+
if (!builder) {
|
|
182
|
+
continue;
|
|
183
|
+
}
|
|
184
|
+
const meta = builder(extra);
|
|
185
|
+
resource._meta = {
|
|
186
|
+
...(resource._meta ?? {}),
|
|
187
|
+
...meta,
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
return result;
|
|
191
|
+
},
|
|
192
|
+
};
|
|
193
|
+
const monitoringEntry = createMiddlewareEntry();
|
|
194
|
+
const entries = [
|
|
195
|
+
...(monitoringEntry ? [monitoringEntry] : []),
|
|
196
|
+
viewListMetaEntry,
|
|
197
|
+
...this.mcpMiddlewareEntries,
|
|
198
|
+
];
|
|
199
|
+
if (entries.length === 0) {
|
|
72
200
|
return;
|
|
73
201
|
}
|
|
74
202
|
const { requestHandlers, notificationHandlers } = getHandlerMaps(this.server);
|
|
75
|
-
const entries = this.mcpMiddlewareEntries;
|
|
76
|
-
// Wrap existing handlers and proxy future .set() for lazy SDK registration
|
|
77
203
|
const instrumentMap = (map, isNotification) => {
|
|
78
204
|
for (const [method, handler] of map) {
|
|
79
205
|
map.set(method, buildMiddlewareChain(method, isNotification, handler, entries));
|
|
@@ -84,186 +210,359 @@ export class McpServer extends McpServerBase {
|
|
|
84
210
|
instrumentMap(requestHandlers, false);
|
|
85
211
|
instrumentMap(notificationHandlers, true);
|
|
86
212
|
}
|
|
213
|
+
/**
|
|
214
|
+
* Connect to an MCP transport (override of the SDK's `connect`). Use this
|
|
215
|
+
* when you're embedding Skybridge in a host that already manages its own
|
|
216
|
+
* transport (e.g. stdio for desktop apps); for HTTP, prefer {@link McpServer.run}
|
|
217
|
+
* which sets the transport up for you. Locks in any middleware registered
|
|
218
|
+
* via {@link McpServer.mcpMiddleware} — further calls to that method will
|
|
219
|
+
* throw afterwards.
|
|
220
|
+
*/
|
|
87
221
|
async connect(transport) {
|
|
88
222
|
this.applyMcpMiddleware();
|
|
89
|
-
return
|
|
223
|
+
return McpServerBase.prototype.connect.call(this, transport);
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* Per-request stateless connect. The SDK's `Protocol` only allows one
|
|
227
|
+
* transport per instance, so we can't reuse this `McpServer` across
|
|
228
|
+
* concurrent requests. The SDK's idiomatic fix is a `() => McpServer`
|
|
229
|
+
* factory, but that would break Skybridge's singleton API — so instead
|
|
230
|
+
* we build a fresh underlying `Server` per request and share the main
|
|
231
|
+
* server's handler maps by reference. The cast is unavoidable: there's
|
|
232
|
+
* no public API to inject handler maps. `getHandlerMaps` validates the
|
|
233
|
+
* read side and fails fast on SDK field renames.
|
|
234
|
+
*/
|
|
235
|
+
async connectStatelessTransport(transport) {
|
|
236
|
+
this.applyMcpMiddleware();
|
|
237
|
+
const { requestHandlers, notificationHandlers } = getHandlerMaps(this.server);
|
|
238
|
+
const fresh = new SdkServer(this.serverInfo, this.serverOptions);
|
|
239
|
+
const target = fresh;
|
|
240
|
+
target._requestHandlers = requestHandlers;
|
|
241
|
+
target._notificationHandlers = notificationHandlers;
|
|
242
|
+
await fresh.connect(transport);
|
|
90
243
|
}
|
|
244
|
+
/**
|
|
245
|
+
* Start the HTTP server. Listens on `process.env.__PORT` (default `3000`),
|
|
246
|
+
* mounts the `/mcp` route, applies any custom Express middleware registered
|
|
247
|
+
* via {@link McpServer.use} / {@link McpServer.useOnError}, and locks in
|
|
248
|
+
* any MCP middleware registered via {@link McpServer.mcpMiddleware}.
|
|
249
|
+
*
|
|
250
|
+
* On Cloudflare Workers / workerd, returns an object exposing `fetch` so
|
|
251
|
+
* the runtime can bridge incoming requests to the Node HTTP server. On
|
|
252
|
+
* Vercel (`VERCEL === "1"`), returns the Express app directly so the
|
|
253
|
+
* serverless function entry can call it as a `(req, res)` handler. On
|
|
254
|
+
* Node, returns `undefined` once listening.
|
|
255
|
+
*/
|
|
91
256
|
async run() {
|
|
92
257
|
this.applyMcpMiddleware();
|
|
93
|
-
if (
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
258
|
+
if (process.env.VERCEL === "1") {
|
|
259
|
+
// createApp only reads httpServer inside its dev-only branch
|
|
260
|
+
// (viewsDevServer); under VERCEL=1 + NODE_ENV=production it's a
|
|
261
|
+
// bare object passed to satisfy the required parameter.
|
|
262
|
+
const httpServer = http.createServer();
|
|
263
|
+
await createApp({
|
|
264
|
+
mcpServer: this,
|
|
265
|
+
httpServer,
|
|
266
|
+
errorMiddleware: this.customErrorMiddleware,
|
|
97
267
|
});
|
|
268
|
+
return this.express;
|
|
98
269
|
}
|
|
99
|
-
const
|
|
270
|
+
const httpServer = http.createServer();
|
|
271
|
+
await createApp({
|
|
272
|
+
mcpServer: this,
|
|
273
|
+
httpServer,
|
|
274
|
+
errorMiddleware: this.customErrorMiddleware,
|
|
275
|
+
});
|
|
276
|
+
httpServer.on("request", this.express);
|
|
100
277
|
const port = parseInt(process.env.__PORT ?? "3000", 10);
|
|
101
278
|
await new Promise((resolve, reject) => {
|
|
102
|
-
|
|
103
|
-
server.on("error", (error) => {
|
|
279
|
+
httpServer.on("error", (error) => {
|
|
104
280
|
console.error("Failed to start server:", error);
|
|
105
281
|
reject(error);
|
|
106
282
|
});
|
|
107
|
-
|
|
283
|
+
httpServer.listen(port, () => {
|
|
108
284
|
resolve();
|
|
109
285
|
});
|
|
110
286
|
});
|
|
111
|
-
|
|
287
|
+
// On workerd, bridge the Node http server to a Workers fetch handler.
|
|
288
|
+
// The specifier is held in a variable to sidestep tsc's module resolution
|
|
289
|
+
// (`cloudflare:node` only exists under wrangler/workerd).
|
|
290
|
+
if (typeof navigator !== "undefined" &&
|
|
291
|
+
navigator.userAgent === "Cloudflare-Workers") {
|
|
112
292
|
const cloudflareNode = "cloudflare:node";
|
|
113
293
|
const { httpServerHandler } = await import(cloudflareNode);
|
|
114
294
|
return httpServerHandler({ port });
|
|
115
295
|
}
|
|
116
|
-
|
|
117
|
-
|
|
296
|
+
const shutdown = () => {
|
|
297
|
+
// Drop both handlers so a second signal falls through to Node's default
|
|
298
|
+
// (force-quit on a second Ctrl+C while drain is hanging).
|
|
299
|
+
process.off("SIGTERM", shutdown);
|
|
300
|
+
process.off("SIGINT", shutdown);
|
|
301
|
+
httpServer.close(() => process.exit(0));
|
|
302
|
+
// Force exit if connections don't drain in time so the port is still
|
|
303
|
+
// released promptly (e.g. for nodemon restarts).
|
|
304
|
+
setTimeout(() => process.exit(0), 3000).unref();
|
|
305
|
+
};
|
|
306
|
+
process.on("SIGTERM", shutdown);
|
|
307
|
+
process.on("SIGINT", shutdown);
|
|
308
|
+
return undefined;
|
|
309
|
+
}
|
|
310
|
+
enforceOneToolPerView(component, toolName) {
|
|
311
|
+
const existingTool = this.claimedViews.get(component);
|
|
312
|
+
if (existingTool) {
|
|
313
|
+
throw new Error(`skybridge: view "${component}" is already used by tool "${existingTool}". Tool "${toolName}" cannot also reference it — each view backs exactly one tool.`);
|
|
118
314
|
}
|
|
315
|
+
this.claimedViews.set(component, toolName);
|
|
119
316
|
}
|
|
120
|
-
|
|
121
|
-
const
|
|
122
|
-
const
|
|
123
|
-
|
|
317
|
+
resolveViewRequestContext(extra) {
|
|
318
|
+
const isProduction = process.env.NODE_ENV === "production";
|
|
319
|
+
const headers = extra?.requestInfo?.headers || {};
|
|
320
|
+
const header = (key) => {
|
|
321
|
+
const val = headers[key];
|
|
322
|
+
return Array.isArray(val) ? val[0] : val;
|
|
124
323
|
};
|
|
125
|
-
|
|
126
|
-
|
|
324
|
+
const isClaude = header("user-agent") === "Claude-User";
|
|
325
|
+
let serverUrl;
|
|
326
|
+
const forwardedHost = header("x-forwarded-host");
|
|
327
|
+
const origin = header("origin");
|
|
328
|
+
const host = header("host");
|
|
329
|
+
if (forwardedHost) {
|
|
330
|
+
const proto = header("x-forwarded-proto") || "https";
|
|
331
|
+
serverUrl = `${proto}://${forwardedHost}`;
|
|
332
|
+
}
|
|
333
|
+
else if (origin) {
|
|
334
|
+
serverUrl = origin;
|
|
335
|
+
}
|
|
336
|
+
else if (host) {
|
|
337
|
+
const proto = ["127.0.0.1:", "localhost:"].some((p) => host.startsWith(p))
|
|
338
|
+
? "http"
|
|
339
|
+
: "https";
|
|
340
|
+
serverUrl = `${proto}://${host}`;
|
|
341
|
+
}
|
|
342
|
+
else {
|
|
343
|
+
const devPort = process.env.__PORT || "3000";
|
|
344
|
+
serverUrl = `http://localhost:${devPort}`;
|
|
345
|
+
}
|
|
346
|
+
const connectDomains = [serverUrl];
|
|
347
|
+
if (!isProduction) {
|
|
348
|
+
const wsUrl = new URL(serverUrl);
|
|
349
|
+
wsUrl.protocol = wsUrl.protocol === "https:" ? "wss:" : "ws:";
|
|
350
|
+
connectDomains.push(wsUrl.origin);
|
|
351
|
+
}
|
|
352
|
+
let contentMetaOverrides = {};
|
|
353
|
+
if (isClaude) {
|
|
354
|
+
const pathname = extra?.requestInfo?.url?.pathname ?? "";
|
|
355
|
+
const rawUrl = header("x-alpic-forwarded-url") ?? `${serverUrl}${pathname}`;
|
|
356
|
+
// Strip a lone trailing slash so the hash matches the connector URL
|
|
357
|
+
// as registered with Claude (which has no trailing slash on bare origins).
|
|
358
|
+
const url = rawUrl.endsWith("/") ? rawUrl.slice(0, -1) : rawUrl;
|
|
359
|
+
const hash = crypto
|
|
360
|
+
.createHash("sha256")
|
|
361
|
+
.update(url)
|
|
362
|
+
.digest("hex")
|
|
363
|
+
.slice(0, 32);
|
|
364
|
+
contentMetaOverrides = { domain: `${hash}.claudemcpcontent.com` };
|
|
365
|
+
}
|
|
366
|
+
return { serverUrl, connectDomains, contentMetaOverrides };
|
|
367
|
+
}
|
|
368
|
+
registerViewResources(toolName, view, toolMeta) {
|
|
369
|
+
const hosts = view.hosts ?? ["apps-sdk", "mcp-app"];
|
|
370
|
+
// Append a content-derived version param so hosts (e.g. ChatGPT) bust
|
|
371
|
+
// their cache when the bundle changes, but keep the URI stable across
|
|
372
|
+
// `tools/list` calls when the bundle hasn't changed.
|
|
373
|
+
const versionParam = this.computeViewVersionParam(view.component);
|
|
374
|
+
if (hosts.includes("apps-sdk")) {
|
|
375
|
+
const viewResource = {
|
|
127
376
|
hostType: "apps-sdk",
|
|
128
|
-
uri: `ui://
|
|
377
|
+
uri: `ui://views/apps-sdk/${view.component}.html${versionParam}`,
|
|
129
378
|
mimeType: "text/html+skybridge",
|
|
130
379
|
buildContentMeta: ({ resourceDomains, connectDomains, domain }, overrides) => {
|
|
131
|
-
const userUi = userMeta?.ui;
|
|
132
|
-
const userCsp = userUi?.csp;
|
|
133
380
|
const defaults = {
|
|
134
381
|
"openai/widgetCSP": {
|
|
135
382
|
resource_domains: resourceDomains,
|
|
136
383
|
connect_domains: connectDomains,
|
|
137
384
|
},
|
|
138
385
|
"openai/widgetDomain": domain,
|
|
139
|
-
"openai/widgetDescription":
|
|
386
|
+
"openai/widgetDescription": view.description,
|
|
140
387
|
};
|
|
141
|
-
const
|
|
388
|
+
const fromView = {
|
|
142
389
|
"openai/widgetCSP": {
|
|
143
|
-
resource_domains:
|
|
144
|
-
connect_domains:
|
|
145
|
-
frame_domains:
|
|
146
|
-
redirect_domains:
|
|
390
|
+
resource_domains: view.csp?.resourceDomains,
|
|
391
|
+
connect_domains: view.csp?.connectDomains,
|
|
392
|
+
frame_domains: view.csp?.frameDomains,
|
|
393
|
+
redirect_domains: view.csp?.redirectDomains,
|
|
147
394
|
},
|
|
148
|
-
"openai/widgetDomain":
|
|
149
|
-
"openai/widgetPrefersBorder":
|
|
395
|
+
"openai/widgetDomain": view.domain,
|
|
396
|
+
"openai/widgetPrefersBorder": view.prefersBorder,
|
|
150
397
|
};
|
|
151
|
-
const
|
|
152
|
-
|
|
398
|
+
const base = mergeWithUnion(mergeWithUnion(defaults, fromView), {
|
|
399
|
+
"openai/widgetDomain": overrides.domain,
|
|
400
|
+
});
|
|
401
|
+
if (view._meta) {
|
|
402
|
+
return { ...base, ...view._meta };
|
|
403
|
+
}
|
|
404
|
+
return base;
|
|
153
405
|
},
|
|
154
406
|
};
|
|
155
|
-
this.
|
|
156
|
-
name,
|
|
157
|
-
|
|
158
|
-
|
|
407
|
+
this.registerViewResource({
|
|
408
|
+
name: toolName,
|
|
409
|
+
viewResource,
|
|
410
|
+
view,
|
|
159
411
|
});
|
|
160
|
-
toolMeta["openai/outputTemplate"] =
|
|
412
|
+
toolMeta["openai/outputTemplate"] = viewResource.uri;
|
|
161
413
|
}
|
|
162
|
-
if (
|
|
163
|
-
const
|
|
414
|
+
if (hosts.includes("mcp-app")) {
|
|
415
|
+
const viewResource = {
|
|
164
416
|
hostType: "mcp-app",
|
|
165
|
-
uri: `ui://
|
|
417
|
+
uri: `ui://views/ext-apps/${view.component}.html${versionParam}`,
|
|
166
418
|
mimeType: "text/html;profile=mcp-app",
|
|
167
|
-
buildContentMeta: ({ resourceDomains, connectDomains, domain }, overrides) => {
|
|
419
|
+
buildContentMeta: ({ resourceDomains, connectDomains, domain, baseUriDomains }, overrides) => {
|
|
168
420
|
const defaults = {
|
|
169
421
|
ui: {
|
|
170
422
|
csp: {
|
|
171
423
|
resourceDomains,
|
|
172
424
|
connectDomains,
|
|
425
|
+
baseUriDomains,
|
|
173
426
|
},
|
|
174
427
|
domain,
|
|
175
428
|
},
|
|
176
429
|
};
|
|
177
|
-
|
|
178
|
-
ui: {
|
|
430
|
+
const fromView = {
|
|
431
|
+
ui: {
|
|
432
|
+
...(view.description && { description: view.description }),
|
|
433
|
+
...(view.prefersBorder !== undefined && {
|
|
434
|
+
prefersBorder: view.prefersBorder,
|
|
435
|
+
}),
|
|
436
|
+
...(view.domain && { domain: view.domain }),
|
|
437
|
+
csp: {
|
|
438
|
+
...(view.csp?.resourceDomains && {
|
|
439
|
+
resourceDomains: view.csp.resourceDomains,
|
|
440
|
+
}),
|
|
441
|
+
...(view.csp?.connectDomains && {
|
|
442
|
+
connectDomains: view.csp.connectDomains,
|
|
443
|
+
}),
|
|
444
|
+
...(view.csp?.frameDomains && {
|
|
445
|
+
frameDomains: view.csp.frameDomains,
|
|
446
|
+
}),
|
|
447
|
+
...(view.csp?.baseUriDomains && {
|
|
448
|
+
baseUriDomains: view.csp.baseUriDomains,
|
|
449
|
+
}),
|
|
450
|
+
...(view.csp?.redirectDomains && {
|
|
451
|
+
redirectDomains: view.csp.redirectDomains,
|
|
452
|
+
}),
|
|
453
|
+
},
|
|
454
|
+
},
|
|
455
|
+
};
|
|
456
|
+
const base = mergeWithUnion(mergeWithUnion(defaults, fromView), {
|
|
457
|
+
ui: overrides,
|
|
179
458
|
});
|
|
459
|
+
if (view._meta) {
|
|
460
|
+
return { ...base, ...view._meta };
|
|
461
|
+
}
|
|
462
|
+
return base;
|
|
180
463
|
},
|
|
181
464
|
};
|
|
182
|
-
this.
|
|
183
|
-
name,
|
|
184
|
-
|
|
185
|
-
|
|
465
|
+
this.registerViewResource({
|
|
466
|
+
name: toolName,
|
|
467
|
+
viewResource,
|
|
468
|
+
view,
|
|
186
469
|
});
|
|
187
470
|
// @ts-expect-error - For backwards compatibility with Claude current implementation of the specs
|
|
188
|
-
toolMeta["ui/resourceUri"] =
|
|
189
|
-
toolMeta.ui = { resourceUri:
|
|
471
|
+
toolMeta["ui/resourceUri"] = viewResource.uri;
|
|
472
|
+
toolMeta.ui = { ...toolMeta.ui, resourceUri: viewResource.uri };
|
|
190
473
|
}
|
|
191
|
-
this.registerTool(name, {
|
|
192
|
-
...toolConfig,
|
|
193
|
-
_meta: toolMeta,
|
|
194
|
-
}, toolCallback);
|
|
195
|
-
return this;
|
|
196
|
-
}
|
|
197
|
-
registerTool(name, config, cb) {
|
|
198
|
-
super.registerTool(name, config, cb);
|
|
199
|
-
return this;
|
|
200
474
|
}
|
|
201
|
-
|
|
202
|
-
const { hostType, uri:
|
|
203
|
-
|
|
475
|
+
registerViewResource({ name, viewResource, view, }) {
|
|
476
|
+
const { hostType, uri: viewUri, mimeType, buildContentMeta } = viewResource;
|
|
477
|
+
const buildMeta = (extra) => {
|
|
478
|
+
const { serverUrl, connectDomains, contentMetaOverrides } = this.resolveViewRequestContext(extra);
|
|
479
|
+
return buildContentMeta({
|
|
480
|
+
resourceDomains: [serverUrl],
|
|
481
|
+
connectDomains,
|
|
482
|
+
domain: serverUrl,
|
|
483
|
+
baseUriDomains: [serverUrl],
|
|
484
|
+
}, contentMetaOverrides);
|
|
485
|
+
};
|
|
486
|
+
this.viewMetaBuilders.set(viewUri, buildMeta);
|
|
487
|
+
this.registerResource(name, viewUri, { description: view.description }, async (uri, extra) => {
|
|
204
488
|
const isProduction = process.env.NODE_ENV === "production";
|
|
205
|
-
const
|
|
206
|
-
const isClaude = extra?.requestInfo?.headers?.["user-agent"] === "Claude-User";
|
|
207
|
-
const hostFromHeaders = extra?.requestInfo?.headers?.["x-forwarded-host"] ??
|
|
208
|
-
extra?.requestInfo?.headers?.host;
|
|
209
|
-
const useExternalHost = isProduction || useForwardedHost || isClaude;
|
|
210
|
-
const devPort = process.env.__PORT || "3000";
|
|
211
|
-
const serverUrl = useExternalHost
|
|
212
|
-
? `https://${hostFromHeaders}`
|
|
213
|
-
: `http://localhost:${devPort}`;
|
|
489
|
+
const { serverUrl } = this.resolveViewRequestContext(extra);
|
|
214
490
|
const html = isProduction
|
|
215
491
|
? templateHelper.renderProduction({
|
|
216
492
|
hostType,
|
|
217
493
|
serverUrl,
|
|
218
|
-
|
|
219
|
-
styleFile: this.lookupDistFile("style.css"),
|
|
494
|
+
viewFile: this.lookupViewFile(view.component),
|
|
495
|
+
styleFile: this.lookupDistFile("style.css") ?? "",
|
|
220
496
|
})
|
|
221
497
|
: templateHelper.renderDevelopment({
|
|
222
498
|
hostType,
|
|
223
499
|
serverUrl,
|
|
224
|
-
|
|
225
|
-
widgetName: name,
|
|
500
|
+
viewName: view.component,
|
|
226
501
|
});
|
|
227
|
-
const connectDomains = [serverUrl];
|
|
228
|
-
if (!isProduction) {
|
|
229
|
-
const hmrPort = process.env.__SKYBRIDGE_HMR_PORT ?? DEFAULT_HMR_PORT;
|
|
230
|
-
const VITE_HMR_WEBSOCKET_DEFAULT_URL = `ws://localhost:${hmrPort}`;
|
|
231
|
-
connectDomains.push(VITE_HMR_WEBSOCKET_DEFAULT_URL);
|
|
232
|
-
}
|
|
233
|
-
const pathname = extra?.requestInfo?.url?.pathname ?? "";
|
|
234
|
-
const url = `https://${hostFromHeaders}${pathname}`;
|
|
235
|
-
const hash = crypto
|
|
236
|
-
.createHash("sha256")
|
|
237
|
-
.update(url)
|
|
238
|
-
.digest("hex")
|
|
239
|
-
.slice(0, 32);
|
|
240
|
-
const contentMetaOverrides = isClaude
|
|
241
|
-
? {
|
|
242
|
-
domain: `${hash}.claudemcpcontent.com`,
|
|
243
|
-
}
|
|
244
|
-
: {};
|
|
245
|
-
const contentMeta = buildContentMeta({
|
|
246
|
-
resourceDomains: [serverUrl],
|
|
247
|
-
connectDomains,
|
|
248
|
-
domain: serverUrl,
|
|
249
|
-
baseUriDomains: [serverUrl],
|
|
250
|
-
}, contentMetaOverrides);
|
|
251
502
|
return {
|
|
252
503
|
contents: [
|
|
253
|
-
{ uri: uri.href, mimeType, text: html, _meta:
|
|
504
|
+
{ uri: uri.href, mimeType, text: html, _meta: buildMeta(extra) },
|
|
254
505
|
],
|
|
255
506
|
};
|
|
256
507
|
});
|
|
257
508
|
}
|
|
509
|
+
wrapHandler(cb, { attachViewUUID }) {
|
|
510
|
+
return async (args, extra) => {
|
|
511
|
+
const result = await cb(args, extra);
|
|
512
|
+
return {
|
|
513
|
+
...result,
|
|
514
|
+
content: normalizeContent(result.content),
|
|
515
|
+
...(attachViewUUID && {
|
|
516
|
+
_meta: {
|
|
517
|
+
...result._meta,
|
|
518
|
+
viewUUID: crypto.randomUUID(),
|
|
519
|
+
},
|
|
520
|
+
}),
|
|
521
|
+
};
|
|
522
|
+
};
|
|
523
|
+
}
|
|
524
|
+
computeViewVersionParam(viewName) {
|
|
525
|
+
if (process.env.NODE_ENV !== "production") {
|
|
526
|
+
return "";
|
|
527
|
+
}
|
|
528
|
+
try {
|
|
529
|
+
const viewFile = this.lookupViewFile(viewName);
|
|
530
|
+
const styleFile = this.lookupDistFile("style.css") ?? "";
|
|
531
|
+
const hash = crypto
|
|
532
|
+
.createHash("sha256")
|
|
533
|
+
.update(viewFile)
|
|
534
|
+
.update("\0")
|
|
535
|
+
.update(styleFile)
|
|
536
|
+
.digest("hex")
|
|
537
|
+
.slice(0, 8);
|
|
538
|
+
return `?v=${hash}`;
|
|
539
|
+
}
|
|
540
|
+
catch {
|
|
541
|
+
return "";
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
lookupViewFile(viewName) {
|
|
545
|
+
const manifest = this.readManifest();
|
|
546
|
+
for (const entry of Object.values(manifest)) {
|
|
547
|
+
if (entry?.isEntry && entry.name === viewName && entry.file) {
|
|
548
|
+
return entry.file;
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
throw new Error(`View "${viewName}" not found in Vite manifest. Did the build complete successfully? Look for an entry with name "${viewName}" in dist/assets/.vite/manifest.json.`);
|
|
552
|
+
}
|
|
258
553
|
lookupDistFile(key) {
|
|
259
554
|
const manifest = this.readManifest();
|
|
260
555
|
return manifest[key]?.file;
|
|
261
556
|
}
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
557
|
+
/**
|
|
558
|
+
* Inject the Vite manifest as a value rather than letting `readManifest()`
|
|
559
|
+
* load it from disk. Required for runtimes without a usable filesystem
|
|
560
|
+
* (Cloudflare Workers, etc.) — the user's `skybridge build` emits the
|
|
561
|
+
* manifest as a JS module which the entry imports and passes here.
|
|
562
|
+
*/
|
|
563
|
+
setViteManifest(manifest) {
|
|
564
|
+
this.viteManifest = manifest;
|
|
565
|
+
return this;
|
|
267
566
|
}
|
|
268
567
|
readManifest() {
|
|
269
568
|
if (this.viteManifest) {
|
|
@@ -271,5 +570,31 @@ export class McpServer extends McpServerBase {
|
|
|
271
570
|
}
|
|
272
571
|
return JSON.parse(readFileSync(path.join(process.cwd(), "dist", "assets", ".vite", "manifest.json"), "utf-8"));
|
|
273
572
|
}
|
|
573
|
+
registerTool(...args) {
|
|
574
|
+
const baseFn = McpServerBase.prototype.registerTool;
|
|
575
|
+
if (typeof args[0] === "string") {
|
|
576
|
+
baseFn.call(this, args[0], args[1], args[2]);
|
|
577
|
+
return this;
|
|
578
|
+
}
|
|
579
|
+
const config = args[0];
|
|
580
|
+
const cb = args[1];
|
|
581
|
+
const { name, view, securitySchemes, _meta: userToolMeta, ...toolFields } = config;
|
|
582
|
+
const toolMeta = { ...userToolMeta };
|
|
583
|
+
if (securitySchemes) {
|
|
584
|
+
// SEP-1488 puts `securitySchemes` at the top level of the tool
|
|
585
|
+
// descriptor, but the SDK's `registerTool` drops unknown top-level
|
|
586
|
+
// fields, so the canonical spot isn't reachable without intercepting
|
|
587
|
+
// `tools/list`. Use the `_meta` back-compat mirror documented in the
|
|
588
|
+
// Apps SDK reference until SEP-1488 lands in the spec.
|
|
589
|
+
toolMeta.securitySchemes = securitySchemes;
|
|
590
|
+
}
|
|
591
|
+
if (view) {
|
|
592
|
+
this.enforceOneToolPerView(view.component, name);
|
|
593
|
+
this.registerViewResources(name, view, toolMeta);
|
|
594
|
+
}
|
|
595
|
+
const wrappedCb = this.wrapHandler(cb, { attachViewUUID: Boolean(view) });
|
|
596
|
+
baseFn.call(this, name, { ...toolFields, _meta: toolMeta }, wrappedCb);
|
|
597
|
+
return this;
|
|
598
|
+
}
|
|
274
599
|
}
|
|
275
600
|
//# sourceMappingURL=server.js.map
|