skybridge 0.0.0-dev.e8c657d → 0.0.0-dev.e8f3d89
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 +9 -0
- 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/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/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 +24 -0
- package/dist/server/auth/providers/custom.js +37 -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 +107 -0
- package/dist/server/auth/providers/custom.test.js.map +1 -0
- package/dist/server/auth/providers/descope.d.ts +13 -0
- package/dist/server/auth/providers/descope.js +31 -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 +37 -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/auth.d.ts +1 -0
- package/dist/server/auth.js +1 -0
- package/dist/server/auth.js.map +1 -1
- 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 +10 -3
- package/dist/server/index.js +8 -2
- 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 +161 -4
- package/dist/server/server.js +225 -60
- 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 +4 -1
- package/dist/web/bridges/apps-sdk/adaptor.js +9 -0
- 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/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 +4 -1
- package/dist/web/bridges/mcp-app/adaptor.js +12 -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 +88 -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 +2 -0
- package/dist/web/hooks/index.js +2 -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-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 +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-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 +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-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 +26 -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 +6 -3
package/dist/server/server.d.ts
CHANGED
|
@@ -4,14 +4,28 @@ import { McpServer as McpServerBase } from "@modelcontextprotocol/sdk/server/mcp
|
|
|
4
4
|
import type { AnySchema, SchemaOutput, ZodRawShapeCompat } from "@modelcontextprotocol/sdk/server/zod-compat.js";
|
|
5
5
|
import type { RequestHandlerExtra } from "@modelcontextprotocol/sdk/shared/protocol.js";
|
|
6
6
|
import type { ContentBlock, Implementation, RequestMeta, ServerNotification, ServerRequest, ServerResult, ToolAnnotations } from "@modelcontextprotocol/sdk/types.js";
|
|
7
|
-
import { type ErrorRequestHandler, type Express, type RequestHandler } from "express";
|
|
7
|
+
import express, { type ErrorRequestHandler, type Express, type RequestHandler } from "express";
|
|
8
|
+
import type { OAuthConfig } from "./auth/index.js";
|
|
8
9
|
import type { McpExtra, McpExtraFor, McpMethodString, McpMiddlewareFilter, McpMiddlewareFn, McpResultFor, McpTypedMiddlewareFn, McpWildcard } from "./middleware.js";
|
|
10
|
+
/**
|
|
11
|
+
* Type marker for a registered tool — carries its input, output, and response
|
|
12
|
+
* metadata shapes so views can infer types from `typeof server`.
|
|
13
|
+
*
|
|
14
|
+
* You normally never construct this by hand; it is produced by `registerTool`
|
|
15
|
+
* and consumed by helpers like {@link InferTools} and {@link generateHelpers}.
|
|
16
|
+
*/
|
|
9
17
|
export type ToolDef<TInput = unknown, TOutput = unknown, TResponseMetadata = unknown> = {
|
|
10
18
|
input: TInput;
|
|
11
19
|
output: TOutput;
|
|
12
20
|
responseMetadata: TResponseMetadata;
|
|
13
21
|
};
|
|
22
|
+
/** Which host runtime a view targets — `"apps-sdk"` (ChatGPT) or `"mcp-app"` (MCP Apps spec). */
|
|
14
23
|
export type ViewHostType = "apps-sdk" | "mcp-app";
|
|
24
|
+
/**
|
|
25
|
+
* Content Security Policy origins attached to a view's resource. Each list is
|
|
26
|
+
* passed through to the host's CSP for the view iframe; omit a field to inherit
|
|
27
|
+
* the host's default for that directive.
|
|
28
|
+
*/
|
|
15
29
|
export interface ViewCsp {
|
|
16
30
|
/** Origins for static assets (images, fonts, scripts, styles). */
|
|
17
31
|
resourceDomains?: string[];
|
|
@@ -24,16 +38,33 @@ export interface ViewCsp {
|
|
|
24
38
|
/** Origins allowed in `<base href>` tags (mcp-apps only). */
|
|
25
39
|
baseUriDomains?: string[];
|
|
26
40
|
}
|
|
41
|
+
/**
|
|
42
|
+
* Registry of view component names. The Skybridge Vite plugin augments this
|
|
43
|
+
* interface in the generated `.skybridge/views.d.ts` with one key per view
|
|
44
|
+
* file, which narrows {@link ViewName} from `string` to the concrete union.
|
|
45
|
+
*/
|
|
27
46
|
export interface ViewNameRegistry {
|
|
28
47
|
}
|
|
48
|
+
/** Union of valid view component names. Narrowed by {@link ViewNameRegistry}. */
|
|
29
49
|
export type ViewName = keyof ViewNameRegistry & string;
|
|
50
|
+
/**
|
|
51
|
+
* Pass under `view` in a tool's `registerTool` config to render the tool's
|
|
52
|
+
* result through a Skybridge view instead of a plain text response.
|
|
53
|
+
*/
|
|
30
54
|
export interface ViewConfig {
|
|
55
|
+
/** Filename of the view module (without extension) — matches a file in your `viewsDir`. */
|
|
31
56
|
component: ViewName;
|
|
57
|
+
/** Human-readable label the host may show alongside the view. */
|
|
32
58
|
description?: string;
|
|
59
|
+
/** Restrict where the view is rendered. Defaults to all known hosts. */
|
|
33
60
|
hosts?: ViewHostType[];
|
|
61
|
+
/** Apps SDK only: request a visible border around the widget. */
|
|
34
62
|
prefersBorder?: boolean;
|
|
63
|
+
/** Apps SDK only: override the iframe's served domain (advanced). */
|
|
35
64
|
domain?: string;
|
|
65
|
+
/** Per-view CSP overrides — see {@link ViewCsp}. */
|
|
36
66
|
csp?: ViewCsp;
|
|
67
|
+
/** Free-form metadata forwarded on the view resource's `_meta`. */
|
|
37
68
|
_meta?: Record<string, unknown>;
|
|
38
69
|
}
|
|
39
70
|
export type SecurityScheme = {
|
|
@@ -42,15 +73,44 @@ export type SecurityScheme = {
|
|
|
42
73
|
type: "oauth2";
|
|
43
74
|
scopes?: string[];
|
|
44
75
|
};
|
|
76
|
+
/**
|
|
77
|
+
* Options forwarded to the built-in `express.json()` body parser. Derived
|
|
78
|
+
* from Express's own types so the public API doesn't depend on `body-parser`.
|
|
79
|
+
*/
|
|
80
|
+
export type JsonOptions = NonNullable<Parameters<typeof express.json>[0]>;
|
|
81
|
+
/** Skybridge-specific server options, passed as the third `McpServer` constructor argument. */
|
|
82
|
+
export interface SkybridgeServerOptions {
|
|
83
|
+
/** Options for the built-in `express.json()` middleware, e.g. `{ limit: "10mb" }`. */
|
|
84
|
+
json?: JsonOptions;
|
|
85
|
+
/** Resource-server OAuth config. When set, mounts well-known metadata and bearer auth on `/mcp`. */
|
|
86
|
+
oauth?: OAuthConfig;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Well-known keys recognized by host runtimes when set on a tool's `_meta`.
|
|
90
|
+
* Use {@link ToolMeta} to also pass arbitrary custom metadata alongside these.
|
|
91
|
+
*
|
|
92
|
+
* @see https://developers.openai.com/apps-sdk/reference#tool-descriptor-parameters
|
|
93
|
+
*/
|
|
45
94
|
export interface KnownToolMeta {
|
|
95
|
+
/** Apps SDK: allow the rendered view to call this tool from inside its iframe. */
|
|
46
96
|
"openai/widgetAccessible"?: boolean;
|
|
97
|
+
/** Apps SDK: status text shown while the tool is running (e.g. `"Searching trips"`). */
|
|
47
98
|
"openai/toolInvocation/invoking"?: string;
|
|
99
|
+
/** Apps SDK: status text shown once the tool returns (e.g. `"Found 3 trips"`). */
|
|
48
100
|
"openai/toolInvocation/invoked"?: string;
|
|
101
|
+
/** Apps SDK: input parameters that hold file references — the host attaches uploaded files to them. */
|
|
49
102
|
"openai/fileParams"?: string[];
|
|
103
|
+
/** MCP Apps: control whether the tool is exposed to the model, the app, or both. */
|
|
50
104
|
ui?: Pick<McpUiToolMeta, "visibility">;
|
|
51
105
|
securitySchemes?: SecurityScheme[];
|
|
52
106
|
}
|
|
107
|
+
/** {@link KnownToolMeta} merged with arbitrary string-keyed metadata for custom flags. */
|
|
53
108
|
export type ToolMeta = KnownToolMeta & Record<string, unknown>;
|
|
109
|
+
/**
|
|
110
|
+
* Convenient return type for tool handlers — a plain string, a single
|
|
111
|
+
* {@link ContentBlock}, or an array. Skybridge normalizes it to the MCP
|
|
112
|
+
* `content: ContentBlock[]` shape before responding.
|
|
113
|
+
*/
|
|
54
114
|
export type HandlerContent = string | ContentBlock | ContentBlock[];
|
|
55
115
|
/**
|
|
56
116
|
* Type-level marker interface for cross-package type inference.
|
|
@@ -141,10 +201,26 @@ type ToolHandler<TInput extends ZodRawShapeCompat, TReturn extends {
|
|
|
141
201
|
} = {
|
|
142
202
|
content?: HandlerContent;
|
|
143
203
|
}> = (args: ShapeOutput<TInput>, extra: ToolHandlerExtra) => TReturn | Promise<TReturn>;
|
|
204
|
+
/**
|
|
205
|
+
* Coerce a tool handler's return value into an MCP `content` array. Strings
|
|
206
|
+
* become a single `TextContent`; a single block is wrapped in an array;
|
|
207
|
+
* `undefined` produces `[]`. Mostly used internally — exported so consumers
|
|
208
|
+
* who build content lazily can apply the same normalization.
|
|
209
|
+
*/
|
|
144
210
|
export declare function normalizeContent(content: HandlerContent | undefined): ContentBlock[];
|
|
145
211
|
interface McpServerBaseOmitted extends Omit<McpServerBase, "registerTool" | "connect"> {
|
|
146
212
|
}
|
|
147
213
|
declare const McpServerBaseOmitted: new (...args: ConstructorParameters<typeof McpServerBase>) => McpServerBaseOmitted;
|
|
214
|
+
/**
|
|
215
|
+
* Prime the build-time Vite manifest before user code constructs its
|
|
216
|
+
* `McpServer`. Called from the generated `dist/__entry.js`; not part of the
|
|
217
|
+
* user-facing API.
|
|
218
|
+
*
|
|
219
|
+
* @internal
|
|
220
|
+
*/
|
|
221
|
+
export declare function __setBuildManifest(manifest: Record<string, {
|
|
222
|
+
file: string;
|
|
223
|
+
}>): void;
|
|
148
224
|
export declare class McpServer<TTools extends Record<string, ToolDef> = Record<never, ToolDef>> extends McpServerBaseOmitted {
|
|
149
225
|
readonly $types: McpServerTypes<TTools>;
|
|
150
226
|
/**
|
|
@@ -152,7 +228,9 @@ export declare class McpServer<TTools extends Record<string, ToolDef> = Record<n
|
|
|
152
228
|
* custom routes, middleware, or settings — e.g.
|
|
153
229
|
* `server.express.get("/health", ...)`.
|
|
154
230
|
*
|
|
155
|
-
* `express.json()` is pre-applied
|
|
231
|
+
* `express.json()` is pre-applied — tune it via the constructor's third
|
|
232
|
+
* argument, e.g. `new McpServer(info, {}, { json: { limit: "10mb" } })`.
|
|
233
|
+
* Register your handlers before `run()`;
|
|
156
234
|
* after `run()`, dev-mode middleware, the `/mcp` route, and the default
|
|
157
235
|
* error handler are appended in that order.
|
|
158
236
|
*
|
|
@@ -164,12 +242,41 @@ export declare class McpServer<TTools extends Record<string, ToolDef> = Record<n
|
|
|
164
242
|
private mcpMiddlewareEntries;
|
|
165
243
|
private mcpMiddlewareApplied;
|
|
166
244
|
private claimedViews;
|
|
245
|
+
private viewMetaBuilders;
|
|
246
|
+
/**
|
|
247
|
+
* Maps a view resource's query-less path to its canonical registered URI
|
|
248
|
+
* (the one carrying the `?v=` cache key). Lets `resources/read` resolve the
|
|
249
|
+
* underlying view no matter which version param the consumer sends, since
|
|
250
|
+
* the param is only a cache key, not part of the resource's identity.
|
|
251
|
+
*/
|
|
252
|
+
private viewUriByPath;
|
|
167
253
|
private viteManifest;
|
|
168
254
|
private readonly serverInfo;
|
|
169
255
|
private readonly serverOptions?;
|
|
170
|
-
constructor(serverInfo: Implementation, options?: ServerOptions);
|
|
256
|
+
constructor(serverInfo: Implementation, options?: ServerOptions, skybridgeOptions?: SkybridgeServerOptions);
|
|
257
|
+
/**
|
|
258
|
+
* Register Express middleware on the underlying app. Mirrors `app.use` —
|
|
259
|
+
* pass handlers directly or a path-prefixed handler list. Register before
|
|
260
|
+
* {@link McpServer.run}; ordering matches Express.
|
|
261
|
+
*
|
|
262
|
+
* Note: Alpic Cloud only routes traffic to `/mcp`. Custom paths work
|
|
263
|
+
* locally and on self-hosted deployments.
|
|
264
|
+
*/
|
|
171
265
|
use(...handlers: RequestHandler[]): this;
|
|
172
266
|
use(path: string, ...handlers: RequestHandler[]): this;
|
|
267
|
+
/**
|
|
268
|
+
* Register Express error-handling middleware to run after the built-in
|
|
269
|
+
* `/mcp` route (or your custom route). Use this to log or transform errors
|
|
270
|
+
* thrown by tool handlers before the default error handler responds.
|
|
271
|
+
*
|
|
272
|
+
* @example
|
|
273
|
+
* ```ts
|
|
274
|
+
* server.useOnError((err, _req, _res, next) => {
|
|
275
|
+
* logger.error(err);
|
|
276
|
+
* next(err);
|
|
277
|
+
* });
|
|
278
|
+
* ```
|
|
279
|
+
*/
|
|
173
280
|
useOnError(...handlers: ErrorRequestHandler[]): this;
|
|
174
281
|
useOnError(path: string, ...handlers: ErrorRequestHandler[]): this;
|
|
175
282
|
/** Register MCP protocol-level middleware (catch-all). */
|
|
@@ -204,6 +311,14 @@ export declare class McpServer<TTools extends Record<string, ToolDef> = Record<n
|
|
|
204
311
|
*/
|
|
205
312
|
mcpMiddleware(filter: McpMiddlewareFilter, handler: McpMiddlewareFn): this;
|
|
206
313
|
private applyMcpMiddleware;
|
|
314
|
+
/**
|
|
315
|
+
* Connect to an MCP transport (override of the SDK's `connect`). Use this
|
|
316
|
+
* when you're embedding Skybridge in a host that already manages its own
|
|
317
|
+
* transport (e.g. stdio for desktop apps); for HTTP, prefer {@link McpServer.run}
|
|
318
|
+
* which sets the transport up for you. Locks in any middleware registered
|
|
319
|
+
* via {@link McpServer.mcpMiddleware} — further calls to that method will
|
|
320
|
+
* throw afterwards.
|
|
321
|
+
*/
|
|
207
322
|
connect(transport: Parameters<typeof McpServerBase.prototype.connect>[0]): Promise<void>;
|
|
208
323
|
/**
|
|
209
324
|
* Per-request stateless connect. The SDK's `Protocol` only allows one
|
|
@@ -216,10 +331,23 @@ export declare class McpServer<TTools extends Record<string, ToolDef> = Record<n
|
|
|
216
331
|
* read side and fails fast on SDK field renames.
|
|
217
332
|
*/
|
|
218
333
|
connectStatelessTransport(transport: Parameters<typeof McpServerBase.prototype.connect>[0]): Promise<void>;
|
|
334
|
+
/**
|
|
335
|
+
* Start the HTTP server. Listens on `process.env.__PORT` (default `3000`),
|
|
336
|
+
* mounts the `/mcp` route, applies any custom Express middleware registered
|
|
337
|
+
* via {@link McpServer.use} / {@link McpServer.useOnError}, and locks in
|
|
338
|
+
* any MCP middleware registered via {@link McpServer.mcpMiddleware}.
|
|
339
|
+
*
|
|
340
|
+
* On Cloudflare Workers / workerd, returns an object exposing `fetch` so
|
|
341
|
+
* the runtime can bridge incoming requests to the Node HTTP server. On
|
|
342
|
+
* Vercel (`VERCEL === "1"`), returns the Express app directly so the
|
|
343
|
+
* serverless function entry can call it as a `(req, res)` handler. On
|
|
344
|
+
* Node, returns `undefined` once listening.
|
|
345
|
+
*/
|
|
219
346
|
run(): Promise<{
|
|
220
347
|
fetch: (...args: unknown[]) => unknown;
|
|
221
|
-
} | undefined>;
|
|
348
|
+
} | Express | undefined>;
|
|
222
349
|
private enforceOneToolPerView;
|
|
350
|
+
private resolveViewRequestContext;
|
|
223
351
|
private registerViewResources;
|
|
224
352
|
private registerViewResource;
|
|
225
353
|
private wrapHandler;
|
|
@@ -236,6 +364,35 @@ export declare class McpServer<TTools extends Record<string, ToolDef> = Record<n
|
|
|
236
364
|
file: string;
|
|
237
365
|
}>): this;
|
|
238
366
|
private readManifest;
|
|
367
|
+
/**
|
|
368
|
+
* Register a tool. Pass a `config` describing the tool (name, schemas,
|
|
369
|
+
* optional {@link ViewConfig}, optional {@link ToolMeta}) and a handler that
|
|
370
|
+
* returns the tool's result.
|
|
371
|
+
*
|
|
372
|
+
* Chain calls to build up a server: each call returns a new `McpServer`
|
|
373
|
+
* type that captures the tool's input/output/`_meta` shape so the
|
|
374
|
+
* resulting `typeof server` can drive {@link generateHelpers}.
|
|
375
|
+
*
|
|
376
|
+
* The handler's return shape determines the output types: the
|
|
377
|
+
* `structuredContent` field becomes the tool's typed output, and `_meta`
|
|
378
|
+
* becomes its `responseMetadata`. The `content` field is normalized through
|
|
379
|
+
* {@link normalizeContent}.
|
|
380
|
+
*
|
|
381
|
+
* @example
|
|
382
|
+
* ```ts
|
|
383
|
+
* server.registerTool({
|
|
384
|
+
* name: "search",
|
|
385
|
+
* inputSchema: { query: z.string() },
|
|
386
|
+
* outputSchema: { results: z.array(z.string()) },
|
|
387
|
+
* view: { component: "search" },
|
|
388
|
+
* }, async ({ query }) => ({
|
|
389
|
+
* content: `Found results for ${query}`,
|
|
390
|
+
* structuredContent: { results: [...] },
|
|
391
|
+
* }));
|
|
392
|
+
* ```
|
|
393
|
+
*
|
|
394
|
+
* @see https://docs.skybridge.tech/api-reference/register-tool
|
|
395
|
+
*/
|
|
239
396
|
registerTool<TName extends string, InputArgs extends ZodRawShapeCompat, TReturn extends {
|
|
240
397
|
content?: HandlerContent;
|
|
241
398
|
}>(config: ToolConfig<InputArgs> & {
|
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,21 @@ const mergeWithUnion = (target, source) => {
|
|
|
17
19
|
}
|
|
18
20
|
});
|
|
19
21
|
};
|
|
22
|
+
/**
|
|
23
|
+
* Drop the query string from a `ui://` view URI, leaving the bare path. The
|
|
24
|
+
* `?v=` cache key is the only query we append, so a plain split is enough and
|
|
25
|
+
* sidesteps `URL` normalization quirks on the non-special `ui:` scheme.
|
|
26
|
+
*/
|
|
27
|
+
function stripQuery(uri) {
|
|
28
|
+
const queryIndex = uri.indexOf("?");
|
|
29
|
+
return queryIndex === -1 ? uri : uri.slice(0, queryIndex);
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Coerce a tool handler's return value into an MCP `content` array. Strings
|
|
33
|
+
* become a single `TextContent`; a single block is wrapped in an array;
|
|
34
|
+
* `undefined` produces `[]`. Mostly used internally — exported so consumers
|
|
35
|
+
* who build content lazily can apply the same normalization.
|
|
36
|
+
*/
|
|
20
37
|
export function normalizeContent(content) {
|
|
21
38
|
if (content === undefined) {
|
|
22
39
|
return [];
|
|
@@ -30,13 +47,60 @@ export function normalizeContent(content) {
|
|
|
30
47
|
return [content];
|
|
31
48
|
}
|
|
32
49
|
const McpServerBaseOmitted = McpServerBase;
|
|
50
|
+
/**
|
|
51
|
+
* The Skybridge server. Extends the MCP SDK's `McpServer` with a typed tool
|
|
52
|
+
* registry, view resources, an embedded Express app, and protocol-level
|
|
53
|
+
* middleware. Construct it with the same `Implementation` info you would pass
|
|
54
|
+
* to the SDK, chain {@link McpServer.registerTool} calls to declare tools,
|
|
55
|
+
* then call {@link McpServer.run} to start the HTTP server.
|
|
56
|
+
*
|
|
57
|
+
* The `TTools` generic accumulates each registered tool's input/output/meta
|
|
58
|
+
* shape, so `typeof server` carries enough information for view-side helpers
|
|
59
|
+
* like {@link generateHelpers} to produce fully-typed hooks.
|
|
60
|
+
*
|
|
61
|
+
* @typeParam TTools - Accumulated tool registry. Filled in by `registerTool`
|
|
62
|
+
* chaining; you almost never set this manually.
|
|
63
|
+
*
|
|
64
|
+
* @example
|
|
65
|
+
* ```ts
|
|
66
|
+
* const server = new McpServer({ name: "my-app", version: "1.0.0" }, {})
|
|
67
|
+
* .registerTool({
|
|
68
|
+
* name: "search",
|
|
69
|
+
* inputSchema: { query: z.string() },
|
|
70
|
+
* view: { component: "search" },
|
|
71
|
+
* }, async ({ query }) => ({ content: `Results for ${query}` }));
|
|
72
|
+
*
|
|
73
|
+
* await server.run();
|
|
74
|
+
* export type AppType = typeof server;
|
|
75
|
+
* ```
|
|
76
|
+
*
|
|
77
|
+
* @see https://docs.skybridge.tech/api-reference/mcp-server
|
|
78
|
+
*/
|
|
79
|
+
// Side channel populated by `dist/__entry.js` before user code is imported.
|
|
80
|
+
// Set at module scope rather than passed through the constructor because the
|
|
81
|
+
// wrapper has the manifest before the user's `new McpServer(...)` runs, and
|
|
82
|
+
// threading it through every call site (including user templates) is exactly
|
|
83
|
+
// the boilerplate this design is trying to hide.
|
|
84
|
+
let pendingBuildManifest = null;
|
|
85
|
+
/**
|
|
86
|
+
* Prime the build-time Vite manifest before user code constructs its
|
|
87
|
+
* `McpServer`. Called from the generated `dist/__entry.js`; not part of the
|
|
88
|
+
* user-facing API.
|
|
89
|
+
*
|
|
90
|
+
* @internal
|
|
91
|
+
*/
|
|
92
|
+
export function __setBuildManifest(manifest) {
|
|
93
|
+
pendingBuildManifest = manifest;
|
|
94
|
+
}
|
|
33
95
|
export class McpServer extends McpServerBaseOmitted {
|
|
34
96
|
/**
|
|
35
97
|
* The underlying Express app. Use this to extend the HTTP server with
|
|
36
98
|
* custom routes, middleware, or settings — e.g.
|
|
37
99
|
* `server.express.get("/health", ...)`.
|
|
38
100
|
*
|
|
39
|
-
* `express.json()` is pre-applied
|
|
101
|
+
* `express.json()` is pre-applied — tune it via the constructor's third
|
|
102
|
+
* argument, e.g. `new McpServer(info, {}, { json: { limit: "10mb" } })`.
|
|
103
|
+
* Register your handlers before `run()`;
|
|
40
104
|
* after `run()`, dev-mode middleware, the `/mcp` route, and the default
|
|
41
105
|
* error handler are appended in that order.
|
|
42
106
|
*
|
|
@@ -48,15 +112,35 @@ export class McpServer extends McpServerBaseOmitted {
|
|
|
48
112
|
mcpMiddlewareEntries = [];
|
|
49
113
|
mcpMiddlewareApplied = false;
|
|
50
114
|
claimedViews = new Map();
|
|
115
|
+
viewMetaBuilders = new Map();
|
|
116
|
+
/**
|
|
117
|
+
* Maps a view resource's query-less path to its canonical registered URI
|
|
118
|
+
* (the one carrying the `?v=` cache key). Lets `resources/read` resolve the
|
|
119
|
+
* underlying view no matter which version param the consumer sends, since
|
|
120
|
+
* the param is only a cache key, not part of the resource's identity.
|
|
121
|
+
*/
|
|
122
|
+
viewUriByPath = new Map();
|
|
51
123
|
viteManifest = null;
|
|
52
124
|
serverInfo;
|
|
53
125
|
serverOptions;
|
|
54
|
-
constructor(serverInfo, options) {
|
|
126
|
+
constructor(serverInfo, options, skybridgeOptions) {
|
|
55
127
|
super(serverInfo, options);
|
|
56
128
|
this.serverInfo = serverInfo;
|
|
57
129
|
this.serverOptions = options;
|
|
58
130
|
this.express = express();
|
|
59
|
-
this.express.use(express.json());
|
|
131
|
+
this.express.use(express.json(skybridgeOptions?.json));
|
|
132
|
+
if (skybridgeOptions?.oauth) {
|
|
133
|
+
setupOAuth(this.express, skybridgeOptions.oauth);
|
|
134
|
+
}
|
|
135
|
+
// Pick up the manifest if `dist/__entry.js` primed it before importing
|
|
136
|
+
// user code. Consume-once: clear after the first construction so a
|
|
137
|
+
// subsequent test that doesn't prime can't inherit stale state.
|
|
138
|
+
// Explicit `setViteManifest` calls still win because they happen after
|
|
139
|
+
// construction.
|
|
140
|
+
if (pendingBuildManifest) {
|
|
141
|
+
this.setViteManifest(pendingBuildManifest);
|
|
142
|
+
pendingBuildManifest = null;
|
|
143
|
+
}
|
|
60
144
|
}
|
|
61
145
|
use(pathOrHandler, ...handlers) {
|
|
62
146
|
// Branching is load-bearing: Express's `app.use` overloads can't be
|
|
@@ -109,10 +193,69 @@ export class McpServer extends McpServerBaseOmitted {
|
|
|
109
193
|
return;
|
|
110
194
|
}
|
|
111
195
|
this.mcpMiddlewareApplied = true;
|
|
196
|
+
// Surface view-resource _meta on `resources/list` (per ext-apps spec:
|
|
197
|
+
// hosts/checkers read CSP & domain at list time before fetching content).
|
|
198
|
+
const viewListMetaEntry = {
|
|
199
|
+
filter: "resources/list",
|
|
200
|
+
handler: async (_req, extra, next) => {
|
|
201
|
+
const result = (await next());
|
|
202
|
+
for (const resource of result.resources) {
|
|
203
|
+
const builder = this.viewMetaBuilders.get(resource.uri);
|
|
204
|
+
if (!builder) {
|
|
205
|
+
continue;
|
|
206
|
+
}
|
|
207
|
+
const meta = builder(extra);
|
|
208
|
+
resource._meta = {
|
|
209
|
+
...(resource._meta ?? {}),
|
|
210
|
+
...meta,
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
return result;
|
|
214
|
+
},
|
|
215
|
+
};
|
|
216
|
+
// Resolve a view's `resources/read` by its query-less path so the
|
|
217
|
+
// underlying asset is served no matter the `?v=` value (stale cache key,
|
|
218
|
+
// no param, etc.). The version param is a cache-busting hint for external
|
|
219
|
+
// consumers; it must not gate resolution. We rewrite the lookup URI to the
|
|
220
|
+
// canonical registered one, then restore the requested URI on the response
|
|
221
|
+
// so the consumer-facing URI is never rewritten.
|
|
222
|
+
const viewReadResolveEntry = {
|
|
223
|
+
filter: "resources/read",
|
|
224
|
+
handler: async (req, _extra, next) => {
|
|
225
|
+
const requested = req.params.uri;
|
|
226
|
+
if (typeof requested !== "string") {
|
|
227
|
+
return next();
|
|
228
|
+
}
|
|
229
|
+
const path = stripQuery(requested);
|
|
230
|
+
const canonical = this.viewUriByPath.get(path);
|
|
231
|
+
if (!canonical) {
|
|
232
|
+
return next();
|
|
233
|
+
}
|
|
234
|
+
req.params.uri = canonical;
|
|
235
|
+
try {
|
|
236
|
+
const result = (await next());
|
|
237
|
+
for (const content of result.contents ?? []) {
|
|
238
|
+
if (typeof content.uri === "string" &&
|
|
239
|
+
stripQuery(content.uri) === path) {
|
|
240
|
+
content.uri = requested;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
return result;
|
|
244
|
+
}
|
|
245
|
+
finally {
|
|
246
|
+
// Restore the shared request params so middleware outer to us never
|
|
247
|
+
// observes the rewritten lookup URI after next() unwinds.
|
|
248
|
+
req.params.uri = requested;
|
|
249
|
+
}
|
|
250
|
+
},
|
|
251
|
+
};
|
|
112
252
|
const monitoringEntry = createMiddlewareEntry();
|
|
113
|
-
const entries =
|
|
114
|
-
? [monitoringEntry
|
|
115
|
-
|
|
253
|
+
const entries = [
|
|
254
|
+
...(monitoringEntry ? [monitoringEntry] : []),
|
|
255
|
+
viewListMetaEntry,
|
|
256
|
+
viewReadResolveEntry,
|
|
257
|
+
...this.mcpMiddlewareEntries,
|
|
258
|
+
];
|
|
116
259
|
if (entries.length === 0) {
|
|
117
260
|
return;
|
|
118
261
|
}
|
|
@@ -127,6 +270,14 @@ export class McpServer extends McpServerBaseOmitted {
|
|
|
127
270
|
instrumentMap(requestHandlers, false);
|
|
128
271
|
instrumentMap(notificationHandlers, true);
|
|
129
272
|
}
|
|
273
|
+
/**
|
|
274
|
+
* Connect to an MCP transport (override of the SDK's `connect`). Use this
|
|
275
|
+
* when you're embedding Skybridge in a host that already manages its own
|
|
276
|
+
* transport (e.g. stdio for desktop apps); for HTTP, prefer {@link McpServer.run}
|
|
277
|
+
* which sets the transport up for you. Locks in any middleware registered
|
|
278
|
+
* via {@link McpServer.mcpMiddleware} — further calls to that method will
|
|
279
|
+
* throw afterwards.
|
|
280
|
+
*/
|
|
130
281
|
async connect(transport) {
|
|
131
282
|
this.applyMcpMiddleware();
|
|
132
283
|
return McpServerBase.prototype.connect.call(this, transport);
|
|
@@ -150,8 +301,32 @@ export class McpServer extends McpServerBaseOmitted {
|
|
|
150
301
|
target._notificationHandlers = notificationHandlers;
|
|
151
302
|
await fresh.connect(transport);
|
|
152
303
|
}
|
|
304
|
+
/**
|
|
305
|
+
* Start the HTTP server. Listens on `process.env.__PORT` (default `3000`),
|
|
306
|
+
* mounts the `/mcp` route, applies any custom Express middleware registered
|
|
307
|
+
* via {@link McpServer.use} / {@link McpServer.useOnError}, and locks in
|
|
308
|
+
* any MCP middleware registered via {@link McpServer.mcpMiddleware}.
|
|
309
|
+
*
|
|
310
|
+
* On Cloudflare Workers / workerd, returns an object exposing `fetch` so
|
|
311
|
+
* the runtime can bridge incoming requests to the Node HTTP server. On
|
|
312
|
+
* Vercel (`VERCEL === "1"`), returns the Express app directly so the
|
|
313
|
+
* serverless function entry can call it as a `(req, res)` handler. On
|
|
314
|
+
* Node, returns `undefined` once listening.
|
|
315
|
+
*/
|
|
153
316
|
async run() {
|
|
154
317
|
this.applyMcpMiddleware();
|
|
318
|
+
if (process.env.VERCEL === "1") {
|
|
319
|
+
// createApp only reads httpServer inside its dev-only branch
|
|
320
|
+
// (viewsDevServer); under VERCEL=1 + NODE_ENV=production it's a
|
|
321
|
+
// bare object passed to satisfy the required parameter.
|
|
322
|
+
const httpServer = http.createServer();
|
|
323
|
+
await createApp({
|
|
324
|
+
mcpServer: this,
|
|
325
|
+
httpServer,
|
|
326
|
+
errorMiddleware: this.customErrorMiddleware,
|
|
327
|
+
});
|
|
328
|
+
return this.express;
|
|
329
|
+
}
|
|
155
330
|
const httpServer = http.createServer();
|
|
156
331
|
await createApp({
|
|
157
332
|
mcpServer: this,
|
|
@@ -199,6 +374,37 @@ export class McpServer extends McpServerBaseOmitted {
|
|
|
199
374
|
}
|
|
200
375
|
this.claimedViews.set(component, toolName);
|
|
201
376
|
}
|
|
377
|
+
resolveViewRequestContext(extra) {
|
|
378
|
+
const isProduction = process.env.NODE_ENV === "production";
|
|
379
|
+
const headers = extra?.requestInfo?.headers || {};
|
|
380
|
+
const header = (key) => {
|
|
381
|
+
const val = headers[key];
|
|
382
|
+
return Array.isArray(val) ? val[0] : val;
|
|
383
|
+
};
|
|
384
|
+
const isClaude = header("user-agent") === "Claude-User";
|
|
385
|
+
const serverUrl = resolveServerOrigin(header);
|
|
386
|
+
const connectDomains = [serverUrl];
|
|
387
|
+
if (!isProduction) {
|
|
388
|
+
const wsUrl = new URL(serverUrl);
|
|
389
|
+
wsUrl.protocol = wsUrl.protocol === "https:" ? "wss:" : "ws:";
|
|
390
|
+
connectDomains.push(wsUrl.origin);
|
|
391
|
+
}
|
|
392
|
+
let contentMetaOverrides = {};
|
|
393
|
+
if (isClaude) {
|
|
394
|
+
const pathname = extra?.requestInfo?.url?.pathname ?? "";
|
|
395
|
+
const rawUrl = header("x-alpic-forwarded-url") ?? `${serverUrl}${pathname}`;
|
|
396
|
+
// Strip a lone trailing slash so the hash matches the connector URL
|
|
397
|
+
// as registered with Claude (which has no trailing slash on bare origins).
|
|
398
|
+
const url = rawUrl.endsWith("/") ? rawUrl.slice(0, -1) : rawUrl;
|
|
399
|
+
const hash = crypto
|
|
400
|
+
.createHash("sha256")
|
|
401
|
+
.update(url)
|
|
402
|
+
.digest("hex")
|
|
403
|
+
.slice(0, 32);
|
|
404
|
+
contentMetaOverrides = { domain: `${hash}.claudemcpcontent.com` };
|
|
405
|
+
}
|
|
406
|
+
return { serverUrl, connectDomains, contentMetaOverrides };
|
|
407
|
+
}
|
|
202
408
|
registerViewResources(toolName, view, toolMeta) {
|
|
203
409
|
const hosts = view.hosts ?? ["apps-sdk", "mcp-app"];
|
|
204
410
|
// Append a content-derived version param so hosts (e.g. ChatGPT) bust
|
|
@@ -308,35 +514,20 @@ export class McpServer extends McpServerBaseOmitted {
|
|
|
308
514
|
}
|
|
309
515
|
registerViewResource({ name, viewResource, view, }) {
|
|
310
516
|
const { hostType, uri: viewUri, mimeType, buildContentMeta } = viewResource;
|
|
517
|
+
const buildMeta = (extra) => {
|
|
518
|
+
const { serverUrl, connectDomains, contentMetaOverrides } = this.resolveViewRequestContext(extra);
|
|
519
|
+
return buildContentMeta({
|
|
520
|
+
resourceDomains: [serverUrl],
|
|
521
|
+
connectDomains,
|
|
522
|
+
domain: serverUrl,
|
|
523
|
+
baseUriDomains: [serverUrl],
|
|
524
|
+
}, contentMetaOverrides);
|
|
525
|
+
};
|
|
526
|
+
this.viewMetaBuilders.set(viewUri, buildMeta);
|
|
527
|
+
this.viewUriByPath.set(stripQuery(viewUri), viewUri);
|
|
311
528
|
this.registerResource(name, viewUri, { description: view.description }, async (uri, extra) => {
|
|
312
529
|
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
|
-
}
|
|
530
|
+
const { serverUrl } = this.resolveViewRequestContext(extra);
|
|
340
531
|
const html = isProduction
|
|
341
532
|
? templateHelper.renderProduction({
|
|
342
533
|
hostType,
|
|
@@ -349,35 +540,9 @@ export class McpServer extends McpServerBaseOmitted {
|
|
|
349
540
|
serverUrl,
|
|
350
541
|
viewName: view.component,
|
|
351
542
|
});
|
|
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
543
|
return {
|
|
379
544
|
contents: [
|
|
380
|
-
{ uri: uri.href, mimeType, text: html, _meta:
|
|
545
|
+
{ uri: uri.href, mimeType, text: html, _meta: buildMeta(extra) },
|
|
381
546
|
],
|
|
382
547
|
};
|
|
383
548
|
});
|