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
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import { test } from "node:test";
|
|
2
|
+
import assert from "node:assert";
|
|
3
|
+
import {
|
|
4
|
+
transformToolHandler,
|
|
5
|
+
type UserToolResponse,
|
|
6
|
+
} from "../transformers/tool";
|
|
7
|
+
import { httpRequestContextProvider } from "../../contexts/http-request-context";
|
|
8
|
+
import { clientInfoContextProvider } from "../../contexts/client-info-context";
|
|
9
|
+
|
|
10
|
+
type MinimalExtra = {
|
|
11
|
+
signal: AbortSignal;
|
|
12
|
+
requestId: string;
|
|
13
|
+
sendNotification: (notification: unknown) => Promise<void>;
|
|
14
|
+
sendRequest: (request: unknown, resultSchema: unknown) => Promise<unknown>;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
const createMinimalExtra = (requestId: string): MinimalExtra => ({
|
|
18
|
+
signal: new AbortController().signal,
|
|
19
|
+
requestId,
|
|
20
|
+
sendNotification: async () => undefined,
|
|
21
|
+
sendRequest: async () => ({}),
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
const invokeHandler = (
|
|
25
|
+
handler: unknown,
|
|
26
|
+
requestId: string
|
|
27
|
+
): Promise<UserToolResponse> => {
|
|
28
|
+
const typedHandler = handler as (
|
|
29
|
+
args: Record<string, never>,
|
|
30
|
+
extra: MinimalExtra
|
|
31
|
+
) => Promise<UserToolResponse>;
|
|
32
|
+
|
|
33
|
+
return typedHandler({}, createMinimalExtra(requestId));
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
test("transformToolHandler forwards clientInfo through tool extra arguments", async () => {
|
|
37
|
+
const transformedHandler = transformToolHandler((args, extra) => {
|
|
38
|
+
return {
|
|
39
|
+
structuredContent: {
|
|
40
|
+
...args,
|
|
41
|
+
clientInfoName: extra.clientInfo?.name,
|
|
42
|
+
clientInfoVersion: extra.clientInfo?.version,
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
const result = await new Promise<UserToolResponse>((resolve, reject) => {
|
|
48
|
+
httpRequestContextProvider(
|
|
49
|
+
{
|
|
50
|
+
id: "request-id",
|
|
51
|
+
headers: {},
|
|
52
|
+
clientInfo: {
|
|
53
|
+
name: "cursor",
|
|
54
|
+
version: "0.50.1",
|
|
55
|
+
title: "Cursor",
|
|
56
|
+
},
|
|
57
|
+
},
|
|
58
|
+
() => {
|
|
59
|
+
invokeHandler(transformedHandler, "rpc-1").then(resolve).catch(reject);
|
|
60
|
+
}
|
|
61
|
+
);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
assert.deepStrictEqual(result, {
|
|
65
|
+
structuredContent: {
|
|
66
|
+
clientInfoName: "cursor",
|
|
67
|
+
clientInfoVersion: "0.50.1",
|
|
68
|
+
},
|
|
69
|
+
content: [
|
|
70
|
+
{
|
|
71
|
+
type: "text",
|
|
72
|
+
text: '{"clientInfoName":"cursor","clientInfoVersion":"0.50.1"}',
|
|
73
|
+
},
|
|
74
|
+
],
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
test("transformToolHandler leaves clientInfo undefined when request has no initialize metadata", async () => {
|
|
79
|
+
const transformedHandler = transformToolHandler((_args, extra) => {
|
|
80
|
+
return {
|
|
81
|
+
structuredContent: {
|
|
82
|
+
hasClientInfo: extra.clientInfo !== undefined,
|
|
83
|
+
},
|
|
84
|
+
};
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
const result = await new Promise<UserToolResponse>((resolve, reject) => {
|
|
88
|
+
httpRequestContextProvider(
|
|
89
|
+
{
|
|
90
|
+
id: "request-id-2",
|
|
91
|
+
headers: {},
|
|
92
|
+
clientInfo: undefined,
|
|
93
|
+
},
|
|
94
|
+
() => {
|
|
95
|
+
invokeHandler(transformedHandler, "rpc-2").then(resolve).catch(reject);
|
|
96
|
+
}
|
|
97
|
+
);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
assert.deepStrictEqual(result, {
|
|
101
|
+
structuredContent: {
|
|
102
|
+
hasClientInfo: false,
|
|
103
|
+
},
|
|
104
|
+
content: [
|
|
105
|
+
{
|
|
106
|
+
type: "text",
|
|
107
|
+
text: '{"hasClientInfo":false}',
|
|
108
|
+
},
|
|
109
|
+
],
|
|
110
|
+
});
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
test("transformToolHandler uses stdio clientInfo context as fallback", async () => {
|
|
114
|
+
const transformedHandler = transformToolHandler((_args, extra) => {
|
|
115
|
+
return {
|
|
116
|
+
structuredContent: {
|
|
117
|
+
clientInfoName: extra.clientInfo?.name,
|
|
118
|
+
clientInfoVersion: extra.clientInfo?.version,
|
|
119
|
+
},
|
|
120
|
+
};
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
const result = await new Promise<UserToolResponse>((resolve, reject) => {
|
|
124
|
+
clientInfoContextProvider(
|
|
125
|
+
{
|
|
126
|
+
clientInfo: {
|
|
127
|
+
name: "opencode",
|
|
128
|
+
version: "1.2.3",
|
|
129
|
+
},
|
|
130
|
+
},
|
|
131
|
+
() => {
|
|
132
|
+
invokeHandler(transformedHandler, "rpc-3").then(resolve).catch(reject);
|
|
133
|
+
}
|
|
134
|
+
);
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
assert.deepStrictEqual(result, {
|
|
138
|
+
structuredContent: {
|
|
139
|
+
clientInfoName: "opencode",
|
|
140
|
+
clientInfoVersion: "1.2.3",
|
|
141
|
+
},
|
|
142
|
+
content: [
|
|
143
|
+
{
|
|
144
|
+
type: "text",
|
|
145
|
+
text: '{"clientInfoName":"opencode","clientInfoVersion":"1.2.3"}',
|
|
146
|
+
},
|
|
147
|
+
],
|
|
148
|
+
});
|
|
149
|
+
});
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import type { JsonRpcMessage } from "@/runtime/transports/http/base-streamable-http";
|
|
2
|
+
import type { McpClientInfo } from "@/types/client-info";
|
|
3
|
+
import type { Implementation } from "@modelcontextprotocol/sdk/types";
|
|
4
|
+
|
|
5
|
+
const isObject = (value: unknown): value is Record<string, unknown> => {
|
|
6
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
const parseClientInfoCandidate = (
|
|
10
|
+
candidate: unknown
|
|
11
|
+
): McpClientInfo | undefined => {
|
|
12
|
+
if (!isObject(candidate)) {
|
|
13
|
+
return undefined;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
if (
|
|
17
|
+
typeof candidate.name !== "string" ||
|
|
18
|
+
typeof candidate.version !== "string"
|
|
19
|
+
) {
|
|
20
|
+
return undefined;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const result: McpClientInfo = {
|
|
24
|
+
name: candidate.name,
|
|
25
|
+
version: candidate.version,
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
if (typeof candidate.title === "string") {
|
|
29
|
+
result.title = candidate.title;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if (typeof candidate.websiteUrl === "string") {
|
|
33
|
+
result.websiteUrl = candidate.websiteUrl;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (typeof candidate.description === "string") {
|
|
37
|
+
result.description = candidate.description;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return result;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
export const mapImplementationToClientInfo = (
|
|
44
|
+
implementation: Implementation | undefined
|
|
45
|
+
): McpClientInfo | undefined => {
|
|
46
|
+
return parseClientInfoCandidate(implementation);
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
export const extractClientInfoFromMessage = (
|
|
50
|
+
message: JsonRpcMessage | undefined
|
|
51
|
+
): McpClientInfo | undefined => {
|
|
52
|
+
if (!message || message.method !== "initialize") {
|
|
53
|
+
return undefined;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
if (!isObject(message.params)) {
|
|
57
|
+
return undefined;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return parseClientInfoCandidate(message.params.clientInfo);
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
export const extractClientInfoFromMessages = (
|
|
64
|
+
payload: unknown
|
|
65
|
+
): McpClientInfo | undefined => {
|
|
66
|
+
const messages = Array.isArray(payload)
|
|
67
|
+
? (payload as JsonRpcMessage[])
|
|
68
|
+
: ([payload] as JsonRpcMessage[]);
|
|
69
|
+
|
|
70
|
+
for (const message of messages) {
|
|
71
|
+
const clientInfo = extractClientInfoFromMessage(message);
|
|
72
|
+
if (clientInfo) {
|
|
73
|
+
return clientInfo;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return undefined;
|
|
78
|
+
};
|
|
@@ -0,0 +1,396 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ElicitRequestFormParamsSchema,
|
|
3
|
+
ElicitRequestURLParamsSchema,
|
|
4
|
+
ElicitResultSchema,
|
|
5
|
+
} from "@modelcontextprotocol/sdk/types";
|
|
6
|
+
import type {
|
|
7
|
+
ElicitFormField,
|
|
8
|
+
ElicitFormRequest,
|
|
9
|
+
ElicitRequest,
|
|
10
|
+
ElicitResult,
|
|
11
|
+
ElicitUrlRequest,
|
|
12
|
+
ToolExtraArguments,
|
|
13
|
+
ToolRequestOptions,
|
|
14
|
+
} from "@/types/tool";
|
|
15
|
+
|
|
16
|
+
type ElicitRequestSender = Pick<ToolExtraArguments, "sendRequest">;
|
|
17
|
+
|
|
18
|
+
const ALLOWED_FORM_REQUEST_KEYS = new Set(["message", "mode", "requestedSchema"]);
|
|
19
|
+
const ALLOWED_URL_REQUEST_KEYS = new Set([
|
|
20
|
+
"elicitationId",
|
|
21
|
+
"message",
|
|
22
|
+
"mode",
|
|
23
|
+
"url",
|
|
24
|
+
]);
|
|
25
|
+
const ALLOWED_FORM_SCHEMA_KEYS = new Set(["properties", "required", "type"]);
|
|
26
|
+
const ALLOWED_STRING_FIELD_KEYS = new Set([
|
|
27
|
+
"default",
|
|
28
|
+
"description",
|
|
29
|
+
"format",
|
|
30
|
+
"maxLength",
|
|
31
|
+
"minLength",
|
|
32
|
+
"title",
|
|
33
|
+
"type",
|
|
34
|
+
]);
|
|
35
|
+
const ALLOWED_ENUM_FIELD_KEYS = new Set([
|
|
36
|
+
"default",
|
|
37
|
+
"description",
|
|
38
|
+
"enum",
|
|
39
|
+
"enumNames",
|
|
40
|
+
"title",
|
|
41
|
+
"type",
|
|
42
|
+
]);
|
|
43
|
+
const ALLOWED_BOOLEAN_FIELD_KEYS = new Set([
|
|
44
|
+
"default",
|
|
45
|
+
"description",
|
|
46
|
+
"title",
|
|
47
|
+
"type",
|
|
48
|
+
]);
|
|
49
|
+
const ALLOWED_NUMBER_FIELD_KEYS = new Set([
|
|
50
|
+
"default",
|
|
51
|
+
"description",
|
|
52
|
+
"maximum",
|
|
53
|
+
"minimum",
|
|
54
|
+
"title",
|
|
55
|
+
"type",
|
|
56
|
+
]);
|
|
57
|
+
const ALLOWED_STRING_FORMATS = new Set(["date", "date-time", "email", "uri"]);
|
|
58
|
+
|
|
59
|
+
export async function elicitFromTool(
|
|
60
|
+
extra: ElicitRequestSender,
|
|
61
|
+
request: ElicitRequest,
|
|
62
|
+
options?: ToolRequestOptions
|
|
63
|
+
): Promise<ElicitResult> {
|
|
64
|
+
const params = normalizeElicitationRequest(request);
|
|
65
|
+
const result = await extra.sendRequest(
|
|
66
|
+
{ method: "elicitation/create", params },
|
|
67
|
+
ElicitResultSchema,
|
|
68
|
+
options
|
|
69
|
+
);
|
|
70
|
+
|
|
71
|
+
return {
|
|
72
|
+
...result,
|
|
73
|
+
content: result.content ?? undefined,
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function normalizeElicitationRequest(request: ElicitRequest) {
|
|
78
|
+
const mode = request.mode ?? "form";
|
|
79
|
+
|
|
80
|
+
if (mode === "url") {
|
|
81
|
+
assertValidUrlRequest(request);
|
|
82
|
+
return ElicitRequestURLParamsSchema.parse(request);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
if (mode !== "form") {
|
|
86
|
+
throw new Error(
|
|
87
|
+
`Unsupported elicitation mode "${String((request as { mode?: unknown }).mode)}". Expected "form" or "url".`
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
assertValidFormRequest(request);
|
|
92
|
+
return ElicitRequestFormParamsSchema.parse({
|
|
93
|
+
...request,
|
|
94
|
+
mode: "form",
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function assertValidFormRequest(
|
|
99
|
+
request: ElicitRequest
|
|
100
|
+
): asserts request is ElicitFormRequest {
|
|
101
|
+
assertPlainObject(request, "Elicitation request must be a plain object.");
|
|
102
|
+
assertAllowedKeys(request, ALLOWED_FORM_REQUEST_KEYS, "Form elicitation");
|
|
103
|
+
assertString(request.message, "Form elicitation requires a string message.");
|
|
104
|
+
|
|
105
|
+
const requestedSchema = request.requestedSchema;
|
|
106
|
+
assertPlainObject(
|
|
107
|
+
requestedSchema,
|
|
108
|
+
"Form elicitation requires a flat object schema."
|
|
109
|
+
);
|
|
110
|
+
assertAllowedKeys(
|
|
111
|
+
requestedSchema,
|
|
112
|
+
ALLOWED_FORM_SCHEMA_KEYS,
|
|
113
|
+
"Form elicitation schema"
|
|
114
|
+
);
|
|
115
|
+
|
|
116
|
+
if (requestedSchema.type !== "object") {
|
|
117
|
+
throw new Error('Form elicitation schema must use type "object".');
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
assertPlainObject(
|
|
121
|
+
requestedSchema.properties,
|
|
122
|
+
"Form elicitation schema requires a properties object."
|
|
123
|
+
);
|
|
124
|
+
|
|
125
|
+
for (const [fieldName, field] of Object.entries(requestedSchema.properties)) {
|
|
126
|
+
assertSupportedFormField(fieldName, field);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
if (typeof requestedSchema.required === "undefined") {
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
if (
|
|
134
|
+
!Array.isArray(requestedSchema.required) ||
|
|
135
|
+
requestedSchema.required.some((fieldName) => typeof fieldName !== "string")
|
|
136
|
+
) {
|
|
137
|
+
throw new Error(
|
|
138
|
+
"Form elicitation schema required fields must be a string array."
|
|
139
|
+
);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
for (const fieldName of requestedSchema.required) {
|
|
143
|
+
if (!(fieldName in requestedSchema.properties)) {
|
|
144
|
+
throw new Error(
|
|
145
|
+
`Form elicitation schema required field "${fieldName}" is not defined in properties.`
|
|
146
|
+
);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function assertValidUrlRequest(
|
|
152
|
+
request: ElicitRequest
|
|
153
|
+
): asserts request is ElicitUrlRequest {
|
|
154
|
+
assertPlainObject(request, "Elicitation request must be a plain object.");
|
|
155
|
+
assertAllowedKeys(request, ALLOWED_URL_REQUEST_KEYS, "URL elicitation");
|
|
156
|
+
assertString(request.message, "URL elicitation requires a string message.");
|
|
157
|
+
assertNonEmptyString(
|
|
158
|
+
request.elicitationId,
|
|
159
|
+
"URL elicitation requires a non-empty elicitationId."
|
|
160
|
+
);
|
|
161
|
+
assertNonEmptyString(request.url, "URL elicitation requires a non-empty URL.");
|
|
162
|
+
|
|
163
|
+
let parsedUrl: URL;
|
|
164
|
+
try {
|
|
165
|
+
parsedUrl = new URL(request.url);
|
|
166
|
+
} catch {
|
|
167
|
+
throw new Error("URL elicitation requires a valid http or https URL.");
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
if (parsedUrl.protocol !== "http:" && parsedUrl.protocol !== "https:") {
|
|
171
|
+
throw new Error("URL elicitation requires a valid http or https URL.");
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
function assertSupportedFormField(
|
|
176
|
+
fieldName: string,
|
|
177
|
+
field: unknown
|
|
178
|
+
): asserts field is ElicitFormField {
|
|
179
|
+
assertPlainObject(
|
|
180
|
+
field,
|
|
181
|
+
`Form elicitation field "${fieldName}" must be a plain object.`
|
|
182
|
+
);
|
|
183
|
+
assertOptionalString(
|
|
184
|
+
field.title,
|
|
185
|
+
`Form elicitation field "${fieldName}" title must be a string.`
|
|
186
|
+
);
|
|
187
|
+
assertOptionalString(
|
|
188
|
+
field.description,
|
|
189
|
+
`Form elicitation field "${fieldName}" description must be a string.`
|
|
190
|
+
);
|
|
191
|
+
|
|
192
|
+
if (field.type === "string" && "enum" in field) {
|
|
193
|
+
assertAllowedKeys(
|
|
194
|
+
field,
|
|
195
|
+
ALLOWED_ENUM_FIELD_KEYS,
|
|
196
|
+
`Form elicitation field "${fieldName}"`
|
|
197
|
+
);
|
|
198
|
+
assertStringArray(
|
|
199
|
+
field.enum,
|
|
200
|
+
`Form elicitation field "${fieldName}" enum values must be strings.`
|
|
201
|
+
);
|
|
202
|
+
assertOptionalStringArray(
|
|
203
|
+
field.enumNames,
|
|
204
|
+
`Form elicitation field "${fieldName}" enumNames must be strings.`
|
|
205
|
+
);
|
|
206
|
+
|
|
207
|
+
if (
|
|
208
|
+
typeof field.enumNames !== "undefined" &&
|
|
209
|
+
field.enumNames.length !== field.enum.length
|
|
210
|
+
) {
|
|
211
|
+
throw new Error(
|
|
212
|
+
`Form elicitation field "${fieldName}" enumNames length must match enum length.`
|
|
213
|
+
);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
assertOptionalString(
|
|
217
|
+
field.default,
|
|
218
|
+
`Form elicitation field "${fieldName}" default must be a string.`
|
|
219
|
+
);
|
|
220
|
+
|
|
221
|
+
if (
|
|
222
|
+
typeof field.default !== "undefined" &&
|
|
223
|
+
!field.enum.includes(field.default)
|
|
224
|
+
) {
|
|
225
|
+
throw new Error(
|
|
226
|
+
`Form elicitation field "${fieldName}" default must be one of its enum values.`
|
|
227
|
+
);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
return;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
if (field.type === "string" && !("enum" in field)) {
|
|
234
|
+
assertAllowedKeys(
|
|
235
|
+
field,
|
|
236
|
+
ALLOWED_STRING_FIELD_KEYS,
|
|
237
|
+
`Form elicitation field "${fieldName}"`
|
|
238
|
+
);
|
|
239
|
+
assertOptionalNumber(
|
|
240
|
+
field.minLength,
|
|
241
|
+
`Form elicitation field "${fieldName}" minLength must be a number.`
|
|
242
|
+
);
|
|
243
|
+
assertOptionalNumber(
|
|
244
|
+
field.maxLength,
|
|
245
|
+
`Form elicitation field "${fieldName}" maxLength must be a number.`
|
|
246
|
+
);
|
|
247
|
+
assertOptionalString(
|
|
248
|
+
field.default,
|
|
249
|
+
`Form elicitation field "${fieldName}" default must be a string.`
|
|
250
|
+
);
|
|
251
|
+
|
|
252
|
+
const format = field.format;
|
|
253
|
+
assertOptionalString(
|
|
254
|
+
format,
|
|
255
|
+
`Form elicitation field "${fieldName}" format must be a string.`
|
|
256
|
+
);
|
|
257
|
+
|
|
258
|
+
if (typeof format !== "undefined" && !ALLOWED_STRING_FORMATS.has(format)) {
|
|
259
|
+
throw new Error(
|
|
260
|
+
`Form elicitation field "${fieldName}" format must be one of: ${Array.from(ALLOWED_STRING_FORMATS).join(", ")}.`
|
|
261
|
+
);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
return;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
if (field.type === "boolean") {
|
|
268
|
+
assertAllowedKeys(
|
|
269
|
+
field,
|
|
270
|
+
ALLOWED_BOOLEAN_FIELD_KEYS,
|
|
271
|
+
`Form elicitation field "${fieldName}"`
|
|
272
|
+
);
|
|
273
|
+
|
|
274
|
+
if (
|
|
275
|
+
typeof field.default !== "undefined" &&
|
|
276
|
+
typeof field.default !== "boolean"
|
|
277
|
+
) {
|
|
278
|
+
throw new Error(
|
|
279
|
+
`Form elicitation field "${fieldName}" default must be a boolean.`
|
|
280
|
+
);
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
return;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
if (field.type === "number" || field.type === "integer") {
|
|
287
|
+
assertAllowedKeys(
|
|
288
|
+
field,
|
|
289
|
+
ALLOWED_NUMBER_FIELD_KEYS,
|
|
290
|
+
`Form elicitation field "${fieldName}"`
|
|
291
|
+
);
|
|
292
|
+
assertOptionalNumber(
|
|
293
|
+
field.minimum,
|
|
294
|
+
`Form elicitation field "${fieldName}" minimum must be a number.`
|
|
295
|
+
);
|
|
296
|
+
assertOptionalNumber(
|
|
297
|
+
field.maximum,
|
|
298
|
+
`Form elicitation field "${fieldName}" maximum must be a number.`
|
|
299
|
+
);
|
|
300
|
+
assertOptionalNumber(
|
|
301
|
+
field.default,
|
|
302
|
+
`Form elicitation field "${fieldName}" default must be a number.`
|
|
303
|
+
);
|
|
304
|
+
|
|
305
|
+
return;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
throw new Error(
|
|
309
|
+
`Form elicitation field "${fieldName}" uses unsupported type "${String((field as { type?: unknown }).type)}". Supported types are string, boolean, number, integer, and string enums.`
|
|
310
|
+
);
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
function assertAllowedKeys(
|
|
314
|
+
value: Record<string, unknown>,
|
|
315
|
+
allowedKeys: Set<string>,
|
|
316
|
+
label: string
|
|
317
|
+
): void {
|
|
318
|
+
const extraKeys = Object.keys(value).filter((key) => !allowedKeys.has(key));
|
|
319
|
+
|
|
320
|
+
if (extraKeys.length === 0) {
|
|
321
|
+
return;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
throw new Error(
|
|
325
|
+
`${label} includes unsupported key(s): ${extraKeys.join(", ")}.`
|
|
326
|
+
);
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
function assertPlainObject(
|
|
330
|
+
value: unknown,
|
|
331
|
+
errorMessage: string
|
|
332
|
+
): asserts value is Record<string, unknown> {
|
|
333
|
+
if (
|
|
334
|
+
typeof value !== "object" ||
|
|
335
|
+
value === null ||
|
|
336
|
+
Array.isArray(value)
|
|
337
|
+
) {
|
|
338
|
+
throw new Error(errorMessage);
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
function assertString(
|
|
343
|
+
value: unknown,
|
|
344
|
+
errorMessage: string
|
|
345
|
+
): asserts value is string {
|
|
346
|
+
if (typeof value !== "string") {
|
|
347
|
+
throw new Error(errorMessage);
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
function assertNonEmptyString(
|
|
352
|
+
value: unknown,
|
|
353
|
+
errorMessage: string
|
|
354
|
+
): asserts value is string {
|
|
355
|
+
if (typeof value !== "string" || value.trim().length === 0) {
|
|
356
|
+
throw new Error(errorMessage);
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
function assertOptionalString(
|
|
361
|
+
value: unknown,
|
|
362
|
+
errorMessage: string
|
|
363
|
+
): asserts value is string | undefined {
|
|
364
|
+
if (typeof value !== "undefined" && typeof value !== "string") {
|
|
365
|
+
throw new Error(errorMessage);
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
function assertOptionalNumber(
|
|
370
|
+
value: unknown,
|
|
371
|
+
errorMessage: string
|
|
372
|
+
): asserts value is number | undefined {
|
|
373
|
+
if (typeof value !== "undefined" && typeof value !== "number") {
|
|
374
|
+
throw new Error(errorMessage);
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
function assertStringArray(
|
|
379
|
+
value: unknown,
|
|
380
|
+
errorMessage: string
|
|
381
|
+
): asserts value is string[] {
|
|
382
|
+
if (!Array.isArray(value) || value.some((item) => typeof item !== "string")) {
|
|
383
|
+
throw new Error(errorMessage);
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
function assertOptionalStringArray(
|
|
388
|
+
value: unknown,
|
|
389
|
+
errorMessage: string
|
|
390
|
+
): asserts value is string[] | undefined {
|
|
391
|
+
if (typeof value === "undefined") {
|
|
392
|
+
return;
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
assertStringArray(value, errorMessage);
|
|
396
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export const MCP_APPS_PROTOCOL_VERSION = "2026-01-26";
|
|
2
|
+
|
|
3
|
+
export const INITIALIZE_METHOD = "ui/initialize";
|
|
4
|
+
export const INITIALIZED_METHOD = "ui/notifications/initialized";
|
|
5
|
+
export const REQUEST_DISPLAY_MODE_METHOD = "ui/request-display-mode";
|
|
6
|
+
export const REQUEST_DISPLAY_MODE_METHOD_LEGACY = "ui/requestDisplayMode";
|
|
7
|
+
export const OPEN_LINK_METHOD = "ui/open-link";
|
|
8
|
+
export const OPEN_LINK_METHOD_LEGACY = "ui/openLink";
|
|
9
|
+
export const UPDATE_MODEL_CONTEXT_METHOD = "ui/update-model-context";
|
|
10
|
+
export const UI_MESSAGE_METHOD = "ui/message";
|
|
11
|
+
export const LOG_MESSAGE_METHOD = "notifications/message";
|
|
12
|
+
export const RESOURCES_READ_METHOD = "resources/read";
|
|
13
|
+
export const HOST_CONTEXT_CHANGED_METHOD =
|
|
14
|
+
"ui/notifications/host-context-changed";
|
|
15
|
+
export const HOST_CONTEXT_CHANGED_METHOD_LEGACY = "ui/hostContextChanged";
|
|
16
|
+
export const SIZE_CHANGED_METHOD = "ui/notifications/size-changed";
|