xmcp 0.6.6 → 0.6.8
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 +32 -0
- package/dist/cli.js +55 -36
- package/dist/compiler/config/index.d.ts +61 -54
- package/dist/compiler/config/schemas/template.d.ts +5 -0
- package/dist/compiler/config/schemas/transport/http.d.ts +82 -82
- package/dist/host-bridge.d.ts +4 -0
- package/dist/host-bridge.js +1 -0
- package/dist/index.d.ts +8 -2
- package/dist/index.js +5 -5
- package/dist/runtime/adapter-express.js +45 -26
- package/dist/runtime/adapter-nestjs.js +45 -26
- package/dist/runtime/adapter-nextjs.js +44 -25
- package/dist/runtime/contexts/client-info-context.d.ts +8 -0
- package/dist/runtime/contexts/http-request-context.d.ts +3 -1
- package/dist/runtime/headers.js +1 -1
- package/dist/runtime/http.js +53 -34
- package/dist/runtime/stdio.js +32 -24
- package/dist/runtime/utils/elicitation.d.ts +4 -0
- package/dist/runtime/utils/ext-apps-registry.d.ts +132 -0
- package/dist/runtime/utils/mcp-app-protocol.d.ts +14 -0
- package/dist/runtime/utils/mcp-app-types.d.ts +103 -0
- package/dist/runtime/utils/mcp-host-bridge.d.ts +43 -0
- package/dist/runtime/utils/path-to-tool-name.d.ts +20 -0
- package/dist/runtime/utils/prompt-loader.d.ts +11 -0
- package/dist/runtime/utils/prompts.d.ts +8 -0
- package/dist/runtime/utils/react/generate-html.d.ts +1 -0
- package/dist/runtime/utils/react/index.d.ts +4 -0
- package/dist/runtime/utils/resource-loader.d.ts +11 -0
- package/dist/runtime/utils/resources.d.ts +4 -0
- package/dist/runtime/utils/server.d.ts +37 -0
- package/dist/runtime/utils/tool-loader.d.ts +11 -0
- package/dist/runtime/utils/tools.d.ts +11 -0
- package/dist/runtime/utils/transformers/prompt.d.ts +32 -0
- package/dist/runtime/utils/transformers/resource.d.ts +26 -0
- package/dist/runtime/utils/transformers/tool.d.ts +30 -0
- package/dist/runtime/utils/ui/flatten-meta.d.ts +6 -0
- package/dist/runtime/utils/ui/split-meta.d.ts +17 -0
- package/dist/runtime/utils/utils/resource-uri-composer.d.ts +60 -0
- package/dist/runtime/utils/validators/index.d.ts +13 -0
- package/dist/types/client-info.d.ts +12 -0
- package/dist/types/tool.d.ts +69 -14
- package/dist/types/ui-meta.d.ts +15 -0
- package/dist/utils/context.d.ts +1 -1
- package/package.json +20 -1
- package/src/client/index.ts +4 -1
- package/src/compiler/client/component-compiler.ts +26 -5
- package/src/compiler/config/injection.ts +1 -0
- package/src/compiler/config/schemas/template.ts +13 -0
- package/src/compiler/generate-tools-code.ts +4 -6
- package/src/compiler/get-bundler-config/index.ts +1 -0
- package/src/compiler/get-bundler-config/plugins/index.ts +0 -1
- package/src/compiler/index.ts +24 -7
- package/src/host-bridge.ts +36 -0
- package/src/index.ts +44 -1
- package/src/runtime/adapters/express/index.ts +45 -47
- package/src/runtime/adapters/nestjs/xmcp.service.ts +4 -3
- package/src/runtime/adapters/nextjs/handler/node-to-web-adapter.ts +40 -6
- package/src/runtime/adapters/nextjs/handler/request-converter.ts +20 -0
- package/src/runtime/adapters/nextjs/handler/server-lifecycle.ts +10 -6
- package/src/runtime/adapters/nextjs/index.ts +41 -25
- package/src/runtime/contexts/client-info-context.ts +16 -0
- package/src/runtime/contexts/http-request-context.ts +2 -0
- package/src/runtime/headers.ts +37 -2
- package/src/runtime/platforms/cloudflare/cors.ts +0 -1
- package/src/runtime/platforms/cloudflare/worker.ts +46 -44
- package/src/runtime/transports/http/index.ts +0 -4
- package/src/runtime/transports/http/stateless-streamable-http.ts +131 -233
- package/src/runtime/transports/stdio/index.ts +40 -9
- package/src/runtime/utils/__tests__/client-info.test.ts +99 -0
- package/src/runtime/utils/__tests__/tool-extra.test.ts +149 -0
- package/src/runtime/utils/client-info.ts +78 -0
- package/src/runtime/utils/elicitation.ts +396 -0
- package/src/runtime/utils/mcp-app-protocol.ts +16 -0
- package/src/runtime/utils/mcp-app-types.ts +113 -0
- package/src/runtime/utils/mcp-host-bridge.ts +473 -0
- package/src/runtime/utils/prompt-loader.ts +151 -0
- package/src/runtime/utils/react/generate-html.ts +12 -4
- package/src/runtime/utils/resource-loader.ts +151 -0
- package/src/runtime/utils/resources.ts +0 -8
- package/src/runtime/utils/server.ts +31 -40
- package/src/runtime/utils/tool-loader.ts +149 -0
- package/src/runtime/utils/tools.ts +9 -6
- package/src/runtime/utils/transformers/tool.ts +35 -5
- package/src/runtime/utils/ui/__tests__/split-meta.test.ts +42 -0
- package/src/runtime/utils/ui/split-meta.ts +4 -1
- package/src/types/client-info.ts +12 -0
- package/src/types/injected.d.ts +62 -0
- package/src/types/tool.ts +99 -17
- package/src/types/ui-meta.ts +17 -1
- package/src/utils/context.ts +2 -2
package/dist/types/tool.d.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { z } from "zod/v3";
|
|
2
2
|
import type { ZodType as ZodTypeV4, infer as inferV4 } from "zod";
|
|
3
|
+
import type { ElicitResult as McpElicitResult } from "@modelcontextprotocol/sdk/types";
|
|
3
4
|
import { UIMetadata } from "./ui-meta";
|
|
5
|
+
import type { McpClientInfo } from "./client-info";
|
|
4
6
|
export interface ToolAnnotations {
|
|
5
7
|
/** Human-readable title for the tool */
|
|
6
8
|
title?: string;
|
|
@@ -28,8 +30,70 @@ export interface ToolMetadata {
|
|
|
28
30
|
};
|
|
29
31
|
}
|
|
30
32
|
type CompatibleZodType = z.ZodTypeAny | ZodTypeV4<unknown>;
|
|
33
|
+
type InferCompatibleZodType<T extends CompatibleZodType> = T extends z.ZodTypeAny ? z.infer<T> : T extends ZodTypeV4<unknown> ? inferV4<T> : never;
|
|
31
34
|
export type ToolSchema = Record<string, CompatibleZodType>;
|
|
32
35
|
export type ToolOutputSchema = Record<string, CompatibleZodType>;
|
|
36
|
+
export type ElicitResult = McpElicitResult;
|
|
37
|
+
export interface ToolRequestOptions {
|
|
38
|
+
/** Progress notification callback */
|
|
39
|
+
onprogress?: (progress: any) => void;
|
|
40
|
+
/** Abort signal for cancelling the request */
|
|
41
|
+
signal?: AbortSignal;
|
|
42
|
+
/** Request timeout in milliseconds */
|
|
43
|
+
timeout?: number;
|
|
44
|
+
/** Whether receiving progress notifications resets the timeout */
|
|
45
|
+
resetTimeoutOnProgress?: boolean;
|
|
46
|
+
/** Maximum total time to wait for a response */
|
|
47
|
+
maxTotalTimeout?: number;
|
|
48
|
+
/** Additional transport-specific options */
|
|
49
|
+
[key: string]: unknown;
|
|
50
|
+
}
|
|
51
|
+
type ElicitStringFormat = "date" | "uri" | "email" | "date-time";
|
|
52
|
+
interface ElicitFieldBase {
|
|
53
|
+
title?: string;
|
|
54
|
+
description?: string;
|
|
55
|
+
}
|
|
56
|
+
export interface ElicitStringField extends ElicitFieldBase {
|
|
57
|
+
type: "string";
|
|
58
|
+
minLength?: number;
|
|
59
|
+
maxLength?: number;
|
|
60
|
+
format?: ElicitStringFormat;
|
|
61
|
+
default?: string;
|
|
62
|
+
}
|
|
63
|
+
export interface ElicitEnumField extends ElicitFieldBase {
|
|
64
|
+
type: "string";
|
|
65
|
+
enum: string[];
|
|
66
|
+
enumNames?: string[];
|
|
67
|
+
default?: string;
|
|
68
|
+
}
|
|
69
|
+
export interface ElicitBooleanField extends ElicitFieldBase {
|
|
70
|
+
type: "boolean";
|
|
71
|
+
default?: boolean;
|
|
72
|
+
}
|
|
73
|
+
export interface ElicitNumberField extends ElicitFieldBase {
|
|
74
|
+
type: "number" | "integer";
|
|
75
|
+
minimum?: number;
|
|
76
|
+
maximum?: number;
|
|
77
|
+
default?: number;
|
|
78
|
+
}
|
|
79
|
+
export type ElicitFormField = ElicitStringField | ElicitEnumField | ElicitBooleanField | ElicitNumberField;
|
|
80
|
+
export interface ElicitFormSchema {
|
|
81
|
+
type: "object";
|
|
82
|
+
properties: Record<string, ElicitFormField>;
|
|
83
|
+
required?: string[];
|
|
84
|
+
}
|
|
85
|
+
export interface ElicitFormRequest {
|
|
86
|
+
mode?: "form";
|
|
87
|
+
message: string;
|
|
88
|
+
requestedSchema: ElicitFormSchema;
|
|
89
|
+
}
|
|
90
|
+
export interface ElicitUrlRequest {
|
|
91
|
+
mode: "url";
|
|
92
|
+
message: string;
|
|
93
|
+
url: string;
|
|
94
|
+
elicitationId: string;
|
|
95
|
+
}
|
|
96
|
+
export type ElicitRequest = ElicitFormRequest | ElicitUrlRequest;
|
|
33
97
|
/**
|
|
34
98
|
* Extra arguments passed to MCP tool functions.
|
|
35
99
|
*/
|
|
@@ -65,23 +129,14 @@ export interface ToolExtraArguments {
|
|
|
65
129
|
/** The headers of the request */
|
|
66
130
|
headers: Record<string, string | string[] | undefined>;
|
|
67
131
|
};
|
|
132
|
+
/** MCP client metadata from initialize.params.clientInfo, when available */
|
|
133
|
+
clientInfo?: McpClientInfo;
|
|
68
134
|
/** Sends a notification that relates to the current request being handled */
|
|
69
135
|
sendNotification: (notification: any) => Promise<void>;
|
|
70
136
|
/** Sends a request that relates to the current request being handled */
|
|
71
|
-
sendRequest: <U extends
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
/** Abort signal for cancelling the request */
|
|
75
|
-
signal?: AbortSignal;
|
|
76
|
-
/** Request timeout in milliseconds */
|
|
77
|
-
timeout?: number;
|
|
78
|
-
/** Whether receiving progress notifications resets the timeout */
|
|
79
|
-
resetTimeoutOnProgress?: boolean;
|
|
80
|
-
/** Maximum total time to wait for a response */
|
|
81
|
-
maxTotalTimeout?: number;
|
|
82
|
-
/** Additional transport-specific options */
|
|
83
|
-
[key: string]: unknown;
|
|
84
|
-
}) => Promise<z.infer<U>>;
|
|
137
|
+
sendRequest: <U extends CompatibleZodType>(request: any, resultSchema: U, options?: ToolRequestOptions) => Promise<InferCompatibleZodType<U>>;
|
|
138
|
+
/** Requests user input from the connected client */
|
|
139
|
+
elicit: (request: ElicitRequest, options?: ToolRequestOptions) => Promise<ElicitResult>;
|
|
85
140
|
}
|
|
86
141
|
export type InferSchema<T extends Record<string, unknown>> = {
|
|
87
142
|
[K in keyof T]: T[K] extends z.ZodTypeAny ? z.infer<T[K]> : T[K] extends ZodTypeV4<unknown> ? inferV4<T[K]> : never;
|
package/dist/types/ui-meta.d.ts
CHANGED
|
@@ -4,12 +4,27 @@ export interface UIMetadata {
|
|
|
4
4
|
* Auto-generated as `ui://app/{toolName}.html` when omitted.
|
|
5
5
|
*/
|
|
6
6
|
resourceUri?: string;
|
|
7
|
+
/**
|
|
8
|
+
* Visibility hint for how prominently hosts should surface the widget entry point.
|
|
9
|
+
*/
|
|
10
|
+
visibility?: Array<"model" | "app">;
|
|
7
11
|
/**
|
|
8
12
|
* Content Security Policy configuration for widget resources.
|
|
9
13
|
*/
|
|
10
14
|
csp?: {
|
|
11
15
|
connectDomains?: string[];
|
|
12
16
|
resourceDomains?: string[];
|
|
17
|
+
frameDomains?: string[];
|
|
18
|
+
baseUriDomains?: string[];
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* Browser permission hints requested by the widget iframe.
|
|
22
|
+
*/
|
|
23
|
+
permissions?: {
|
|
24
|
+
camera?: Record<string, never>;
|
|
25
|
+
microphone?: Record<string, never>;
|
|
26
|
+
geolocation?: Record<string, never>;
|
|
27
|
+
clipboardWrite?: Record<string, never>;
|
|
13
28
|
};
|
|
14
29
|
/**
|
|
15
30
|
* Optional dedicated origin for this widget.
|
package/dist/utils/context.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ type GetContext<T extends DefaultContext> = () => T;
|
|
|
3
3
|
type SetContext<T extends DefaultContext> = (data: Partial<T>) => void;
|
|
4
4
|
export interface Context<T extends DefaultContext> {
|
|
5
5
|
/** Run a callback with a specific context. */
|
|
6
|
-
provider: (initialValue: T, callback: () =>
|
|
6
|
+
provider: <R>(initialValue: T, callback: () => R) => R;
|
|
7
7
|
/** Get the context. */
|
|
8
8
|
getContext: GetContext<T>;
|
|
9
9
|
/** Partially update the context. */
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "xmcp",
|
|
3
3
|
"description": "The MCP framework",
|
|
4
|
-
"version": "0.6.
|
|
4
|
+
"version": "0.6.8",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "xmcp",
|
|
7
7
|
"email": "support@xmcp.dev",
|
|
@@ -40,11 +40,24 @@
|
|
|
40
40
|
"exports": {
|
|
41
41
|
".": {
|
|
42
42
|
"types": "./dist/index.d.ts",
|
|
43
|
+
"import": "./dist/index.js",
|
|
43
44
|
"require": "./dist/index.js",
|
|
44
45
|
"default": "./dist/index.js"
|
|
45
46
|
},
|
|
47
|
+
"./host-bridge": {
|
|
48
|
+
"types": "./dist/host-bridge.d.ts",
|
|
49
|
+
"import": "./dist/host-bridge.js",
|
|
50
|
+
"require": "./dist/host-bridge.js",
|
|
51
|
+
"default": "./dist/host-bridge.js"
|
|
52
|
+
},
|
|
53
|
+
"./headers": {
|
|
54
|
+
"types": "./dist/runtime/headers.d.ts",
|
|
55
|
+
"require": "./dist/runtime/headers.js",
|
|
56
|
+
"default": "./dist/runtime/headers.js"
|
|
57
|
+
},
|
|
46
58
|
"./cloudflare": {
|
|
47
59
|
"types": "./dist/cloudflare.d.ts",
|
|
60
|
+
"import": "./dist/cloudflare.js",
|
|
48
61
|
"require": "./dist/cloudflare.js",
|
|
49
62
|
"default": "./dist/cloudflare.js"
|
|
50
63
|
},
|
|
@@ -52,6 +65,12 @@
|
|
|
52
65
|
},
|
|
53
66
|
"typesVersions": {
|
|
54
67
|
"*": {
|
|
68
|
+
"host-bridge": [
|
|
69
|
+
"dist/host-bridge.d.ts"
|
|
70
|
+
],
|
|
71
|
+
"headers": [
|
|
72
|
+
"dist/runtime/headers.d.ts"
|
|
73
|
+
],
|
|
55
74
|
"cloudflare": [
|
|
56
75
|
"dist/cloudflare.d.ts"
|
|
57
76
|
]
|
package/src/client/index.ts
CHANGED
|
@@ -125,12 +125,33 @@ export class ClientComponentCompiler {
|
|
|
125
125
|
root.render(React.createElement(Component, props));
|
|
126
126
|
}
|
|
127
127
|
|
|
128
|
+
render({});
|
|
129
|
+
|
|
128
130
|
window.addEventListener("message", (event) => {
|
|
129
131
|
if (event.source !== window.parent && event.source !== null) return;
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
132
|
+
const method = event.data?.method;
|
|
133
|
+
const isInputNotification =
|
|
134
|
+
method === "ui/notifications/tool-input" ||
|
|
135
|
+
method === "tool-input" ||
|
|
136
|
+
method === "mcp-apps:tool-input" ||
|
|
137
|
+
method === "ui/notifications/tool-input-partial";
|
|
138
|
+
|
|
139
|
+
const isResultNotification = method === "ui/notifications/tool-result";
|
|
140
|
+
|
|
141
|
+
if (!isInputNotification && !isResultNotification) return;
|
|
142
|
+
|
|
143
|
+
const args = isResultNotification
|
|
144
|
+
? event.data?.params?.structuredContent?.args ??
|
|
145
|
+
event.data?.params?.arguments ??
|
|
146
|
+
event.data?.params?.input ??
|
|
147
|
+
event.data?.arguments ??
|
|
148
|
+
{}
|
|
149
|
+
: event.data?.params?.arguments ??
|
|
150
|
+
event.data?.params?.input ??
|
|
151
|
+
event.data?.arguments ??
|
|
152
|
+
{};
|
|
153
|
+
|
|
154
|
+
render(args);
|
|
134
155
|
});
|
|
135
156
|
`;
|
|
136
157
|
}
|
|
@@ -221,7 +242,7 @@ export class ClientComponentCompiler {
|
|
|
221
242
|
concatenateModules: true,
|
|
222
243
|
moduleIds: "deterministic",
|
|
223
244
|
},
|
|
224
|
-
cache:
|
|
245
|
+
cache: false,
|
|
225
246
|
};
|
|
226
247
|
}
|
|
227
248
|
}
|
|
@@ -29,6 +29,19 @@ const templateConfigBaseSchema = z.object({
|
|
|
29
29
|
})
|
|
30
30
|
)
|
|
31
31
|
.optional(),
|
|
32
|
+
/**
|
|
33
|
+
* Instructions describing how to use the server and its features.
|
|
34
|
+
*
|
|
35
|
+
* This is sent in the MCP `initialize` response and can be used by clients
|
|
36
|
+
* to improve the LLM's understanding of available tools, resources, etc.
|
|
37
|
+
*
|
|
38
|
+
* Instructions should focus on cross-tool relationships, workflow patterns,
|
|
39
|
+
* and constraints, but should not duplicate information already in tool descriptions.
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* instructions: "Use tool-a before tool-b. Always check resources first."
|
|
43
|
+
*/
|
|
44
|
+
instructions: z.string().optional(),
|
|
32
45
|
/**
|
|
33
46
|
* Custom home page content for the `/` endpoint.
|
|
34
47
|
*
|
|
@@ -33,7 +33,7 @@ export function generateToolsExportCode(): string {
|
|
|
33
33
|
|
|
34
34
|
${importStatements}
|
|
35
35
|
|
|
36
|
-
/**
|
|
36
|
+
/**
|
|
37
37
|
* Runtime-accessible tools function that works from any context.
|
|
38
38
|
* Generated at build time - always up to date with discovered tools.
|
|
39
39
|
* @returns {Promise<ToolRegistry>}
|
|
@@ -65,7 +65,7 @@ export async function getTools() {
|
|
|
65
65
|
if (!("parse" in val) || typeof val.parse !== "function") return false;
|
|
66
66
|
return true;
|
|
67
67
|
});
|
|
68
|
-
|
|
68
|
+
|
|
69
69
|
if (isValidSchema) {
|
|
70
70
|
toolSchema = schema;
|
|
71
71
|
} else {
|
|
@@ -89,10 +89,8 @@ export async function getTools() {
|
|
|
89
89
|
}
|
|
90
90
|
}
|
|
91
91
|
|
|
92
|
-
// Make sure tools has annotations with a title
|
|
93
|
-
|
|
94
|
-
toolConfig.annotations = {};
|
|
95
|
-
}
|
|
92
|
+
// Make sure tools has annotations with a title — shallow copy to avoid mutating shared objects
|
|
93
|
+
toolConfig.annotations = { ...(toolConfig.annotations ?? {}) };
|
|
96
94
|
if (toolConfig.annotations.title === undefined) {
|
|
97
95
|
toolConfig.annotations.title = toolConfig.name;
|
|
98
96
|
}
|
package/src/compiler/index.ts
CHANGED
|
@@ -130,13 +130,18 @@ export async function compile({ onBuild }: CompileOptions = {}) {
|
|
|
130
130
|
onAdd: async (filePath) => {
|
|
131
131
|
addWatchedPath(promptPaths, filePath);
|
|
132
132
|
if (compilerStarted) {
|
|
133
|
-
await generateCode();
|
|
133
|
+
await generateCode({ rebuildClientBundles: false });
|
|
134
|
+
}
|
|
135
|
+
},
|
|
136
|
+
onChange: async () => {
|
|
137
|
+
if (compilerStarted) {
|
|
138
|
+
await generateCode({ rebuildClientBundles: false });
|
|
134
139
|
}
|
|
135
140
|
},
|
|
136
141
|
onUnlink: async (filePath) => {
|
|
137
142
|
removeWatchedPath(promptPaths, filePath);
|
|
138
143
|
if (compilerStarted) {
|
|
139
|
-
await generateCode();
|
|
144
|
+
await generateCode({ rebuildClientBundles: false });
|
|
140
145
|
}
|
|
141
146
|
},
|
|
142
147
|
});
|
|
@@ -154,13 +159,18 @@ export async function compile({ onBuild }: CompileOptions = {}) {
|
|
|
154
159
|
onAdd: async (filePath) => {
|
|
155
160
|
addWatchedPath(resourcePaths, filePath);
|
|
156
161
|
if (compilerStarted) {
|
|
157
|
-
await generateCode();
|
|
162
|
+
await generateCode({ rebuildClientBundles: false });
|
|
163
|
+
}
|
|
164
|
+
},
|
|
165
|
+
onChange: async () => {
|
|
166
|
+
if (compilerStarted) {
|
|
167
|
+
await generateCode({ rebuildClientBundles: false });
|
|
158
168
|
}
|
|
159
169
|
},
|
|
160
170
|
onUnlink: async (filePath) => {
|
|
161
171
|
removeWatchedPath(resourcePaths, filePath);
|
|
162
172
|
if (compilerStarted) {
|
|
163
|
-
await generateCode();
|
|
173
|
+
await generateCode({ rebuildClientBundles: false });
|
|
164
174
|
}
|
|
165
175
|
},
|
|
166
176
|
});
|
|
@@ -430,9 +440,16 @@ async function buildClientBundles(): Promise<Map<string, string> | undefined> {
|
|
|
430
440
|
* Generates all runtime code and builds client bundles if needed
|
|
431
441
|
* This centralizes all code generation logic including client bundle building
|
|
432
442
|
*/
|
|
433
|
-
async function generateCode(
|
|
434
|
-
|
|
435
|
-
|
|
443
|
+
async function generateCode({
|
|
444
|
+
rebuildClientBundles = true,
|
|
445
|
+
}: {
|
|
446
|
+
rebuildClientBundles?: boolean;
|
|
447
|
+
} = {}) {
|
|
448
|
+
const { clientBundles: currentClientBundles } = compilerContext.getContext();
|
|
449
|
+
const clientBundles =
|
|
450
|
+
rebuildClientBundles || currentClientBundles === undefined
|
|
451
|
+
? await buildClientBundles()
|
|
452
|
+
: currentClientBundles;
|
|
436
453
|
|
|
437
454
|
// Store in context for import map generation
|
|
438
455
|
compilerContext.setContext({ clientBundles });
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
export { createMcpHostBridge } from "./runtime/utils/mcp-host-bridge";
|
|
2
|
+
export type {
|
|
3
|
+
McpHostBridge,
|
|
4
|
+
McpHostBridgeState,
|
|
5
|
+
CreateMcpHostBridgeOptions,
|
|
6
|
+
} from "./runtime/utils/mcp-host-bridge";
|
|
7
|
+
export type {
|
|
8
|
+
McpHostCallToolParams,
|
|
9
|
+
McpHostContext,
|
|
10
|
+
McpHostCapabilities,
|
|
11
|
+
McpHostContainerDimensions,
|
|
12
|
+
McpHostOpenLinkParams,
|
|
13
|
+
McpHostReadResourceResult,
|
|
14
|
+
McpHostRequestDisplayModeParams,
|
|
15
|
+
McpHostRequestDisplayModeResult,
|
|
16
|
+
McpHostSendMessageParams,
|
|
17
|
+
McpHostMessageResult,
|
|
18
|
+
McpHostModelContextResult,
|
|
19
|
+
McpHostToolContentItem,
|
|
20
|
+
McpHostToolResult,
|
|
21
|
+
McpUiDisplayMode,
|
|
22
|
+
McpHostUpdateModelContextParams,
|
|
23
|
+
} from "./runtime/utils/mcp-app-types";
|
|
24
|
+
export {
|
|
25
|
+
MCP_APPS_PROTOCOL_VERSION,
|
|
26
|
+
INITIALIZE_METHOD,
|
|
27
|
+
INITIALIZED_METHOD,
|
|
28
|
+
REQUEST_DISPLAY_MODE_METHOD,
|
|
29
|
+
OPEN_LINK_METHOD,
|
|
30
|
+
UPDATE_MODEL_CONTEXT_METHOD,
|
|
31
|
+
UI_MESSAGE_METHOD,
|
|
32
|
+
LOG_MESSAGE_METHOD,
|
|
33
|
+
RESOURCES_READ_METHOD,
|
|
34
|
+
HOST_CONTEXT_CHANGED_METHOD,
|
|
35
|
+
SIZE_CHANGED_METHOD,
|
|
36
|
+
} from "./runtime/utils/mcp-app-protocol";
|
package/src/index.ts
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
import dotenv from "dotenv";
|
|
2
|
-
export type {
|
|
2
|
+
export type {
|
|
3
|
+
Middleware,
|
|
4
|
+
WebMiddleware,
|
|
5
|
+
WebMiddlewareContext,
|
|
6
|
+
} from "./types/middleware";
|
|
3
7
|
dotenv.config();
|
|
4
8
|
|
|
5
9
|
export type {
|
|
@@ -8,7 +12,9 @@ export type {
|
|
|
8
12
|
ToolOutputSchema,
|
|
9
13
|
ToolExtraArguments,
|
|
10
14
|
InferSchema,
|
|
15
|
+
ElicitResult,
|
|
11
16
|
} from "./types/tool";
|
|
17
|
+
export type { McpClientInfo } from "./types/client-info";
|
|
12
18
|
export type { PromptMetadata } from "./types/prompt";
|
|
13
19
|
export type { ResourceMetadata } from "./types/resource";
|
|
14
20
|
export type { UIMetadata } from "./types/ui-meta";
|
|
@@ -21,6 +27,7 @@ export { jwtAuthMiddleware } from "./auth/jwt";
|
|
|
21
27
|
export { createContext } from "./utils/context";
|
|
22
28
|
|
|
23
29
|
export { completable } from "@modelcontextprotocol/sdk/server/completable";
|
|
30
|
+
export { UrlElicitationRequiredError } from "@modelcontextprotocol/sdk/types";
|
|
24
31
|
|
|
25
32
|
export {
|
|
26
33
|
createHTTPClient,
|
|
@@ -49,3 +56,39 @@ export type {
|
|
|
49
56
|
export { isEnvHeader, headersToRecord } from "./client/headers";
|
|
50
57
|
|
|
51
58
|
export { extractToolNamesFromRequest } from "./runtime/utils/request-tool-names";
|
|
59
|
+
export { createMcpHostBridge } from "./runtime/utils/mcp-host-bridge";
|
|
60
|
+
export type {
|
|
61
|
+
McpHostBridge,
|
|
62
|
+
McpHostBridgeState,
|
|
63
|
+
CreateMcpHostBridgeOptions,
|
|
64
|
+
} from "./runtime/utils/mcp-host-bridge";
|
|
65
|
+
export type {
|
|
66
|
+
McpHostCallToolParams,
|
|
67
|
+
McpHostContext,
|
|
68
|
+
McpHostCapabilities,
|
|
69
|
+
McpHostContainerDimensions,
|
|
70
|
+
McpHostOpenLinkParams,
|
|
71
|
+
McpHostReadResourceResult,
|
|
72
|
+
McpHostRequestDisplayModeParams,
|
|
73
|
+
McpHostRequestDisplayModeResult,
|
|
74
|
+
McpHostSendMessageParams,
|
|
75
|
+
McpHostMessageResult,
|
|
76
|
+
McpHostModelContextResult,
|
|
77
|
+
McpHostToolContentItem,
|
|
78
|
+
McpHostToolResult,
|
|
79
|
+
McpUiDisplayMode,
|
|
80
|
+
McpHostUpdateModelContextParams,
|
|
81
|
+
} from "./runtime/utils/mcp-app-types";
|
|
82
|
+
export {
|
|
83
|
+
MCP_APPS_PROTOCOL_VERSION,
|
|
84
|
+
INITIALIZE_METHOD,
|
|
85
|
+
INITIALIZED_METHOD,
|
|
86
|
+
REQUEST_DISPLAY_MODE_METHOD,
|
|
87
|
+
OPEN_LINK_METHOD,
|
|
88
|
+
UPDATE_MODEL_CONTEXT_METHOD,
|
|
89
|
+
UI_MESSAGE_METHOD,
|
|
90
|
+
LOG_MESSAGE_METHOD,
|
|
91
|
+
RESOURCES_READ_METHOD,
|
|
92
|
+
HOST_CONTEXT_CHANGED_METHOD,
|
|
93
|
+
SIZE_CHANGED_METHOD,
|
|
94
|
+
} from "./runtime/utils/mcp-app-protocol";
|
|
@@ -4,74 +4,72 @@ import { StatelessHttpServerTransport } from "@/runtime/transports/http/stateles
|
|
|
4
4
|
import { setHeaders } from "@/runtime/transports/http/cors";
|
|
5
5
|
import { httpRequestContextProvider } from "@/runtime/contexts/http-request-context";
|
|
6
6
|
import { randomUUID } from "node:crypto";
|
|
7
|
+
import { extractClientInfoFromMessages } from "@/runtime/utils/client-info";
|
|
7
8
|
|
|
8
9
|
// cors config
|
|
9
|
-
// @ts-expect-error: injected by compiler
|
|
10
10
|
const corsOrigin = HTTP_CORS_ORIGIN as string;
|
|
11
|
-
// @ts-expect-error: injected by compiler
|
|
12
11
|
const corsMethods = HTTP_CORS_METHODS as string;
|
|
13
|
-
// @ts-expect-error: injected by compiler
|
|
14
12
|
const corsAllowedHeaders = HTTP_CORS_ALLOWED_HEADERS as string;
|
|
15
|
-
// @ts-expect-error: injected by compiler
|
|
16
13
|
const corsExposedHeaders = HTTP_CORS_EXPOSED_HEADERS as string;
|
|
17
|
-
// @ts-expect-error: injected by compiler
|
|
18
14
|
const corsCredentials = HTTP_CORS_CREDENTIALS as boolean;
|
|
19
|
-
// @ts-expect-error: injected by compiler
|
|
20
15
|
const corsMaxAge = HTTP_CORS_MAX_AGE as number;
|
|
21
16
|
|
|
22
|
-
// @ts-expect-error: injected by compiler
|
|
23
17
|
const debug = HTTP_DEBUG as boolean;
|
|
24
|
-
// @ts-expect-error: injected by compiler
|
|
25
18
|
const bodySizeLimit = HTTP_BODY_SIZE_LIMIT as string;
|
|
26
19
|
|
|
27
20
|
export async function xmcpHandler(req: Request, res: Response) {
|
|
28
21
|
return new Promise((resolve) => {
|
|
29
22
|
const id = randomUUID();
|
|
30
|
-
|
|
31
|
-
try {
|
|
32
|
-
setHeaders(
|
|
33
|
-
res,
|
|
34
|
-
{
|
|
35
|
-
origin: corsOrigin,
|
|
36
|
-
methods: corsMethods,
|
|
37
|
-
allowedHeaders: corsAllowedHeaders,
|
|
38
|
-
exposedHeaders: corsExposedHeaders,
|
|
39
|
-
credentials: corsCredentials,
|
|
40
|
-
maxAge: corsMaxAge,
|
|
41
|
-
},
|
|
42
|
-
req.headers.origin
|
|
43
|
-
);
|
|
23
|
+
const clientInfo = extractClientInfoFromMessages(req.body);
|
|
44
24
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
25
|
+
httpRequestContextProvider(
|
|
26
|
+
{ id, headers: req.headers, clientInfo },
|
|
27
|
+
async () => {
|
|
28
|
+
try {
|
|
29
|
+
setHeaders(
|
|
30
|
+
res,
|
|
31
|
+
{
|
|
32
|
+
origin: corsOrigin,
|
|
33
|
+
methods: corsMethods,
|
|
34
|
+
allowedHeaders: corsAllowedHeaders,
|
|
35
|
+
exposedHeaders: corsExposedHeaders,
|
|
36
|
+
credentials: corsCredentials,
|
|
37
|
+
maxAge: corsMaxAge,
|
|
38
|
+
},
|
|
39
|
+
req.headers.origin
|
|
40
|
+
);
|
|
41
|
+
|
|
42
|
+
const server = await createServer();
|
|
43
|
+
const transport = new StatelessHttpServerTransport(
|
|
44
|
+
debug,
|
|
45
|
+
bodySizeLimit || "10mb"
|
|
46
|
+
);
|
|
50
47
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
48
|
+
// cleanup when request/connection closes
|
|
49
|
+
res.on("close", () => {
|
|
50
|
+
transport.close();
|
|
51
|
+
server.close();
|
|
52
|
+
});
|
|
56
53
|
|
|
57
|
-
|
|
54
|
+
await server.connect(transport);
|
|
58
55
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
});
|
|
62
|
-
} catch (error) {
|
|
63
|
-
console.error("[HTTP-server] Error handling MCP request:", error);
|
|
64
|
-
if (!res.headersSent) {
|
|
65
|
-
res.status(500).json({
|
|
66
|
-
jsonrpc: "2.0",
|
|
67
|
-
error: {
|
|
68
|
-
code: -32603,
|
|
69
|
-
message: "Internal server error",
|
|
70
|
-
},
|
|
71
|
-
id: null,
|
|
56
|
+
await transport.handleRequest(req, res, req.body).then(() => {
|
|
57
|
+
resolve(res);
|
|
72
58
|
});
|
|
59
|
+
} catch (error) {
|
|
60
|
+
console.error("[HTTP-server] Error handling MCP request:", error);
|
|
61
|
+
if (!res.headersSent) {
|
|
62
|
+
res.status(500).json({
|
|
63
|
+
jsonrpc: "2.0",
|
|
64
|
+
error: {
|
|
65
|
+
code: -32603,
|
|
66
|
+
message: "Internal server error",
|
|
67
|
+
},
|
|
68
|
+
id: null,
|
|
69
|
+
});
|
|
70
|
+
}
|
|
73
71
|
}
|
|
74
72
|
}
|
|
75
|
-
|
|
73
|
+
);
|
|
76
74
|
});
|
|
77
75
|
}
|
|
@@ -11,11 +11,10 @@ import { setHeaders } from "@/runtime/transports/http/cors";
|
|
|
11
11
|
import { httpRequestContextProvider } from "@/runtime/contexts/http-request-context";
|
|
12
12
|
import { randomUUID } from "node:crypto";
|
|
13
13
|
import type { CorsConfig } from "@/compiler/config";
|
|
14
|
+
import { extractClientInfoFromMessages } from "@/runtime/utils/client-info";
|
|
14
15
|
|
|
15
|
-
// @ts-expect-error: injected by compiler
|
|
16
16
|
const corsConfig = HTTP_CORS_CONFIG as CorsConfig;
|
|
17
17
|
|
|
18
|
-
// @ts-expect-error: injected by compiler
|
|
19
18
|
const httpConfig = HTTP_CONFIG as {
|
|
20
19
|
port: number;
|
|
21
20
|
host: string;
|
|
@@ -43,8 +42,10 @@ export class XmcpService implements OnModuleInit, OnModuleDestroy {
|
|
|
43
42
|
this.logger.debug(`Request ${requestId} started`);
|
|
44
43
|
|
|
45
44
|
return new Promise((resolve) => {
|
|
45
|
+
const clientInfo = extractClientInfoFromMessages(req.body);
|
|
46
|
+
|
|
46
47
|
httpRequestContextProvider(
|
|
47
|
-
{ id: requestId, headers: req.headers },
|
|
48
|
+
{ id: requestId, headers: req.headers, clientInfo },
|
|
48
49
|
async () => {
|
|
49
50
|
try {
|
|
50
51
|
setHeaders(res, corsConfig, req.headers.origin);
|