skybridge 0.35.21 → 0.36.0
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 +8 -8
- package/dist/cli/header.js +1 -1
- package/dist/cli/header.js.map +1 -1
- package/dist/cli/use-nodemon.js +2 -2
- package/dist/cli/use-nodemon.js.map +1 -1
- package/dist/cli/use-tunnel.d.ts +12 -5
- package/dist/cli/use-tunnel.js +34 -29
- 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 +28 -7
- package/dist/commands/build.js.map +1 -1
- package/dist/commands/dev.d.ts +1 -0
- package/dist/commands/dev.js +7 -2
- package/dist/commands/dev.js.map +1 -1
- package/dist/commands/start.js +7 -10
- package/dist/commands/start.js.map +1 -1
- package/dist/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.js +70 -0
- package/dist/server/content-helpers.test.js.map +1 -0
- package/dist/server/express.js +3 -3
- package/dist/server/express.js.map +1 -1
- package/dist/server/express.test.js +39 -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 +95 -72
- package/dist/server/server.js +204 -73
- package/dist/server/server.js.map +1 -1
- package/dist/server/templateHelper.d.ts +5 -5
- package/dist/server/templates/development.hbs +2 -2
- package/dist/server/templates/production.hbs +1 -1
- 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.js +523 -0
- package/dist/test/view.test.js.map +1 -0
- 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-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 +8 -1
- package/tsconfig.base.json +2 -0
- 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 → server/content-helpers.test.d.ts} +0 -0
- /package/dist/{web/hooks/use-widget-state.test.d.ts → test/view.test.d.ts} +0 -0
- /package/dist/web/{plugin/validate-widget.test.d.ts → hooks/use-view-state.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,51 @@ 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 readonly serverInfo;
|
|
107
|
+
private readonly serverOptions?;
|
|
108
|
+
constructor(serverInfo: Implementation, options?: ServerOptions);
|
|
91
109
|
use(...handlers: RequestHandler[]): this;
|
|
92
110
|
use(path: string, ...handlers: RequestHandler[]): this;
|
|
93
111
|
useOnError(...handlers: ErrorRequestHandler[]): this;
|
|
94
112
|
useOnError(path: string, ...handlers: ErrorRequestHandler[]): this;
|
|
95
|
-
/**
|
|
96
|
-
* Register MCP protocol-level middleware (catch-all).
|
|
97
|
-
*/
|
|
113
|
+
/** Register MCP protocol-level middleware (catch-all). */
|
|
98
114
|
mcpMiddleware(handler: McpMiddlewareFn): this;
|
|
99
|
-
/**
|
|
100
|
-
* Register MCP protocol-level middleware for all requests (`extra` is `McpExtra`).
|
|
101
|
-
*/
|
|
115
|
+
/** Register MCP protocol-level middleware for all requests (`extra` is `McpExtra`). */
|
|
102
116
|
mcpMiddleware(filter: "request", handler: (request: {
|
|
103
117
|
method: string;
|
|
104
118
|
params: Record<string, unknown>;
|
|
105
119
|
}, extra: McpExtra, next: () => Promise<ServerResult>) => Promise<unknown> | unknown): this;
|
|
106
|
-
/**
|
|
107
|
-
* Register MCP protocol-level middleware for all notifications (`extra` is `undefined`).
|
|
108
|
-
*/
|
|
120
|
+
/** Register MCP protocol-level middleware for all notifications (`extra` is `undefined`). */
|
|
109
121
|
mcpMiddleware(filter: "notification", handler: (request: {
|
|
110
122
|
method: string;
|
|
111
123
|
params: Record<string, unknown>;
|
|
@@ -131,20 +143,31 @@ export declare class McpServer<TTools extends Record<string, ToolDef> = Record<n
|
|
|
131
143
|
mcpMiddleware(filter: McpMiddlewareFilter, handler: McpMiddlewareFn): this;
|
|
132
144
|
private applyMcpMiddleware;
|
|
133
145
|
connect(transport: Parameters<typeof McpServerBase.prototype.connect>[0]): Promise<void>;
|
|
146
|
+
/**
|
|
147
|
+
* Per-request stateless connect. The SDK's `Protocol` only allows one
|
|
148
|
+
* transport per instance, so we can't reuse this `McpServer` across
|
|
149
|
+
* concurrent requests. The SDK's idiomatic fix is a `() => McpServer`
|
|
150
|
+
* factory, but that would break Skybridge's singleton API — so instead
|
|
151
|
+
* we build a fresh underlying `Server` per request and share the main
|
|
152
|
+
* server's handler maps by reference. The cast is unavoidable: there's
|
|
153
|
+
* no public API to inject handler maps. `getHandlerMaps` validates the
|
|
154
|
+
* read side and fails fast on SDK field renames.
|
|
155
|
+
*/
|
|
156
|
+
connectStatelessTransport(transport: Parameters<typeof McpServerBase.prototype.connect>[0]): Promise<void>;
|
|
134
157
|
run(): Promise<void>;
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
registerTool<TName extends string, InputArgs extends ZodRawShapeCompat, TReturn extends {
|
|
142
|
-
content: CallToolResult["content"];
|
|
143
|
-
}>(name: TName, config: ToolConfig<InputArgs>, cb: ToolHandler<InputArgs, TReturn>): AddTool<TTools, TName, InputArgs, ExtractStructuredContent<TReturn>, ExtractMeta<TReturn>>;
|
|
144
|
-
registerTool<InputArgs extends ZodRawShapeCompat>(name: string, config: ToolConfig<InputArgs>, cb: ToolHandler<InputArgs>): RegisteredTool;
|
|
145
|
-
private registerWidgetResource;
|
|
158
|
+
private enforceOneToolPerView;
|
|
159
|
+
private registerViewResources;
|
|
160
|
+
private registerViewResource;
|
|
161
|
+
private wrapHandler;
|
|
162
|
+
private computeViewVersionParam;
|
|
163
|
+
private lookupViewFile;
|
|
146
164
|
private lookupDistFile;
|
|
147
|
-
private lookupDistFileWithIndexFallback;
|
|
148
165
|
private readManifest;
|
|
166
|
+
registerTool<TName extends string, InputArgs extends ZodRawShapeCompat, TReturn extends {
|
|
167
|
+
content?: HandlerContent;
|
|
168
|
+
}>(config: ToolConfig<InputArgs> & {
|
|
169
|
+
name: TName;
|
|
170
|
+
}, cb: ToolHandler<InputArgs, TReturn>): AddTool<TTools, TName, InputArgs, ExtractStructuredContent<TReturn>, ExtractMeta<TReturn>>;
|
|
171
|
+
registerTool<InputArgs extends ZodRawShapeCompat>(config: ToolConfig<InputArgs>, cb: ToolHandler<InputArgs>): this;
|
|
149
172
|
}
|
|
150
173
|
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,33 @@ 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
|
+
serverInfo;
|
|
40
|
+
serverOptions;
|
|
41
|
+
constructor(serverInfo, options) {
|
|
42
|
+
super(serverInfo, options);
|
|
43
|
+
this.serverInfo = serverInfo;
|
|
44
|
+
this.serverOptions = options;
|
|
45
|
+
}
|
|
24
46
|
use(pathOrHandler, ...handlers) {
|
|
25
47
|
if (typeof pathOrHandler === "string") {
|
|
26
48
|
this.customMiddleware.push({
|
|
@@ -83,7 +105,6 @@ export class McpServer extends McpServerBase {
|
|
|
83
105
|
return;
|
|
84
106
|
}
|
|
85
107
|
const { requestHandlers, notificationHandlers } = getHandlerMaps(this.server);
|
|
86
|
-
// Wrap existing handlers and proxy future .set() for lazy SDK registration
|
|
87
108
|
const instrumentMap = (map, isNotification) => {
|
|
88
109
|
for (const [method, handler] of map) {
|
|
89
110
|
map.set(method, buildMiddlewareChain(method, isNotification, handler, entries));
|
|
@@ -96,7 +117,26 @@ export class McpServer extends McpServerBase {
|
|
|
96
117
|
}
|
|
97
118
|
async connect(transport) {
|
|
98
119
|
this.applyMcpMiddleware();
|
|
99
|
-
return
|
|
120
|
+
return McpServerBase.prototype.connect.call(this, transport);
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Per-request stateless connect. The SDK's `Protocol` only allows one
|
|
124
|
+
* transport per instance, so we can't reuse this `McpServer` across
|
|
125
|
+
* concurrent requests. The SDK's idiomatic fix is a `() => McpServer`
|
|
126
|
+
* factory, but that would break Skybridge's singleton API — so instead
|
|
127
|
+
* we build a fresh underlying `Server` per request and share the main
|
|
128
|
+
* server's handler maps by reference. The cast is unavoidable: there's
|
|
129
|
+
* no public API to inject handler maps. `getHandlerMaps` validates the
|
|
130
|
+
* read side and fails fast on SDK field renames.
|
|
131
|
+
*/
|
|
132
|
+
async connectStatelessTransport(transport) {
|
|
133
|
+
this.applyMcpMiddleware();
|
|
134
|
+
const { requestHandlers, notificationHandlers } = getHandlerMaps(this.server);
|
|
135
|
+
const fresh = new SdkServer(this.serverInfo, this.serverOptions);
|
|
136
|
+
const target = fresh;
|
|
137
|
+
target._requestHandlers = requestHandlers;
|
|
138
|
+
target._notificationHandlers = notificationHandlers;
|
|
139
|
+
await fresh.connect(transport);
|
|
100
140
|
}
|
|
101
141
|
async run() {
|
|
102
142
|
this.applyMcpMiddleware();
|
|
@@ -110,7 +150,7 @@ export class McpServer extends McpServerBase {
|
|
|
110
150
|
});
|
|
111
151
|
}
|
|
112
152
|
httpServer.on("request", this.express);
|
|
113
|
-
|
|
153
|
+
await new Promise((resolve, reject) => {
|
|
114
154
|
httpServer.on("error", (error) => {
|
|
115
155
|
console.error("Failed to start server:", error);
|
|
116
156
|
reject(error);
|
|
@@ -120,101 +160,136 @@ export class McpServer extends McpServerBase {
|
|
|
120
160
|
resolve();
|
|
121
161
|
});
|
|
122
162
|
});
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
163
|
+
const shutdown = () => {
|
|
164
|
+
// Drop both handlers so a second signal falls through to Node's default
|
|
165
|
+
// (force-quit on a second Ctrl+C while drain is hanging).
|
|
166
|
+
process.off("SIGTERM", shutdown);
|
|
167
|
+
process.off("SIGINT", shutdown);
|
|
168
|
+
httpServer.close(() => process.exit(0));
|
|
169
|
+
// Force exit if connections don't drain in time so the port is still
|
|
170
|
+
// released promptly (e.g. for nodemon restarts).
|
|
171
|
+
setTimeout(() => process.exit(0), 3000).unref();
|
|
128
172
|
};
|
|
129
|
-
|
|
130
|
-
|
|
173
|
+
process.on("SIGTERM", shutdown);
|
|
174
|
+
process.on("SIGINT", shutdown);
|
|
175
|
+
}
|
|
176
|
+
enforceOneToolPerView(component, toolName) {
|
|
177
|
+
const existingTool = this.claimedViews.get(component);
|
|
178
|
+
if (existingTool) {
|
|
179
|
+
throw new Error(`skybridge: view "${component}" is already used by tool "${existingTool}". Tool "${toolName}" cannot also reference it — each view backs exactly one tool.`);
|
|
180
|
+
}
|
|
181
|
+
this.claimedViews.set(component, toolName);
|
|
182
|
+
}
|
|
183
|
+
registerViewResources(toolName, view, toolMeta) {
|
|
184
|
+
const hosts = view.hosts ?? ["apps-sdk", "mcp-app"];
|
|
185
|
+
// Append a content-derived version param so hosts (e.g. ChatGPT) bust
|
|
186
|
+
// their cache when the bundle changes, but keep the URI stable across
|
|
187
|
+
// `tools/list` calls when the bundle hasn't changed.
|
|
188
|
+
const versionParam = this.computeViewVersionParam(view.component);
|
|
189
|
+
if (hosts.includes("apps-sdk")) {
|
|
190
|
+
const viewResource = {
|
|
131
191
|
hostType: "apps-sdk",
|
|
132
|
-
uri: `ui://
|
|
192
|
+
uri: `ui://views/apps-sdk/${view.component}.html${versionParam}`,
|
|
133
193
|
mimeType: "text/html+skybridge",
|
|
134
194
|
buildContentMeta: ({ resourceDomains, connectDomains, domain }, overrides) => {
|
|
135
|
-
const userUi = userMeta?.ui;
|
|
136
|
-
const userCsp = userUi?.csp;
|
|
137
195
|
const defaults = {
|
|
138
196
|
"openai/widgetCSP": {
|
|
139
197
|
resource_domains: resourceDomains,
|
|
140
198
|
connect_domains: connectDomains,
|
|
141
199
|
},
|
|
142
200
|
"openai/widgetDomain": domain,
|
|
143
|
-
"openai/widgetDescription":
|
|
201
|
+
"openai/widgetDescription": view.description,
|
|
144
202
|
};
|
|
145
|
-
const
|
|
203
|
+
const fromView = {
|
|
146
204
|
"openai/widgetCSP": {
|
|
147
|
-
resource_domains:
|
|
148
|
-
connect_domains:
|
|
149
|
-
frame_domains:
|
|
150
|
-
redirect_domains:
|
|
205
|
+
resource_domains: view.csp?.resourceDomains,
|
|
206
|
+
connect_domains: view.csp?.connectDomains,
|
|
207
|
+
frame_domains: view.csp?.frameDomains,
|
|
208
|
+
redirect_domains: view.csp?.redirectDomains,
|
|
151
209
|
},
|
|
152
|
-
"openai/widgetDomain":
|
|
153
|
-
"openai/widgetPrefersBorder":
|
|
210
|
+
"openai/widgetDomain": view.domain,
|
|
211
|
+
"openai/widgetPrefersBorder": view.prefersBorder,
|
|
154
212
|
};
|
|
155
|
-
const
|
|
156
|
-
|
|
213
|
+
const base = mergeWithUnion(mergeWithUnion(defaults, fromView), {
|
|
214
|
+
"openai/widgetDomain": overrides.domain,
|
|
215
|
+
});
|
|
216
|
+
if (view._meta) {
|
|
217
|
+
return { ...base, ...view._meta };
|
|
218
|
+
}
|
|
219
|
+
return base;
|
|
157
220
|
},
|
|
158
221
|
};
|
|
159
|
-
this.
|
|
160
|
-
name,
|
|
161
|
-
|
|
162
|
-
|
|
222
|
+
this.registerViewResource({
|
|
223
|
+
name: toolName,
|
|
224
|
+
viewResource,
|
|
225
|
+
view,
|
|
163
226
|
});
|
|
164
|
-
toolMeta["openai/outputTemplate"] =
|
|
227
|
+
toolMeta["openai/outputTemplate"] = viewResource.uri;
|
|
165
228
|
}
|
|
166
|
-
if (
|
|
167
|
-
const
|
|
229
|
+
if (hosts.includes("mcp-app")) {
|
|
230
|
+
const viewResource = {
|
|
168
231
|
hostType: "mcp-app",
|
|
169
|
-
uri: `ui://
|
|
232
|
+
uri: `ui://views/ext-apps/${view.component}.html${versionParam}`,
|
|
170
233
|
mimeType: "text/html;profile=mcp-app",
|
|
171
|
-
buildContentMeta: ({ resourceDomains, connectDomains, domain }, overrides) => {
|
|
234
|
+
buildContentMeta: ({ resourceDomains, connectDomains, domain, baseUriDomains }, overrides) => {
|
|
172
235
|
const defaults = {
|
|
173
236
|
ui: {
|
|
174
237
|
csp: {
|
|
175
238
|
resourceDomains,
|
|
176
239
|
connectDomains,
|
|
240
|
+
baseUriDomains,
|
|
177
241
|
},
|
|
178
242
|
domain,
|
|
179
243
|
},
|
|
180
244
|
};
|
|
181
|
-
|
|
182
|
-
ui: {
|
|
245
|
+
const fromView = {
|
|
246
|
+
ui: {
|
|
247
|
+
...(view.description && { description: view.description }),
|
|
248
|
+
...(view.prefersBorder !== undefined && {
|
|
249
|
+
prefersBorder: view.prefersBorder,
|
|
250
|
+
}),
|
|
251
|
+
...(view.domain && { domain: view.domain }),
|
|
252
|
+
csp: {
|
|
253
|
+
...(view.csp?.resourceDomains && {
|
|
254
|
+
resourceDomains: view.csp.resourceDomains,
|
|
255
|
+
}),
|
|
256
|
+
...(view.csp?.connectDomains && {
|
|
257
|
+
connectDomains: view.csp.connectDomains,
|
|
258
|
+
}),
|
|
259
|
+
...(view.csp?.frameDomains && {
|
|
260
|
+
frameDomains: view.csp.frameDomains,
|
|
261
|
+
}),
|
|
262
|
+
...(view.csp?.baseUriDomains && {
|
|
263
|
+
baseUriDomains: view.csp.baseUriDomains,
|
|
264
|
+
}),
|
|
265
|
+
...(view.csp?.redirectDomains && {
|
|
266
|
+
redirectDomains: view.csp.redirectDomains,
|
|
267
|
+
}),
|
|
268
|
+
},
|
|
269
|
+
},
|
|
270
|
+
};
|
|
271
|
+
const base = mergeWithUnion(mergeWithUnion(defaults, fromView), {
|
|
272
|
+
ui: overrides,
|
|
183
273
|
});
|
|
274
|
+
if (view._meta) {
|
|
275
|
+
return { ...base, ...view._meta };
|
|
276
|
+
}
|
|
277
|
+
return base;
|
|
184
278
|
},
|
|
185
279
|
};
|
|
186
|
-
this.
|
|
187
|
-
name,
|
|
188
|
-
|
|
189
|
-
|
|
280
|
+
this.registerViewResource({
|
|
281
|
+
name: toolName,
|
|
282
|
+
viewResource,
|
|
283
|
+
view,
|
|
190
284
|
});
|
|
191
285
|
// @ts-expect-error - For backwards compatibility with Claude current implementation of the specs
|
|
192
|
-
toolMeta["ui/resourceUri"] =
|
|
193
|
-
toolMeta.ui = { resourceUri:
|
|
286
|
+
toolMeta["ui/resourceUri"] = viewResource.uri;
|
|
287
|
+
toolMeta.ui = { resourceUri: viewResource.uri };
|
|
194
288
|
}
|
|
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
289
|
}
|
|
215
|
-
|
|
216
|
-
const { hostType, uri:
|
|
217
|
-
this.registerResource(name,
|
|
290
|
+
registerViewResource({ name, viewResource, view, }) {
|
|
291
|
+
const { hostType, uri: viewUri, mimeType, buildContentMeta } = viewResource;
|
|
292
|
+
this.registerResource(name, viewUri, { description: view.description }, async (uri, extra) => {
|
|
218
293
|
const isProduction = process.env.NODE_ENV === "production";
|
|
219
294
|
const isClaude = extra?.requestInfo?.headers?.["user-agent"] === "Claude-User";
|
|
220
295
|
const headers = extra?.requestInfo?.headers || {};
|
|
@@ -247,13 +322,13 @@ export class McpServer extends McpServerBase {
|
|
|
247
322
|
? templateHelper.renderProduction({
|
|
248
323
|
hostType,
|
|
249
324
|
serverUrl,
|
|
250
|
-
|
|
251
|
-
styleFile: this.lookupDistFile("style.css"),
|
|
325
|
+
viewFile: this.lookupViewFile(view.component),
|
|
326
|
+
styleFile: this.lookupDistFile("style.css") ?? "",
|
|
252
327
|
})
|
|
253
328
|
: templateHelper.renderDevelopment({
|
|
254
329
|
hostType,
|
|
255
330
|
serverUrl,
|
|
256
|
-
|
|
331
|
+
viewName: view.component,
|
|
257
332
|
});
|
|
258
333
|
const connectDomains = [serverUrl];
|
|
259
334
|
if (!isProduction) {
|
|
@@ -288,18 +363,74 @@ export class McpServer extends McpServerBase {
|
|
|
288
363
|
};
|
|
289
364
|
});
|
|
290
365
|
}
|
|
291
|
-
|
|
366
|
+
wrapHandler(cb, { attachViewUUID }) {
|
|
367
|
+
return async (args, extra) => {
|
|
368
|
+
const result = await cb(args, extra);
|
|
369
|
+
return {
|
|
370
|
+
...result,
|
|
371
|
+
content: normalizeContent(result.content),
|
|
372
|
+
...(attachViewUUID && {
|
|
373
|
+
_meta: {
|
|
374
|
+
...result._meta,
|
|
375
|
+
viewUUID: crypto.randomUUID(),
|
|
376
|
+
},
|
|
377
|
+
}),
|
|
378
|
+
};
|
|
379
|
+
};
|
|
380
|
+
}
|
|
381
|
+
computeViewVersionParam(viewName) {
|
|
382
|
+
if (process.env.NODE_ENV !== "production") {
|
|
383
|
+
return "";
|
|
384
|
+
}
|
|
385
|
+
try {
|
|
386
|
+
const viewFile = this.lookupViewFile(viewName);
|
|
387
|
+
const styleFile = this.lookupDistFile("style.css") ?? "";
|
|
388
|
+
const hash = crypto
|
|
389
|
+
.createHash("sha256")
|
|
390
|
+
.update(viewFile)
|
|
391
|
+
.update("\0")
|
|
392
|
+
.update(styleFile)
|
|
393
|
+
.digest("hex")
|
|
394
|
+
.slice(0, 8);
|
|
395
|
+
return `?v=${hash}`;
|
|
396
|
+
}
|
|
397
|
+
catch {
|
|
398
|
+
return "";
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
lookupViewFile(viewName) {
|
|
292
402
|
const manifest = this.readManifest();
|
|
293
|
-
|
|
403
|
+
for (const entry of Object.values(manifest)) {
|
|
404
|
+
if (entry?.isEntry && entry.name === viewName && entry.file) {
|
|
405
|
+
return entry.file;
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
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.`);
|
|
294
409
|
}
|
|
295
|
-
|
|
410
|
+
lookupDistFile(key) {
|
|
296
411
|
const manifest = this.readManifest();
|
|
297
|
-
|
|
298
|
-
const indexFileKey = `${basePath}/index.tsx`;
|
|
299
|
-
return manifest[flatFileKey]?.file ?? manifest[indexFileKey]?.file;
|
|
412
|
+
return manifest[key]?.file;
|
|
300
413
|
}
|
|
301
414
|
readManifest() {
|
|
302
415
|
return JSON.parse(readFileSync(path.join(process.cwd(), "dist", "assets", ".vite", "manifest.json"), "utf-8"));
|
|
303
416
|
}
|
|
417
|
+
registerTool(...args) {
|
|
418
|
+
const baseFn = McpServerBase.prototype.registerTool;
|
|
419
|
+
if (typeof args[0] === "string") {
|
|
420
|
+
baseFn.call(this, args[0], args[1], args[2]);
|
|
421
|
+
return this;
|
|
422
|
+
}
|
|
423
|
+
const config = args[0];
|
|
424
|
+
const cb = args[1];
|
|
425
|
+
const { name, view, _meta: userToolMeta, ...toolFields } = config;
|
|
426
|
+
const toolMeta = { ...userToolMeta };
|
|
427
|
+
if (view) {
|
|
428
|
+
this.enforceOneToolPerView(view.component, name);
|
|
429
|
+
this.registerViewResources(name, view, toolMeta);
|
|
430
|
+
}
|
|
431
|
+
const wrappedCb = this.wrapHandler(cb, { attachViewUUID: Boolean(view) });
|
|
432
|
+
baseFn.call(this, name, { ...toolFields, _meta: toolMeta }, wrappedCb);
|
|
433
|
+
return this;
|
|
434
|
+
}
|
|
304
435
|
}
|
|
305
436
|
//# sourceMappingURL=server.js.map
|