skybridge 0.0.0-dev.f89b570 → 0.0.0-dev.f8d2f8c
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 +24 -16
- 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/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.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-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 +41 -6
- package/dist/cli/use-typescript-check.js.map +1 -1
- package/dist/commands/build.js +63 -7
- package/dist/commands/build.js.map +1 -1
- package/dist/commands/dev.d.ts +3 -1
- package/dist/commands/dev.js +46 -8
- package/dist/commands/dev.js.map +1 -1
- package/dist/commands/start.js +7 -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 +5 -6
- package/dist/server/asset-base-url-transform-plugin.js +9 -10
- package/dist/server/asset-base-url-transform-plugin.js.map +1 -1
- package/dist/server/asset-base-url-transform-plugin.test.js +41 -13
- package/dist/server/asset-base-url-transform-plugin.test.js.map +1 -1
- package/dist/server/content-helpers.d.ts +27 -0
- package/dist/server/content-helpers.js +46 -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 +52 -23
- package/dist/server/express.js.map +1 -1
- package/dist/server/express.test.js +381 -25
- package/dist/server/express.test.js.map +1 -1
- package/dist/server/index.d.ts +5 -3
- package/dist/server/index.js +3 -2
- 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 +124 -0
- package/dist/server/middleware.js +93 -0
- package/dist/server/middleware.js.map +1 -0
- package/dist/server/middleware.test-d.d.ts +1 -0
- package/dist/server/middleware.test-d.js +75 -0
- package/dist/server/middleware.test-d.js.map +1 -0
- package/dist/server/middleware.test.d.ts +1 -0
- package/dist/server/middleware.test.js +493 -0
- package/dist/server/middleware.test.js.map +1 -0
- package/dist/server/server.d.ts +158 -67
- package/dist/server/server.js +364 -102
- package/dist/server/server.js.map +1 -1
- package/dist/server/templateHelper.d.ts +5 -8
- package/dist/server/templateHelper.js +3 -22
- 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 +523 -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 +6 -4
- package/dist/web/bridges/apps-sdk/adaptor.js +37 -16
- package/dist/web/bridges/apps-sdk/adaptor.js.map +1 -1
- 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 +16 -6
- package/dist/web/bridges/apps-sdk/types.js.map +1 -1
- package/dist/web/bridges/apps-sdk/use-apps-sdk-context.js.map +1 -1
- 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 +20 -8
- package/dist/web/bridges/mcp-app/adaptor.js +135 -62
- package/dist/web/bridges/mcp-app/adaptor.js.map +1 -1
- package/dist/web/bridges/mcp-app/bridge.d.ts +13 -30
- package/dist/web/bridges/mcp-app/bridge.js +43 -201
- 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 +5 -3
- package/dist/web/bridges/mcp-app/use-mcp-app-context.js +2 -2
- 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 +1 -41
- package/dist/web/bridges/mcp-app/use-mcp-app-context.test.js.map +1 -1
- package/dist/web/bridges/types.d.ts +19 -10
- package/dist/web/bridges/types.js.map +1 -1
- 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.js +17 -3
- package/dist/web/create-store.js.map +1 -1
- package/dist/web/create-store.test.js +17 -17
- package/dist/web/create-store.test.js.map +1 -1
- package/dist/web/data-llm.d.ts +1 -1
- package/dist/web/data-llm.js +3 -3
- package/dist/web/data-llm.js.map +1 -1
- package/dist/web/data-llm.test.js +23 -22
- package/dist/web/data-llm.test.js.map +1 -1
- package/dist/web/generate-helpers.d.ts +20 -18
- package/dist/web/generate-helpers.js +20 -18
- package/dist/web/generate-helpers.js.map +1 -1
- package/dist/web/generate-helpers.test-d.js +26 -26
- 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 +1 -1
- package/dist/web/hooks/index.js +1 -1
- package/dist/web/hooks/index.js.map +1 -1
- package/dist/web/hooks/test/utils.js +4 -0
- package/dist/web/hooks/test/utils.js.map +1 -1
- 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 +0 -4
- package/dist/web/hooks/use-call-tool.test.js.map +1 -1
- 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-files.d.ts +2 -1
- package/dist/web/hooks/use-files.js +1 -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.js.map +1 -1
- package/dist/web/hooks/use-layout.test.js +3 -3
- package/dist/web/hooks/use-layout.test.js.map +1 -1
- package/dist/web/hooks/use-open-external.js.map +1 -1
- package/dist/web/hooks/use-open-external.test.js +15 -10
- package/dist/web/hooks/use-open-external.test.js.map +1 -1
- package/dist/web/hooks/use-request-modal.d.ts +1 -1
- package/dist/web/hooks/use-request-modal.js +4 -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-send-follow-up-message.d.ts +2 -1
- package/dist/web/hooks/use-send-follow-up-message.js +2 -2
- package/dist/web/hooks/use-send-follow-up-message.js.map +1 -1
- 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.js.map +1 -1
- package/dist/web/hooks/use-tool-info.test-d.js.map +1 -1
- package/dist/web/hooks/use-tool-info.test.js +1 -1
- package/dist/web/hooks/use-tool-info.test.js.map +1 -1
- package/dist/web/hooks/use-user.js +18 -2
- package/dist/web/hooks/use-user.js.map +1 -1
- package/dist/web/hooks/use-user.test.js +29 -1
- package/dist/web/hooks/use-user.test.js.map +1 -1
- package/dist/web/hooks/use-view-state.d.ts +4 -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 +1 -0
- package/dist/web/{mount-widget.js → mount-view.js} +2 -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 +4 -1
- package/dist/web/plugin/plugin.js +127 -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.js.map +1 -1
- package/package.json +31 -21
- 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.hbs +0 -67
- package/dist/server/templates/production.hbs +0 -6
- 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/tunnel-control-server.test.d.ts} +0 -0
- /package/dist/{web/hooks/use-widget-state.test.d.ts → cli/tunnel-handler.test.d.ts} +0 -0
- /package/dist/{web/plugin/validate-widget.test.d.ts → cli/tunnel.test.d.ts} +0 -0
package/dist/server/server.js
CHANGED
|
@@ -2,10 +2,13 @@ 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";
|
|
11
|
+
import { buildMiddlewareChain, getHandlerMaps } from "./middleware.js";
|
|
9
12
|
import { templateHelper } from "./templateHelper.js";
|
|
10
13
|
const mergeWithUnion = (target, source) => {
|
|
11
14
|
return mergeWith(target, source, (targetVal, sourceVal) => {
|
|
@@ -14,168 +17,358 @@ const mergeWithUnion = (target, source) => {
|
|
|
14
17
|
}
|
|
15
18
|
});
|
|
16
19
|
};
|
|
17
|
-
export
|
|
20
|
+
export function normalizeContent(content) {
|
|
21
|
+
if (content === undefined) {
|
|
22
|
+
return [];
|
|
23
|
+
}
|
|
24
|
+
if (typeof content === "string") {
|
|
25
|
+
return [{ type: "text", text: content }];
|
|
26
|
+
}
|
|
27
|
+
if (Array.isArray(content)) {
|
|
28
|
+
return content;
|
|
29
|
+
}
|
|
30
|
+
return [content];
|
|
31
|
+
}
|
|
32
|
+
const McpServerBaseOmitted = McpServerBase;
|
|
33
|
+
export class McpServer extends McpServerBaseOmitted {
|
|
34
|
+
/**
|
|
35
|
+
* The underlying Express app. Use this to extend the HTTP server with
|
|
36
|
+
* custom routes, middleware, or settings — e.g.
|
|
37
|
+
* `server.express.get("/health", ...)`.
|
|
38
|
+
*
|
|
39
|
+
* `express.json()` is pre-applied. Register your handlers before `run()`;
|
|
40
|
+
* after `run()`, dev-mode middleware, the `/mcp` route, and the default
|
|
41
|
+
* error handler are appended in that order.
|
|
42
|
+
*
|
|
43
|
+
* Note: Alpic Cloud only routes traffic to `/mcp` — custom routes work
|
|
44
|
+
* locally and on self-hosted deployments.
|
|
45
|
+
*/
|
|
18
46
|
express;
|
|
19
|
-
|
|
47
|
+
customErrorMiddleware = [];
|
|
48
|
+
mcpMiddlewareEntries = [];
|
|
49
|
+
mcpMiddlewareApplied = false;
|
|
50
|
+
claimedViews = new Map();
|
|
51
|
+
viteManifest = null;
|
|
52
|
+
serverInfo;
|
|
53
|
+
serverOptions;
|
|
54
|
+
constructor(serverInfo, options) {
|
|
55
|
+
super(serverInfo, options);
|
|
56
|
+
this.serverInfo = serverInfo;
|
|
57
|
+
this.serverOptions = options;
|
|
58
|
+
this.express = express();
|
|
59
|
+
this.express.use(express.json());
|
|
60
|
+
}
|
|
20
61
|
use(pathOrHandler, ...handlers) {
|
|
62
|
+
// Branching is load-bearing: Express's `app.use` overloads can't be
|
|
63
|
+
// resolved against a `string | RequestHandler` union, so we narrow.
|
|
21
64
|
if (typeof pathOrHandler === "string") {
|
|
22
|
-
this.
|
|
23
|
-
path: pathOrHandler,
|
|
24
|
-
handlers,
|
|
25
|
-
});
|
|
65
|
+
this.express.use(pathOrHandler, ...handlers);
|
|
26
66
|
}
|
|
27
67
|
else {
|
|
28
|
-
this.
|
|
68
|
+
this.express.use(pathOrHandler, ...handlers);
|
|
69
|
+
}
|
|
70
|
+
return this;
|
|
71
|
+
}
|
|
72
|
+
useOnError(pathOrHandler, ...handlers) {
|
|
73
|
+
if (typeof pathOrHandler === "string") {
|
|
74
|
+
this.customErrorMiddleware.push({ path: pathOrHandler, handlers });
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
this.customErrorMiddleware.push({
|
|
29
78
|
handlers: [pathOrHandler, ...handlers],
|
|
30
79
|
});
|
|
31
80
|
}
|
|
32
81
|
return this;
|
|
33
82
|
}
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
83
|
+
mcpMiddleware(filterOrHandler,
|
|
84
|
+
// biome-ignore lint/suspicious/noExplicitAny: overloads narrow the handler type at call sites; implementation must accept all variants
|
|
85
|
+
maybeHandler) {
|
|
86
|
+
if (this.mcpMiddlewareApplied) {
|
|
87
|
+
throw new Error("Cannot register MCP middleware after run() or connect() has been called");
|
|
88
|
+
}
|
|
89
|
+
const handler = maybeHandler;
|
|
90
|
+
if (typeof filterOrHandler === "function") {
|
|
91
|
+
this.mcpMiddlewareEntries.push({
|
|
92
|
+
filter: null,
|
|
93
|
+
handler: filterOrHandler,
|
|
39
94
|
});
|
|
40
95
|
}
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
96
|
+
else if (handler) {
|
|
97
|
+
this.mcpMiddlewareEntries.push({
|
|
98
|
+
filter: filterOrHandler,
|
|
99
|
+
handler,
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
else {
|
|
103
|
+
throw new Error("mcpMiddleware requires a handler function when a filter is provided");
|
|
104
|
+
}
|
|
105
|
+
return this;
|
|
106
|
+
}
|
|
107
|
+
applyMcpMiddleware() {
|
|
108
|
+
if (this.mcpMiddlewareApplied) {
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
this.mcpMiddlewareApplied = true;
|
|
112
|
+
const monitoringEntry = createMiddlewareEntry();
|
|
113
|
+
const entries = monitoringEntry
|
|
114
|
+
? [monitoringEntry, ...this.mcpMiddlewareEntries]
|
|
115
|
+
: this.mcpMiddlewareEntries;
|
|
116
|
+
if (entries.length === 0) {
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
const { requestHandlers, notificationHandlers } = getHandlerMaps(this.server);
|
|
120
|
+
const instrumentMap = (map, isNotification) => {
|
|
121
|
+
for (const [method, handler] of map) {
|
|
122
|
+
map.set(method, buildMiddlewareChain(method, isNotification, handler, entries));
|
|
123
|
+
}
|
|
124
|
+
const originalSet = map.set.bind(map);
|
|
125
|
+
map.set = (method, handler) => originalSet(method, buildMiddlewareChain(method, isNotification, handler, entries));
|
|
126
|
+
};
|
|
127
|
+
instrumentMap(requestHandlers, false);
|
|
128
|
+
instrumentMap(notificationHandlers, true);
|
|
129
|
+
}
|
|
130
|
+
async connect(transport) {
|
|
131
|
+
this.applyMcpMiddleware();
|
|
132
|
+
return McpServerBase.prototype.connect.call(this, transport);
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Per-request stateless connect. The SDK's `Protocol` only allows one
|
|
136
|
+
* transport per instance, so we can't reuse this `McpServer` across
|
|
137
|
+
* concurrent requests. The SDK's idiomatic fix is a `() => McpServer`
|
|
138
|
+
* factory, but that would break Skybridge's singleton API — so instead
|
|
139
|
+
* we build a fresh underlying `Server` per request and share the main
|
|
140
|
+
* server's handler maps by reference. The cast is unavoidable: there's
|
|
141
|
+
* no public API to inject handler maps. `getHandlerMaps` validates the
|
|
142
|
+
* read side and fails fast on SDK field renames.
|
|
143
|
+
*/
|
|
144
|
+
async connectStatelessTransport(transport) {
|
|
145
|
+
this.applyMcpMiddleware();
|
|
146
|
+
const { requestHandlers, notificationHandlers } = getHandlerMaps(this.server);
|
|
147
|
+
const fresh = new SdkServer(this.serverInfo, this.serverOptions);
|
|
148
|
+
const target = fresh;
|
|
149
|
+
target._requestHandlers = requestHandlers;
|
|
150
|
+
target._notificationHandlers = notificationHandlers;
|
|
151
|
+
await fresh.connect(transport);
|
|
152
|
+
}
|
|
153
|
+
async run() {
|
|
154
|
+
this.applyMcpMiddleware();
|
|
155
|
+
const httpServer = http.createServer();
|
|
156
|
+
await createApp({
|
|
157
|
+
mcpServer: this,
|
|
158
|
+
httpServer,
|
|
159
|
+
errorMiddleware: this.customErrorMiddleware,
|
|
160
|
+
});
|
|
161
|
+
httpServer.on("request", this.express);
|
|
162
|
+
const port = parseInt(process.env.__PORT ?? "3000", 10);
|
|
163
|
+
await new Promise((resolve, reject) => {
|
|
164
|
+
httpServer.on("error", (error) => {
|
|
45
165
|
console.error("Failed to start server:", error);
|
|
46
166
|
reject(error);
|
|
47
167
|
});
|
|
48
|
-
|
|
49
|
-
server.listen(port, () => {
|
|
168
|
+
httpServer.listen(port, () => {
|
|
50
169
|
resolve();
|
|
51
170
|
});
|
|
52
171
|
});
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
172
|
+
// On workerd, bridge the Node http server to a Workers fetch handler.
|
|
173
|
+
// The specifier is held in a variable to sidestep tsc's module resolution
|
|
174
|
+
// (`cloudflare:node` only exists under wrangler/workerd).
|
|
175
|
+
if (typeof navigator !== "undefined" &&
|
|
176
|
+
navigator.userAgent === "Cloudflare-Workers") {
|
|
177
|
+
const cloudflareNode = "cloudflare:node";
|
|
178
|
+
const { httpServerHandler } = await import(cloudflareNode);
|
|
179
|
+
return httpServerHandler({ port });
|
|
180
|
+
}
|
|
181
|
+
const shutdown = () => {
|
|
182
|
+
// Drop both handlers so a second signal falls through to Node's default
|
|
183
|
+
// (force-quit on a second Ctrl+C while drain is hanging).
|
|
184
|
+
process.off("SIGTERM", shutdown);
|
|
185
|
+
process.off("SIGINT", shutdown);
|
|
186
|
+
httpServer.close(() => process.exit(0));
|
|
187
|
+
// Force exit if connections don't drain in time so the port is still
|
|
188
|
+
// released promptly (e.g. for nodemon restarts).
|
|
189
|
+
setTimeout(() => process.exit(0), 3000).unref();
|
|
58
190
|
};
|
|
59
|
-
|
|
60
|
-
|
|
191
|
+
process.on("SIGTERM", shutdown);
|
|
192
|
+
process.on("SIGINT", shutdown);
|
|
193
|
+
return undefined;
|
|
194
|
+
}
|
|
195
|
+
enforceOneToolPerView(component, toolName) {
|
|
196
|
+
const existingTool = this.claimedViews.get(component);
|
|
197
|
+
if (existingTool) {
|
|
198
|
+
throw new Error(`skybridge: view "${component}" is already used by tool "${existingTool}". Tool "${toolName}" cannot also reference it — each view backs exactly one tool.`);
|
|
199
|
+
}
|
|
200
|
+
this.claimedViews.set(component, toolName);
|
|
201
|
+
}
|
|
202
|
+
registerViewResources(toolName, view, toolMeta) {
|
|
203
|
+
const hosts = view.hosts ?? ["apps-sdk", "mcp-app"];
|
|
204
|
+
// Append a content-derived version param so hosts (e.g. ChatGPT) bust
|
|
205
|
+
// their cache when the bundle changes, but keep the URI stable across
|
|
206
|
+
// `tools/list` calls when the bundle hasn't changed.
|
|
207
|
+
const versionParam = this.computeViewVersionParam(view.component);
|
|
208
|
+
if (hosts.includes("apps-sdk")) {
|
|
209
|
+
const viewResource = {
|
|
61
210
|
hostType: "apps-sdk",
|
|
62
|
-
uri: `ui://
|
|
211
|
+
uri: `ui://views/apps-sdk/${view.component}.html${versionParam}`,
|
|
63
212
|
mimeType: "text/html+skybridge",
|
|
64
213
|
buildContentMeta: ({ resourceDomains, connectDomains, domain }, overrides) => {
|
|
65
|
-
const userUi = userMeta?.ui;
|
|
66
|
-
const userCsp = userUi?.csp;
|
|
67
214
|
const defaults = {
|
|
68
215
|
"openai/widgetCSP": {
|
|
69
216
|
resource_domains: resourceDomains,
|
|
70
217
|
connect_domains: connectDomains,
|
|
71
218
|
},
|
|
72
219
|
"openai/widgetDomain": domain,
|
|
73
|
-
"openai/widgetDescription":
|
|
220
|
+
"openai/widgetDescription": view.description,
|
|
74
221
|
};
|
|
75
|
-
const
|
|
222
|
+
const fromView = {
|
|
76
223
|
"openai/widgetCSP": {
|
|
77
|
-
resource_domains:
|
|
78
|
-
connect_domains:
|
|
79
|
-
frame_domains:
|
|
80
|
-
redirect_domains:
|
|
224
|
+
resource_domains: view.csp?.resourceDomains,
|
|
225
|
+
connect_domains: view.csp?.connectDomains,
|
|
226
|
+
frame_domains: view.csp?.frameDomains,
|
|
227
|
+
redirect_domains: view.csp?.redirectDomains,
|
|
81
228
|
},
|
|
82
|
-
"openai/widgetDomain":
|
|
83
|
-
"openai/widgetPrefersBorder":
|
|
229
|
+
"openai/widgetDomain": view.domain,
|
|
230
|
+
"openai/widgetPrefersBorder": view.prefersBorder,
|
|
84
231
|
};
|
|
85
|
-
const
|
|
86
|
-
|
|
232
|
+
const base = mergeWithUnion(mergeWithUnion(defaults, fromView), {
|
|
233
|
+
"openai/widgetDomain": overrides.domain,
|
|
234
|
+
});
|
|
235
|
+
if (view._meta) {
|
|
236
|
+
return { ...base, ...view._meta };
|
|
237
|
+
}
|
|
238
|
+
return base;
|
|
87
239
|
},
|
|
88
240
|
};
|
|
89
|
-
this.
|
|
90
|
-
name,
|
|
91
|
-
|
|
92
|
-
|
|
241
|
+
this.registerViewResource({
|
|
242
|
+
name: toolName,
|
|
243
|
+
viewResource,
|
|
244
|
+
view,
|
|
93
245
|
});
|
|
94
|
-
toolMeta["openai/outputTemplate"] =
|
|
246
|
+
toolMeta["openai/outputTemplate"] = viewResource.uri;
|
|
95
247
|
}
|
|
96
|
-
if (
|
|
97
|
-
const
|
|
248
|
+
if (hosts.includes("mcp-app")) {
|
|
249
|
+
const viewResource = {
|
|
98
250
|
hostType: "mcp-app",
|
|
99
|
-
uri: `ui://
|
|
251
|
+
uri: `ui://views/ext-apps/${view.component}.html${versionParam}`,
|
|
100
252
|
mimeType: "text/html;profile=mcp-app",
|
|
101
|
-
buildContentMeta: ({ resourceDomains, connectDomains, domain }, overrides) => {
|
|
253
|
+
buildContentMeta: ({ resourceDomains, connectDomains, domain, baseUriDomains }, overrides) => {
|
|
102
254
|
const defaults = {
|
|
103
255
|
ui: {
|
|
104
256
|
csp: {
|
|
105
257
|
resourceDomains,
|
|
106
258
|
connectDomains,
|
|
259
|
+
baseUriDomains,
|
|
107
260
|
},
|
|
108
261
|
domain,
|
|
109
262
|
},
|
|
110
263
|
};
|
|
111
|
-
|
|
112
|
-
ui: {
|
|
264
|
+
const fromView = {
|
|
265
|
+
ui: {
|
|
266
|
+
...(view.description && { description: view.description }),
|
|
267
|
+
...(view.prefersBorder !== undefined && {
|
|
268
|
+
prefersBorder: view.prefersBorder,
|
|
269
|
+
}),
|
|
270
|
+
...(view.domain && { domain: view.domain }),
|
|
271
|
+
csp: {
|
|
272
|
+
...(view.csp?.resourceDomains && {
|
|
273
|
+
resourceDomains: view.csp.resourceDomains,
|
|
274
|
+
}),
|
|
275
|
+
...(view.csp?.connectDomains && {
|
|
276
|
+
connectDomains: view.csp.connectDomains,
|
|
277
|
+
}),
|
|
278
|
+
...(view.csp?.frameDomains && {
|
|
279
|
+
frameDomains: view.csp.frameDomains,
|
|
280
|
+
}),
|
|
281
|
+
...(view.csp?.baseUriDomains && {
|
|
282
|
+
baseUriDomains: view.csp.baseUriDomains,
|
|
283
|
+
}),
|
|
284
|
+
...(view.csp?.redirectDomains && {
|
|
285
|
+
redirectDomains: view.csp.redirectDomains,
|
|
286
|
+
}),
|
|
287
|
+
},
|
|
288
|
+
},
|
|
289
|
+
};
|
|
290
|
+
const base = mergeWithUnion(mergeWithUnion(defaults, fromView), {
|
|
291
|
+
ui: overrides,
|
|
113
292
|
});
|
|
293
|
+
if (view._meta) {
|
|
294
|
+
return { ...base, ...view._meta };
|
|
295
|
+
}
|
|
296
|
+
return base;
|
|
114
297
|
},
|
|
115
298
|
};
|
|
116
|
-
this.
|
|
117
|
-
name,
|
|
118
|
-
|
|
119
|
-
|
|
299
|
+
this.registerViewResource({
|
|
300
|
+
name: toolName,
|
|
301
|
+
viewResource,
|
|
302
|
+
view,
|
|
120
303
|
});
|
|
121
304
|
// @ts-expect-error - For backwards compatibility with Claude current implementation of the specs
|
|
122
|
-
toolMeta["ui/resourceUri"] =
|
|
123
|
-
toolMeta.ui = { resourceUri:
|
|
305
|
+
toolMeta["ui/resourceUri"] = viewResource.uri;
|
|
306
|
+
toolMeta.ui = { resourceUri: viewResource.uri };
|
|
124
307
|
}
|
|
125
|
-
this.registerTool(name, {
|
|
126
|
-
...toolConfig,
|
|
127
|
-
_meta: toolMeta,
|
|
128
|
-
}, toolCallback);
|
|
129
|
-
return this;
|
|
130
|
-
}
|
|
131
|
-
registerTool(name, config, cb) {
|
|
132
|
-
super.registerTool(name, config, cb);
|
|
133
|
-
return this;
|
|
134
308
|
}
|
|
135
|
-
|
|
136
|
-
const { hostType, uri:
|
|
137
|
-
this.registerResource(name,
|
|
309
|
+
registerViewResource({ name, viewResource, view, }) {
|
|
310
|
+
const { hostType, uri: viewUri, mimeType, buildContentMeta } = viewResource;
|
|
311
|
+
this.registerResource(name, viewUri, { description: view.description }, async (uri, extra) => {
|
|
138
312
|
const isProduction = process.env.NODE_ENV === "production";
|
|
139
|
-
const useForwardedHost = process.env.SKYBRIDGE_USE_FORWARDED_HOST === "true";
|
|
140
313
|
const isClaude = extra?.requestInfo?.headers?.["user-agent"] === "Claude-User";
|
|
141
|
-
const
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
314
|
+
const headers = extra?.requestInfo?.headers || {};
|
|
315
|
+
const header = (key) => {
|
|
316
|
+
const val = headers[key];
|
|
317
|
+
return Array.isArray(val) ? val[0] : val;
|
|
318
|
+
};
|
|
319
|
+
let serverUrl;
|
|
320
|
+
const forwardedHost = header("x-forwarded-host");
|
|
321
|
+
const origin = header("origin");
|
|
322
|
+
const host = header("host");
|
|
323
|
+
if (forwardedHost) {
|
|
324
|
+
const proto = header("x-forwarded-proto") || "https";
|
|
325
|
+
serverUrl = `${proto}://${forwardedHost}`;
|
|
326
|
+
}
|
|
327
|
+
else if (origin) {
|
|
328
|
+
serverUrl = origin;
|
|
329
|
+
}
|
|
330
|
+
else if (host) {
|
|
331
|
+
const proto = ["127.0.0.1:", "localhost:"].some((p) => host.startsWith(p))
|
|
332
|
+
? "http"
|
|
333
|
+
: "https";
|
|
334
|
+
serverUrl = `${proto}://${host}`;
|
|
335
|
+
}
|
|
336
|
+
else {
|
|
337
|
+
const devPort = process.env.__PORT || "3000";
|
|
338
|
+
serverUrl = `http://localhost:${devPort}`;
|
|
339
|
+
}
|
|
148
340
|
const html = isProduction
|
|
149
341
|
? templateHelper.renderProduction({
|
|
150
342
|
hostType,
|
|
151
343
|
serverUrl,
|
|
152
|
-
|
|
153
|
-
styleFile: this.lookupDistFile("style.css"),
|
|
344
|
+
viewFile: this.lookupViewFile(view.component),
|
|
345
|
+
styleFile: this.lookupDistFile("style.css") ?? "",
|
|
154
346
|
})
|
|
155
347
|
: templateHelper.renderDevelopment({
|
|
156
348
|
hostType,
|
|
157
349
|
serverUrl,
|
|
158
|
-
|
|
159
|
-
widgetName: name,
|
|
350
|
+
viewName: view.component,
|
|
160
351
|
});
|
|
161
352
|
const connectDomains = [serverUrl];
|
|
162
353
|
if (!isProduction) {
|
|
163
|
-
const
|
|
164
|
-
|
|
165
|
-
connectDomains.push(
|
|
354
|
+
const wsUrl = new URL(serverUrl);
|
|
355
|
+
wsUrl.protocol = wsUrl.protocol === "https:" ? "wss:" : "ws:";
|
|
356
|
+
connectDomains.push(wsUrl.origin);
|
|
357
|
+
}
|
|
358
|
+
let contentMetaOverrides = {};
|
|
359
|
+
if (isClaude) {
|
|
360
|
+
const pathname = extra?.requestInfo?.url?.pathname ?? "";
|
|
361
|
+
const rawUrl = header("x-alpic-forwarded-url") ?? `${serverUrl}${pathname}`;
|
|
362
|
+
// Strip a lone trailing slash so the hash matches the connector URL
|
|
363
|
+
// as registered with Claude (which has no trailing slash on bare origins).
|
|
364
|
+
const url = rawUrl.endsWith("/") ? rawUrl.slice(0, -1) : rawUrl;
|
|
365
|
+
const hash = crypto
|
|
366
|
+
.createHash("sha256")
|
|
367
|
+
.update(url)
|
|
368
|
+
.digest("hex")
|
|
369
|
+
.slice(0, 32);
|
|
370
|
+
contentMetaOverrides = { domain: `${hash}.claudemcpcontent.com` };
|
|
166
371
|
}
|
|
167
|
-
const pathname = extra?.requestInfo?.url?.pathname ?? "";
|
|
168
|
-
const url = `https://${hostFromHeaders}${pathname}`;
|
|
169
|
-
const hash = crypto
|
|
170
|
-
.createHash("sha256")
|
|
171
|
-
.update(url)
|
|
172
|
-
.digest("hex")
|
|
173
|
-
.slice(0, 32);
|
|
174
|
-
const contentMetaOverrides = isClaude
|
|
175
|
-
? {
|
|
176
|
-
domain: `${hash}.claudemcpcontent.com`,
|
|
177
|
-
}
|
|
178
|
-
: {};
|
|
179
372
|
const contentMeta = buildContentMeta({
|
|
180
373
|
resourceDomains: [serverUrl],
|
|
181
374
|
connectDomains,
|
|
@@ -189,18 +382,87 @@ export class McpServer extends McpServerBase {
|
|
|
189
382
|
};
|
|
190
383
|
});
|
|
191
384
|
}
|
|
385
|
+
wrapHandler(cb, { attachViewUUID }) {
|
|
386
|
+
return async (args, extra) => {
|
|
387
|
+
const result = await cb(args, extra);
|
|
388
|
+
return {
|
|
389
|
+
...result,
|
|
390
|
+
content: normalizeContent(result.content),
|
|
391
|
+
...(attachViewUUID && {
|
|
392
|
+
_meta: {
|
|
393
|
+
...result._meta,
|
|
394
|
+
viewUUID: crypto.randomUUID(),
|
|
395
|
+
},
|
|
396
|
+
}),
|
|
397
|
+
};
|
|
398
|
+
};
|
|
399
|
+
}
|
|
400
|
+
computeViewVersionParam(viewName) {
|
|
401
|
+
if (process.env.NODE_ENV !== "production") {
|
|
402
|
+
return "";
|
|
403
|
+
}
|
|
404
|
+
try {
|
|
405
|
+
const viewFile = this.lookupViewFile(viewName);
|
|
406
|
+
const styleFile = this.lookupDistFile("style.css") ?? "";
|
|
407
|
+
const hash = crypto
|
|
408
|
+
.createHash("sha256")
|
|
409
|
+
.update(viewFile)
|
|
410
|
+
.update("\0")
|
|
411
|
+
.update(styleFile)
|
|
412
|
+
.digest("hex")
|
|
413
|
+
.slice(0, 8);
|
|
414
|
+
return `?v=${hash}`;
|
|
415
|
+
}
|
|
416
|
+
catch {
|
|
417
|
+
return "";
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
lookupViewFile(viewName) {
|
|
421
|
+
const manifest = this.readManifest();
|
|
422
|
+
for (const entry of Object.values(manifest)) {
|
|
423
|
+
if (entry?.isEntry && entry.name === viewName && entry.file) {
|
|
424
|
+
return entry.file;
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
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.`);
|
|
428
|
+
}
|
|
192
429
|
lookupDistFile(key) {
|
|
193
430
|
const manifest = this.readManifest();
|
|
194
431
|
return manifest[key]?.file;
|
|
195
432
|
}
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
433
|
+
/**
|
|
434
|
+
* Inject the Vite manifest as a value rather than letting `readManifest()`
|
|
435
|
+
* load it from disk. Required for runtimes without a usable filesystem
|
|
436
|
+
* (Cloudflare Workers, etc.) — the user's `skybridge build` emits the
|
|
437
|
+
* manifest as a JS module which the entry imports and passes here.
|
|
438
|
+
*/
|
|
439
|
+
setViteManifest(manifest) {
|
|
440
|
+
this.viteManifest = manifest;
|
|
441
|
+
return this;
|
|
201
442
|
}
|
|
202
443
|
readManifest() {
|
|
444
|
+
if (this.viteManifest) {
|
|
445
|
+
return this.viteManifest;
|
|
446
|
+
}
|
|
203
447
|
return JSON.parse(readFileSync(path.join(process.cwd(), "dist", "assets", ".vite", "manifest.json"), "utf-8"));
|
|
204
448
|
}
|
|
449
|
+
registerTool(...args) {
|
|
450
|
+
const baseFn = McpServerBase.prototype.registerTool;
|
|
451
|
+
if (typeof args[0] === "string") {
|
|
452
|
+
baseFn.call(this, args[0], args[1], args[2]);
|
|
453
|
+
return this;
|
|
454
|
+
}
|
|
455
|
+
const config = args[0];
|
|
456
|
+
const cb = args[1];
|
|
457
|
+
const { name, view, _meta: userToolMeta, ...toolFields } = config;
|
|
458
|
+
const toolMeta = { ...userToolMeta };
|
|
459
|
+
if (view) {
|
|
460
|
+
this.enforceOneToolPerView(view.component, name);
|
|
461
|
+
this.registerViewResources(name, view, toolMeta);
|
|
462
|
+
}
|
|
463
|
+
const wrappedCb = this.wrapHandler(cb, { attachViewUUID: Boolean(view) });
|
|
464
|
+
baseFn.call(this, name, { ...toolFields, _meta: toolMeta }, wrappedCb);
|
|
465
|
+
return this;
|
|
466
|
+
}
|
|
205
467
|
}
|
|
206
468
|
//# sourceMappingURL=server.js.map
|