skybridge 0.0.0-dev.e071d95 → 0.0.0-dev.e07ec67
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 +1 -0
- package/dist/cli/build-helpers.d.ts +10 -0
- package/dist/cli/build-helpers.js +93 -0
- package/dist/cli/build-helpers.js.map +1 -0
- package/dist/cli/build-helpers.test.d.ts +1 -0
- package/dist/cli/build-helpers.test.js +89 -0
- package/dist/cli/build-helpers.test.js.map +1 -0
- package/dist/cli/build-steps.d.ts +2 -0
- package/dist/cli/build-steps.js +68 -0
- package/dist/cli/build-steps.js.map +1 -0
- package/dist/cli/build-steps.test.d.ts +1 -0
- package/dist/cli/build-steps.test.js +52 -0
- package/dist/cli/build-steps.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.d.ts +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/run-plain.d.ts +18 -0
- package/dist/cli/run-plain.js +89 -0
- package/dist/cli/run-plain.js.map +1 -0
- package/dist/cli/use-execute-steps.d.ts +1 -1
- package/dist/cli/use-nodemon.d.ts +14 -0
- package/dist/cli/use-nodemon.js +71 -60
- package/dist/cli/use-nodemon.js.map +1 -1
- package/dist/cli/use-typescript-check.d.ts +8 -2
- package/dist/cli/use-typescript-check.js +70 -67
- package/dist/cli/use-typescript-check.js.map +1 -1
- package/dist/commands/build.d.ts +0 -3
- package/dist/commands/build.js +2 -73
- package/dist/commands/build.js.map +1 -1
- package/dist/commands/create.js.map +1 -1
- package/dist/commands/dev.d.ts +1 -0
- package/dist/commands/dev.js +49 -1
- package/dist/commands/dev.js.map +1 -1
- package/dist/commands/start.js +7 -1
- 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/auth/discovery.d.ts +32 -0
- package/dist/server/auth/discovery.js +56 -0
- package/dist/server/auth/discovery.js.map +1 -0
- package/dist/server/auth/discovery.test.d.ts +1 -0
- package/dist/server/auth/discovery.test.js +93 -0
- package/dist/server/auth/discovery.test.js.map +1 -0
- package/dist/server/auth/index.d.ts +18 -0
- package/dist/server/auth/index.js +2 -0
- package/dist/server/auth/index.js.map +1 -0
- package/dist/server/auth/providers/auth0.d.ts +18 -0
- package/dist/server/auth/providers/auth0.js +31 -0
- package/dist/server/auth/providers/auth0.js.map +1 -0
- package/dist/server/auth/providers/auth0.test.d.ts +1 -0
- package/dist/server/auth/providers/auth0.test.js +48 -0
- package/dist/server/auth/providers/auth0.test.js.map +1 -0
- package/dist/server/auth/providers/clerk.d.ts +14 -0
- package/dist/server/auth/providers/clerk.js +16 -0
- package/dist/server/auth/providers/clerk.js.map +1 -0
- package/dist/server/auth/providers/clerk.test.d.ts +1 -0
- package/dist/server/auth/providers/clerk.test.js +28 -0
- package/dist/server/auth/providers/clerk.test.js.map +1 -0
- package/dist/server/auth/providers/custom.d.ts +30 -0
- package/dist/server/auth/providers/custom.js +40 -0
- package/dist/server/auth/providers/custom.js.map +1 -0
- package/dist/server/auth/providers/custom.test.d.ts +1 -0
- package/dist/server/auth/providers/custom.test.js +128 -0
- package/dist/server/auth/providers/custom.test.js.map +1 -0
- package/dist/server/auth/providers/descope.d.ts +15 -0
- package/dist/server/auth/providers/descope.js +35 -0
- package/dist/server/auth/providers/descope.js.map +1 -0
- package/dist/server/auth/providers/descope.test.d.ts +1 -0
- package/dist/server/auth/providers/descope.test.js +39 -0
- package/dist/server/auth/providers/descope.test.js.map +1 -0
- package/dist/server/auth/providers/shared.d.ts +2 -0
- package/dist/server/auth/providers/shared.js +6 -0
- package/dist/server/auth/providers/shared.js.map +1 -0
- package/dist/server/auth/providers/shared.test.d.ts +1 -0
- package/dist/server/auth/providers/shared.test.js +10 -0
- package/dist/server/auth/providers/shared.test.js.map +1 -0
- package/dist/server/auth/providers/stytch.d.ts +12 -0
- package/dist/server/auth/providers/stytch.js +13 -0
- package/dist/server/auth/providers/stytch.js.map +1 -0
- package/dist/server/auth/providers/workos.d.ts +11 -0
- package/dist/server/auth/providers/workos.js +12 -0
- package/dist/server/auth/providers/workos.js.map +1 -0
- package/dist/server/auth/setup.d.ts +4 -0
- package/dist/server/auth/setup.js +51 -0
- package/dist/server/auth/setup.js.map +1 -0
- package/dist/server/auth/setup.test.d.ts +1 -0
- package/dist/server/auth/setup.test.js +185 -0
- package/dist/server/auth/setup.test.js.map +1 -0
- package/dist/server/auth/verify.d.ts +12 -0
- package/dist/server/auth/verify.js +38 -0
- package/dist/server/auth/verify.js.map +1 -0
- package/dist/server/auth/verify.test.d.ts +1 -0
- package/dist/server/auth/verify.test.js +100 -0
- package/dist/server/auth/verify.test.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 +40 -0
- package/dist/server/content-helpers.js +33 -0
- package/dist/server/content-helpers.js.map +1 -1
- package/dist/server/express.test.js +61 -0
- package/dist/server/express.test.js.map +1 -1
- package/dist/server/file-ref.d.ts +20 -0
- package/dist/server/file-ref.js +19 -0
- package/dist/server/file-ref.js.map +1 -1
- package/dist/server/index.d.ts +9 -2
- package/dist/server/index.js +7 -1
- package/dist/server/index.js.map +1 -1
- package/dist/server/middleware.d.ts +16 -3
- package/dist/server/middleware.js.map +1 -1
- package/dist/server/requestOrigin.d.ts +7 -0
- package/dist/server/requestOrigin.js +25 -0
- package/dist/server/requestOrigin.js.map +1 -0
- package/dist/server/server.d.ts +176 -5
- package/dist/server/server.js +250 -126
- package/dist/server/server.js.map +1 -1
- package/dist/server/templates.generated.d.ts +2 -2
- package/dist/server/view-name.test-d.d.ts +1 -0
- package/dist/server/view-name.test-d.js +8 -0
- package/dist/server/view-name.test-d.js.map +1 -0
- package/dist/server/view-resource-resolution.test.d.ts +6 -0
- package/dist/server/view-resource-resolution.test.js +171 -0
- package/dist/server/view-resource-resolution.test.js.map +1 -0
- package/dist/test/utils.d.ts +2 -2
- package/dist/test/view.test.js +70 -102
- package/dist/test/view.test.js.map +1 -1
- package/dist/web/bridges/adaptor.d.ts +51 -0
- package/dist/web/bridges/adaptor.js +330 -0
- package/dist/web/bridges/adaptor.js.map +1 -0
- package/dist/web/bridges/adaptor.test.d.ts +1 -0
- package/dist/web/bridges/adaptor.test.js +208 -0
- package/dist/web/bridges/adaptor.test.js.map +1 -0
- package/dist/web/bridges/apps-sdk/bridge.d.ts +6 -1
- package/dist/web/bridges/apps-sdk/bridge.js +15 -3
- package/dist/web/bridges/apps-sdk/bridge.js.map +1 -1
- package/dist/web/bridges/apps-sdk/index.d.ts +0 -1
- package/dist/web/bridges/apps-sdk/index.js +0 -1
- package/dist/web/bridges/apps-sdk/index.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 +9 -7
- package/dist/web/bridges/get-adaptor.js.map +1 -1
- package/dist/web/bridges/get-adaptor.test.d.ts +1 -0
- package/dist/web/bridges/get-adaptor.test.js +32 -0
- package/dist/web/bridges/get-adaptor.test.js.map +1 -0
- package/dist/web/bridges/mcp-app/bridge.d.ts +15 -2
- package/dist/web/bridges/mcp-app/bridge.js +68 -4
- package/dist/web/bridges/mcp-app/bridge.js.map +1 -1
- package/dist/web/bridges/mcp-app/bridge.test.d.ts +1 -0
- package/dist/web/bridges/mcp-app/bridge.test.js +17 -0
- package/dist/web/bridges/mcp-app/bridge.test.js.map +1 -0
- package/dist/web/bridges/mcp-app/index.d.ts +0 -1
- package/dist/web/bridges/mcp-app/index.js +0 -1
- package/dist/web/bridges/mcp-app/index.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/view-tools.test.d.ts +1 -0
- package/dist/web/bridges/mcp-app/view-tools.test.js +143 -0
- package/dist/web/bridges/mcp-app/view-tools.test.js.map +1 -0
- package/dist/web/bridges/types.d.ts +91 -1
- package/dist/web/bridges/types.js +14 -1
- package/dist/web/bridges/types.js.map +1 -1
- package/dist/web/bridges/types.test.d.ts +1 -0
- package/dist/web/bridges/types.test.js +19 -0
- package/dist/web/bridges/types.test.js.map +1 -0
- 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.d.ts +1 -1
- package/dist/web/components/modal-provider.js +5 -4
- 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 +26 -0
- package/dist/web/create-store.js.map +1 -1
- package/dist/web/create-store.test.js +8 -5
- 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 +28 -0
- package/dist/web/data-llm.js.map +1 -1
- package/dist/web/data-llm.test.js +18 -6
- package/dist/web/data-llm.test.js.map +1 -1
- package/dist/web/generate-helpers.d.ts +2 -0
- package/dist/web/generate-helpers.js +2 -0
- package/dist/web/generate-helpers.js.map +1 -1
- package/dist/web/generate-helpers.test-d.js +4 -2
- package/dist/web/generate-helpers.test-d.js.map +1 -1
- package/dist/web/hooks/index.d.ts +1 -0
- package/dist/web/hooks/index.js +1 -0
- package/dist/web/hooks/index.js.map +1 -1
- package/dist/web/hooks/test/utils.js.map +1 -1
- package/dist/web/hooks/use-call-tool.d.ts +56 -11
- 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.js +37 -23
- 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.js +56 -20
- package/dist/web/hooks/use-display-mode.test.js.map +1 -1
- package/dist/web/hooks/use-download.test.js +11 -3
- package/dist/web/hooks/use-download.test.js.map +1 -1
- package/dist/web/hooks/use-files.d.ts +32 -0
- package/dist/web/hooks/use-files.js +32 -0
- package/dist/web/hooks/use-files.js.map +1 -1
- package/dist/web/hooks/use-files.test.js +10 -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 +59 -26
- 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 +13 -3
- 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 +14 -0
- package/dist/web/hooks/use-request-close.js +13 -0
- package/dist/web/hooks/use-request-close.js.map +1 -1
- package/dist/web/hooks/use-request-close.test.js +35 -40
- package/dist/web/hooks/use-request-close.test.js.map +1 -1
- package/dist/web/hooks/use-request-modal.d.ts +16 -1
- package/dist/web/hooks/use-request-modal.js +16 -1
- package/dist/web/hooks/use-request-modal.js.map +1 -1
- package/dist/web/hooks/use-request-modal.test.js +10 -0
- package/dist/web/hooks/use-request-modal.test.js.map +1 -1
- package/dist/web/hooks/use-request-size.d.ts +17 -0
- package/dist/web/hooks/use-request-size.js +16 -0
- package/dist/web/hooks/use-request-size.js.map +1 -1
- package/dist/web/hooks/use-request-size.test.js +35 -53
- package/dist/web/hooks/use-request-size.test.js.map +1 -1
- package/dist/web/hooks/use-send-follow-up-message.d.ts +17 -0
- package/dist/web/hooks/use-send-follow-up-message.js +17 -0
- 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 +28 -8
- 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 +42 -32
- 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 +2 -0
- package/dist/web/hooks/use-user.js.map +1 -1
- package/dist/web/hooks/use-user.test.js +81 -35
- package/dist/web/hooks/use-user.test.js.map +1 -1
- package/dist/web/hooks/use-view-state.d.ts +21 -0
- package/dist/web/hooks/use-view-state.js.map +1 -1
- package/dist/web/hooks/use-view-state.test.js +6 -2
- package/dist/web/hooks/use-view-state.test.js.map +1 -1
- package/dist/web/mount-view.d.ts +19 -0
- package/dist/web/mount-view.js +19 -0
- package/dist/web/mount-view.js.map +1 -1
- package/dist/web/plugin/plugin.d.ts +28 -0
- package/dist/web/plugin/plugin.js +26 -1
- package/dist/web/plugin/plugin.js.map +1 -1
- package/dist/web/types.d.ts +4 -0
- package/dist/web/types.js.map +1 -1
- package/package.json +22 -19
- package/dist/web/bridges/apps-sdk/adaptor.d.ts +0 -27
- package/dist/web/bridges/apps-sdk/adaptor.js +0 -112
- package/dist/web/bridges/apps-sdk/adaptor.js.map +0 -1
- package/dist/web/bridges/mcp-app/adaptor.d.ts +0 -51
- package/dist/web/bridges/mcp-app/adaptor.js +0 -279
- package/dist/web/bridges/mcp-app/adaptor.js.map +0 -1
package/dist/server/server.js
CHANGED
|
@@ -6,9 +6,11 @@ import { Server as SdkServer, } from "@modelcontextprotocol/sdk/server/index.js"
|
|
|
6
6
|
import { McpServer as McpServerBase } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
7
7
|
import { mergeWith, union } from "es-toolkit";
|
|
8
8
|
import express, {} from "express";
|
|
9
|
+
import { setupOAuth } from "./auth/setup.js";
|
|
9
10
|
import { createApp } from "./express.js";
|
|
10
11
|
import { createMiddlewareEntry } from "./metric.js";
|
|
11
12
|
import { buildMiddlewareChain, getHandlerMaps } from "./middleware.js";
|
|
13
|
+
import { resolveServerOrigin } from "./requestOrigin.js";
|
|
12
14
|
import { templateHelper } from "./templateHelper.js";
|
|
13
15
|
const mergeWithUnion = (target, source) => {
|
|
14
16
|
return mergeWith(target, source, (targetVal, sourceVal) => {
|
|
@@ -17,6 +19,34 @@ const mergeWithUnion = (target, source) => {
|
|
|
17
19
|
}
|
|
18
20
|
});
|
|
19
21
|
};
|
|
22
|
+
/**
|
|
23
|
+
* Normalize an `x-forwarded-prefix` value into a leading-slash, no-trailing-slash
|
|
24
|
+
* path. Takes the first hop of a comma-separated proxy chain.
|
|
25
|
+
* "/v1/", "v1", "/v1, /internal" → "/v1"; "", "/", undefined → "".
|
|
26
|
+
*/
|
|
27
|
+
function normalizeForwardedPrefix(raw) {
|
|
28
|
+
const firstHop = raw?.split(",")[0]?.trim() ?? "";
|
|
29
|
+
const trimmed = firstHop.replace(/\/+$/, "");
|
|
30
|
+
if (trimmed === "") {
|
|
31
|
+
return "";
|
|
32
|
+
}
|
|
33
|
+
return trimmed.startsWith("/") ? trimmed : `/${trimmed}`;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Drop the query string from a `ui://` view URI, leaving the bare path. The
|
|
37
|
+
* `?v=` cache key is the only query we append, so a plain split is enough and
|
|
38
|
+
* sidesteps `URL` normalization quirks on the non-special `ui:` scheme.
|
|
39
|
+
*/
|
|
40
|
+
function stripQuery(uri) {
|
|
41
|
+
const queryIndex = uri.indexOf("?");
|
|
42
|
+
return queryIndex === -1 ? uri : uri.slice(0, queryIndex);
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Coerce a tool handler's return value into an MCP `content` array. Strings
|
|
46
|
+
* become a single `TextContent`; a single block is wrapped in an array;
|
|
47
|
+
* `undefined` produces `[]`. Mostly used internally — exported so consumers
|
|
48
|
+
* who build content lazily can apply the same normalization.
|
|
49
|
+
*/
|
|
20
50
|
export function normalizeContent(content) {
|
|
21
51
|
if (content === undefined) {
|
|
22
52
|
return [];
|
|
@@ -30,13 +60,60 @@ export function normalizeContent(content) {
|
|
|
30
60
|
return [content];
|
|
31
61
|
}
|
|
32
62
|
const McpServerBaseOmitted = McpServerBase;
|
|
63
|
+
/**
|
|
64
|
+
* The Skybridge server. Extends the MCP SDK's `McpServer` with a typed tool
|
|
65
|
+
* registry, view resources, an embedded Express app, and protocol-level
|
|
66
|
+
* middleware. Construct it with the same `Implementation` info you would pass
|
|
67
|
+
* to the SDK, chain {@link McpServer.registerTool} calls to declare tools,
|
|
68
|
+
* then call {@link McpServer.run} to start the HTTP server.
|
|
69
|
+
*
|
|
70
|
+
* The `TTools` generic accumulates each registered tool's input/output/meta
|
|
71
|
+
* shape, so `typeof server` carries enough information for view-side helpers
|
|
72
|
+
* like {@link generateHelpers} to produce fully-typed hooks.
|
|
73
|
+
*
|
|
74
|
+
* @typeParam TTools - Accumulated tool registry. Filled in by `registerTool`
|
|
75
|
+
* chaining; you almost never set this manually.
|
|
76
|
+
*
|
|
77
|
+
* @example
|
|
78
|
+
* ```ts
|
|
79
|
+
* const server = new McpServer({ name: "my-app", version: "1.0.0" }, {})
|
|
80
|
+
* .registerTool({
|
|
81
|
+
* name: "search",
|
|
82
|
+
* inputSchema: { query: z.string() },
|
|
83
|
+
* view: { component: "search" },
|
|
84
|
+
* }, async ({ query }) => ({ content: `Results for ${query}` }));
|
|
85
|
+
*
|
|
86
|
+
* await server.run();
|
|
87
|
+
* export type AppType = typeof server;
|
|
88
|
+
* ```
|
|
89
|
+
*
|
|
90
|
+
* @see https://docs.skybridge.tech/api-reference/mcp-server
|
|
91
|
+
*/
|
|
92
|
+
// Side channel populated by `dist/__entry.js` before user code is imported.
|
|
93
|
+
// Set at module scope rather than passed through the constructor because the
|
|
94
|
+
// wrapper has the manifest before the user's `new McpServer(...)` runs, and
|
|
95
|
+
// threading it through every call site (including user templates) is exactly
|
|
96
|
+
// the boilerplate this design is trying to hide.
|
|
97
|
+
let pendingBuildManifest = null;
|
|
98
|
+
/**
|
|
99
|
+
* Prime the build-time Vite manifest before user code constructs its
|
|
100
|
+
* `McpServer`. Called from the generated `dist/__entry.js`; not part of the
|
|
101
|
+
* user-facing API.
|
|
102
|
+
*
|
|
103
|
+
* @internal
|
|
104
|
+
*/
|
|
105
|
+
export function __setBuildManifest(manifest) {
|
|
106
|
+
pendingBuildManifest = manifest;
|
|
107
|
+
}
|
|
33
108
|
export class McpServer extends McpServerBaseOmitted {
|
|
34
109
|
/**
|
|
35
110
|
* The underlying Express app. Use this to extend the HTTP server with
|
|
36
111
|
* custom routes, middleware, or settings — e.g.
|
|
37
112
|
* `server.express.get("/health", ...)`.
|
|
38
113
|
*
|
|
39
|
-
* `express.json()` is pre-applied
|
|
114
|
+
* `express.json()` is pre-applied — tune it via the constructor's third
|
|
115
|
+
* argument, e.g. `new McpServer(info, {}, { json: { limit: "10mb" } })`.
|
|
116
|
+
* Register your handlers before `run()`;
|
|
40
117
|
* after `run()`, dev-mode middleware, the `/mcp` route, and the default
|
|
41
118
|
* error handler are appended in that order.
|
|
42
119
|
*
|
|
@@ -49,15 +126,34 @@ export class McpServer extends McpServerBaseOmitted {
|
|
|
49
126
|
mcpMiddlewareApplied = false;
|
|
50
127
|
claimedViews = new Map();
|
|
51
128
|
viewMetaBuilders = new Map();
|
|
129
|
+
/**
|
|
130
|
+
* Maps a view resource's query-less path to its canonical registered URI
|
|
131
|
+
* (the one carrying the `?v=` cache key). Lets `resources/read` resolve the
|
|
132
|
+
* underlying view no matter which version param the consumer sends, since
|
|
133
|
+
* the param is only a cache key, not part of the resource's identity.
|
|
134
|
+
*/
|
|
135
|
+
viewUriByPath = new Map();
|
|
52
136
|
viteManifest = null;
|
|
53
137
|
serverInfo;
|
|
54
138
|
serverOptions;
|
|
55
|
-
constructor(serverInfo, options) {
|
|
139
|
+
constructor(serverInfo, options, skybridgeOptions) {
|
|
56
140
|
super(serverInfo, options);
|
|
57
141
|
this.serverInfo = serverInfo;
|
|
58
142
|
this.serverOptions = options;
|
|
59
143
|
this.express = express();
|
|
60
|
-
this.express.use(express.json());
|
|
144
|
+
this.express.use(express.json(skybridgeOptions?.json));
|
|
145
|
+
if (skybridgeOptions?.oauth) {
|
|
146
|
+
setupOAuth(this.express, skybridgeOptions.oauth);
|
|
147
|
+
}
|
|
148
|
+
// Pick up the manifest if `dist/__entry.js` primed it before importing
|
|
149
|
+
// user code. Consume-once: clear after the first construction so a
|
|
150
|
+
// subsequent test that doesn't prime can't inherit stale state.
|
|
151
|
+
// Explicit `setViteManifest` calls still win because they happen after
|
|
152
|
+
// construction.
|
|
153
|
+
if (pendingBuildManifest) {
|
|
154
|
+
this.setViteManifest(pendingBuildManifest);
|
|
155
|
+
pendingBuildManifest = null;
|
|
156
|
+
}
|
|
61
157
|
}
|
|
62
158
|
use(pathOrHandler, ...handlers) {
|
|
63
159
|
// Branching is load-bearing: Express's `app.use` overloads can't be
|
|
@@ -130,10 +226,47 @@ export class McpServer extends McpServerBaseOmitted {
|
|
|
130
226
|
return result;
|
|
131
227
|
},
|
|
132
228
|
};
|
|
229
|
+
// Resolve a view's `resources/read` by its query-less path so the
|
|
230
|
+
// underlying asset is served no matter the `?v=` value (stale cache key,
|
|
231
|
+
// no param, etc.). The version param is a cache-busting hint for external
|
|
232
|
+
// consumers; it must not gate resolution. We rewrite the lookup URI to the
|
|
233
|
+
// canonical registered one, then restore the requested URI on the response
|
|
234
|
+
// so the consumer-facing URI is never rewritten.
|
|
235
|
+
const viewReadResolveEntry = {
|
|
236
|
+
filter: "resources/read",
|
|
237
|
+
handler: async (req, _extra, next) => {
|
|
238
|
+
const requested = req.params.uri;
|
|
239
|
+
if (typeof requested !== "string") {
|
|
240
|
+
return next();
|
|
241
|
+
}
|
|
242
|
+
const path = stripQuery(requested);
|
|
243
|
+
const canonical = this.viewUriByPath.get(path);
|
|
244
|
+
if (!canonical) {
|
|
245
|
+
return next();
|
|
246
|
+
}
|
|
247
|
+
req.params.uri = canonical;
|
|
248
|
+
try {
|
|
249
|
+
const result = (await next());
|
|
250
|
+
for (const content of result.contents ?? []) {
|
|
251
|
+
if (typeof content.uri === "string" &&
|
|
252
|
+
stripQuery(content.uri) === stripQuery(canonical)) {
|
|
253
|
+
content.uri = requested;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
return result;
|
|
257
|
+
}
|
|
258
|
+
finally {
|
|
259
|
+
// Restore the shared request params so middleware outer to us never
|
|
260
|
+
// observes the rewritten lookup URI after next() unwinds.
|
|
261
|
+
req.params.uri = requested;
|
|
262
|
+
}
|
|
263
|
+
},
|
|
264
|
+
};
|
|
133
265
|
const monitoringEntry = createMiddlewareEntry();
|
|
134
266
|
const entries = [
|
|
135
267
|
...(monitoringEntry ? [monitoringEntry] : []),
|
|
136
268
|
viewListMetaEntry,
|
|
269
|
+
viewReadResolveEntry,
|
|
137
270
|
...this.mcpMiddlewareEntries,
|
|
138
271
|
];
|
|
139
272
|
if (entries.length === 0) {
|
|
@@ -150,6 +283,14 @@ export class McpServer extends McpServerBaseOmitted {
|
|
|
150
283
|
instrumentMap(requestHandlers, false);
|
|
151
284
|
instrumentMap(notificationHandlers, true);
|
|
152
285
|
}
|
|
286
|
+
/**
|
|
287
|
+
* Connect to an MCP transport (override of the SDK's `connect`). Use this
|
|
288
|
+
* when you're embedding Skybridge in a host that already manages its own
|
|
289
|
+
* transport (e.g. stdio for desktop apps); for HTTP, prefer {@link McpServer.run}
|
|
290
|
+
* which sets the transport up for you. Locks in any middleware registered
|
|
291
|
+
* via {@link McpServer.mcpMiddleware} — further calls to that method will
|
|
292
|
+
* throw afterwards.
|
|
293
|
+
*/
|
|
153
294
|
async connect(transport) {
|
|
154
295
|
this.applyMcpMiddleware();
|
|
155
296
|
return McpServerBase.prototype.connect.call(this, transport);
|
|
@@ -173,8 +314,32 @@ export class McpServer extends McpServerBaseOmitted {
|
|
|
173
314
|
target._notificationHandlers = notificationHandlers;
|
|
174
315
|
await fresh.connect(transport);
|
|
175
316
|
}
|
|
317
|
+
/**
|
|
318
|
+
* Start the HTTP server. Listens on `process.env.__PORT` (default `3000`),
|
|
319
|
+
* mounts the `/mcp` route, applies any custom Express middleware registered
|
|
320
|
+
* via {@link McpServer.use} / {@link McpServer.useOnError}, and locks in
|
|
321
|
+
* any MCP middleware registered via {@link McpServer.mcpMiddleware}.
|
|
322
|
+
*
|
|
323
|
+
* On Cloudflare Workers / workerd, returns an object exposing `fetch` so
|
|
324
|
+
* the runtime can bridge incoming requests to the Node HTTP server. On
|
|
325
|
+
* Vercel (`VERCEL === "1"`), returns the Express app directly so the
|
|
326
|
+
* serverless function entry can call it as a `(req, res)` handler. On
|
|
327
|
+
* Node, returns `undefined` once listening.
|
|
328
|
+
*/
|
|
176
329
|
async run() {
|
|
177
330
|
this.applyMcpMiddleware();
|
|
331
|
+
if (process.env.VERCEL === "1") {
|
|
332
|
+
// createApp only reads httpServer inside its dev-only branch
|
|
333
|
+
// (viewsDevServer); under VERCEL=1 + NODE_ENV=production it's a
|
|
334
|
+
// bare object passed to satisfy the required parameter.
|
|
335
|
+
const httpServer = http.createServer();
|
|
336
|
+
await createApp({
|
|
337
|
+
mcpServer: this,
|
|
338
|
+
httpServer,
|
|
339
|
+
errorMiddleware: this.customErrorMiddleware,
|
|
340
|
+
});
|
|
341
|
+
return this.express;
|
|
342
|
+
}
|
|
178
343
|
const httpServer = http.createServer();
|
|
179
344
|
await createApp({
|
|
180
345
|
mcpServer: this,
|
|
@@ -230,27 +395,12 @@ export class McpServer extends McpServerBaseOmitted {
|
|
|
230
395
|
return Array.isArray(val) ? val[0] : val;
|
|
231
396
|
};
|
|
232
397
|
const isClaude = header("user-agent") === "Claude-User";
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
serverUrl = `${proto}://${forwardedHost}`;
|
|
240
|
-
}
|
|
241
|
-
else if (origin) {
|
|
242
|
-
serverUrl = origin;
|
|
243
|
-
}
|
|
244
|
-
else if (host) {
|
|
245
|
-
const proto = ["127.0.0.1:", "localhost:"].some((p) => host.startsWith(p))
|
|
246
|
-
? "http"
|
|
247
|
-
: "https";
|
|
248
|
-
serverUrl = `${proto}://${host}`;
|
|
249
|
-
}
|
|
250
|
-
else {
|
|
251
|
-
const devPort = process.env.__PORT || "3000";
|
|
252
|
-
serverUrl = `http://localhost:${devPort}`;
|
|
253
|
-
}
|
|
398
|
+
const serverUrl = resolveServerOrigin(header);
|
|
399
|
+
// Path prefix the proxy routed this request under (e.g. `foo.com/v1`). Read
|
|
400
|
+
// per-request so one process can serve many hosts/prefixes at once: the
|
|
401
|
+
// origin is recovered from x-forwarded-host, the prefix from
|
|
402
|
+
// x-forwarded-prefix. Empty when served at the origin root.
|
|
403
|
+
const assetsBasePath = normalizeForwardedPrefix(header("x-forwarded-prefix"));
|
|
254
404
|
const connectDomains = [serverUrl];
|
|
255
405
|
if (!isProduction) {
|
|
256
406
|
const wsUrl = new URL(serverUrl);
|
|
@@ -271,114 +421,77 @@ export class McpServer extends McpServerBaseOmitted {
|
|
|
271
421
|
.slice(0, 32);
|
|
272
422
|
contentMetaOverrides = { domain: `${hash}.claudemcpcontent.com` };
|
|
273
423
|
}
|
|
274
|
-
return { serverUrl, connectDomains, contentMetaOverrides };
|
|
424
|
+
return { serverUrl, assetsBasePath, connectDomains, contentMetaOverrides };
|
|
275
425
|
}
|
|
276
426
|
registerViewResources(toolName, view, toolMeta) {
|
|
277
|
-
const hosts = view.hosts ?? ["apps-sdk", "mcp-app"];
|
|
278
427
|
// Append a content-derived version param so hosts (e.g. ChatGPT) bust
|
|
279
428
|
// their cache when the bundle changes, but keep the URI stable across
|
|
280
429
|
// `tools/list` calls when the bundle hasn't changed.
|
|
281
430
|
const versionParam = this.computeViewVersionParam(view.component);
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
431
|
+
const viewResource = {
|
|
432
|
+
hostType: "mcp-app",
|
|
433
|
+
uri: `ui://views/ext-apps/${view.component}.html${versionParam}`,
|
|
434
|
+
mimeType: "text/html;profile=mcp-app",
|
|
435
|
+
buildContentMeta: ({ resourceDomains, connectDomains, domain, baseUriDomains }, overrides) => {
|
|
436
|
+
const defaults = {
|
|
437
|
+
ui: {
|
|
438
|
+
csp: {
|
|
439
|
+
resourceDomains,
|
|
440
|
+
connectDomains,
|
|
441
|
+
baseUriDomains,
|
|
292
442
|
},
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
},
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
this.registerViewResource({
|
|
316
|
-
name: toolName,
|
|
317
|
-
viewResource,
|
|
318
|
-
view,
|
|
319
|
-
});
|
|
320
|
-
toolMeta["openai/outputTemplate"] = viewResource.uri;
|
|
321
|
-
}
|
|
322
|
-
if (hosts.includes("mcp-app")) {
|
|
323
|
-
const viewResource = {
|
|
324
|
-
hostType: "mcp-app",
|
|
325
|
-
uri: `ui://views/ext-apps/${view.component}.html${versionParam}`,
|
|
326
|
-
mimeType: "text/html;profile=mcp-app",
|
|
327
|
-
buildContentMeta: ({ resourceDomains, connectDomains, domain, baseUriDomains }, overrides) => {
|
|
328
|
-
const defaults = {
|
|
329
|
-
ui: {
|
|
330
|
-
csp: {
|
|
331
|
-
resourceDomains,
|
|
332
|
-
connectDomains,
|
|
333
|
-
baseUriDomains,
|
|
334
|
-
},
|
|
335
|
-
domain,
|
|
336
|
-
},
|
|
337
|
-
};
|
|
338
|
-
const fromView = {
|
|
339
|
-
ui: {
|
|
340
|
-
...(view.description && { description: view.description }),
|
|
341
|
-
...(view.prefersBorder !== undefined && {
|
|
342
|
-
prefersBorder: view.prefersBorder,
|
|
443
|
+
domain,
|
|
444
|
+
},
|
|
445
|
+
};
|
|
446
|
+
const fromView = {
|
|
447
|
+
ui: {
|
|
448
|
+
...(view.description && { description: view.description }),
|
|
449
|
+
...(view.prefersBorder !== undefined && {
|
|
450
|
+
prefersBorder: view.prefersBorder,
|
|
451
|
+
}),
|
|
452
|
+
...(view.domain && { domain: view.domain }),
|
|
453
|
+
csp: {
|
|
454
|
+
...(view.csp?.resourceDomains && {
|
|
455
|
+
resourceDomains: view.csp.resourceDomains,
|
|
456
|
+
}),
|
|
457
|
+
...(view.csp?.connectDomains && {
|
|
458
|
+
connectDomains: view.csp.connectDomains,
|
|
459
|
+
}),
|
|
460
|
+
...(view.csp?.frameDomains && {
|
|
461
|
+
frameDomains: view.csp.frameDomains,
|
|
462
|
+
}),
|
|
463
|
+
...(view.csp?.baseUriDomains && {
|
|
464
|
+
baseUriDomains: view.csp.baseUriDomains,
|
|
343
465
|
}),
|
|
344
|
-
...(view.domain && { domain: view.domain }),
|
|
345
|
-
csp: {
|
|
346
|
-
...(view.csp?.resourceDomains && {
|
|
347
|
-
resourceDomains: view.csp.resourceDomains,
|
|
348
|
-
}),
|
|
349
|
-
...(view.csp?.connectDomains && {
|
|
350
|
-
connectDomains: view.csp.connectDomains,
|
|
351
|
-
}),
|
|
352
|
-
...(view.csp?.frameDomains && {
|
|
353
|
-
frameDomains: view.csp.frameDomains,
|
|
354
|
-
}),
|
|
355
|
-
...(view.csp?.baseUriDomains && {
|
|
356
|
-
baseUriDomains: view.csp.baseUriDomains,
|
|
357
|
-
}),
|
|
358
|
-
...(view.csp?.redirectDomains && {
|
|
359
|
-
redirectDomains: view.csp.redirectDomains,
|
|
360
|
-
}),
|
|
361
|
-
},
|
|
362
466
|
},
|
|
363
|
-
}
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
467
|
+
},
|
|
468
|
+
};
|
|
469
|
+
const ui = mergeWithUnion(mergeWithUnion(defaults, fromView), {
|
|
470
|
+
ui: overrides,
|
|
471
|
+
});
|
|
472
|
+
const base = {
|
|
473
|
+
...ui,
|
|
474
|
+
...(view.description && {
|
|
475
|
+
"openai/widgetDescription": view.description,
|
|
476
|
+
}),
|
|
477
|
+
...(view.csp?.redirectDomains && {
|
|
478
|
+
"openai/widgetCSP": { redirect_domains: view.csp.redirectDomains },
|
|
479
|
+
}),
|
|
480
|
+
};
|
|
481
|
+
if (view._meta) {
|
|
482
|
+
return { ...base, ...view._meta };
|
|
483
|
+
}
|
|
484
|
+
return base;
|
|
485
|
+
},
|
|
486
|
+
};
|
|
487
|
+
this.registerViewResource({ name: toolName, viewResource, view });
|
|
488
|
+
// Advertise via the MCP Apps standard pointer only — ChatGPT renders from
|
|
489
|
+
// ui.resourceUri (verified), and not emitting openai/outputTemplate lets us
|
|
490
|
+
// retire the legacy apps-sdk resource later. The legacy apps-sdk URL is still
|
|
491
|
+
// served (see registerViewResource) so already-published apps keep resolving.
|
|
492
|
+
// @ts-expect-error - For backwards compatibility with Claude current implementation of the specs
|
|
493
|
+
toolMeta["ui/resourceUri"] = viewResource.uri;
|
|
494
|
+
toolMeta.ui = { ...toolMeta.ui, resourceUri: viewResource.uri };
|
|
382
495
|
}
|
|
383
496
|
registerViewResource({ name, viewResource, view, }) {
|
|
384
497
|
const { hostType, uri: viewUri, mimeType, buildContentMeta } = viewResource;
|
|
@@ -392,19 +505,25 @@ export class McpServer extends McpServerBaseOmitted {
|
|
|
392
505
|
}, contentMetaOverrides);
|
|
393
506
|
};
|
|
394
507
|
this.viewMetaBuilders.set(viewUri, buildMeta);
|
|
508
|
+
this.viewUriByPath.set(stripQuery(viewUri), viewUri);
|
|
509
|
+
this.serveLegacyAppsSdkUrl(view.component, viewUri);
|
|
395
510
|
this.registerResource(name, viewUri, { description: view.description }, async (uri, extra) => {
|
|
396
511
|
const isProduction = process.env.NODE_ENV === "production";
|
|
397
|
-
const { serverUrl } = this.resolveViewRequestContext(extra);
|
|
512
|
+
const { serverUrl, assetsBasePath } = this.resolveViewRequestContext(extra);
|
|
513
|
+
// The view resolves all assets (template imports + runtime lazy chunks
|
|
514
|
+
// via `window.skybridge.serverUrl`) against this base, so it carries the
|
|
515
|
+
// proxy path prefix. CSP domains in `buildMeta` stay the bare origin.
|
|
516
|
+
const viewBase = `${serverUrl}${assetsBasePath}`;
|
|
398
517
|
const html = isProduction
|
|
399
518
|
? templateHelper.renderProduction({
|
|
400
519
|
hostType,
|
|
401
|
-
serverUrl,
|
|
520
|
+
serverUrl: viewBase,
|
|
402
521
|
viewFile: this.lookupViewFile(view.component),
|
|
403
522
|
styleFile: this.lookupDistFile("style.css") ?? "",
|
|
404
523
|
})
|
|
405
524
|
: templateHelper.renderDevelopment({
|
|
406
525
|
hostType,
|
|
407
|
-
serverUrl,
|
|
526
|
+
serverUrl: viewBase,
|
|
408
527
|
viewName: view.component,
|
|
409
528
|
});
|
|
410
529
|
return {
|
|
@@ -414,6 +533,11 @@ export class McpServer extends McpServerBaseOmitted {
|
|
|
414
533
|
};
|
|
415
534
|
});
|
|
416
535
|
}
|
|
536
|
+
serveLegacyAppsSdkUrl(component, canonicalUri) {
|
|
537
|
+
this.viewUriByPath.set(`ui://views/apps-sdk/${component}.html`, canonicalUri);
|
|
538
|
+
this.viewUriByPath.set(`ui://widgets/apps-sdk/${component}.html`, canonicalUri);
|
|
539
|
+
this.viewUriByPath.set(`ui://widgets/ext-apps/${component}.html`, canonicalUri);
|
|
540
|
+
}
|
|
417
541
|
wrapHandler(cb, { attachViewUUID }) {
|
|
418
542
|
return async (args, extra) => {
|
|
419
543
|
const result = await cb(args, extra);
|