skybridge 0.0.0-dev.89c759f → 0.0.0-dev.89f68f1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +123 -124
- package/dist/cli/build-helpers.d.ts +7 -0
- package/dist/cli/build-helpers.js +82 -0
- package/dist/cli/build-helpers.js.map +1 -0
- package/dist/cli/build-helpers.test.d.ts +1 -0
- package/dist/cli/build-helpers.test.js +64 -0
- package/dist/cli/build-helpers.test.js.map +1 -0
- package/dist/cli/detect-port.d.ts +2 -2
- package/dist/cli/detect-port.js +9 -20
- package/dist/cli/detect-port.js.map +1 -1
- package/dist/cli/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/use-typescript-check.js +1 -1
- package/dist/cli/use-typescript-check.js.map +1 -1
- package/dist/commands/build.d.ts +0 -1
- package/dist/commands/build.js +18 -30
- package/dist/commands/build.js.map +1 -1
- package/dist/commands/create.d.ts +9 -0
- package/dist/commands/create.js +30 -0
- package/dist/commands/create.js.map +1 -0
- package/dist/commands/dev.js +16 -0
- 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/server/asset-base-url-transform-plugin.d.ts +1 -0
- package/dist/server/asset-base-url-transform-plugin.js +16 -1
- package/dist/server/asset-base-url-transform-plugin.js.map +1 -1
- package/dist/server/asset-base-url-transform-plugin.test.js +51 -1
- package/dist/server/asset-base-url-transform-plugin.test.js.map +1 -1
- package/dist/server/auth.d.ts +20 -0
- package/dist/server/auth.js +28 -0
- package/dist/server/auth.js.map +1 -0
- package/dist/server/build-manifest.test.d.ts +1 -0
- package/dist/server/build-manifest.test.js +27 -0
- package/dist/server/build-manifest.test.js.map +1 -0
- package/dist/server/content-helpers.d.ts +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 +28 -0
- package/dist/server/file-ref.js +27 -0
- package/dist/server/file-ref.js.map +1 -0
- package/dist/server/index.d.ts +4 -2
- package/dist/server/index.js +3 -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/server.d.ts +210 -6
- package/dist/server/server.js +250 -62
- package/dist/server/server.js.map +1 -1
- package/dist/server/view-resource-resolution.test.d.ts +6 -0
- package/dist/server/view-resource-resolution.test.js +88 -0
- package/dist/server/view-resource-resolution.test.js.map +1 -0
- package/dist/test/view.test.js +45 -0
- package/dist/test/view.test.js.map +1 -1
- package/dist/web/bridges/apps-sdk/adaptor.d.ts +6 -1
- package/dist/web/bridges/apps-sdk/adaptor.js +22 -1
- package/dist/web/bridges/apps-sdk/adaptor.js.map +1 -1
- package/dist/web/bridges/apps-sdk/bridge.d.ts +1 -0
- package/dist/web/bridges/apps-sdk/bridge.js +1 -0
- package/dist/web/bridges/apps-sdk/bridge.js.map +1 -1
- package/dist/web/bridges/apps-sdk/types.d.ts +2 -0
- package/dist/web/bridges/apps-sdk/types.js.map +1 -1
- package/dist/web/bridges/apps-sdk/use-apps-sdk-context.d.ts +11 -0
- package/dist/web/bridges/apps-sdk/use-apps-sdk-context.js +11 -0
- package/dist/web/bridges/apps-sdk/use-apps-sdk-context.js.map +1 -1
- package/dist/web/bridges/get-adaptor.d.ts +7 -0
- package/dist/web/bridges/get-adaptor.js +7 -0
- package/dist/web/bridges/get-adaptor.js.map +1 -1
- package/dist/web/bridges/mcp-app/adaptor.d.ts +6 -1
- package/dist/web/bridges/mcp-app/adaptor.js +20 -0
- package/dist/web/bridges/mcp-app/adaptor.js.map +1 -1
- package/dist/web/bridges/mcp-app/bridge.d.ts +4 -2
- package/dist/web/bridges/mcp-app/bridge.js +23 -1
- package/dist/web/bridges/mcp-app/bridge.js.map +1 -1
- package/dist/web/bridges/mcp-app/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 +144 -0
- package/dist/web/bridges/mcp-app/view-tools.test.js.map +1 -0
- package/dist/web/bridges/types.d.ts +94 -1
- package/dist/web/bridges/types.js.map +1 -1
- package/dist/web/bridges/use-host-context.d.ts +5 -0
- package/dist/web/bridges/use-host-context.js +5 -0
- package/dist/web/bridges/use-host-context.js.map +1 -1
- package/dist/web/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/data-llm.d.ts +33 -0
- package/dist/web/data-llm.js +28 -0
- package/dist/web/data-llm.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 +4 -0
- package/dist/web/hooks/index.js +4 -0
- package/dist/web/hooks/index.js.map +1 -1
- package/dist/web/hooks/test/utils.d.ts +6 -2
- package/dist/web/hooks/test/utils.js +13 -2
- package/dist/web/hooks/test/utils.js.map +1 -1
- package/dist/web/hooks/use-call-tool.d.ts +45 -0
- package/dist/web/hooks/use-call-tool.js +28 -0
- package/dist/web/hooks/use-call-tool.js.map +1 -1
- package/dist/web/hooks/use-call-tool.test.js +27 -2
- 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-download.d.ts +5 -0
- package/dist/web/hooks/use-download.js +8 -0
- package/dist/web/hooks/use-download.js.map +1 -0
- package/dist/web/hooks/use-download.test.d.ts +1 -0
- package/dist/web/hooks/use-download.test.js +95 -0
- package/dist/web/hooks/use-download.test.js.map +1 -0
- package/dist/web/hooks/use-files.d.ts +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-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-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-register-view-tool.d.ts +38 -0
- package/dist/web/hooks/use-register-view-tool.js +50 -0
- package/dist/web/hooks/use-register-view-tool.js.map +1 -0
- package/dist/web/hooks/use-request-close.d.ts +16 -0
- package/dist/web/hooks/use-request-close.js +21 -0
- package/dist/web/hooks/use-request-close.js.map +1 -0
- package/dist/web/hooks/use-request-close.test.d.ts +1 -0
- package/dist/web/hooks/use-request-close.test.js +52 -0
- package/dist/web/hooks/use-request-close.test.js.map +1 -0
- package/dist/web/hooks/use-request-modal.d.ts +16 -1
- package/dist/web/hooks/use-request-modal.js +16 -1
- package/dist/web/hooks/use-request-modal.js.map +1 -1
- package/dist/web/hooks/use-request-size.d.ts +20 -0
- package/dist/web/hooks/use-request-size.js +24 -0
- package/dist/web/hooks/use-request-size.js.map +1 -0
- package/dist/web/hooks/use-request-size.test.d.ts +1 -0
- package/dist/web/hooks/use-request-size.test.js +65 -0
- package/dist/web/hooks/use-request-size.test.js.map +1 -0
- package/dist/web/hooks/use-send-follow-up-message.d.ts +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-tool-info.d.ts +53 -2
- package/dist/web/hooks/use-tool-info.js +30 -7
- package/dist/web/hooks/use-tool-info.js.map +1 -1
- package/dist/web/hooks/use-tool-info.test-d.js +11 -29
- package/dist/web/hooks/use-tool-info.test-d.js.map +1 -1
- package/dist/web/hooks/use-tool-info.test.js +5 -5
- package/dist/web/hooks/use-tool-info.test.js.map +1 -1
- package/dist/web/hooks/use-user.d.ts +2 -0
- package/dist/web/hooks/use-user.js +2 -0
- package/dist/web/hooks/use-user.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/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 +33 -0
- 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 -16
package/dist/server/server.js
CHANGED
|
@@ -17,6 +17,21 @@ const mergeWithUnion = (target, source) => {
|
|
|
17
17
|
}
|
|
18
18
|
});
|
|
19
19
|
};
|
|
20
|
+
/**
|
|
21
|
+
* Drop the query string from a `ui://` view URI, leaving the bare path. The
|
|
22
|
+
* `?v=` cache key is the only query we append, so a plain split is enough and
|
|
23
|
+
* sidesteps `URL` normalization quirks on the non-special `ui:` scheme.
|
|
24
|
+
*/
|
|
25
|
+
function stripQuery(uri) {
|
|
26
|
+
const queryIndex = uri.indexOf("?");
|
|
27
|
+
return queryIndex === -1 ? uri : uri.slice(0, queryIndex);
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Coerce a tool handler's return value into an MCP `content` array. Strings
|
|
31
|
+
* become a single `TextContent`; a single block is wrapped in an array;
|
|
32
|
+
* `undefined` produces `[]`. Mostly used internally — exported so consumers
|
|
33
|
+
* who build content lazily can apply the same normalization.
|
|
34
|
+
*/
|
|
20
35
|
export function normalizeContent(content) {
|
|
21
36
|
if (content === undefined) {
|
|
22
37
|
return [];
|
|
@@ -30,13 +45,60 @@ export function normalizeContent(content) {
|
|
|
30
45
|
return [content];
|
|
31
46
|
}
|
|
32
47
|
const McpServerBaseOmitted = McpServerBase;
|
|
48
|
+
/**
|
|
49
|
+
* The Skybridge server. Extends the MCP SDK's `McpServer` with a typed tool
|
|
50
|
+
* registry, view resources, an embedded Express app, and protocol-level
|
|
51
|
+
* middleware. Construct it with the same `Implementation` info you would pass
|
|
52
|
+
* to the SDK, chain {@link McpServer.registerTool} calls to declare tools,
|
|
53
|
+
* then call {@link McpServer.run} to start the HTTP server.
|
|
54
|
+
*
|
|
55
|
+
* The `TTools` generic accumulates each registered tool's input/output/meta
|
|
56
|
+
* shape, so `typeof server` carries enough information for view-side helpers
|
|
57
|
+
* like {@link generateHelpers} to produce fully-typed hooks.
|
|
58
|
+
*
|
|
59
|
+
* @typeParam TTools - Accumulated tool registry. Filled in by `registerTool`
|
|
60
|
+
* chaining; you almost never set this manually.
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* ```ts
|
|
64
|
+
* const server = new McpServer({ name: "my-app", version: "1.0.0" }, {})
|
|
65
|
+
* .registerTool({
|
|
66
|
+
* name: "search",
|
|
67
|
+
* inputSchema: { query: z.string() },
|
|
68
|
+
* view: { component: "search" },
|
|
69
|
+
* }, async ({ query }) => ({ content: `Results for ${query}` }));
|
|
70
|
+
*
|
|
71
|
+
* await server.run();
|
|
72
|
+
* export type AppType = typeof server;
|
|
73
|
+
* ```
|
|
74
|
+
*
|
|
75
|
+
* @see https://docs.skybridge.tech/api-reference/mcp-server
|
|
76
|
+
*/
|
|
77
|
+
// Side channel populated by `dist/__entry.js` before user code is imported.
|
|
78
|
+
// Set at module scope rather than passed through the constructor because the
|
|
79
|
+
// wrapper has the manifest before the user's `new McpServer(...)` runs, and
|
|
80
|
+
// threading it through every call site (including user templates) is exactly
|
|
81
|
+
// the boilerplate this design is trying to hide.
|
|
82
|
+
let pendingBuildManifest = null;
|
|
83
|
+
/**
|
|
84
|
+
* Prime the build-time Vite manifest before user code constructs its
|
|
85
|
+
* `McpServer`. Called from the generated `dist/__entry.js`; not part of the
|
|
86
|
+
* user-facing API.
|
|
87
|
+
*
|
|
88
|
+
* @internal
|
|
89
|
+
*/
|
|
90
|
+
export function __setBuildManifest(manifest) {
|
|
91
|
+
pendingBuildManifest = manifest;
|
|
92
|
+
}
|
|
33
93
|
export class McpServer extends McpServerBaseOmitted {
|
|
34
94
|
/**
|
|
35
95
|
* The underlying Express app. Use this to extend the HTTP server with
|
|
36
96
|
* custom routes, middleware, or settings — e.g.
|
|
37
97
|
* `server.express.get("/health", ...)`.
|
|
38
98
|
*
|
|
39
|
-
* `express.json()` is pre-applied
|
|
99
|
+
* `express.json()` is pre-applied — tune it via the constructor's third
|
|
100
|
+
* argument, e.g. `new McpServer(info, {}, { json: { limit: "10mb" } })`.
|
|
101
|
+
* Register your handlers before `run()`;
|
|
40
102
|
* after `run()`, dev-mode middleware, the `/mcp` route, and the default
|
|
41
103
|
* error handler are appended in that order.
|
|
42
104
|
*
|
|
@@ -48,15 +110,32 @@ export class McpServer extends McpServerBaseOmitted {
|
|
|
48
110
|
mcpMiddlewareEntries = [];
|
|
49
111
|
mcpMiddlewareApplied = false;
|
|
50
112
|
claimedViews = new Map();
|
|
113
|
+
viewMetaBuilders = new Map();
|
|
114
|
+
/**
|
|
115
|
+
* Maps a view resource's query-less path to its canonical registered URI
|
|
116
|
+
* (the one carrying the `?v=` cache key). Lets `resources/read` resolve the
|
|
117
|
+
* underlying view no matter which version param the consumer sends, since
|
|
118
|
+
* the param is only a cache key, not part of the resource's identity.
|
|
119
|
+
*/
|
|
120
|
+
viewUriByPath = new Map();
|
|
51
121
|
viteManifest = null;
|
|
52
122
|
serverInfo;
|
|
53
123
|
serverOptions;
|
|
54
|
-
constructor(serverInfo, options) {
|
|
124
|
+
constructor(serverInfo, options, skybridgeOptions) {
|
|
55
125
|
super(serverInfo, options);
|
|
56
126
|
this.serverInfo = serverInfo;
|
|
57
127
|
this.serverOptions = options;
|
|
58
128
|
this.express = express();
|
|
59
|
-
this.express.use(express.json());
|
|
129
|
+
this.express.use(express.json(skybridgeOptions?.json));
|
|
130
|
+
// Pick up the manifest if `dist/__entry.js` primed it before importing
|
|
131
|
+
// user code. Consume-once: clear after the first construction so a
|
|
132
|
+
// subsequent test that doesn't prime can't inherit stale state.
|
|
133
|
+
// Explicit `setViteManifest` calls still win because they happen after
|
|
134
|
+
// construction.
|
|
135
|
+
if (pendingBuildManifest) {
|
|
136
|
+
this.setViteManifest(pendingBuildManifest);
|
|
137
|
+
pendingBuildManifest = null;
|
|
138
|
+
}
|
|
60
139
|
}
|
|
61
140
|
use(pathOrHandler, ...handlers) {
|
|
62
141
|
// Branching is load-bearing: Express's `app.use` overloads can't be
|
|
@@ -109,10 +188,69 @@ export class McpServer extends McpServerBaseOmitted {
|
|
|
109
188
|
return;
|
|
110
189
|
}
|
|
111
190
|
this.mcpMiddlewareApplied = true;
|
|
191
|
+
// Surface view-resource _meta on `resources/list` (per ext-apps spec:
|
|
192
|
+
// hosts/checkers read CSP & domain at list time before fetching content).
|
|
193
|
+
const viewListMetaEntry = {
|
|
194
|
+
filter: "resources/list",
|
|
195
|
+
handler: async (_req, extra, next) => {
|
|
196
|
+
const result = (await next());
|
|
197
|
+
for (const resource of result.resources) {
|
|
198
|
+
const builder = this.viewMetaBuilders.get(resource.uri);
|
|
199
|
+
if (!builder) {
|
|
200
|
+
continue;
|
|
201
|
+
}
|
|
202
|
+
const meta = builder(extra);
|
|
203
|
+
resource._meta = {
|
|
204
|
+
...(resource._meta ?? {}),
|
|
205
|
+
...meta,
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
return result;
|
|
209
|
+
},
|
|
210
|
+
};
|
|
211
|
+
// Resolve a view's `resources/read` by its query-less path so the
|
|
212
|
+
// underlying asset is served no matter the `?v=` value (stale cache key,
|
|
213
|
+
// no param, etc.). The version param is a cache-busting hint for external
|
|
214
|
+
// consumers; it must not gate resolution. We rewrite the lookup URI to the
|
|
215
|
+
// canonical registered one, then restore the requested URI on the response
|
|
216
|
+
// so the consumer-facing URI is never rewritten.
|
|
217
|
+
const viewReadResolveEntry = {
|
|
218
|
+
filter: "resources/read",
|
|
219
|
+
handler: async (req, _extra, next) => {
|
|
220
|
+
const requested = req.params.uri;
|
|
221
|
+
if (typeof requested !== "string") {
|
|
222
|
+
return next();
|
|
223
|
+
}
|
|
224
|
+
const path = stripQuery(requested);
|
|
225
|
+
const canonical = this.viewUriByPath.get(path);
|
|
226
|
+
if (!canonical) {
|
|
227
|
+
return next();
|
|
228
|
+
}
|
|
229
|
+
req.params.uri = canonical;
|
|
230
|
+
try {
|
|
231
|
+
const result = (await next());
|
|
232
|
+
for (const content of result.contents ?? []) {
|
|
233
|
+
if (typeof content.uri === "string" &&
|
|
234
|
+
stripQuery(content.uri) === path) {
|
|
235
|
+
content.uri = requested;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
return result;
|
|
239
|
+
}
|
|
240
|
+
finally {
|
|
241
|
+
// Restore the shared request params so middleware outer to us never
|
|
242
|
+
// observes the rewritten lookup URI after next() unwinds.
|
|
243
|
+
req.params.uri = requested;
|
|
244
|
+
}
|
|
245
|
+
},
|
|
246
|
+
};
|
|
112
247
|
const monitoringEntry = createMiddlewareEntry();
|
|
113
|
-
const entries =
|
|
114
|
-
? [monitoringEntry
|
|
115
|
-
|
|
248
|
+
const entries = [
|
|
249
|
+
...(monitoringEntry ? [monitoringEntry] : []),
|
|
250
|
+
viewListMetaEntry,
|
|
251
|
+
viewReadResolveEntry,
|
|
252
|
+
...this.mcpMiddlewareEntries,
|
|
253
|
+
];
|
|
116
254
|
if (entries.length === 0) {
|
|
117
255
|
return;
|
|
118
256
|
}
|
|
@@ -127,6 +265,14 @@ export class McpServer extends McpServerBaseOmitted {
|
|
|
127
265
|
instrumentMap(requestHandlers, false);
|
|
128
266
|
instrumentMap(notificationHandlers, true);
|
|
129
267
|
}
|
|
268
|
+
/**
|
|
269
|
+
* Connect to an MCP transport (override of the SDK's `connect`). Use this
|
|
270
|
+
* when you're embedding Skybridge in a host that already manages its own
|
|
271
|
+
* transport (e.g. stdio for desktop apps); for HTTP, prefer {@link McpServer.run}
|
|
272
|
+
* which sets the transport up for you. Locks in any middleware registered
|
|
273
|
+
* via {@link McpServer.mcpMiddleware} — further calls to that method will
|
|
274
|
+
* throw afterwards.
|
|
275
|
+
*/
|
|
130
276
|
async connect(transport) {
|
|
131
277
|
this.applyMcpMiddleware();
|
|
132
278
|
return McpServerBase.prototype.connect.call(this, transport);
|
|
@@ -150,8 +296,32 @@ export class McpServer extends McpServerBaseOmitted {
|
|
|
150
296
|
target._notificationHandlers = notificationHandlers;
|
|
151
297
|
await fresh.connect(transport);
|
|
152
298
|
}
|
|
299
|
+
/**
|
|
300
|
+
* Start the HTTP server. Listens on `process.env.__PORT` (default `3000`),
|
|
301
|
+
* mounts the `/mcp` route, applies any custom Express middleware registered
|
|
302
|
+
* via {@link McpServer.use} / {@link McpServer.useOnError}, and locks in
|
|
303
|
+
* any MCP middleware registered via {@link McpServer.mcpMiddleware}.
|
|
304
|
+
*
|
|
305
|
+
* On Cloudflare Workers / workerd, returns an object exposing `fetch` so
|
|
306
|
+
* the runtime can bridge incoming requests to the Node HTTP server. On
|
|
307
|
+
* Vercel (`VERCEL === "1"`), returns the Express app directly so the
|
|
308
|
+
* serverless function entry can call it as a `(req, res)` handler. On
|
|
309
|
+
* Node, returns `undefined` once listening.
|
|
310
|
+
*/
|
|
153
311
|
async run() {
|
|
154
312
|
this.applyMcpMiddleware();
|
|
313
|
+
if (process.env.VERCEL === "1") {
|
|
314
|
+
// createApp only reads httpServer inside its dev-only branch
|
|
315
|
+
// (viewsDevServer); under VERCEL=1 + NODE_ENV=production it's a
|
|
316
|
+
// bare object passed to satisfy the required parameter.
|
|
317
|
+
const httpServer = http.createServer();
|
|
318
|
+
await createApp({
|
|
319
|
+
mcpServer: this,
|
|
320
|
+
httpServer,
|
|
321
|
+
errorMiddleware: this.customErrorMiddleware,
|
|
322
|
+
});
|
|
323
|
+
return this.express;
|
|
324
|
+
}
|
|
155
325
|
const httpServer = http.createServer();
|
|
156
326
|
await createApp({
|
|
157
327
|
mcpServer: this,
|
|
@@ -199,6 +369,57 @@ export class McpServer extends McpServerBaseOmitted {
|
|
|
199
369
|
}
|
|
200
370
|
this.claimedViews.set(component, toolName);
|
|
201
371
|
}
|
|
372
|
+
resolveViewRequestContext(extra) {
|
|
373
|
+
const isProduction = process.env.NODE_ENV === "production";
|
|
374
|
+
const headers = extra?.requestInfo?.headers || {};
|
|
375
|
+
const header = (key) => {
|
|
376
|
+
const val = headers[key];
|
|
377
|
+
return Array.isArray(val) ? val[0] : val;
|
|
378
|
+
};
|
|
379
|
+
const isClaude = header("user-agent") === "Claude-User";
|
|
380
|
+
let serverUrl;
|
|
381
|
+
const forwardedHost = header("x-forwarded-host");
|
|
382
|
+
const origin = header("origin");
|
|
383
|
+
const host = header("host");
|
|
384
|
+
if (forwardedHost) {
|
|
385
|
+
const proto = header("x-forwarded-proto") || "https";
|
|
386
|
+
serverUrl = `${proto}://${forwardedHost}`;
|
|
387
|
+
}
|
|
388
|
+
else if (origin) {
|
|
389
|
+
serverUrl = origin;
|
|
390
|
+
}
|
|
391
|
+
else if (host) {
|
|
392
|
+
const proto = ["127.0.0.1:", "localhost:"].some((p) => host.startsWith(p))
|
|
393
|
+
? "http"
|
|
394
|
+
: "https";
|
|
395
|
+
serverUrl = `${proto}://${host}`;
|
|
396
|
+
}
|
|
397
|
+
else {
|
|
398
|
+
const devPort = process.env.__PORT || "3000";
|
|
399
|
+
serverUrl = `http://localhost:${devPort}`;
|
|
400
|
+
}
|
|
401
|
+
const connectDomains = [serverUrl];
|
|
402
|
+
if (!isProduction) {
|
|
403
|
+
const wsUrl = new URL(serverUrl);
|
|
404
|
+
wsUrl.protocol = wsUrl.protocol === "https:" ? "wss:" : "ws:";
|
|
405
|
+
connectDomains.push(wsUrl.origin);
|
|
406
|
+
}
|
|
407
|
+
let contentMetaOverrides = {};
|
|
408
|
+
if (isClaude) {
|
|
409
|
+
const pathname = extra?.requestInfo?.url?.pathname ?? "";
|
|
410
|
+
const rawUrl = header("x-alpic-forwarded-url") ?? `${serverUrl}${pathname}`;
|
|
411
|
+
// Strip a lone trailing slash so the hash matches the connector URL
|
|
412
|
+
// as registered with Claude (which has no trailing slash on bare origins).
|
|
413
|
+
const url = rawUrl.endsWith("/") ? rawUrl.slice(0, -1) : rawUrl;
|
|
414
|
+
const hash = crypto
|
|
415
|
+
.createHash("sha256")
|
|
416
|
+
.update(url)
|
|
417
|
+
.digest("hex")
|
|
418
|
+
.slice(0, 32);
|
|
419
|
+
contentMetaOverrides = { domain: `${hash}.claudemcpcontent.com` };
|
|
420
|
+
}
|
|
421
|
+
return { serverUrl, connectDomains, contentMetaOverrides };
|
|
422
|
+
}
|
|
202
423
|
registerViewResources(toolName, view, toolMeta) {
|
|
203
424
|
const hosts = view.hosts ?? ["apps-sdk", "mcp-app"];
|
|
204
425
|
// Append a content-derived version param so hosts (e.g. ChatGPT) bust
|
|
@@ -303,40 +524,25 @@ export class McpServer extends McpServerBaseOmitted {
|
|
|
303
524
|
});
|
|
304
525
|
// @ts-expect-error - For backwards compatibility with Claude current implementation of the specs
|
|
305
526
|
toolMeta["ui/resourceUri"] = viewResource.uri;
|
|
306
|
-
toolMeta.ui = { resourceUri: viewResource.uri };
|
|
527
|
+
toolMeta.ui = { ...toolMeta.ui, resourceUri: viewResource.uri };
|
|
307
528
|
}
|
|
308
529
|
}
|
|
309
530
|
registerViewResource({ name, viewResource, view, }) {
|
|
310
531
|
const { hostType, uri: viewUri, mimeType, buildContentMeta } = viewResource;
|
|
532
|
+
const buildMeta = (extra) => {
|
|
533
|
+
const { serverUrl, connectDomains, contentMetaOverrides } = this.resolveViewRequestContext(extra);
|
|
534
|
+
return buildContentMeta({
|
|
535
|
+
resourceDomains: [serverUrl],
|
|
536
|
+
connectDomains,
|
|
537
|
+
domain: serverUrl,
|
|
538
|
+
baseUriDomains: [serverUrl],
|
|
539
|
+
}, contentMetaOverrides);
|
|
540
|
+
};
|
|
541
|
+
this.viewMetaBuilders.set(viewUri, buildMeta);
|
|
542
|
+
this.viewUriByPath.set(stripQuery(viewUri), viewUri);
|
|
311
543
|
this.registerResource(name, viewUri, { description: view.description }, async (uri, extra) => {
|
|
312
544
|
const isProduction = process.env.NODE_ENV === "production";
|
|
313
|
-
const
|
|
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
|
-
}
|
|
545
|
+
const { serverUrl } = this.resolveViewRequestContext(extra);
|
|
340
546
|
const html = isProduction
|
|
341
547
|
? templateHelper.renderProduction({
|
|
342
548
|
hostType,
|
|
@@ -349,35 +555,9 @@ export class McpServer extends McpServerBaseOmitted {
|
|
|
349
555
|
serverUrl,
|
|
350
556
|
viewName: view.component,
|
|
351
557
|
});
|
|
352
|
-
const connectDomains = [serverUrl];
|
|
353
|
-
if (!isProduction) {
|
|
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` };
|
|
371
|
-
}
|
|
372
|
-
const contentMeta = buildContentMeta({
|
|
373
|
-
resourceDomains: [serverUrl],
|
|
374
|
-
connectDomains,
|
|
375
|
-
domain: serverUrl,
|
|
376
|
-
baseUriDomains: [serverUrl],
|
|
377
|
-
}, contentMetaOverrides);
|
|
378
558
|
return {
|
|
379
559
|
contents: [
|
|
380
|
-
{ uri: uri.href, mimeType, text: html, _meta:
|
|
560
|
+
{ uri: uri.href, mimeType, text: html, _meta: buildMeta(extra) },
|
|
381
561
|
],
|
|
382
562
|
};
|
|
383
563
|
});
|
|
@@ -454,8 +634,16 @@ export class McpServer extends McpServerBaseOmitted {
|
|
|
454
634
|
}
|
|
455
635
|
const config = args[0];
|
|
456
636
|
const cb = args[1];
|
|
457
|
-
const { name, view, _meta: userToolMeta, ...toolFields } = config;
|
|
637
|
+
const { name, view, securitySchemes, _meta: userToolMeta, ...toolFields } = config;
|
|
458
638
|
const toolMeta = { ...userToolMeta };
|
|
639
|
+
if (securitySchemes) {
|
|
640
|
+
// SEP-1488 puts `securitySchemes` at the top level of the tool
|
|
641
|
+
// descriptor, but the SDK's `registerTool` drops unknown top-level
|
|
642
|
+
// fields, so the canonical spot isn't reachable without intercepting
|
|
643
|
+
// `tools/list`. Use the `_meta` back-compat mirror documented in the
|
|
644
|
+
// Apps SDK reference until SEP-1488 lands in the spec.
|
|
645
|
+
toolMeta.securitySchemes = securitySchemes;
|
|
646
|
+
}
|
|
459
647
|
if (view) {
|
|
460
648
|
this.enforceOneToolPerView(view.component, name);
|
|
461
649
|
this.registerViewResources(name, view, toolMeta);
|