skybridge 0.0.0-dev.f897bad → 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 +53 -26
- 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 -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 +32 -4
- package/dist/server/middleware.js.map +1 -1
- package/dist/server/middleware.test-d.js +41 -18
- 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 +134 -79
- package/dist/server/server.js +311 -111
- 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 +30 -20
- 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,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,20 +17,64 @@ const mergeWithUnion = (target, source) => {
|
|
|
15
17
|
}
|
|
16
18
|
});
|
|
17
19
|
};
|
|
18
|
-
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
|
+
*/
|
|
19
46
|
express;
|
|
20
|
-
|
|
47
|
+
customErrorMiddleware = [];
|
|
21
48
|
mcpMiddlewareEntries = [];
|
|
22
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
|
+
}
|
|
23
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.
|
|
24
64
|
if (typeof pathOrHandler === "string") {
|
|
25
|
-
this.
|
|
26
|
-
path: pathOrHandler,
|
|
27
|
-
handlers,
|
|
28
|
-
});
|
|
65
|
+
this.express.use(pathOrHandler, ...handlers);
|
|
29
66
|
}
|
|
30
67
|
else {
|
|
31
|
-
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({
|
|
32
78
|
handlers: [pathOrHandler, ...handlers],
|
|
33
79
|
});
|
|
34
80
|
}
|
|
@@ -63,12 +109,14 @@ export class McpServer extends McpServerBase {
|
|
|
63
109
|
return;
|
|
64
110
|
}
|
|
65
111
|
this.mcpMiddlewareApplied = true;
|
|
66
|
-
|
|
112
|
+
const monitoringEntry = createMiddlewareEntry();
|
|
113
|
+
const entries = monitoringEntry
|
|
114
|
+
? [monitoringEntry, ...this.mcpMiddlewareEntries]
|
|
115
|
+
: this.mcpMiddlewareEntries;
|
|
116
|
+
if (entries.length === 0) {
|
|
67
117
|
return;
|
|
68
118
|
}
|
|
69
119
|
const { requestHandlers, notificationHandlers } = getHandlerMaps(this.server);
|
|
70
|
-
const entries = this.mcpMiddlewareEntries;
|
|
71
|
-
// Wrap existing handlers and proxy future .set() for lazy SDK registration
|
|
72
120
|
const instrumentMap = (map, isNotification) => {
|
|
73
121
|
for (const [method, handler] of map) {
|
|
74
122
|
map.set(method, buildMiddlewareChain(method, isNotification, handler, entries));
|
|
@@ -81,163 +129,246 @@ export class McpServer extends McpServerBase {
|
|
|
81
129
|
}
|
|
82
130
|
async connect(transport) {
|
|
83
131
|
this.applyMcpMiddleware();
|
|
84
|
-
return
|
|
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);
|
|
85
152
|
}
|
|
86
153
|
async run() {
|
|
87
154
|
this.applyMcpMiddleware();
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
}
|
|
94
|
-
|
|
155
|
+
const httpServer = http.createServer();
|
|
156
|
+
await createApp({
|
|
157
|
+
mcpServer: this,
|
|
158
|
+
httpServer,
|
|
159
|
+
errorMiddleware: this.customErrorMiddleware,
|
|
160
|
+
});
|
|
161
|
+
httpServer.on("request", this.express);
|
|
95
162
|
const port = parseInt(process.env.__PORT ?? "3000", 10);
|
|
96
163
|
await new Promise((resolve, reject) => {
|
|
97
|
-
|
|
98
|
-
server.on("error", (error) => {
|
|
164
|
+
httpServer.on("error", (error) => {
|
|
99
165
|
console.error("Failed to start server:", error);
|
|
100
166
|
reject(error);
|
|
101
167
|
});
|
|
102
|
-
|
|
168
|
+
httpServer.listen(port, () => {
|
|
103
169
|
resolve();
|
|
104
170
|
});
|
|
105
171
|
});
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
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);
|
|
110
179
|
return httpServerHandler({ port });
|
|
111
180
|
}
|
|
112
|
-
|
|
113
|
-
|
|
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();
|
|
190
|
+
};
|
|
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.`);
|
|
114
199
|
}
|
|
200
|
+
this.claimedViews.set(component, toolName);
|
|
115
201
|
}
|
|
116
|
-
|
|
117
|
-
const
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
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 = {
|
|
123
210
|
hostType: "apps-sdk",
|
|
124
|
-
uri: `ui://
|
|
211
|
+
uri: `ui://views/apps-sdk/${view.component}.html${versionParam}`,
|
|
125
212
|
mimeType: "text/html+skybridge",
|
|
126
213
|
buildContentMeta: ({ resourceDomains, connectDomains, domain }, overrides) => {
|
|
127
|
-
const userUi = userMeta?.ui;
|
|
128
|
-
const userCsp = userUi?.csp;
|
|
129
214
|
const defaults = {
|
|
130
215
|
"openai/widgetCSP": {
|
|
131
216
|
resource_domains: resourceDomains,
|
|
132
217
|
connect_domains: connectDomains,
|
|
133
218
|
},
|
|
134
219
|
"openai/widgetDomain": domain,
|
|
135
|
-
"openai/widgetDescription":
|
|
220
|
+
"openai/widgetDescription": view.description,
|
|
136
221
|
};
|
|
137
|
-
const
|
|
222
|
+
const fromView = {
|
|
138
223
|
"openai/widgetCSP": {
|
|
139
|
-
resource_domains:
|
|
140
|
-
connect_domains:
|
|
141
|
-
frame_domains:
|
|
142
|
-
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,
|
|
143
228
|
},
|
|
144
|
-
"openai/widgetDomain":
|
|
145
|
-
"openai/widgetPrefersBorder":
|
|
229
|
+
"openai/widgetDomain": view.domain,
|
|
230
|
+
"openai/widgetPrefersBorder": view.prefersBorder,
|
|
146
231
|
};
|
|
147
|
-
const
|
|
148
|
-
|
|
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;
|
|
149
239
|
},
|
|
150
240
|
};
|
|
151
|
-
this.
|
|
152
|
-
name,
|
|
153
|
-
|
|
154
|
-
|
|
241
|
+
this.registerViewResource({
|
|
242
|
+
name: toolName,
|
|
243
|
+
viewResource,
|
|
244
|
+
view,
|
|
155
245
|
});
|
|
156
|
-
toolMeta["openai/outputTemplate"] =
|
|
246
|
+
toolMeta["openai/outputTemplate"] = viewResource.uri;
|
|
157
247
|
}
|
|
158
|
-
if (
|
|
159
|
-
const
|
|
248
|
+
if (hosts.includes("mcp-app")) {
|
|
249
|
+
const viewResource = {
|
|
160
250
|
hostType: "mcp-app",
|
|
161
|
-
uri: `ui://
|
|
251
|
+
uri: `ui://views/ext-apps/${view.component}.html${versionParam}`,
|
|
162
252
|
mimeType: "text/html;profile=mcp-app",
|
|
163
|
-
buildContentMeta: ({ resourceDomains, connectDomains, domain }, overrides) => {
|
|
253
|
+
buildContentMeta: ({ resourceDomains, connectDomains, domain, baseUriDomains }, overrides) => {
|
|
164
254
|
const defaults = {
|
|
165
255
|
ui: {
|
|
166
256
|
csp: {
|
|
167
257
|
resourceDomains,
|
|
168
258
|
connectDomains,
|
|
259
|
+
baseUriDomains,
|
|
169
260
|
},
|
|
170
261
|
domain,
|
|
171
262
|
},
|
|
172
263
|
};
|
|
173
|
-
|
|
174
|
-
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,
|
|
175
292
|
});
|
|
293
|
+
if (view._meta) {
|
|
294
|
+
return { ...base, ...view._meta };
|
|
295
|
+
}
|
|
296
|
+
return base;
|
|
176
297
|
},
|
|
177
298
|
};
|
|
178
|
-
this.
|
|
179
|
-
name,
|
|
180
|
-
|
|
181
|
-
|
|
299
|
+
this.registerViewResource({
|
|
300
|
+
name: toolName,
|
|
301
|
+
viewResource,
|
|
302
|
+
view,
|
|
182
303
|
});
|
|
183
304
|
// @ts-expect-error - For backwards compatibility with Claude current implementation of the specs
|
|
184
|
-
toolMeta["ui/resourceUri"] =
|
|
185
|
-
toolMeta.ui = { resourceUri:
|
|
305
|
+
toolMeta["ui/resourceUri"] = viewResource.uri;
|
|
306
|
+
toolMeta.ui = { resourceUri: viewResource.uri };
|
|
186
307
|
}
|
|
187
|
-
this.registerTool(name, {
|
|
188
|
-
...toolConfig,
|
|
189
|
-
_meta: toolMeta,
|
|
190
|
-
}, toolCallback);
|
|
191
|
-
return this;
|
|
192
|
-
}
|
|
193
|
-
registerTool(name, config, cb) {
|
|
194
|
-
super.registerTool(name, config, cb);
|
|
195
|
-
return this;
|
|
196
308
|
}
|
|
197
|
-
|
|
198
|
-
const { hostType, uri:
|
|
199
|
-
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) => {
|
|
200
312
|
const isProduction = process.env.NODE_ENV === "production";
|
|
201
|
-
const useForwardedHost = process.env.SKYBRIDGE_USE_FORWARDED_HOST === "true";
|
|
202
313
|
const isClaude = extra?.requestInfo?.headers?.["user-agent"] === "Claude-User";
|
|
203
|
-
const
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
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
|
+
}
|
|
210
340
|
const html = isProduction
|
|
211
341
|
? templateHelper.renderProduction({
|
|
212
342
|
hostType,
|
|
213
343
|
serverUrl,
|
|
214
|
-
|
|
215
|
-
styleFile: this.lookupDistFile("style.css"),
|
|
344
|
+
viewFile: this.lookupViewFile(view.component),
|
|
345
|
+
styleFile: this.lookupDistFile("style.css") ?? "",
|
|
216
346
|
})
|
|
217
347
|
: templateHelper.renderDevelopment({
|
|
218
348
|
hostType,
|
|
219
349
|
serverUrl,
|
|
220
|
-
|
|
221
|
-
widgetName: name,
|
|
350
|
+
viewName: view.component,
|
|
222
351
|
});
|
|
223
352
|
const connectDomains = [serverUrl];
|
|
224
353
|
if (!isProduction) {
|
|
225
|
-
const
|
|
226
|
-
|
|
227
|
-
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` };
|
|
228
371
|
}
|
|
229
|
-
const pathname = extra?.requestInfo?.url?.pathname ?? "";
|
|
230
|
-
const url = `https://${hostFromHeaders}${pathname}`;
|
|
231
|
-
const hash = crypto
|
|
232
|
-
.createHash("sha256")
|
|
233
|
-
.update(url)
|
|
234
|
-
.digest("hex")
|
|
235
|
-
.slice(0, 32);
|
|
236
|
-
const contentMetaOverrides = isClaude
|
|
237
|
-
? {
|
|
238
|
-
domain: `${hash}.claudemcpcontent.com`,
|
|
239
|
-
}
|
|
240
|
-
: {};
|
|
241
372
|
const contentMeta = buildContentMeta({
|
|
242
373
|
resourceDomains: [serverUrl],
|
|
243
374
|
connectDomains,
|
|
@@ -251,18 +382,87 @@ export class McpServer extends McpServerBase {
|
|
|
251
382
|
};
|
|
252
383
|
});
|
|
253
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
|
+
}
|
|
254
429
|
lookupDistFile(key) {
|
|
255
430
|
const manifest = this.readManifest();
|
|
256
431
|
return manifest[key]?.file;
|
|
257
432
|
}
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
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;
|
|
263
442
|
}
|
|
264
443
|
readManifest() {
|
|
444
|
+
if (this.viteManifest) {
|
|
445
|
+
return this.viteManifest;
|
|
446
|
+
}
|
|
265
447
|
return JSON.parse(readFileSync(path.join(process.cwd(), "dist", "assets", ".vite", "manifest.json"), "utf-8"));
|
|
266
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
|
+
}
|
|
267
467
|
}
|
|
268
468
|
//# sourceMappingURL=server.js.map
|