skybridge 0.0.0-dev.e1f5ed6 → 0.0.0-dev.e210492
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 -9
- package/dist/cli/tunnel-control-server.d.ts +9 -0
- package/dist/cli/tunnel-control-server.js +31 -0
- package/dist/cli/tunnel-control-server.js.map +1 -0
- package/dist/cli/tunnel-control-server.test.js +39 -0
- package/dist/cli/tunnel-control-server.test.js.map +1 -0
- package/dist/cli/tunnel-handler.d.ts +3 -0
- package/dist/cli/tunnel-handler.js +48 -0
- package/dist/cli/tunnel-handler.js.map +1 -0
- package/dist/cli/tunnel-handler.test.js +105 -0
- package/dist/cli/tunnel-handler.test.js.map +1 -0
- package/dist/cli/tunnel.d.ts +57 -0
- package/dist/cli/tunnel.js +154 -0
- package/dist/cli/tunnel.js.map +1 -0
- package/dist/cli/tunnel.test.js +190 -0
- package/dist/cli/tunnel.test.js.map +1 -0
- package/dist/cli/use-nodemon.js +2 -2
- package/dist/cli/use-nodemon.js.map +1 -1
- package/dist/cli/use-open-browser.d.ts +1 -0
- package/dist/cli/use-open-browser.js +44 -0
- package/dist/cli/use-open-browser.js.map +1 -0
- package/dist/cli/use-tunnel.d.ts +1 -1
- package/dist/cli/use-tunnel.js +102 -68
- package/dist/cli/use-tunnel.js.map +1 -1
- package/dist/cli/use-typescript-check.d.ts +1 -0
- package/dist/cli/use-typescript-check.js +41 -6
- package/dist/cli/use-typescript-check.js.map +1 -1
- package/dist/commands/build.js +63 -7
- package/dist/commands/build.js.map +1 -1
- package/dist/commands/dev.d.ts +1 -0
- package/dist/commands/dev.js +34 -3
- package/dist/commands/dev.js.map +1 -1
- package/dist/commands/start.js +3 -8
- package/dist/commands/start.js.map +1 -1
- package/dist/server/asset-base-url-transform-plugin.js +1 -1
- package/dist/server/asset-base-url-transform-plugin.js.map +1 -1
- package/dist/server/asset-base-url-transform-plugin.test.js +29 -0
- package/dist/server/asset-base-url-transform-plugin.test.js.map +1 -1
- package/dist/server/content-helpers.d.ts +27 -0
- package/dist/server/content-helpers.js +46 -0
- package/dist/server/content-helpers.js.map +1 -0
- package/dist/server/content-helpers.test.d.ts +1 -0
- package/dist/server/content-helpers.test.js +70 -0
- package/dist/server/content-helpers.test.js.map +1 -0
- package/dist/server/express.js +32 -6
- package/dist/server/express.js.map +1 -1
- package/dist/server/express.test.js +98 -2
- package/dist/server/express.test.js.map +1 -1
- package/dist/server/index.d.ts +4 -3
- package/dist/server/index.js +3 -2
- package/dist/server/index.js.map +1 -1
- package/dist/server/inferUtilityTypes.d.ts +6 -6
- package/dist/server/middleware.test.js +12 -9
- package/dist/server/middleware.test.js.map +1 -1
- package/dist/server/server.d.ts +108 -73
- package/dist/server/server.js +228 -73
- package/dist/server/server.js.map +1 -1
- package/dist/server/templateHelper.d.ts +5 -7
- package/dist/server/templateHelper.js +3 -22
- package/dist/server/templateHelper.js.map +1 -1
- package/dist/server/templates.generated.d.ts +4 -0
- package/dist/server/templates.generated.js +47 -0
- package/dist/server/templates.generated.js.map +1 -0
- package/dist/server/tunnel-proxy-router.d.ts +7 -0
- package/dist/server/tunnel-proxy-router.js +110 -0
- package/dist/server/tunnel-proxy-router.js.map +1 -0
- package/dist/server/tunnel-proxy-router.test.d.ts +1 -0
- package/dist/server/tunnel-proxy-router.test.js +229 -0
- package/dist/server/tunnel-proxy-router.test.js.map +1 -0
- package/dist/server/viewsDevServer.d.ts +14 -0
- package/dist/server/viewsDevServer.js +45 -0
- package/dist/server/viewsDevServer.js.map +1 -0
- package/dist/test/utils.d.ts +8 -20
- package/dist/test/utils.js +39 -35
- package/dist/test/utils.js.map +1 -1
- package/dist/test/view.test.d.ts +1 -0
- package/dist/test/view.test.js +523 -0
- package/dist/test/view.test.js.map +1 -0
- package/dist/version.js +1 -3
- package/dist/version.js.map +1 -1
- package/dist/web/bridges/apps-sdk/adaptor.d.ts +2 -2
- package/dist/web/bridges/apps-sdk/adaptor.js +8 -2
- package/dist/web/bridges/apps-sdk/adaptor.js.map +1 -1
- package/dist/web/bridges/mcp-app/adaptor.d.ts +6 -6
- package/dist/web/bridges/mcp-app/adaptor.js +24 -24
- package/dist/web/bridges/mcp-app/adaptor.js.map +1 -1
- package/dist/web/bridges/types.d.ts +7 -7
- package/dist/web/components/modal-provider.js +1 -1
- package/dist/web/components/modal-provider.js.map +1 -1
- package/dist/web/create-store.js +9 -9
- package/dist/web/create-store.js.map +1 -1
- package/dist/web/create-store.test.js +14 -16
- package/dist/web/create-store.test.js.map +1 -1
- package/dist/web/data-llm.d.ts +1 -1
- package/dist/web/data-llm.js +3 -3
- package/dist/web/data-llm.js.map +1 -1
- package/dist/web/data-llm.test.js +22 -22
- package/dist/web/data-llm.test.js.map +1 -1
- package/dist/web/generate-helpers.d.ts +20 -18
- package/dist/web/generate-helpers.js +20 -18
- package/dist/web/generate-helpers.js.map +1 -1
- package/dist/web/generate-helpers.test-d.js +26 -26
- package/dist/web/generate-helpers.test-d.js.map +1 -1
- package/dist/web/helpers/state.d.ts +2 -2
- package/dist/web/helpers/state.js +11 -11
- package/dist/web/helpers/state.js.map +1 -1
- package/dist/web/helpers/state.test.js +9 -9
- package/dist/web/helpers/state.test.js.map +1 -1
- package/dist/web/hooks/index.d.ts +1 -1
- package/dist/web/hooks/index.js +1 -1
- package/dist/web/hooks/index.js.map +1 -1
- package/dist/web/hooks/use-request-modal.d.ts +1 -1
- package/dist/web/hooks/use-request-modal.js +4 -4
- package/dist/web/hooks/use-request-modal.js.map +1 -1
- package/dist/web/hooks/use-request-modal.test.js +1 -1
- package/dist/web/hooks/use-request-modal.test.js.map +1 -1
- package/dist/web/hooks/use-view-state.d.ts +4 -0
- package/dist/web/hooks/use-view-state.js +32 -0
- package/dist/web/hooks/use-view-state.js.map +1 -0
- package/dist/web/hooks/use-view-state.test.d.ts +1 -0
- package/dist/web/hooks/{use-widget-state.test.js → use-view-state.test.js} +17 -17
- package/dist/web/hooks/use-view-state.test.js.map +1 -0
- package/dist/web/index.d.ts +1 -2
- package/dist/web/index.js +1 -2
- package/dist/web/index.js.map +1 -1
- package/dist/web/mount-view.d.ts +1 -0
- package/dist/web/{mount-widget.js → mount-view.js} +2 -2
- package/dist/web/mount-view.js.map +1 -0
- package/dist/web/plugin/plugin.d.ts +4 -1
- package/dist/web/plugin/plugin.js +134 -25
- package/dist/web/plugin/plugin.js.map +1 -1
- package/dist/web/plugin/scan-views.d.ts +16 -0
- package/dist/web/plugin/scan-views.js +88 -0
- package/dist/web/plugin/scan-views.js.map +1 -0
- package/dist/web/plugin/scan-views.test.d.ts +1 -0
- package/dist/web/plugin/scan-views.test.js +99 -0
- package/dist/web/plugin/scan-views.test.js.map +1 -0
- package/dist/web/plugin/validate-view.d.ts +1 -0
- package/dist/web/plugin/validate-view.js +9 -0
- package/dist/web/plugin/validate-view.js.map +1 -0
- package/dist/web/plugin/validate-view.test.d.ts +1 -0
- package/dist/web/plugin/validate-view.test.js +24 -0
- package/dist/web/plugin/validate-view.test.js.map +1 -0
- package/package.json +20 -13
- package/tsconfig.base.json +2 -0
- package/dist/server/templates/development.hbs +0 -12
- package/dist/server/templates/production.hbs +0 -6
- package/dist/server/widgetsDevServer.d.ts +0 -13
- package/dist/server/widgetsDevServer.js +0 -52
- package/dist/server/widgetsDevServer.js.map +0 -1
- package/dist/test/widget.test.js +0 -334
- package/dist/test/widget.test.js.map +0 -1
- package/dist/web/hooks/use-widget-state.d.ts +0 -4
- package/dist/web/hooks/use-widget-state.js +0 -32
- package/dist/web/hooks/use-widget-state.js.map +0 -1
- package/dist/web/hooks/use-widget-state.test.js.map +0 -1
- package/dist/web/mount-widget.d.ts +0 -1
- package/dist/web/mount-widget.js.map +0 -1
- package/dist/web/plugin/validate-widget.d.ts +0 -5
- package/dist/web/plugin/validate-widget.js +0 -27
- package/dist/web/plugin/validate-widget.js.map +0 -1
- package/dist/web/plugin/validate-widget.test.js +0 -42
- package/dist/web/plugin/validate-widget.test.js.map +0 -1
- /package/dist/{test/widget.test.d.ts → cli/tunnel-control-server.test.d.ts} +0 -0
- /package/dist/{web/hooks/use-widget-state.test.d.ts → cli/tunnel-handler.test.d.ts} +0 -0
- /package/dist/{web/plugin/validate-widget.test.d.ts → cli/tunnel.test.d.ts} +0 -0
package/dist/server/server.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import type
|
|
2
|
-
import { McpServer as McpServerBase
|
|
1
|
+
import { type ServerOptions } from "@modelcontextprotocol/sdk/server/index.js";
|
|
2
|
+
import { McpServer as McpServerBase } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
3
3
|
import type { AnySchema, SchemaOutput, ZodRawShapeCompat } from "@modelcontextprotocol/sdk/server/zod-compat.js";
|
|
4
4
|
import type { RequestHandlerExtra } from "@modelcontextprotocol/sdk/shared/protocol.js";
|
|
5
|
-
import type {
|
|
5
|
+
import type { ContentBlock, Implementation, ServerNotification, ServerRequest, ServerResult, ToolAnnotations } from "@modelcontextprotocol/sdk/types.js";
|
|
6
6
|
import type { ErrorRequestHandler, RequestHandler } from "express";
|
|
7
7
|
import type { McpExtra, McpExtraFor, McpMethodString, McpMiddlewareFilter, McpMiddlewareFn, McpResultFor, McpTypedMiddlewareFn, McpWildcard } from "./middleware.js";
|
|
8
8
|
export type ToolDef<TInput = unknown, TOutput = unknown, TResponseMetadata = unknown> = {
|
|
@@ -10,36 +10,59 @@ export type ToolDef<TInput = unknown, TOutput = unknown, TResponseMetadata = unk
|
|
|
10
10
|
output: TOutput;
|
|
11
11
|
responseMetadata: TResponseMetadata;
|
|
12
12
|
};
|
|
13
|
+
export type ViewHostType = "apps-sdk" | "mcp-app";
|
|
14
|
+
export interface ViewCsp {
|
|
15
|
+
/** Origins for static assets (images, fonts, scripts, styles). */
|
|
16
|
+
resourceDomains?: string[];
|
|
17
|
+
/** Origins the view may contact via fetch/XHR. */
|
|
18
|
+
connectDomains?: string[];
|
|
19
|
+
/** Origins allowed for iframe embeds (opts into stricter app review). */
|
|
20
|
+
frameDomains?: string[];
|
|
21
|
+
/** Origins that can receive openExternal redirects without the safe-link modal. */
|
|
22
|
+
redirectDomains?: string[];
|
|
23
|
+
/** Origins allowed in `<base href>` tags (mcp-apps only). */
|
|
24
|
+
baseUriDomains?: string[];
|
|
25
|
+
}
|
|
26
|
+
export interface ViewNameRegistry {
|
|
27
|
+
}
|
|
28
|
+
export type ViewName = keyof ViewNameRegistry & string;
|
|
29
|
+
export interface ViewConfig {
|
|
30
|
+
component: ViewName;
|
|
31
|
+
description?: string;
|
|
32
|
+
hosts?: ViewHostType[];
|
|
33
|
+
prefersBorder?: boolean;
|
|
34
|
+
domain?: string;
|
|
35
|
+
csp?: ViewCsp;
|
|
36
|
+
_meta?: Record<string, unknown>;
|
|
37
|
+
}
|
|
38
|
+
export interface KnownToolMeta {
|
|
39
|
+
"openai/widgetAccessible"?: boolean;
|
|
40
|
+
"openai/toolInvocation/invoking"?: string;
|
|
41
|
+
"openai/toolInvocation/invoked"?: string;
|
|
42
|
+
}
|
|
43
|
+
export type ToolMeta = KnownToolMeta & Record<string, unknown>;
|
|
44
|
+
export type HandlerContent = string | ContentBlock | ContentBlock[];
|
|
13
45
|
/**
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
46
|
+
* Type-level marker interface for cross-package type inference.
|
|
47
|
+
*
|
|
48
|
+
* Consumers infer tool types via the structural `$types` property rather than
|
|
49
|
+
* the `McpServer` class generic, because class-generic inference breaks when
|
|
50
|
+
* `McpServer` comes from different package installations (e.g. a consumer
|
|
51
|
+
* with its own `skybridge` dep vs. the in-tree workspace version).
|
|
52
|
+
*
|
|
53
|
+
* Inspired by tRPC's `_def` pattern and Hono's type markers.
|
|
17
54
|
*/
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
* @see https://developers.openai.com/apps-sdk/reference#component-resource-_meta-fields
|
|
22
|
-
*/
|
|
23
|
-
redirectDomains?: string[];
|
|
24
|
-
};
|
|
25
|
-
/** Extended MCP Apps resource metadata with upcoming CSP fields */
|
|
26
|
-
type ExtendedMcpUiResourceMeta = Omit<McpUiResourceMeta, "csp"> & {
|
|
27
|
-
csp?: ExtendedMcpUiResourceCsp;
|
|
28
|
-
};
|
|
29
|
-
/** User-provided resource configuration with optional CSP override */
|
|
30
|
-
export type WidgetResourceMeta = {
|
|
31
|
-
ui?: ExtendedMcpUiResourceMeta;
|
|
32
|
-
} & Resource["_meta"];
|
|
33
|
-
export type WidgetHostType = "apps-sdk" | "mcp-app";
|
|
34
|
-
type McpServerOriginalResourceConfig = Omit<Resource, "uri" | "name" | "mimeType" | "_meta"> & {
|
|
35
|
-
_meta?: WidgetResourceMeta;
|
|
36
|
-
/** Restrict host types to a specific subset */
|
|
37
|
-
hosts?: WidgetHostType[];
|
|
38
|
-
};
|
|
39
|
-
type McpServerOriginalToolConfig = Omit<Parameters<typeof McpServerBase.prototype.registerTool<ZodRawShapeCompat, ZodRawShapeCompat>>[1], "inputSchema" | "outputSchema">;
|
|
55
|
+
export interface McpServerTypes<TTools extends Record<string, ToolDef>> {
|
|
56
|
+
readonly tools: TTools;
|
|
57
|
+
}
|
|
40
58
|
type Simplify<T> = {
|
|
41
59
|
[K in keyof T]: T[K];
|
|
42
60
|
};
|
|
61
|
+
type ShapeOutput<Shape extends ZodRawShapeCompat> = Simplify<{
|
|
62
|
+
[K in keyof Shape as undefined extends SchemaOutput<Shape[K]> ? never : K]: SchemaOutput<Shape[K]>;
|
|
63
|
+
} & {
|
|
64
|
+
[K in keyof Shape as undefined extends SchemaOutput<Shape[K]> ? K : never]?: SchemaOutput<Shape[K]>;
|
|
65
|
+
}>;
|
|
43
66
|
type ExtractStructuredContent<T> = T extends {
|
|
44
67
|
structuredContent: infer SC;
|
|
45
68
|
} ? Simplify<SC> : never;
|
|
@@ -50,62 +73,52 @@ type ExtractMeta<T> = [Extract<T, {
|
|
|
50
73
|
}> extends {
|
|
51
74
|
_meta: infer M;
|
|
52
75
|
} ? Simplify<M> : unknown;
|
|
53
|
-
/**
|
|
54
|
-
* Type-level marker interface for cross-package type inference.
|
|
55
|
-
* This enables TypeScript to infer tool types across package boundaries
|
|
56
|
-
* using structural typing on the $types property, rather than relying on
|
|
57
|
-
* class generic inference which fails when McpServer comes from different
|
|
58
|
-
* package installations.
|
|
59
|
-
*
|
|
60
|
-
* Inspired by tRPC's _def pattern and Hono's type markers.
|
|
61
|
-
*/
|
|
62
|
-
export interface McpServerTypes<TTools extends Record<string, ToolDef>> {
|
|
63
|
-
readonly tools: TTools;
|
|
64
|
-
}
|
|
65
|
-
type ShapeOutput<Shape extends ZodRawShapeCompat> = Simplify<{
|
|
66
|
-
[K in keyof Shape as undefined extends SchemaOutput<Shape[K]> ? never : K]: SchemaOutput<Shape[K]>;
|
|
67
|
-
} & {
|
|
68
|
-
[K in keyof Shape as undefined extends SchemaOutput<Shape[K]> ? K : never]?: SchemaOutput<Shape[K]>;
|
|
69
|
-
}>;
|
|
70
76
|
type AddTool<TTools, TName extends string, TInput extends ZodRawShapeCompat, TOutput, TResponseMetadata = unknown> = McpServer<TTools & {
|
|
71
77
|
[K in TName]: ToolDef<ShapeOutput<TInput>, TOutput, TResponseMetadata>;
|
|
72
78
|
}>;
|
|
73
|
-
|
|
79
|
+
interface ToolConfig<TInput extends ZodRawShapeCompat | AnySchema> {
|
|
80
|
+
name: string;
|
|
74
81
|
title?: string;
|
|
75
82
|
description?: string;
|
|
76
83
|
inputSchema?: TInput;
|
|
77
84
|
outputSchema?: ZodRawShapeCompat | AnySchema;
|
|
78
85
|
annotations?: ToolAnnotations;
|
|
79
|
-
|
|
80
|
-
|
|
86
|
+
view?: ViewConfig;
|
|
87
|
+
_meta?: ToolMeta;
|
|
88
|
+
}
|
|
81
89
|
type ToolHandler<TInput extends ZodRawShapeCompat, TReturn extends {
|
|
82
|
-
content
|
|
83
|
-
} =
|
|
84
|
-
|
|
90
|
+
content?: HandlerContent;
|
|
91
|
+
} = {
|
|
92
|
+
content?: HandlerContent;
|
|
93
|
+
}> = (args: ShapeOutput<TInput>, extra: RequestHandlerExtra<ServerRequest, ServerNotification>) => TReturn | Promise<TReturn>;
|
|
94
|
+
export declare function normalizeContent(content: HandlerContent | undefined): ContentBlock[];
|
|
95
|
+
interface McpServerBaseOmitted extends Omit<McpServerBase, "registerTool" | "connect"> {
|
|
96
|
+
}
|
|
97
|
+
declare const McpServerBaseOmitted: new (...args: ConstructorParameters<typeof McpServerBase>) => McpServerBaseOmitted;
|
|
98
|
+
export declare class McpServer<TTools extends Record<string, ToolDef> = Record<never, ToolDef>> extends McpServerBaseOmitted {
|
|
85
99
|
readonly $types: McpServerTypes<TTools>;
|
|
86
100
|
private express?;
|
|
87
101
|
private customMiddleware;
|
|
88
102
|
private customErrorMiddleware;
|
|
89
103
|
private mcpMiddlewareEntries;
|
|
90
104
|
private mcpMiddlewareApplied;
|
|
105
|
+
private claimedViews;
|
|
106
|
+
private viteManifest;
|
|
107
|
+
private readonly serverInfo;
|
|
108
|
+
private readonly serverOptions?;
|
|
109
|
+
constructor(serverInfo: Implementation, options?: ServerOptions);
|
|
91
110
|
use(...handlers: RequestHandler[]): this;
|
|
92
111
|
use(path: string, ...handlers: RequestHandler[]): this;
|
|
93
112
|
useOnError(...handlers: ErrorRequestHandler[]): this;
|
|
94
113
|
useOnError(path: string, ...handlers: ErrorRequestHandler[]): this;
|
|
95
|
-
/**
|
|
96
|
-
* Register MCP protocol-level middleware (catch-all).
|
|
97
|
-
*/
|
|
114
|
+
/** Register MCP protocol-level middleware (catch-all). */
|
|
98
115
|
mcpMiddleware(handler: McpMiddlewareFn): this;
|
|
99
|
-
/**
|
|
100
|
-
* Register MCP protocol-level middleware for all requests (`extra` is `McpExtra`).
|
|
101
|
-
*/
|
|
116
|
+
/** Register MCP protocol-level middleware for all requests (`extra` is `McpExtra`). */
|
|
102
117
|
mcpMiddleware(filter: "request", handler: (request: {
|
|
103
118
|
method: string;
|
|
104
119
|
params: Record<string, unknown>;
|
|
105
120
|
}, extra: McpExtra, next: () => Promise<ServerResult>) => Promise<unknown> | unknown): this;
|
|
106
|
-
/**
|
|
107
|
-
* Register MCP protocol-level middleware for all notifications (`extra` is `undefined`).
|
|
108
|
-
*/
|
|
121
|
+
/** Register MCP protocol-level middleware for all notifications (`extra` is `undefined`). */
|
|
109
122
|
mcpMiddleware(filter: "notification", handler: (request: {
|
|
110
123
|
method: string;
|
|
111
124
|
params: Record<string, unknown>;
|
|
@@ -131,20 +144,42 @@ export declare class McpServer<TTools extends Record<string, ToolDef> = Record<n
|
|
|
131
144
|
mcpMiddleware(filter: McpMiddlewareFilter, handler: McpMiddlewareFn): this;
|
|
132
145
|
private applyMcpMiddleware;
|
|
133
146
|
connect(transport: Parameters<typeof McpServerBase.prototype.connect>[0]): Promise<void>;
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
147
|
+
/**
|
|
148
|
+
* Per-request stateless connect. The SDK's `Protocol` only allows one
|
|
149
|
+
* transport per instance, so we can't reuse this `McpServer` across
|
|
150
|
+
* concurrent requests. The SDK's idiomatic fix is a `() => McpServer`
|
|
151
|
+
* factory, but that would break Skybridge's singleton API — so instead
|
|
152
|
+
* we build a fresh underlying `Server` per request and share the main
|
|
153
|
+
* server's handler maps by reference. The cast is unavoidable: there's
|
|
154
|
+
* no public API to inject handler maps. `getHandlerMaps` validates the
|
|
155
|
+
* read side and fails fast on SDK field renames.
|
|
156
|
+
*/
|
|
157
|
+
connectStatelessTransport(transport: Parameters<typeof McpServerBase.prototype.connect>[0]): Promise<void>;
|
|
158
|
+
run(): Promise<{
|
|
159
|
+
fetch: (...args: unknown[]) => unknown;
|
|
160
|
+
} | undefined>;
|
|
161
|
+
private enforceOneToolPerView;
|
|
162
|
+
private registerViewResources;
|
|
163
|
+
private registerViewResource;
|
|
164
|
+
private wrapHandler;
|
|
165
|
+
private computeViewVersionParam;
|
|
166
|
+
private lookupViewFile;
|
|
146
167
|
private lookupDistFile;
|
|
147
|
-
|
|
168
|
+
/**
|
|
169
|
+
* Inject the Vite manifest as a value rather than letting `readManifest()`
|
|
170
|
+
* load it from disk. Required for runtimes without a usable filesystem
|
|
171
|
+
* (Cloudflare Workers, etc.) — the user's `skybridge build` emits the
|
|
172
|
+
* manifest as a JS module which the entry imports and passes here.
|
|
173
|
+
*/
|
|
174
|
+
setViteManifest(manifest: Record<string, {
|
|
175
|
+
file: string;
|
|
176
|
+
}>): this;
|
|
148
177
|
private readManifest;
|
|
178
|
+
registerTool<TName extends string, InputArgs extends ZodRawShapeCompat, TReturn extends {
|
|
179
|
+
content?: HandlerContent;
|
|
180
|
+
}>(config: ToolConfig<InputArgs> & {
|
|
181
|
+
name: TName;
|
|
182
|
+
}, cb: ToolHandler<InputArgs, TReturn>): AddTool<TTools, TName, InputArgs, ExtractStructuredContent<TReturn>, ExtractMeta<TReturn>>;
|
|
183
|
+
registerTool<InputArgs extends ZodRawShapeCompat>(config: ToolConfig<InputArgs>, cb: ToolHandler<InputArgs>): this;
|
|
149
184
|
}
|
|
150
185
|
export {};
|
package/dist/server/server.js
CHANGED
|
@@ -2,7 +2,8 @@ import crypto from "node:crypto";
|
|
|
2
2
|
import { readFileSync } from "node:fs";
|
|
3
3
|
import http from "node:http";
|
|
4
4
|
import path from "node:path";
|
|
5
|
-
import {
|
|
5
|
+
import { Server as SdkServer, } from "@modelcontextprotocol/sdk/server/index.js";
|
|
6
|
+
import { McpServer as McpServerBase } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
6
7
|
import { mergeWith, union } from "es-toolkit";
|
|
7
8
|
import { createApp } from "./express.js";
|
|
8
9
|
import { createMiddlewareEntry } from "./metric.js";
|
|
@@ -15,12 +16,34 @@ const mergeWithUnion = (target, source) => {
|
|
|
15
16
|
}
|
|
16
17
|
});
|
|
17
18
|
};
|
|
18
|
-
export
|
|
19
|
+
export function normalizeContent(content) {
|
|
20
|
+
if (content === undefined) {
|
|
21
|
+
return [];
|
|
22
|
+
}
|
|
23
|
+
if (typeof content === "string") {
|
|
24
|
+
return [{ type: "text", text: content }];
|
|
25
|
+
}
|
|
26
|
+
if (Array.isArray(content)) {
|
|
27
|
+
return content;
|
|
28
|
+
}
|
|
29
|
+
return [content];
|
|
30
|
+
}
|
|
31
|
+
const McpServerBaseOmitted = McpServerBase;
|
|
32
|
+
export class McpServer extends McpServerBaseOmitted {
|
|
19
33
|
express;
|
|
20
34
|
customMiddleware = [];
|
|
21
35
|
customErrorMiddleware = [];
|
|
22
36
|
mcpMiddlewareEntries = [];
|
|
23
37
|
mcpMiddlewareApplied = false;
|
|
38
|
+
claimedViews = new Map();
|
|
39
|
+
viteManifest = null;
|
|
40
|
+
serverInfo;
|
|
41
|
+
serverOptions;
|
|
42
|
+
constructor(serverInfo, options) {
|
|
43
|
+
super(serverInfo, options);
|
|
44
|
+
this.serverInfo = serverInfo;
|
|
45
|
+
this.serverOptions = options;
|
|
46
|
+
}
|
|
24
47
|
use(pathOrHandler, ...handlers) {
|
|
25
48
|
if (typeof pathOrHandler === "string") {
|
|
26
49
|
this.customMiddleware.push({
|
|
@@ -83,7 +106,6 @@ export class McpServer extends McpServerBase {
|
|
|
83
106
|
return;
|
|
84
107
|
}
|
|
85
108
|
const { requestHandlers, notificationHandlers } = getHandlerMaps(this.server);
|
|
86
|
-
// Wrap existing handlers and proxy future .set() for lazy SDK registration
|
|
87
109
|
const instrumentMap = (map, isNotification) => {
|
|
88
110
|
for (const [method, handler] of map) {
|
|
89
111
|
map.set(method, buildMiddlewareChain(method, isNotification, handler, entries));
|
|
@@ -96,7 +118,26 @@ export class McpServer extends McpServerBase {
|
|
|
96
118
|
}
|
|
97
119
|
async connect(transport) {
|
|
98
120
|
this.applyMcpMiddleware();
|
|
99
|
-
return
|
|
121
|
+
return McpServerBase.prototype.connect.call(this, transport);
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Per-request stateless connect. The SDK's `Protocol` only allows one
|
|
125
|
+
* transport per instance, so we can't reuse this `McpServer` across
|
|
126
|
+
* concurrent requests. The SDK's idiomatic fix is a `() => McpServer`
|
|
127
|
+
* factory, but that would break Skybridge's singleton API — so instead
|
|
128
|
+
* we build a fresh underlying `Server` per request and share the main
|
|
129
|
+
* server's handler maps by reference. The cast is unavoidable: there's
|
|
130
|
+
* no public API to inject handler maps. `getHandlerMaps` validates the
|
|
131
|
+
* read side and fails fast on SDK field renames.
|
|
132
|
+
*/
|
|
133
|
+
async connectStatelessTransport(transport) {
|
|
134
|
+
this.applyMcpMiddleware();
|
|
135
|
+
const { requestHandlers, notificationHandlers } = getHandlerMaps(this.server);
|
|
136
|
+
const fresh = new SdkServer(this.serverInfo, this.serverOptions);
|
|
137
|
+
const target = fresh;
|
|
138
|
+
target._requestHandlers = requestHandlers;
|
|
139
|
+
target._notificationHandlers = notificationHandlers;
|
|
140
|
+
await fresh.connect(transport);
|
|
100
141
|
}
|
|
101
142
|
async run() {
|
|
102
143
|
this.applyMcpMiddleware();
|
|
@@ -110,111 +151,156 @@ export class McpServer extends McpServerBase {
|
|
|
110
151
|
});
|
|
111
152
|
}
|
|
112
153
|
httpServer.on("request", this.express);
|
|
113
|
-
|
|
154
|
+
const port = parseInt(process.env.__PORT ?? "3000", 10);
|
|
155
|
+
await new Promise((resolve, reject) => {
|
|
114
156
|
httpServer.on("error", (error) => {
|
|
115
157
|
console.error("Failed to start server:", error);
|
|
116
158
|
reject(error);
|
|
117
159
|
});
|
|
118
|
-
const port = parseInt(process.env.__PORT ?? "3000", 10);
|
|
119
160
|
httpServer.listen(port, () => {
|
|
120
161
|
resolve();
|
|
121
162
|
});
|
|
122
163
|
});
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
164
|
+
// On workerd, bridge the Node http server to a Workers fetch handler.
|
|
165
|
+
// The specifier is held in a variable to sidestep tsc's module resolution
|
|
166
|
+
// (`cloudflare:node` only exists under wrangler/workerd).
|
|
167
|
+
if (typeof navigator !== "undefined" &&
|
|
168
|
+
navigator.userAgent === "Cloudflare-Workers") {
|
|
169
|
+
const cloudflareNode = "cloudflare:node";
|
|
170
|
+
const { httpServerHandler } = await import(cloudflareNode);
|
|
171
|
+
return httpServerHandler({ port });
|
|
172
|
+
}
|
|
173
|
+
const shutdown = () => {
|
|
174
|
+
// Drop both handlers so a second signal falls through to Node's default
|
|
175
|
+
// (force-quit on a second Ctrl+C while drain is hanging).
|
|
176
|
+
process.off("SIGTERM", shutdown);
|
|
177
|
+
process.off("SIGINT", shutdown);
|
|
178
|
+
httpServer.close(() => process.exit(0));
|
|
179
|
+
// Force exit if connections don't drain in time so the port is still
|
|
180
|
+
// released promptly (e.g. for nodemon restarts).
|
|
181
|
+
setTimeout(() => process.exit(0), 3000).unref();
|
|
128
182
|
};
|
|
129
|
-
|
|
130
|
-
|
|
183
|
+
process.on("SIGTERM", shutdown);
|
|
184
|
+
process.on("SIGINT", shutdown);
|
|
185
|
+
return undefined;
|
|
186
|
+
}
|
|
187
|
+
enforceOneToolPerView(component, toolName) {
|
|
188
|
+
const existingTool = this.claimedViews.get(component);
|
|
189
|
+
if (existingTool) {
|
|
190
|
+
throw new Error(`skybridge: view "${component}" is already used by tool "${existingTool}". Tool "${toolName}" cannot also reference it — each view backs exactly one tool.`);
|
|
191
|
+
}
|
|
192
|
+
this.claimedViews.set(component, toolName);
|
|
193
|
+
}
|
|
194
|
+
registerViewResources(toolName, view, toolMeta) {
|
|
195
|
+
const hosts = view.hosts ?? ["apps-sdk", "mcp-app"];
|
|
196
|
+
// Append a content-derived version param so hosts (e.g. ChatGPT) bust
|
|
197
|
+
// their cache when the bundle changes, but keep the URI stable across
|
|
198
|
+
// `tools/list` calls when the bundle hasn't changed.
|
|
199
|
+
const versionParam = this.computeViewVersionParam(view.component);
|
|
200
|
+
if (hosts.includes("apps-sdk")) {
|
|
201
|
+
const viewResource = {
|
|
131
202
|
hostType: "apps-sdk",
|
|
132
|
-
uri: `ui://
|
|
203
|
+
uri: `ui://views/apps-sdk/${view.component}.html${versionParam}`,
|
|
133
204
|
mimeType: "text/html+skybridge",
|
|
134
205
|
buildContentMeta: ({ resourceDomains, connectDomains, domain }, overrides) => {
|
|
135
|
-
const userUi = userMeta?.ui;
|
|
136
|
-
const userCsp = userUi?.csp;
|
|
137
206
|
const defaults = {
|
|
138
207
|
"openai/widgetCSP": {
|
|
139
208
|
resource_domains: resourceDomains,
|
|
140
209
|
connect_domains: connectDomains,
|
|
141
210
|
},
|
|
142
211
|
"openai/widgetDomain": domain,
|
|
143
|
-
"openai/widgetDescription":
|
|
212
|
+
"openai/widgetDescription": view.description,
|
|
144
213
|
};
|
|
145
|
-
const
|
|
214
|
+
const fromView = {
|
|
146
215
|
"openai/widgetCSP": {
|
|
147
|
-
resource_domains:
|
|
148
|
-
connect_domains:
|
|
149
|
-
frame_domains:
|
|
150
|
-
redirect_domains:
|
|
216
|
+
resource_domains: view.csp?.resourceDomains,
|
|
217
|
+
connect_domains: view.csp?.connectDomains,
|
|
218
|
+
frame_domains: view.csp?.frameDomains,
|
|
219
|
+
redirect_domains: view.csp?.redirectDomains,
|
|
151
220
|
},
|
|
152
|
-
"openai/widgetDomain":
|
|
153
|
-
"openai/widgetPrefersBorder":
|
|
221
|
+
"openai/widgetDomain": view.domain,
|
|
222
|
+
"openai/widgetPrefersBorder": view.prefersBorder,
|
|
154
223
|
};
|
|
155
|
-
const
|
|
156
|
-
|
|
224
|
+
const base = mergeWithUnion(mergeWithUnion(defaults, fromView), {
|
|
225
|
+
"openai/widgetDomain": overrides.domain,
|
|
226
|
+
});
|
|
227
|
+
if (view._meta) {
|
|
228
|
+
return { ...base, ...view._meta };
|
|
229
|
+
}
|
|
230
|
+
return base;
|
|
157
231
|
},
|
|
158
232
|
};
|
|
159
|
-
this.
|
|
160
|
-
name,
|
|
161
|
-
|
|
162
|
-
|
|
233
|
+
this.registerViewResource({
|
|
234
|
+
name: toolName,
|
|
235
|
+
viewResource,
|
|
236
|
+
view,
|
|
163
237
|
});
|
|
164
|
-
toolMeta["openai/outputTemplate"] =
|
|
238
|
+
toolMeta["openai/outputTemplate"] = viewResource.uri;
|
|
165
239
|
}
|
|
166
|
-
if (
|
|
167
|
-
const
|
|
240
|
+
if (hosts.includes("mcp-app")) {
|
|
241
|
+
const viewResource = {
|
|
168
242
|
hostType: "mcp-app",
|
|
169
|
-
uri: `ui://
|
|
243
|
+
uri: `ui://views/ext-apps/${view.component}.html${versionParam}`,
|
|
170
244
|
mimeType: "text/html;profile=mcp-app",
|
|
171
|
-
buildContentMeta: ({ resourceDomains, connectDomains, domain }, overrides) => {
|
|
245
|
+
buildContentMeta: ({ resourceDomains, connectDomains, domain, baseUriDomains }, overrides) => {
|
|
172
246
|
const defaults = {
|
|
173
247
|
ui: {
|
|
174
248
|
csp: {
|
|
175
249
|
resourceDomains,
|
|
176
250
|
connectDomains,
|
|
251
|
+
baseUriDomains,
|
|
177
252
|
},
|
|
178
253
|
domain,
|
|
179
254
|
},
|
|
180
255
|
};
|
|
181
|
-
|
|
182
|
-
ui: {
|
|
256
|
+
const fromView = {
|
|
257
|
+
ui: {
|
|
258
|
+
...(view.description && { description: view.description }),
|
|
259
|
+
...(view.prefersBorder !== undefined && {
|
|
260
|
+
prefersBorder: view.prefersBorder,
|
|
261
|
+
}),
|
|
262
|
+
...(view.domain && { domain: view.domain }),
|
|
263
|
+
csp: {
|
|
264
|
+
...(view.csp?.resourceDomains && {
|
|
265
|
+
resourceDomains: view.csp.resourceDomains,
|
|
266
|
+
}),
|
|
267
|
+
...(view.csp?.connectDomains && {
|
|
268
|
+
connectDomains: view.csp.connectDomains,
|
|
269
|
+
}),
|
|
270
|
+
...(view.csp?.frameDomains && {
|
|
271
|
+
frameDomains: view.csp.frameDomains,
|
|
272
|
+
}),
|
|
273
|
+
...(view.csp?.baseUriDomains && {
|
|
274
|
+
baseUriDomains: view.csp.baseUriDomains,
|
|
275
|
+
}),
|
|
276
|
+
...(view.csp?.redirectDomains && {
|
|
277
|
+
redirectDomains: view.csp.redirectDomains,
|
|
278
|
+
}),
|
|
279
|
+
},
|
|
280
|
+
},
|
|
281
|
+
};
|
|
282
|
+
const base = mergeWithUnion(mergeWithUnion(defaults, fromView), {
|
|
283
|
+
ui: overrides,
|
|
183
284
|
});
|
|
285
|
+
if (view._meta) {
|
|
286
|
+
return { ...base, ...view._meta };
|
|
287
|
+
}
|
|
288
|
+
return base;
|
|
184
289
|
},
|
|
185
290
|
};
|
|
186
|
-
this.
|
|
187
|
-
name,
|
|
188
|
-
|
|
189
|
-
|
|
291
|
+
this.registerViewResource({
|
|
292
|
+
name: toolName,
|
|
293
|
+
viewResource,
|
|
294
|
+
view,
|
|
190
295
|
});
|
|
191
296
|
// @ts-expect-error - For backwards compatibility with Claude current implementation of the specs
|
|
192
|
-
toolMeta["ui/resourceUri"] =
|
|
193
|
-
toolMeta.ui = { resourceUri:
|
|
297
|
+
toolMeta["ui/resourceUri"] = viewResource.uri;
|
|
298
|
+
toolMeta.ui = { resourceUri: viewResource.uri };
|
|
194
299
|
}
|
|
195
|
-
const wrappedToolCallback = async (args, extra) => {
|
|
196
|
-
const result = await toolCallback(args, extra);
|
|
197
|
-
return {
|
|
198
|
-
...result,
|
|
199
|
-
_meta: {
|
|
200
|
-
...result._meta,
|
|
201
|
-
viewUUID: crypto.randomUUID(),
|
|
202
|
-
},
|
|
203
|
-
};
|
|
204
|
-
};
|
|
205
|
-
this.registerTool(name, {
|
|
206
|
-
...toolConfig,
|
|
207
|
-
_meta: toolMeta,
|
|
208
|
-
}, wrappedToolCallback);
|
|
209
|
-
return this;
|
|
210
|
-
}
|
|
211
|
-
registerTool(name, config, cb) {
|
|
212
|
-
super.registerTool(name, config, cb);
|
|
213
|
-
return this;
|
|
214
300
|
}
|
|
215
|
-
|
|
216
|
-
const { hostType, uri:
|
|
217
|
-
this.registerResource(name,
|
|
301
|
+
registerViewResource({ name, viewResource, view, }) {
|
|
302
|
+
const { hostType, uri: viewUri, mimeType, buildContentMeta } = viewResource;
|
|
303
|
+
this.registerResource(name, viewUri, { description: view.description }, async (uri, extra) => {
|
|
218
304
|
const isProduction = process.env.NODE_ENV === "production";
|
|
219
305
|
const isClaude = extra?.requestInfo?.headers?.["user-agent"] === "Claude-User";
|
|
220
306
|
const headers = extra?.requestInfo?.headers || {};
|
|
@@ -247,13 +333,13 @@ export class McpServer extends McpServerBase {
|
|
|
247
333
|
? templateHelper.renderProduction({
|
|
248
334
|
hostType,
|
|
249
335
|
serverUrl,
|
|
250
|
-
|
|
251
|
-
styleFile: this.lookupDistFile("style.css"),
|
|
336
|
+
viewFile: this.lookupViewFile(view.component),
|
|
337
|
+
styleFile: this.lookupDistFile("style.css") ?? "",
|
|
252
338
|
})
|
|
253
339
|
: templateHelper.renderDevelopment({
|
|
254
340
|
hostType,
|
|
255
341
|
serverUrl,
|
|
256
|
-
|
|
342
|
+
viewName: view.component,
|
|
257
343
|
});
|
|
258
344
|
const connectDomains = [serverUrl];
|
|
259
345
|
if (!isProduction) {
|
|
@@ -288,18 +374,87 @@ export class McpServer extends McpServerBase {
|
|
|
288
374
|
};
|
|
289
375
|
});
|
|
290
376
|
}
|
|
377
|
+
wrapHandler(cb, { attachViewUUID }) {
|
|
378
|
+
return async (args, extra) => {
|
|
379
|
+
const result = await cb(args, extra);
|
|
380
|
+
return {
|
|
381
|
+
...result,
|
|
382
|
+
content: normalizeContent(result.content),
|
|
383
|
+
...(attachViewUUID && {
|
|
384
|
+
_meta: {
|
|
385
|
+
...result._meta,
|
|
386
|
+
viewUUID: crypto.randomUUID(),
|
|
387
|
+
},
|
|
388
|
+
}),
|
|
389
|
+
};
|
|
390
|
+
};
|
|
391
|
+
}
|
|
392
|
+
computeViewVersionParam(viewName) {
|
|
393
|
+
if (process.env.NODE_ENV !== "production") {
|
|
394
|
+
return "";
|
|
395
|
+
}
|
|
396
|
+
try {
|
|
397
|
+
const viewFile = this.lookupViewFile(viewName);
|
|
398
|
+
const styleFile = this.lookupDistFile("style.css") ?? "";
|
|
399
|
+
const hash = crypto
|
|
400
|
+
.createHash("sha256")
|
|
401
|
+
.update(viewFile)
|
|
402
|
+
.update("\0")
|
|
403
|
+
.update(styleFile)
|
|
404
|
+
.digest("hex")
|
|
405
|
+
.slice(0, 8);
|
|
406
|
+
return `?v=${hash}`;
|
|
407
|
+
}
|
|
408
|
+
catch {
|
|
409
|
+
return "";
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
lookupViewFile(viewName) {
|
|
413
|
+
const manifest = this.readManifest();
|
|
414
|
+
for (const entry of Object.values(manifest)) {
|
|
415
|
+
if (entry?.isEntry && entry.name === viewName && entry.file) {
|
|
416
|
+
return entry.file;
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
throw new Error(`View "${viewName}" not found in Vite manifest. Did the build complete successfully? Look for an entry with name "${viewName}" in dist/assets/.vite/manifest.json.`);
|
|
420
|
+
}
|
|
291
421
|
lookupDistFile(key) {
|
|
292
422
|
const manifest = this.readManifest();
|
|
293
423
|
return manifest[key]?.file;
|
|
294
424
|
}
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
425
|
+
/**
|
|
426
|
+
* Inject the Vite manifest as a value rather than letting `readManifest()`
|
|
427
|
+
* load it from disk. Required for runtimes without a usable filesystem
|
|
428
|
+
* (Cloudflare Workers, etc.) — the user's `skybridge build` emits the
|
|
429
|
+
* manifest as a JS module which the entry imports and passes here.
|
|
430
|
+
*/
|
|
431
|
+
setViteManifest(manifest) {
|
|
432
|
+
this.viteManifest = manifest;
|
|
433
|
+
return this;
|
|
300
434
|
}
|
|
301
435
|
readManifest() {
|
|
436
|
+
if (this.viteManifest) {
|
|
437
|
+
return this.viteManifest;
|
|
438
|
+
}
|
|
302
439
|
return JSON.parse(readFileSync(path.join(process.cwd(), "dist", "assets", ".vite", "manifest.json"), "utf-8"));
|
|
303
440
|
}
|
|
441
|
+
registerTool(...args) {
|
|
442
|
+
const baseFn = McpServerBase.prototype.registerTool;
|
|
443
|
+
if (typeof args[0] === "string") {
|
|
444
|
+
baseFn.call(this, args[0], args[1], args[2]);
|
|
445
|
+
return this;
|
|
446
|
+
}
|
|
447
|
+
const config = args[0];
|
|
448
|
+
const cb = args[1];
|
|
449
|
+
const { name, view, _meta: userToolMeta, ...toolFields } = config;
|
|
450
|
+
const toolMeta = { ...userToolMeta };
|
|
451
|
+
if (view) {
|
|
452
|
+
this.enforceOneToolPerView(view.component, name);
|
|
453
|
+
this.registerViewResources(name, view, toolMeta);
|
|
454
|
+
}
|
|
455
|
+
const wrappedCb = this.wrapHandler(cb, { attachViewUUID: Boolean(view) });
|
|
456
|
+
baseFn.call(this, name, { ...toolFields, _meta: toolMeta }, wrappedCb);
|
|
457
|
+
return this;
|
|
458
|
+
}
|
|
304
459
|
}
|
|
305
460
|
//# sourceMappingURL=server.js.map
|