skybridge 0.0.0-dev.bc2c12c → 0.0.0-dev.bc518c9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +123 -124
- package/dist/cli/build-helpers.d.ts +5 -0
- package/dist/cli/build-helpers.js +60 -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 +44 -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 +8 -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-open-tunnel-browser.d.ts +6 -0
- package/dist/cli/use-open-tunnel-browser.js +19 -0
- package/dist/cli/use-open-tunnel-browser.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 +8 -32
- package/dist/commands/build.js.map +1 -1
- package/dist/commands/create.d.ts +9 -0
- package/dist/commands/create.js +30 -0
- package/dist/commands/create.js.map +1 -0
- package/dist/commands/dev.js +19 -1
- package/dist/commands/dev.js.map +1 -1
- package/dist/server/asset-base-url-transform-plugin.d.ts +1 -0
- package/dist/server/asset-base-url-transform-plugin.js +16 -1
- package/dist/server/asset-base-url-transform-plugin.js.map +1 -1
- package/dist/server/asset-base-url-transform-plugin.test.js +51 -1
- package/dist/server/asset-base-url-transform-plugin.test.js.map +1 -1
- package/dist/server/auth.d.ts +20 -0
- package/dist/server/auth.js +28 -0
- package/dist/server/auth.js.map +1 -0
- package/dist/server/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 +30 -0
- package/dist/server/express.test.js.map +1 -1
- package/dist/server/file-ref.d.ts +28 -0
- package/dist/server/file-ref.js +27 -0
- package/dist/server/file-ref.js.map +1 -0
- package/dist/server/index.d.ts +3 -1
- package/dist/server/index.js +2 -0
- package/dist/server/index.js.map +1 -1
- package/dist/server/middleware.d.ts +16 -3
- package/dist/server/middleware.js.map +1 -1
- package/dist/server/server.d.ts +207 -3
- package/dist/server/server.js +166 -59
- package/dist/server/server.js.map +1 -1
- 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 +3 -1
- package/dist/web/bridges/apps-sdk/adaptor.js +12 -1
- package/dist/web/bridges/apps-sdk/adaptor.js.map +1 -1
- package/dist/web/bridges/apps-sdk/bridge.d.ts +1 -0
- package/dist/web/bridges/apps-sdk/bridge.js +1 -0
- package/dist/web/bridges/apps-sdk/bridge.js.map +1 -1
- package/dist/web/bridges/apps-sdk/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 +3 -1
- package/dist/web/bridges/mcp-app/adaptor.js +9 -0
- package/dist/web/bridges/mcp-app/adaptor.js.map +1 -1
- package/dist/web/bridges/mcp-app/bridge.d.ts +1 -0
- package/dist/web/bridges/mcp-app/bridge.js +1 -0
- 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/types.d.ts +55 -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/hooks/index.d.ts +1 -0
- package/dist/web/hooks/index.js +1 -0
- package/dist/web/hooks/index.js.map +1 -1
- package/dist/web/hooks/test/utils.d.ts +6 -2
- package/dist/web/hooks/test/utils.js +13 -2
- package/dist/web/hooks/test/utils.js.map +1 -1
- package/dist/web/hooks/use-call-tool.d.ts +45 -0
- package/dist/web/hooks/use-call-tool.js +28 -0
- package/dist/web/hooks/use-call-tool.js.map +1 -1
- package/dist/web/hooks/use-call-tool.test.js +27 -2
- package/dist/web/hooks/use-call-tool.test.js.map +1 -1
- package/dist/web/hooks/use-display-mode.d.ts +20 -0
- package/dist/web/hooks/use-display-mode.js +20 -0
- package/dist/web/hooks/use-display-mode.js.map +1 -1
- package/dist/web/hooks/use-download.d.ts +5 -0
- package/dist/web/hooks/use-download.js +8 -0
- package/dist/web/hooks/use-download.js.map +1 -0
- package/dist/web/hooks/use-download.test.d.ts +1 -0
- package/dist/web/hooks/use-download.test.js +95 -0
- package/dist/web/hooks/use-download.test.js.map +1 -0
- package/dist/web/hooks/use-files.d.ts +32 -0
- package/dist/web/hooks/use-files.js +32 -0
- package/dist/web/hooks/use-files.js.map +1 -1
- package/dist/web/hooks/use-layout.d.ts +2 -0
- package/dist/web/hooks/use-layout.js +2 -0
- package/dist/web/hooks/use-layout.js.map +1 -1
- package/dist/web/hooks/use-open-external.d.ts +17 -0
- package/dist/web/hooks/use-open-external.js +16 -0
- package/dist/web/hooks/use-open-external.js.map +1 -1
- package/dist/web/hooks/use-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 +33 -0
- package/dist/web/hooks/use-tool-info.js +26 -0
- package/dist/web/hooks/use-tool-info.js.map +1 -1
- package/dist/web/hooks/use-user.d.ts +2 -0
- package/dist/web/hooks/use-user.js +2 -0
- package/dist/web/hooks/use-user.js.map +1 -1
- package/dist/web/hooks/use-view-state.d.ts +21 -0
- package/dist/web/hooks/use-view-state.js.map +1 -1
- package/dist/web/mount-view.d.ts +19 -0
- package/dist/web/mount-view.js +19 -0
- package/dist/web/mount-view.js.map +1 -1
- package/dist/web/plugin/plugin.d.ts +28 -0
- package/dist/web/plugin/plugin.js +33 -0
- package/dist/web/plugin/plugin.js.map +1 -1
- package/dist/web/types.d.ts +4 -0
- package/dist/web/types.js.map +1 -1
- package/package.json +22 -16
package/dist/server/server.d.ts
CHANGED
|
@@ -1,16 +1,30 @@
|
|
|
1
|
+
import type { McpUiToolMeta } from "@modelcontextprotocol/ext-apps";
|
|
1
2
|
import { type ServerOptions } from "@modelcontextprotocol/sdk/server/index.js";
|
|
2
3
|
import { McpServer as McpServerBase } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
3
4
|
import type { AnySchema, SchemaOutput, ZodRawShapeCompat } from "@modelcontextprotocol/sdk/server/zod-compat.js";
|
|
4
5
|
import type { RequestHandlerExtra } from "@modelcontextprotocol/sdk/shared/protocol.js";
|
|
5
|
-
import type { ContentBlock, Implementation, ServerNotification, ServerRequest, ServerResult, ToolAnnotations } from "@modelcontextprotocol/sdk/types.js";
|
|
6
|
+
import type { ContentBlock, Implementation, RequestMeta, ServerNotification, ServerRequest, ServerResult, ToolAnnotations } from "@modelcontextprotocol/sdk/types.js";
|
|
6
7
|
import { type ErrorRequestHandler, type Express, type RequestHandler } from "express";
|
|
7
8
|
import type { McpExtra, McpExtraFor, McpMethodString, McpMiddlewareFilter, McpMiddlewareFn, McpResultFor, McpTypedMiddlewareFn, McpWildcard } from "./middleware.js";
|
|
9
|
+
/**
|
|
10
|
+
* Type marker for a registered tool — carries its input, output, and response
|
|
11
|
+
* metadata shapes so views can infer types from `typeof server`.
|
|
12
|
+
*
|
|
13
|
+
* You normally never construct this by hand; it is produced by `registerTool`
|
|
14
|
+
* and consumed by helpers like {@link InferTools} and {@link generateHelpers}.
|
|
15
|
+
*/
|
|
8
16
|
export type ToolDef<TInput = unknown, TOutput = unknown, TResponseMetadata = unknown> = {
|
|
9
17
|
input: TInput;
|
|
10
18
|
output: TOutput;
|
|
11
19
|
responseMetadata: TResponseMetadata;
|
|
12
20
|
};
|
|
21
|
+
/** Which host runtime a view targets — `"apps-sdk"` (ChatGPT) or `"mcp-app"` (MCP Apps spec). */
|
|
13
22
|
export type ViewHostType = "apps-sdk" | "mcp-app";
|
|
23
|
+
/**
|
|
24
|
+
* Content Security Policy origins attached to a view's resource. Each list is
|
|
25
|
+
* passed through to the host's CSP for the view iframe; omit a field to inherit
|
|
26
|
+
* the host's default for that directive.
|
|
27
|
+
*/
|
|
14
28
|
export interface ViewCsp {
|
|
15
29
|
/** Origins for static assets (images, fonts, scripts, styles). */
|
|
16
30
|
resourceDomains?: string[];
|
|
@@ -23,24 +37,67 @@ export interface ViewCsp {
|
|
|
23
37
|
/** Origins allowed in `<base href>` tags (mcp-apps only). */
|
|
24
38
|
baseUriDomains?: string[];
|
|
25
39
|
}
|
|
40
|
+
/**
|
|
41
|
+
* Registry of view component names. The Skybridge Vite plugin augments this
|
|
42
|
+
* interface in the generated `.skybridge/views.d.ts` with one key per view
|
|
43
|
+
* file, which narrows {@link ViewName} from `string` to the concrete union.
|
|
44
|
+
*/
|
|
26
45
|
export interface ViewNameRegistry {
|
|
27
46
|
}
|
|
47
|
+
/** Union of valid view component names. Narrowed by {@link ViewNameRegistry}. */
|
|
28
48
|
export type ViewName = keyof ViewNameRegistry & string;
|
|
49
|
+
/**
|
|
50
|
+
* Pass under `view` in a tool's `registerTool` config to render the tool's
|
|
51
|
+
* result through a Skybridge view instead of a plain text response.
|
|
52
|
+
*/
|
|
29
53
|
export interface ViewConfig {
|
|
54
|
+
/** Filename of the view module (without extension) — matches a file in your `viewsDir`. */
|
|
30
55
|
component: ViewName;
|
|
56
|
+
/** Human-readable label the host may show alongside the view. */
|
|
31
57
|
description?: string;
|
|
58
|
+
/** Restrict where the view is rendered. Defaults to all known hosts. */
|
|
32
59
|
hosts?: ViewHostType[];
|
|
60
|
+
/** Apps SDK only: request a visible border around the widget. */
|
|
33
61
|
prefersBorder?: boolean;
|
|
62
|
+
/** Apps SDK only: override the iframe's served domain (advanced). */
|
|
34
63
|
domain?: string;
|
|
64
|
+
/** Per-view CSP overrides — see {@link ViewCsp}. */
|
|
35
65
|
csp?: ViewCsp;
|
|
66
|
+
/** Free-form metadata forwarded on the view resource's `_meta`. */
|
|
36
67
|
_meta?: Record<string, unknown>;
|
|
37
68
|
}
|
|
69
|
+
export type SecurityScheme = {
|
|
70
|
+
type: "noauth";
|
|
71
|
+
} | {
|
|
72
|
+
type: "oauth2";
|
|
73
|
+
scopes?: string[];
|
|
74
|
+
};
|
|
75
|
+
/**
|
|
76
|
+
* Well-known keys recognized by host runtimes when set on a tool's `_meta`.
|
|
77
|
+
* Use {@link ToolMeta} to also pass arbitrary custom metadata alongside these.
|
|
78
|
+
*
|
|
79
|
+
* @see https://developers.openai.com/apps-sdk/reference#tool-descriptor-parameters
|
|
80
|
+
*/
|
|
38
81
|
export interface KnownToolMeta {
|
|
82
|
+
/** Apps SDK: allow the rendered view to call this tool from inside its iframe. */
|
|
39
83
|
"openai/widgetAccessible"?: boolean;
|
|
84
|
+
/** Apps SDK: status text shown while the tool is running (e.g. `"Searching trips"`). */
|
|
40
85
|
"openai/toolInvocation/invoking"?: string;
|
|
86
|
+
/** Apps SDK: status text shown once the tool returns (e.g. `"Found 3 trips"`). */
|
|
41
87
|
"openai/toolInvocation/invoked"?: string;
|
|
88
|
+
/** Apps SDK: input parameters that hold file references — the host attaches uploaded files to them. */
|
|
89
|
+
"openai/fileParams"?: string[];
|
|
90
|
+
/** MCP Apps: control whether the tool is exposed to the model, the app, or both. */
|
|
91
|
+
ui?: Pick<McpUiToolMeta, "visibility">;
|
|
92
|
+
securitySchemes?: SecurityScheme[];
|
|
42
93
|
}
|
|
94
|
+
/** {@link KnownToolMeta} merged with arbitrary string-keyed metadata for custom flags. */
|
|
43
95
|
export type ToolMeta = KnownToolMeta & Record<string, unknown>;
|
|
96
|
+
/**
|
|
97
|
+
* Convenient return type for tool handlers — a plain string, a single
|
|
98
|
+
* {@link ContentBlock}, or an array. Skybridge normalizes it to the MCP
|
|
99
|
+
* `content: ContentBlock[]` shape before responding.
|
|
100
|
+
*/
|
|
44
101
|
export type HandlerContent = string | ContentBlock | ContentBlock[];
|
|
45
102
|
/**
|
|
46
103
|
* Type-level marker interface for cross-package type inference.
|
|
@@ -84,17 +141,92 @@ interface ToolConfig<TInput extends ZodRawShapeCompat | AnySchema> {
|
|
|
84
141
|
outputSchema?: ZodRawShapeCompat | AnySchema;
|
|
85
142
|
annotations?: ToolAnnotations;
|
|
86
143
|
view?: ViewConfig;
|
|
144
|
+
/**
|
|
145
|
+
* Declares which auth schemes this tool supports (e.g. `noauth`, `oauth2`).
|
|
146
|
+
* Lets clients label tools that require sign-in before calling, and pass
|
|
147
|
+
* the right scopes through the OAuth flow. Listing both `noauth` and
|
|
148
|
+
* `oauth2` signals that the tool works for anonymous callers and gives
|
|
149
|
+
* enhanced behavior to authenticated ones.
|
|
150
|
+
*/
|
|
151
|
+
securitySchemes?: SecurityScheme[];
|
|
87
152
|
_meta?: ToolMeta;
|
|
88
153
|
}
|
|
154
|
+
/**
|
|
155
|
+
* Optional client-supplied hints attached to `params._meta` on every tool call
|
|
156
|
+
* by the Apps SDK host. Hints only: never use for authorization, and tolerate
|
|
157
|
+
* absence.
|
|
158
|
+
* @see https://developers.openai.com/apps-sdk/reference#_meta-fields-the-client-provides
|
|
159
|
+
*/
|
|
160
|
+
export interface ClientHintsMeta {
|
|
161
|
+
/** Requested locale (BCP-47, e.g. `"en-US"`). */
|
|
162
|
+
"openai/locale"?: string;
|
|
163
|
+
/** Browser user-agent */
|
|
164
|
+
"openai/userAgent"?: string;
|
|
165
|
+
/** Coarse user location. May be partially populated. */
|
|
166
|
+
"openai/userLocation"?: {
|
|
167
|
+
city?: string;
|
|
168
|
+
region?: string;
|
|
169
|
+
country?: string;
|
|
170
|
+
timezone?: string;
|
|
171
|
+
longitude?: number;
|
|
172
|
+
latitude?: number;
|
|
173
|
+
};
|
|
174
|
+
/** Anonymized user id. */
|
|
175
|
+
"openai/subject"?: string;
|
|
176
|
+
/** Anonymized conversation id, stable within a ChatGPT session. */
|
|
177
|
+
"openai/session"?: string;
|
|
178
|
+
/** Anonymized organization id, when the user account is part of an organization. */
|
|
179
|
+
"openai/organization"?: string;
|
|
180
|
+
/** Stable id for the currently mounted widget instance. */
|
|
181
|
+
"openai/widgetSessionId"?: string;
|
|
182
|
+
}
|
|
183
|
+
type ToolHandlerExtra = Omit<RequestHandlerExtra<ServerRequest, ServerNotification>, "_meta"> & {
|
|
184
|
+
_meta?: RequestMeta & ClientHintsMeta;
|
|
185
|
+
};
|
|
89
186
|
type ToolHandler<TInput extends ZodRawShapeCompat, TReturn extends {
|
|
90
187
|
content?: HandlerContent;
|
|
91
188
|
} = {
|
|
92
189
|
content?: HandlerContent;
|
|
93
|
-
}> = (args: ShapeOutput<TInput>, extra:
|
|
190
|
+
}> = (args: ShapeOutput<TInput>, extra: ToolHandlerExtra) => TReturn | Promise<TReturn>;
|
|
191
|
+
/**
|
|
192
|
+
* Coerce a tool handler's return value into an MCP `content` array. Strings
|
|
193
|
+
* become a single `TextContent`; a single block is wrapped in an array;
|
|
194
|
+
* `undefined` produces `[]`. Mostly used internally — exported so consumers
|
|
195
|
+
* who build content lazily can apply the same normalization.
|
|
196
|
+
*/
|
|
94
197
|
export declare function normalizeContent(content: HandlerContent | undefined): ContentBlock[];
|
|
95
198
|
interface McpServerBaseOmitted extends Omit<McpServerBase, "registerTool" | "connect"> {
|
|
96
199
|
}
|
|
97
200
|
declare const McpServerBaseOmitted: new (...args: ConstructorParameters<typeof McpServerBase>) => McpServerBaseOmitted;
|
|
201
|
+
/**
|
|
202
|
+
* The Skybridge server. Extends the MCP SDK's `McpServer` with a typed tool
|
|
203
|
+
* registry, view resources, an embedded Express app, and protocol-level
|
|
204
|
+
* middleware. Construct it with the same `Implementation` info you would pass
|
|
205
|
+
* to the SDK, chain {@link McpServer.registerTool} calls to declare tools,
|
|
206
|
+
* then call {@link McpServer.run} to start the HTTP server.
|
|
207
|
+
*
|
|
208
|
+
* The `TTools` generic accumulates each registered tool's input/output/meta
|
|
209
|
+
* shape, so `typeof server` carries enough information for view-side helpers
|
|
210
|
+
* like {@link generateHelpers} to produce fully-typed hooks.
|
|
211
|
+
*
|
|
212
|
+
* @typeParam TTools - Accumulated tool registry. Filled in by `registerTool`
|
|
213
|
+
* chaining; you almost never set this manually.
|
|
214
|
+
*
|
|
215
|
+
* @example
|
|
216
|
+
* ```ts
|
|
217
|
+
* const server = new McpServer({ name: "my-app", version: "1.0.0" }, {})
|
|
218
|
+
* .registerTool({
|
|
219
|
+
* name: "search",
|
|
220
|
+
* inputSchema: { query: z.string() },
|
|
221
|
+
* view: { component: "search" },
|
|
222
|
+
* }, async ({ query }) => ({ content: `Results for ${query}` }));
|
|
223
|
+
*
|
|
224
|
+
* await server.run();
|
|
225
|
+
* export type AppType = typeof server;
|
|
226
|
+
* ```
|
|
227
|
+
*
|
|
228
|
+
* @see https://docs.skybridge.tech/api-reference/mcp-server
|
|
229
|
+
*/
|
|
98
230
|
export declare class McpServer<TTools extends Record<string, ToolDef> = Record<never, ToolDef>> extends McpServerBaseOmitted {
|
|
99
231
|
readonly $types: McpServerTypes<TTools>;
|
|
100
232
|
/**
|
|
@@ -114,12 +246,34 @@ export declare class McpServer<TTools extends Record<string, ToolDef> = Record<n
|
|
|
114
246
|
private mcpMiddlewareEntries;
|
|
115
247
|
private mcpMiddlewareApplied;
|
|
116
248
|
private claimedViews;
|
|
249
|
+
private viewMetaBuilders;
|
|
117
250
|
private viteManifest;
|
|
118
251
|
private readonly serverInfo;
|
|
119
252
|
private readonly serverOptions?;
|
|
120
253
|
constructor(serverInfo: Implementation, options?: ServerOptions);
|
|
254
|
+
/**
|
|
255
|
+
* Register Express middleware on the underlying app. Mirrors `app.use` —
|
|
256
|
+
* pass handlers directly or a path-prefixed handler list. Register before
|
|
257
|
+
* {@link McpServer.run}; ordering matches Express.
|
|
258
|
+
*
|
|
259
|
+
* Note: Alpic Cloud only routes traffic to `/mcp`. Custom paths work
|
|
260
|
+
* locally and on self-hosted deployments.
|
|
261
|
+
*/
|
|
121
262
|
use(...handlers: RequestHandler[]): this;
|
|
122
263
|
use(path: string, ...handlers: RequestHandler[]): this;
|
|
264
|
+
/**
|
|
265
|
+
* Register Express error-handling middleware to run after the built-in
|
|
266
|
+
* `/mcp` route (or your custom route). Use this to log or transform errors
|
|
267
|
+
* thrown by tool handlers before the default error handler responds.
|
|
268
|
+
*
|
|
269
|
+
* @example
|
|
270
|
+
* ```ts
|
|
271
|
+
* server.useOnError((err, _req, _res, next) => {
|
|
272
|
+
* logger.error(err);
|
|
273
|
+
* next(err);
|
|
274
|
+
* });
|
|
275
|
+
* ```
|
|
276
|
+
*/
|
|
123
277
|
useOnError(...handlers: ErrorRequestHandler[]): this;
|
|
124
278
|
useOnError(path: string, ...handlers: ErrorRequestHandler[]): this;
|
|
125
279
|
/** Register MCP protocol-level middleware (catch-all). */
|
|
@@ -154,6 +308,14 @@ export declare class McpServer<TTools extends Record<string, ToolDef> = Record<n
|
|
|
154
308
|
*/
|
|
155
309
|
mcpMiddleware(filter: McpMiddlewareFilter, handler: McpMiddlewareFn): this;
|
|
156
310
|
private applyMcpMiddleware;
|
|
311
|
+
/**
|
|
312
|
+
* Connect to an MCP transport (override of the SDK's `connect`). Use this
|
|
313
|
+
* when you're embedding Skybridge in a host that already manages its own
|
|
314
|
+
* transport (e.g. stdio for desktop apps); for HTTP, prefer {@link McpServer.run}
|
|
315
|
+
* which sets the transport up for you. Locks in any middleware registered
|
|
316
|
+
* via {@link McpServer.mcpMiddleware} — further calls to that method will
|
|
317
|
+
* throw afterwards.
|
|
318
|
+
*/
|
|
157
319
|
connect(transport: Parameters<typeof McpServerBase.prototype.connect>[0]): Promise<void>;
|
|
158
320
|
/**
|
|
159
321
|
* Per-request stateless connect. The SDK's `Protocol` only allows one
|
|
@@ -166,10 +328,23 @@ export declare class McpServer<TTools extends Record<string, ToolDef> = Record<n
|
|
|
166
328
|
* read side and fails fast on SDK field renames.
|
|
167
329
|
*/
|
|
168
330
|
connectStatelessTransport(transport: Parameters<typeof McpServerBase.prototype.connect>[0]): Promise<void>;
|
|
331
|
+
/**
|
|
332
|
+
* Start the HTTP server. Listens on `process.env.__PORT` (default `3000`),
|
|
333
|
+
* mounts the `/mcp` route, applies any custom Express middleware registered
|
|
334
|
+
* via {@link McpServer.use} / {@link McpServer.useOnError}, and locks in
|
|
335
|
+
* any MCP middleware registered via {@link McpServer.mcpMiddleware}.
|
|
336
|
+
*
|
|
337
|
+
* On Cloudflare Workers / workerd, returns an object exposing `fetch` so
|
|
338
|
+
* the runtime can bridge incoming requests to the Node HTTP server. On
|
|
339
|
+
* Vercel (`VERCEL === "1"`), returns the Express app directly so the
|
|
340
|
+
* serverless function entry can call it as a `(req, res)` handler. On
|
|
341
|
+
* Node, returns `undefined` once listening.
|
|
342
|
+
*/
|
|
169
343
|
run(): Promise<{
|
|
170
344
|
fetch: (...args: unknown[]) => unknown;
|
|
171
|
-
} | undefined>;
|
|
345
|
+
} | Express | undefined>;
|
|
172
346
|
private enforceOneToolPerView;
|
|
347
|
+
private resolveViewRequestContext;
|
|
173
348
|
private registerViewResources;
|
|
174
349
|
private registerViewResource;
|
|
175
350
|
private wrapHandler;
|
|
@@ -186,6 +361,35 @@ export declare class McpServer<TTools extends Record<string, ToolDef> = Record<n
|
|
|
186
361
|
file: string;
|
|
187
362
|
}>): this;
|
|
188
363
|
private readManifest;
|
|
364
|
+
/**
|
|
365
|
+
* Register a tool. Pass a `config` describing the tool (name, schemas,
|
|
366
|
+
* optional {@link ViewConfig}, optional {@link ToolMeta}) and a handler that
|
|
367
|
+
* returns the tool's result.
|
|
368
|
+
*
|
|
369
|
+
* Chain calls to build up a server: each call returns a new `McpServer`
|
|
370
|
+
* type that captures the tool's input/output/`_meta` shape so the
|
|
371
|
+
* resulting `typeof server` can drive {@link generateHelpers}.
|
|
372
|
+
*
|
|
373
|
+
* The handler's return shape determines the output types: the
|
|
374
|
+
* `structuredContent` field becomes the tool's typed output, and `_meta`
|
|
375
|
+
* becomes its `responseMetadata`. The `content` field is normalized through
|
|
376
|
+
* {@link normalizeContent}.
|
|
377
|
+
*
|
|
378
|
+
* @example
|
|
379
|
+
* ```ts
|
|
380
|
+
* server.registerTool({
|
|
381
|
+
* name: "search",
|
|
382
|
+
* inputSchema: { query: z.string() },
|
|
383
|
+
* outputSchema: { results: z.array(z.string()) },
|
|
384
|
+
* view: { component: "search" },
|
|
385
|
+
* }, async ({ query }) => ({
|
|
386
|
+
* content: `Found results for ${query}`,
|
|
387
|
+
* structuredContent: { results: [...] },
|
|
388
|
+
* }));
|
|
389
|
+
* ```
|
|
390
|
+
*
|
|
391
|
+
* @see https://docs.skybridge.tech/api-reference/register-tool
|
|
392
|
+
*/
|
|
189
393
|
registerTool<TName extends string, InputArgs extends ZodRawShapeCompat, TReturn extends {
|
|
190
394
|
content?: HandlerContent;
|
|
191
395
|
}>(config: ToolConfig<InputArgs> & {
|
package/dist/server/server.js
CHANGED
|
@@ -17,6 +17,12 @@ const mergeWithUnion = (target, source) => {
|
|
|
17
17
|
}
|
|
18
18
|
});
|
|
19
19
|
};
|
|
20
|
+
/**
|
|
21
|
+
* Coerce a tool handler's return value into an MCP `content` array. Strings
|
|
22
|
+
* become a single `TextContent`; a single block is wrapped in an array;
|
|
23
|
+
* `undefined` produces `[]`. Mostly used internally — exported so consumers
|
|
24
|
+
* who build content lazily can apply the same normalization.
|
|
25
|
+
*/
|
|
20
26
|
export function normalizeContent(content) {
|
|
21
27
|
if (content === undefined) {
|
|
22
28
|
return [];
|
|
@@ -30,6 +36,35 @@ export function normalizeContent(content) {
|
|
|
30
36
|
return [content];
|
|
31
37
|
}
|
|
32
38
|
const McpServerBaseOmitted = McpServerBase;
|
|
39
|
+
/**
|
|
40
|
+
* The Skybridge server. Extends the MCP SDK's `McpServer` with a typed tool
|
|
41
|
+
* registry, view resources, an embedded Express app, and protocol-level
|
|
42
|
+
* middleware. Construct it with the same `Implementation` info you would pass
|
|
43
|
+
* to the SDK, chain {@link McpServer.registerTool} calls to declare tools,
|
|
44
|
+
* then call {@link McpServer.run} to start the HTTP server.
|
|
45
|
+
*
|
|
46
|
+
* The `TTools` generic accumulates each registered tool's input/output/meta
|
|
47
|
+
* shape, so `typeof server` carries enough information for view-side helpers
|
|
48
|
+
* like {@link generateHelpers} to produce fully-typed hooks.
|
|
49
|
+
*
|
|
50
|
+
* @typeParam TTools - Accumulated tool registry. Filled in by `registerTool`
|
|
51
|
+
* chaining; you almost never set this manually.
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* ```ts
|
|
55
|
+
* const server = new McpServer({ name: "my-app", version: "1.0.0" }, {})
|
|
56
|
+
* .registerTool({
|
|
57
|
+
* name: "search",
|
|
58
|
+
* inputSchema: { query: z.string() },
|
|
59
|
+
* view: { component: "search" },
|
|
60
|
+
* }, async ({ query }) => ({ content: `Results for ${query}` }));
|
|
61
|
+
*
|
|
62
|
+
* await server.run();
|
|
63
|
+
* export type AppType = typeof server;
|
|
64
|
+
* ```
|
|
65
|
+
*
|
|
66
|
+
* @see https://docs.skybridge.tech/api-reference/mcp-server
|
|
67
|
+
*/
|
|
33
68
|
export class McpServer extends McpServerBaseOmitted {
|
|
34
69
|
/**
|
|
35
70
|
* The underlying Express app. Use this to extend the HTTP server with
|
|
@@ -48,6 +83,7 @@ export class McpServer extends McpServerBaseOmitted {
|
|
|
48
83
|
mcpMiddlewareEntries = [];
|
|
49
84
|
mcpMiddlewareApplied = false;
|
|
50
85
|
claimedViews = new Map();
|
|
86
|
+
viewMetaBuilders = new Map();
|
|
51
87
|
viteManifest = null;
|
|
52
88
|
serverInfo;
|
|
53
89
|
serverOptions;
|
|
@@ -109,10 +145,32 @@ export class McpServer extends McpServerBaseOmitted {
|
|
|
109
145
|
return;
|
|
110
146
|
}
|
|
111
147
|
this.mcpMiddlewareApplied = true;
|
|
148
|
+
// Surface view-resource _meta on `resources/list` (per ext-apps spec:
|
|
149
|
+
// hosts/checkers read CSP & domain at list time before fetching content).
|
|
150
|
+
const viewListMetaEntry = {
|
|
151
|
+
filter: "resources/list",
|
|
152
|
+
handler: async (_req, extra, next) => {
|
|
153
|
+
const result = (await next());
|
|
154
|
+
for (const resource of result.resources) {
|
|
155
|
+
const builder = this.viewMetaBuilders.get(resource.uri);
|
|
156
|
+
if (!builder) {
|
|
157
|
+
continue;
|
|
158
|
+
}
|
|
159
|
+
const meta = builder(extra);
|
|
160
|
+
resource._meta = {
|
|
161
|
+
...(resource._meta ?? {}),
|
|
162
|
+
...meta,
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
return result;
|
|
166
|
+
},
|
|
167
|
+
};
|
|
112
168
|
const monitoringEntry = createMiddlewareEntry();
|
|
113
|
-
const entries =
|
|
114
|
-
? [monitoringEntry
|
|
115
|
-
|
|
169
|
+
const entries = [
|
|
170
|
+
...(monitoringEntry ? [monitoringEntry] : []),
|
|
171
|
+
viewListMetaEntry,
|
|
172
|
+
...this.mcpMiddlewareEntries,
|
|
173
|
+
];
|
|
116
174
|
if (entries.length === 0) {
|
|
117
175
|
return;
|
|
118
176
|
}
|
|
@@ -127,6 +185,14 @@ export class McpServer extends McpServerBaseOmitted {
|
|
|
127
185
|
instrumentMap(requestHandlers, false);
|
|
128
186
|
instrumentMap(notificationHandlers, true);
|
|
129
187
|
}
|
|
188
|
+
/**
|
|
189
|
+
* Connect to an MCP transport (override of the SDK's `connect`). Use this
|
|
190
|
+
* when you're embedding Skybridge in a host that already manages its own
|
|
191
|
+
* transport (e.g. stdio for desktop apps); for HTTP, prefer {@link McpServer.run}
|
|
192
|
+
* which sets the transport up for you. Locks in any middleware registered
|
|
193
|
+
* via {@link McpServer.mcpMiddleware} — further calls to that method will
|
|
194
|
+
* throw afterwards.
|
|
195
|
+
*/
|
|
130
196
|
async connect(transport) {
|
|
131
197
|
this.applyMcpMiddleware();
|
|
132
198
|
return McpServerBase.prototype.connect.call(this, transport);
|
|
@@ -150,8 +216,32 @@ export class McpServer extends McpServerBaseOmitted {
|
|
|
150
216
|
target._notificationHandlers = notificationHandlers;
|
|
151
217
|
await fresh.connect(transport);
|
|
152
218
|
}
|
|
219
|
+
/**
|
|
220
|
+
* Start the HTTP server. Listens on `process.env.__PORT` (default `3000`),
|
|
221
|
+
* mounts the `/mcp` route, applies any custom Express middleware registered
|
|
222
|
+
* via {@link McpServer.use} / {@link McpServer.useOnError}, and locks in
|
|
223
|
+
* any MCP middleware registered via {@link McpServer.mcpMiddleware}.
|
|
224
|
+
*
|
|
225
|
+
* On Cloudflare Workers / workerd, returns an object exposing `fetch` so
|
|
226
|
+
* the runtime can bridge incoming requests to the Node HTTP server. On
|
|
227
|
+
* Vercel (`VERCEL === "1"`), returns the Express app directly so the
|
|
228
|
+
* serverless function entry can call it as a `(req, res)` handler. On
|
|
229
|
+
* Node, returns `undefined` once listening.
|
|
230
|
+
*/
|
|
153
231
|
async run() {
|
|
154
232
|
this.applyMcpMiddleware();
|
|
233
|
+
if (process.env.VERCEL === "1") {
|
|
234
|
+
// createApp only reads httpServer inside its dev-only branch
|
|
235
|
+
// (viewsDevServer); under VERCEL=1 + NODE_ENV=production it's a
|
|
236
|
+
// bare object passed to satisfy the required parameter.
|
|
237
|
+
const httpServer = http.createServer();
|
|
238
|
+
await createApp({
|
|
239
|
+
mcpServer: this,
|
|
240
|
+
httpServer,
|
|
241
|
+
errorMiddleware: this.customErrorMiddleware,
|
|
242
|
+
});
|
|
243
|
+
return this.express;
|
|
244
|
+
}
|
|
155
245
|
const httpServer = http.createServer();
|
|
156
246
|
await createApp({
|
|
157
247
|
mcpServer: this,
|
|
@@ -199,6 +289,57 @@ export class McpServer extends McpServerBaseOmitted {
|
|
|
199
289
|
}
|
|
200
290
|
this.claimedViews.set(component, toolName);
|
|
201
291
|
}
|
|
292
|
+
resolveViewRequestContext(extra) {
|
|
293
|
+
const isProduction = process.env.NODE_ENV === "production";
|
|
294
|
+
const headers = extra?.requestInfo?.headers || {};
|
|
295
|
+
const header = (key) => {
|
|
296
|
+
const val = headers[key];
|
|
297
|
+
return Array.isArray(val) ? val[0] : val;
|
|
298
|
+
};
|
|
299
|
+
const isClaude = header("user-agent") === "Claude-User";
|
|
300
|
+
let serverUrl;
|
|
301
|
+
const forwardedHost = header("x-forwarded-host");
|
|
302
|
+
const origin = header("origin");
|
|
303
|
+
const host = header("host");
|
|
304
|
+
if (forwardedHost) {
|
|
305
|
+
const proto = header("x-forwarded-proto") || "https";
|
|
306
|
+
serverUrl = `${proto}://${forwardedHost}`;
|
|
307
|
+
}
|
|
308
|
+
else if (origin) {
|
|
309
|
+
serverUrl = origin;
|
|
310
|
+
}
|
|
311
|
+
else if (host) {
|
|
312
|
+
const proto = ["127.0.0.1:", "localhost:"].some((p) => host.startsWith(p))
|
|
313
|
+
? "http"
|
|
314
|
+
: "https";
|
|
315
|
+
serverUrl = `${proto}://${host}`;
|
|
316
|
+
}
|
|
317
|
+
else {
|
|
318
|
+
const devPort = process.env.__PORT || "3000";
|
|
319
|
+
serverUrl = `http://localhost:${devPort}`;
|
|
320
|
+
}
|
|
321
|
+
const connectDomains = [serverUrl];
|
|
322
|
+
if (!isProduction) {
|
|
323
|
+
const wsUrl = new URL(serverUrl);
|
|
324
|
+
wsUrl.protocol = wsUrl.protocol === "https:" ? "wss:" : "ws:";
|
|
325
|
+
connectDomains.push(wsUrl.origin);
|
|
326
|
+
}
|
|
327
|
+
let contentMetaOverrides = {};
|
|
328
|
+
if (isClaude) {
|
|
329
|
+
const pathname = extra?.requestInfo?.url?.pathname ?? "";
|
|
330
|
+
const rawUrl = header("x-alpic-forwarded-url") ?? `${serverUrl}${pathname}`;
|
|
331
|
+
// Strip a lone trailing slash so the hash matches the connector URL
|
|
332
|
+
// as registered with Claude (which has no trailing slash on bare origins).
|
|
333
|
+
const url = rawUrl.endsWith("/") ? rawUrl.slice(0, -1) : rawUrl;
|
|
334
|
+
const hash = crypto
|
|
335
|
+
.createHash("sha256")
|
|
336
|
+
.update(url)
|
|
337
|
+
.digest("hex")
|
|
338
|
+
.slice(0, 32);
|
|
339
|
+
contentMetaOverrides = { domain: `${hash}.claudemcpcontent.com` };
|
|
340
|
+
}
|
|
341
|
+
return { serverUrl, connectDomains, contentMetaOverrides };
|
|
342
|
+
}
|
|
202
343
|
registerViewResources(toolName, view, toolMeta) {
|
|
203
344
|
const hosts = view.hosts ?? ["apps-sdk", "mcp-app"];
|
|
204
345
|
// Append a content-derived version param so hosts (e.g. ChatGPT) bust
|
|
@@ -303,40 +444,24 @@ export class McpServer extends McpServerBaseOmitted {
|
|
|
303
444
|
});
|
|
304
445
|
// @ts-expect-error - For backwards compatibility with Claude current implementation of the specs
|
|
305
446
|
toolMeta["ui/resourceUri"] = viewResource.uri;
|
|
306
|
-
toolMeta.ui = { resourceUri: viewResource.uri };
|
|
447
|
+
toolMeta.ui = { ...toolMeta.ui, resourceUri: viewResource.uri };
|
|
307
448
|
}
|
|
308
449
|
}
|
|
309
450
|
registerViewResource({ name, viewResource, view, }) {
|
|
310
451
|
const { hostType, uri: viewUri, mimeType, buildContentMeta } = viewResource;
|
|
452
|
+
const buildMeta = (extra) => {
|
|
453
|
+
const { serverUrl, connectDomains, contentMetaOverrides } = this.resolveViewRequestContext(extra);
|
|
454
|
+
return buildContentMeta({
|
|
455
|
+
resourceDomains: [serverUrl],
|
|
456
|
+
connectDomains,
|
|
457
|
+
domain: serverUrl,
|
|
458
|
+
baseUriDomains: [serverUrl],
|
|
459
|
+
}, contentMetaOverrides);
|
|
460
|
+
};
|
|
461
|
+
this.viewMetaBuilders.set(viewUri, buildMeta);
|
|
311
462
|
this.registerResource(name, viewUri, { description: view.description }, async (uri, extra) => {
|
|
312
463
|
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
|
-
}
|
|
464
|
+
const { serverUrl } = this.resolveViewRequestContext(extra);
|
|
340
465
|
const html = isProduction
|
|
341
466
|
? templateHelper.renderProduction({
|
|
342
467
|
hostType,
|
|
@@ -349,35 +474,9 @@ export class McpServer extends McpServerBaseOmitted {
|
|
|
349
474
|
serverUrl,
|
|
350
475
|
viewName: view.component,
|
|
351
476
|
});
|
|
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
477
|
return {
|
|
379
478
|
contents: [
|
|
380
|
-
{ uri: uri.href, mimeType, text: html, _meta:
|
|
479
|
+
{ uri: uri.href, mimeType, text: html, _meta: buildMeta(extra) },
|
|
381
480
|
],
|
|
382
481
|
};
|
|
383
482
|
});
|
|
@@ -454,8 +553,16 @@ export class McpServer extends McpServerBaseOmitted {
|
|
|
454
553
|
}
|
|
455
554
|
const config = args[0];
|
|
456
555
|
const cb = args[1];
|
|
457
|
-
const { name, view, _meta: userToolMeta, ...toolFields } = config;
|
|
556
|
+
const { name, view, securitySchemes, _meta: userToolMeta, ...toolFields } = config;
|
|
458
557
|
const toolMeta = { ...userToolMeta };
|
|
558
|
+
if (securitySchemes) {
|
|
559
|
+
// SEP-1488 puts `securitySchemes` at the top level of the tool
|
|
560
|
+
// descriptor, but the SDK's `registerTool` drops unknown top-level
|
|
561
|
+
// fields, so the canonical spot isn't reachable without intercepting
|
|
562
|
+
// `tools/list`. Use the `_meta` back-compat mirror documented in the
|
|
563
|
+
// Apps SDK reference until SEP-1488 lands in the spec.
|
|
564
|
+
toolMeta.securitySchemes = securitySchemes;
|
|
565
|
+
}
|
|
459
566
|
if (view) {
|
|
460
567
|
this.enforceOneToolPerView(view.component, name);
|
|
461
568
|
this.registerViewResources(name, view, toolMeta);
|