@modelcontextprotocol/server 2.0.0-alpha.1 → 2.0.0-alpha.3
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 +6 -2
- package/dist/{src-IKPjmxu7.mjs → ajvProvider-Birb50r-.mjs} +21 -3421
- package/dist/ajvProvider-Birb50r-.mjs.map +1 -0
- package/dist/ajvProvider-DZ_siXcF.d.mts +983 -0
- package/dist/ajvProvider-DZ_siXcF.d.mts.map +1 -0
- package/dist/cfWorkerProvider-BrJKpSFH.mjs +954 -0
- package/dist/cfWorkerProvider-BrJKpSFH.mjs.map +1 -0
- package/dist/cfWorkerProvider-DUhk5Ewx.d.mts +52 -0
- package/dist/cfWorkerProvider-DUhk5Ewx.d.mts.map +1 -0
- package/dist/chunk-BRhqBsOc.mjs +42 -0
- package/dist/index.d.mts +1526 -334
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +91 -766
- package/dist/index.mjs.map +1 -1
- package/dist/shimsNode.d.mts +1 -1
- package/dist/shimsNode.mjs +1 -1
- package/dist/shimsWorkerd.d.mts +1 -1
- package/dist/shimsWorkerd.mjs +1 -1
- package/dist/shimsWorkerd.mjs.map +1 -1
- package/dist/src-Pa1iAvsj.mjs +3386 -0
- package/dist/src-Pa1iAvsj.mjs.map +1 -0
- package/dist/stdio.d.mts +49 -0
- package/dist/stdio.d.mts.map +1 -0
- package/dist/stdio.mjs +106 -0
- package/dist/stdio.mjs.map +1 -0
- package/dist/transport-DMKhEchd.d.mts +8657 -0
- package/dist/transport-DMKhEchd.d.mts.map +1 -0
- package/dist/types-R2RTIcjk.d.mts +66 -0
- package/dist/types-R2RTIcjk.d.mts.map +1 -0
- package/dist/validators/ajv.d.mts +2 -0
- package/dist/validators/ajv.mjs +4 -0
- package/dist/validators/cfWorker.d.mts +2 -0
- package/dist/validators/cfWorker.mjs +3 -0
- package/package.json +37 -18
- package/dist/index-DbrUKkja.d.mts +0 -5153
- package/dist/index-DbrUKkja.d.mts.map +0 -1
- package/dist/src-IKPjmxu7.mjs.map +0 -1
|
@@ -1,3289 +1,5 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { Validator } from "@cfworker/json-schema";
|
|
1
|
+
import { r as __toESM, t as __commonJSMin } from "./chunk-BRhqBsOc.mjs";
|
|
3
2
|
|
|
4
|
-
//#region rolldown:runtime
|
|
5
|
-
var __create = Object.create;
|
|
6
|
-
var __defProp = Object.defineProperty;
|
|
7
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
8
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
9
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
10
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
11
|
-
var __commonJSMin = (cb, mod) => () => (mod || cb((mod = { exports: {} }).exports, mod), mod.exports);
|
|
12
|
-
var __copyProps = (to, from, except, desc) => {
|
|
13
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
-
for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
15
|
-
key = keys[i];
|
|
16
|
-
if (!__hasOwnProp.call(to, key) && key !== except) {
|
|
17
|
-
__defProp(to, key, {
|
|
18
|
-
get: ((k) => from[k]).bind(null, key),
|
|
19
|
-
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
20
|
-
});
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
return to;
|
|
25
|
-
};
|
|
26
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
27
|
-
value: mod,
|
|
28
|
-
enumerable: true
|
|
29
|
-
}) : target, mod));
|
|
30
|
-
|
|
31
|
-
//#endregion
|
|
32
|
-
//#region ../core/src/auth/errors.ts
|
|
33
|
-
/**
|
|
34
|
-
* OAuth error codes as defined by {@link https://datatracker.ietf.org/doc/html/rfc6749#section-5.2 | RFC 6749}
|
|
35
|
-
* and extensions.
|
|
36
|
-
*/
|
|
37
|
-
let OAuthErrorCode = /* @__PURE__ */ function(OAuthErrorCode$1) {
|
|
38
|
-
/**
|
|
39
|
-
* The request is missing a required parameter, includes an invalid parameter value,
|
|
40
|
-
* includes a parameter more than once, or is otherwise malformed.
|
|
41
|
-
*/
|
|
42
|
-
OAuthErrorCode$1["InvalidRequest"] = "invalid_request";
|
|
43
|
-
/**
|
|
44
|
-
* Client authentication failed (e.g., unknown client, no client authentication included,
|
|
45
|
-
* or unsupported authentication method).
|
|
46
|
-
*/
|
|
47
|
-
OAuthErrorCode$1["InvalidClient"] = "invalid_client";
|
|
48
|
-
/**
|
|
49
|
-
* The provided authorization grant or refresh token is invalid, expired, revoked,
|
|
50
|
-
* does not match the redirection URI used in the authorization request, or was issued to another client.
|
|
51
|
-
*/
|
|
52
|
-
OAuthErrorCode$1["InvalidGrant"] = "invalid_grant";
|
|
53
|
-
/**
|
|
54
|
-
* The authenticated client is not authorized to use this authorization grant type.
|
|
55
|
-
*/
|
|
56
|
-
OAuthErrorCode$1["UnauthorizedClient"] = "unauthorized_client";
|
|
57
|
-
/**
|
|
58
|
-
* The authorization grant type is not supported by the authorization server.
|
|
59
|
-
*/
|
|
60
|
-
OAuthErrorCode$1["UnsupportedGrantType"] = "unsupported_grant_type";
|
|
61
|
-
/**
|
|
62
|
-
* The requested scope is invalid, unknown, malformed, or exceeds the scope granted by the resource owner.
|
|
63
|
-
*/
|
|
64
|
-
OAuthErrorCode$1["InvalidScope"] = "invalid_scope";
|
|
65
|
-
/**
|
|
66
|
-
* The resource owner or authorization server denied the request.
|
|
67
|
-
*/
|
|
68
|
-
OAuthErrorCode$1["AccessDenied"] = "access_denied";
|
|
69
|
-
/**
|
|
70
|
-
* The authorization server encountered an unexpected condition that prevented it from fulfilling the request.
|
|
71
|
-
*/
|
|
72
|
-
OAuthErrorCode$1["ServerError"] = "server_error";
|
|
73
|
-
/**
|
|
74
|
-
* The authorization server is currently unable to handle the request due to temporary overloading or maintenance.
|
|
75
|
-
*/
|
|
76
|
-
OAuthErrorCode$1["TemporarilyUnavailable"] = "temporarily_unavailable";
|
|
77
|
-
/**
|
|
78
|
-
* The authorization server does not support obtaining an authorization code using this method.
|
|
79
|
-
*/
|
|
80
|
-
OAuthErrorCode$1["UnsupportedResponseType"] = "unsupported_response_type";
|
|
81
|
-
/**
|
|
82
|
-
* The authorization server does not support the requested token type.
|
|
83
|
-
*/
|
|
84
|
-
OAuthErrorCode$1["UnsupportedTokenType"] = "unsupported_token_type";
|
|
85
|
-
/**
|
|
86
|
-
* The access token provided is expired, revoked, malformed, or invalid for other reasons.
|
|
87
|
-
*/
|
|
88
|
-
OAuthErrorCode$1["InvalidToken"] = "invalid_token";
|
|
89
|
-
/**
|
|
90
|
-
* The HTTP method used is not allowed for this endpoint. (Custom, non-standard error)
|
|
91
|
-
*/
|
|
92
|
-
OAuthErrorCode$1["MethodNotAllowed"] = "method_not_allowed";
|
|
93
|
-
/**
|
|
94
|
-
* Rate limit exceeded. (Custom, non-standard error based on RFC 6585)
|
|
95
|
-
*/
|
|
96
|
-
OAuthErrorCode$1["TooManyRequests"] = "too_many_requests";
|
|
97
|
-
/**
|
|
98
|
-
* The client metadata is invalid. (Custom error for dynamic client registration - RFC 7591)
|
|
99
|
-
*/
|
|
100
|
-
OAuthErrorCode$1["InvalidClientMetadata"] = "invalid_client_metadata";
|
|
101
|
-
/**
|
|
102
|
-
* The request requires higher privileges than provided by the access token.
|
|
103
|
-
*/
|
|
104
|
-
OAuthErrorCode$1["InsufficientScope"] = "insufficient_scope";
|
|
105
|
-
/**
|
|
106
|
-
* The requested resource is invalid, missing, unknown, or malformed. (Custom error for resource indicators - RFC 8707)
|
|
107
|
-
*/
|
|
108
|
-
OAuthErrorCode$1["InvalidTarget"] = "invalid_target";
|
|
109
|
-
return OAuthErrorCode$1;
|
|
110
|
-
}({});
|
|
111
|
-
/**
|
|
112
|
-
* OAuth error class for all OAuth-related errors.
|
|
113
|
-
*/
|
|
114
|
-
var OAuthError = class OAuthError extends Error {
|
|
115
|
-
constructor(code, message, errorUri) {
|
|
116
|
-
super(message);
|
|
117
|
-
this.code = code;
|
|
118
|
-
this.errorUri = errorUri;
|
|
119
|
-
this.name = "OAuthError";
|
|
120
|
-
}
|
|
121
|
-
/**
|
|
122
|
-
* Converts the error to a standard OAuth error response object.
|
|
123
|
-
*/
|
|
124
|
-
toResponseObject() {
|
|
125
|
-
const response = {
|
|
126
|
-
error: this.code,
|
|
127
|
-
error_description: this.message
|
|
128
|
-
};
|
|
129
|
-
if (this.errorUri) response.error_uri = this.errorUri;
|
|
130
|
-
return response;
|
|
131
|
-
}
|
|
132
|
-
/**
|
|
133
|
-
* Creates an {@linkcode OAuthError} from an OAuth error response.
|
|
134
|
-
*/
|
|
135
|
-
static fromResponse(response) {
|
|
136
|
-
return new OAuthError(response.error, response.error_description ?? response.error, response.error_uri);
|
|
137
|
-
}
|
|
138
|
-
};
|
|
139
|
-
|
|
140
|
-
//#endregion
|
|
141
|
-
//#region ../core/src/errors/sdkErrors.ts
|
|
142
|
-
/**
|
|
143
|
-
* Error codes for SDK errors (local errors that never cross the wire).
|
|
144
|
-
* Unlike {@linkcode ProtocolErrorCode} which uses numeric JSON-RPC codes, `SdkErrorCode` uses
|
|
145
|
-
* descriptive string values for better developer experience.
|
|
146
|
-
*
|
|
147
|
-
* These errors are thrown locally by the SDK and are never serialized as
|
|
148
|
-
* JSON-RPC error responses.
|
|
149
|
-
*/
|
|
150
|
-
let SdkErrorCode = /* @__PURE__ */ function(SdkErrorCode$1) {
|
|
151
|
-
/** Transport is not connected */
|
|
152
|
-
SdkErrorCode$1["NotConnected"] = "NOT_CONNECTED";
|
|
153
|
-
/** Transport is already connected */
|
|
154
|
-
SdkErrorCode$1["AlreadyConnected"] = "ALREADY_CONNECTED";
|
|
155
|
-
/** Protocol is not initialized */
|
|
156
|
-
SdkErrorCode$1["NotInitialized"] = "NOT_INITIALIZED";
|
|
157
|
-
/** Required capability is not supported by the remote side */
|
|
158
|
-
SdkErrorCode$1["CapabilityNotSupported"] = "CAPABILITY_NOT_SUPPORTED";
|
|
159
|
-
/** Request timed out waiting for response */
|
|
160
|
-
SdkErrorCode$1["RequestTimeout"] = "REQUEST_TIMEOUT";
|
|
161
|
-
/** Connection was closed */
|
|
162
|
-
SdkErrorCode$1["ConnectionClosed"] = "CONNECTION_CLOSED";
|
|
163
|
-
/** Failed to send message */
|
|
164
|
-
SdkErrorCode$1["SendFailed"] = "SEND_FAILED";
|
|
165
|
-
SdkErrorCode$1["ClientHttpNotImplemented"] = "CLIENT_HTTP_NOT_IMPLEMENTED";
|
|
166
|
-
SdkErrorCode$1["ClientHttpAuthentication"] = "CLIENT_HTTP_AUTHENTICATION";
|
|
167
|
-
SdkErrorCode$1["ClientHttpForbidden"] = "CLIENT_HTTP_FORBIDDEN";
|
|
168
|
-
SdkErrorCode$1["ClientHttpUnexpectedContent"] = "CLIENT_HTTP_UNEXPECTED_CONTENT";
|
|
169
|
-
SdkErrorCode$1["ClientHttpFailedToOpenStream"] = "CLIENT_HTTP_FAILED_TO_OPEN_STREAM";
|
|
170
|
-
SdkErrorCode$1["ClientHttpFailedToTerminateSession"] = "CLIENT_HTTP_FAILED_TO_TERMINATE_SESSION";
|
|
171
|
-
return SdkErrorCode$1;
|
|
172
|
-
}({});
|
|
173
|
-
/**
|
|
174
|
-
* SDK errors are local errors that never cross the wire.
|
|
175
|
-
* They are distinct from {@linkcode ProtocolError} which represents JSON-RPC protocol errors
|
|
176
|
-
* that are serialized and sent as error responses.
|
|
177
|
-
*
|
|
178
|
-
* @example
|
|
179
|
-
* ```ts source="./sdkErrors.examples.ts#SdkError_basicUsage"
|
|
180
|
-
* try {
|
|
181
|
-
* // Throwing an SDK error
|
|
182
|
-
* throw new SdkError(SdkErrorCode.NotConnected, 'Transport is not connected');
|
|
183
|
-
* } catch (error) {
|
|
184
|
-
* // Checking error type by code
|
|
185
|
-
* if (error instanceof SdkError && error.code === SdkErrorCode.RequestTimeout) {
|
|
186
|
-
* // Handle timeout
|
|
187
|
-
* }
|
|
188
|
-
* }
|
|
189
|
-
* ```
|
|
190
|
-
*/
|
|
191
|
-
var SdkError = class extends Error {
|
|
192
|
-
constructor(code, message, data) {
|
|
193
|
-
super(message);
|
|
194
|
-
this.code = code;
|
|
195
|
-
this.data = data;
|
|
196
|
-
this.name = "SdkError";
|
|
197
|
-
}
|
|
198
|
-
};
|
|
199
|
-
|
|
200
|
-
//#endregion
|
|
201
|
-
//#region ../core/src/shared/auth.ts
|
|
202
|
-
/**
|
|
203
|
-
* Reusable URL validation that disallows `javascript:` scheme
|
|
204
|
-
*/
|
|
205
|
-
const SafeUrlSchema = z.url().superRefine((val, ctx) => {
|
|
206
|
-
if (!URL.canParse(val)) {
|
|
207
|
-
ctx.addIssue({
|
|
208
|
-
code: z.ZodIssueCode.custom,
|
|
209
|
-
message: "URL must be parseable",
|
|
210
|
-
fatal: true
|
|
211
|
-
});
|
|
212
|
-
return z.NEVER;
|
|
213
|
-
}
|
|
214
|
-
}).refine((url) => {
|
|
215
|
-
const u = new URL(url);
|
|
216
|
-
return u.protocol !== "javascript:" && u.protocol !== "data:" && u.protocol !== "vbscript:";
|
|
217
|
-
}, { message: "URL cannot use javascript:, data:, or vbscript: scheme" });
|
|
218
|
-
/**
|
|
219
|
-
* RFC 9728 OAuth Protected Resource Metadata
|
|
220
|
-
*/
|
|
221
|
-
const OAuthProtectedResourceMetadataSchema = z.looseObject({
|
|
222
|
-
resource: z.string().url(),
|
|
223
|
-
authorization_servers: z.array(SafeUrlSchema).optional(),
|
|
224
|
-
jwks_uri: z.string().url().optional(),
|
|
225
|
-
scopes_supported: z.array(z.string()).optional(),
|
|
226
|
-
bearer_methods_supported: z.array(z.string()).optional(),
|
|
227
|
-
resource_signing_alg_values_supported: z.array(z.string()).optional(),
|
|
228
|
-
resource_name: z.string().optional(),
|
|
229
|
-
resource_documentation: z.string().optional(),
|
|
230
|
-
resource_policy_uri: z.string().url().optional(),
|
|
231
|
-
resource_tos_uri: z.string().url().optional(),
|
|
232
|
-
tls_client_certificate_bound_access_tokens: z.boolean().optional(),
|
|
233
|
-
authorization_details_types_supported: z.array(z.string()).optional(),
|
|
234
|
-
dpop_signing_alg_values_supported: z.array(z.string()).optional(),
|
|
235
|
-
dpop_bound_access_tokens_required: z.boolean().optional()
|
|
236
|
-
});
|
|
237
|
-
/**
|
|
238
|
-
* RFC 8414 OAuth 2.0 Authorization Server Metadata
|
|
239
|
-
*/
|
|
240
|
-
const OAuthMetadataSchema = z.looseObject({
|
|
241
|
-
issuer: z.string(),
|
|
242
|
-
authorization_endpoint: SafeUrlSchema,
|
|
243
|
-
token_endpoint: SafeUrlSchema,
|
|
244
|
-
registration_endpoint: SafeUrlSchema.optional(),
|
|
245
|
-
scopes_supported: z.array(z.string()).optional(),
|
|
246
|
-
response_types_supported: z.array(z.string()),
|
|
247
|
-
response_modes_supported: z.array(z.string()).optional(),
|
|
248
|
-
grant_types_supported: z.array(z.string()).optional(),
|
|
249
|
-
token_endpoint_auth_methods_supported: z.array(z.string()).optional(),
|
|
250
|
-
token_endpoint_auth_signing_alg_values_supported: z.array(z.string()).optional(),
|
|
251
|
-
service_documentation: SafeUrlSchema.optional(),
|
|
252
|
-
revocation_endpoint: SafeUrlSchema.optional(),
|
|
253
|
-
revocation_endpoint_auth_methods_supported: z.array(z.string()).optional(),
|
|
254
|
-
revocation_endpoint_auth_signing_alg_values_supported: z.array(z.string()).optional(),
|
|
255
|
-
introspection_endpoint: z.string().optional(),
|
|
256
|
-
introspection_endpoint_auth_methods_supported: z.array(z.string()).optional(),
|
|
257
|
-
introspection_endpoint_auth_signing_alg_values_supported: z.array(z.string()).optional(),
|
|
258
|
-
code_challenge_methods_supported: z.array(z.string()).optional(),
|
|
259
|
-
client_id_metadata_document_supported: z.boolean().optional()
|
|
260
|
-
});
|
|
261
|
-
/**
|
|
262
|
-
* OpenID Connect Discovery 1.0 Provider Metadata
|
|
263
|
-
*
|
|
264
|
-
* @see https://openid.net/specs/openid-connect-discovery-1_0.html#ProviderMetadata
|
|
265
|
-
*/
|
|
266
|
-
const OpenIdProviderMetadataSchema = z.looseObject({
|
|
267
|
-
issuer: z.string(),
|
|
268
|
-
authorization_endpoint: SafeUrlSchema,
|
|
269
|
-
token_endpoint: SafeUrlSchema,
|
|
270
|
-
userinfo_endpoint: SafeUrlSchema.optional(),
|
|
271
|
-
jwks_uri: SafeUrlSchema,
|
|
272
|
-
registration_endpoint: SafeUrlSchema.optional(),
|
|
273
|
-
scopes_supported: z.array(z.string()).optional(),
|
|
274
|
-
response_types_supported: z.array(z.string()),
|
|
275
|
-
response_modes_supported: z.array(z.string()).optional(),
|
|
276
|
-
grant_types_supported: z.array(z.string()).optional(),
|
|
277
|
-
acr_values_supported: z.array(z.string()).optional(),
|
|
278
|
-
subject_types_supported: z.array(z.string()),
|
|
279
|
-
id_token_signing_alg_values_supported: z.array(z.string()),
|
|
280
|
-
id_token_encryption_alg_values_supported: z.array(z.string()).optional(),
|
|
281
|
-
id_token_encryption_enc_values_supported: z.array(z.string()).optional(),
|
|
282
|
-
userinfo_signing_alg_values_supported: z.array(z.string()).optional(),
|
|
283
|
-
userinfo_encryption_alg_values_supported: z.array(z.string()).optional(),
|
|
284
|
-
userinfo_encryption_enc_values_supported: z.array(z.string()).optional(),
|
|
285
|
-
request_object_signing_alg_values_supported: z.array(z.string()).optional(),
|
|
286
|
-
request_object_encryption_alg_values_supported: z.array(z.string()).optional(),
|
|
287
|
-
request_object_encryption_enc_values_supported: z.array(z.string()).optional(),
|
|
288
|
-
token_endpoint_auth_methods_supported: z.array(z.string()).optional(),
|
|
289
|
-
token_endpoint_auth_signing_alg_values_supported: z.array(z.string()).optional(),
|
|
290
|
-
display_values_supported: z.array(z.string()).optional(),
|
|
291
|
-
claim_types_supported: z.array(z.string()).optional(),
|
|
292
|
-
claims_supported: z.array(z.string()).optional(),
|
|
293
|
-
service_documentation: z.string().optional(),
|
|
294
|
-
claims_locales_supported: z.array(z.string()).optional(),
|
|
295
|
-
ui_locales_supported: z.array(z.string()).optional(),
|
|
296
|
-
claims_parameter_supported: z.boolean().optional(),
|
|
297
|
-
request_parameter_supported: z.boolean().optional(),
|
|
298
|
-
request_uri_parameter_supported: z.boolean().optional(),
|
|
299
|
-
require_request_uri_registration: z.boolean().optional(),
|
|
300
|
-
op_policy_uri: SafeUrlSchema.optional(),
|
|
301
|
-
op_tos_uri: SafeUrlSchema.optional(),
|
|
302
|
-
client_id_metadata_document_supported: z.boolean().optional()
|
|
303
|
-
});
|
|
304
|
-
/**
|
|
305
|
-
* OpenID Connect Discovery metadata that may include OAuth 2.0 fields
|
|
306
|
-
* This schema represents the real-world scenario where OIDC providers
|
|
307
|
-
* return a mix of OpenID Connect and OAuth 2.0 metadata fields
|
|
308
|
-
*/
|
|
309
|
-
const OpenIdProviderDiscoveryMetadataSchema = z.object({
|
|
310
|
-
...OpenIdProviderMetadataSchema.shape,
|
|
311
|
-
...OAuthMetadataSchema.pick({ code_challenge_methods_supported: true }).shape
|
|
312
|
-
});
|
|
313
|
-
/**
|
|
314
|
-
* OAuth 2.1 token response
|
|
315
|
-
*/
|
|
316
|
-
const OAuthTokensSchema = z.object({
|
|
317
|
-
access_token: z.string(),
|
|
318
|
-
id_token: z.string().optional(),
|
|
319
|
-
token_type: z.string(),
|
|
320
|
-
expires_in: z.coerce.number().optional(),
|
|
321
|
-
scope: z.string().optional(),
|
|
322
|
-
refresh_token: z.string().optional()
|
|
323
|
-
}).strip();
|
|
324
|
-
/**
|
|
325
|
-
* RFC 8693 §2.2.1 Token Exchange response for ID-JAG tokens.
|
|
326
|
-
*
|
|
327
|
-
* `token_type` is intentionally optional: per RFC 8693 §2.2.1 it is informational when
|
|
328
|
-
* the issued token is not an access token, and per RFC 6749 §5.1 it is case-insensitive,
|
|
329
|
-
* so strict checking rejects conformant IdPs.
|
|
330
|
-
*/
|
|
331
|
-
const IdJagTokenExchangeResponseSchema = z.object({
|
|
332
|
-
issued_token_type: z.literal("urn:ietf:params:oauth:token-type:id-jag"),
|
|
333
|
-
access_token: z.string(),
|
|
334
|
-
token_type: z.string().optional(),
|
|
335
|
-
expires_in: z.number().optional(),
|
|
336
|
-
scope: z.string().optional()
|
|
337
|
-
}).strip();
|
|
338
|
-
/**
|
|
339
|
-
* OAuth 2.1 error response
|
|
340
|
-
*/
|
|
341
|
-
const OAuthErrorResponseSchema = z.object({
|
|
342
|
-
error: z.string(),
|
|
343
|
-
error_description: z.string().optional(),
|
|
344
|
-
error_uri: z.string().optional()
|
|
345
|
-
});
|
|
346
|
-
/**
|
|
347
|
-
* Optional version of {@linkcode SafeUrlSchema} that allows empty string for backward compatibility on `tos_uri` and `logo_uri`
|
|
348
|
-
*/
|
|
349
|
-
const OptionalSafeUrlSchema = SafeUrlSchema.optional().or(z.literal("").transform(() => void 0));
|
|
350
|
-
/**
|
|
351
|
-
* RFC 7591 OAuth 2.0 Dynamic Client Registration metadata
|
|
352
|
-
*/
|
|
353
|
-
const OAuthClientMetadataSchema = z.object({
|
|
354
|
-
redirect_uris: z.array(SafeUrlSchema),
|
|
355
|
-
token_endpoint_auth_method: z.string().optional(),
|
|
356
|
-
grant_types: z.array(z.string()).optional(),
|
|
357
|
-
response_types: z.array(z.string()).optional(),
|
|
358
|
-
client_name: z.string().optional(),
|
|
359
|
-
client_uri: SafeUrlSchema.optional(),
|
|
360
|
-
logo_uri: OptionalSafeUrlSchema,
|
|
361
|
-
scope: z.string().optional(),
|
|
362
|
-
contacts: z.array(z.string()).optional(),
|
|
363
|
-
tos_uri: OptionalSafeUrlSchema,
|
|
364
|
-
policy_uri: z.string().optional(),
|
|
365
|
-
jwks_uri: SafeUrlSchema.optional(),
|
|
366
|
-
jwks: z.any().optional(),
|
|
367
|
-
software_id: z.string().optional(),
|
|
368
|
-
software_version: z.string().optional(),
|
|
369
|
-
software_statement: z.string().optional()
|
|
370
|
-
}).strip();
|
|
371
|
-
/**
|
|
372
|
-
* RFC 7591 OAuth 2.0 Dynamic Client Registration client information
|
|
373
|
-
*/
|
|
374
|
-
const OAuthClientInformationSchema = z.object({
|
|
375
|
-
client_id: z.string(),
|
|
376
|
-
client_secret: z.string().optional(),
|
|
377
|
-
client_id_issued_at: z.number().optional(),
|
|
378
|
-
client_secret_expires_at: z.number().optional()
|
|
379
|
-
}).strip();
|
|
380
|
-
/**
|
|
381
|
-
* RFC 7591 OAuth 2.0 Dynamic Client Registration full response (client information plus metadata)
|
|
382
|
-
*/
|
|
383
|
-
const OAuthClientInformationFullSchema = OAuthClientMetadataSchema.merge(OAuthClientInformationSchema);
|
|
384
|
-
/**
|
|
385
|
-
* RFC 7591 OAuth 2.0 Dynamic Client Registration error response
|
|
386
|
-
*/
|
|
387
|
-
const OAuthClientRegistrationErrorSchema = z.object({
|
|
388
|
-
error: z.string(),
|
|
389
|
-
error_description: z.string().optional()
|
|
390
|
-
}).strip();
|
|
391
|
-
/**
|
|
392
|
-
* RFC 7009 OAuth 2.0 Token Revocation request
|
|
393
|
-
*/
|
|
394
|
-
const OAuthTokenRevocationRequestSchema = z.object({
|
|
395
|
-
token: z.string(),
|
|
396
|
-
token_type_hint: z.string().optional()
|
|
397
|
-
}).strip();
|
|
398
|
-
|
|
399
|
-
//#endregion
|
|
400
|
-
//#region ../core/src/shared/authUtils.ts
|
|
401
|
-
/**
|
|
402
|
-
* Utilities for handling OAuth resource URIs.
|
|
403
|
-
*/
|
|
404
|
-
/**
|
|
405
|
-
* Converts a server URL to a resource URL by removing the fragment.
|
|
406
|
-
* {@link https://datatracker.ietf.org/doc/html/rfc8707#section-2 | RFC 8707 section 2}
|
|
407
|
-
* states that resource URIs "MUST NOT include a fragment component".
|
|
408
|
-
* Keeps everything else unchanged (scheme, domain, port, path, query).
|
|
409
|
-
*/
|
|
410
|
-
function resourceUrlFromServerUrl(url) {
|
|
411
|
-
const resourceURL = typeof url === "string" ? new URL(url) : new URL(url.href);
|
|
412
|
-
resourceURL.hash = "";
|
|
413
|
-
return resourceURL;
|
|
414
|
-
}
|
|
415
|
-
/**
|
|
416
|
-
* Checks if a requested resource URL matches a configured resource URL.
|
|
417
|
-
* A requested resource matches if it has the same scheme, domain, port,
|
|
418
|
-
* and its path starts with the configured resource's path.
|
|
419
|
-
*
|
|
420
|
-
* @param options - The options object
|
|
421
|
-
* @param options.requestedResource - The resource URL being requested
|
|
422
|
-
* @param options.configuredResource - The resource URL that has been configured
|
|
423
|
-
* @returns true if the requested resource matches the configured resource, false otherwise
|
|
424
|
-
*/
|
|
425
|
-
function checkResourceAllowed({ requestedResource, configuredResource }) {
|
|
426
|
-
const requested = typeof requestedResource === "string" ? new URL(requestedResource) : new URL(requestedResource.href);
|
|
427
|
-
const configured = typeof configuredResource === "string" ? new URL(configuredResource) : new URL(configuredResource.href);
|
|
428
|
-
if (requested.origin !== configured.origin) return false;
|
|
429
|
-
if (requested.pathname.length < configured.pathname.length) return false;
|
|
430
|
-
const requestedPath = requested.pathname.endsWith("/") ? requested.pathname : requested.pathname + "/";
|
|
431
|
-
const configuredPath = configured.pathname.endsWith("/") ? configured.pathname : configured.pathname + "/";
|
|
432
|
-
return requestedPath.startsWith(configuredPath);
|
|
433
|
-
}
|
|
434
|
-
|
|
435
|
-
//#endregion
|
|
436
|
-
//#region ../core/src/shared/metadataUtils.ts
|
|
437
|
-
/**
|
|
438
|
-
* Utilities for working with {@linkcode BaseMetadata} objects.
|
|
439
|
-
*/
|
|
440
|
-
/**
|
|
441
|
-
* Gets the display name for an object with {@linkcode BaseMetadata}.
|
|
442
|
-
* For tools, the precedence is: `title` → {@linkcode index.ToolAnnotations | annotations}.`title` → `name`
|
|
443
|
-
* For other objects: `title` → `name`
|
|
444
|
-
* This implements the spec requirement: "if no title is provided, name should be used for display purposes"
|
|
445
|
-
*/
|
|
446
|
-
function getDisplayName(metadata) {
|
|
447
|
-
if (metadata.title !== void 0 && metadata.title !== "") return metadata.title;
|
|
448
|
-
if ("annotations" in metadata && metadata.annotations?.title) return metadata.annotations.title;
|
|
449
|
-
return metadata.name;
|
|
450
|
-
}
|
|
451
|
-
|
|
452
|
-
//#endregion
|
|
453
|
-
//#region ../core/src/types/constants.ts
|
|
454
|
-
const LATEST_PROTOCOL_VERSION = "2025-11-25";
|
|
455
|
-
const DEFAULT_NEGOTIATED_PROTOCOL_VERSION = "2025-03-26";
|
|
456
|
-
const SUPPORTED_PROTOCOL_VERSIONS = [
|
|
457
|
-
LATEST_PROTOCOL_VERSION,
|
|
458
|
-
"2025-06-18",
|
|
459
|
-
"2025-03-26",
|
|
460
|
-
"2024-11-05",
|
|
461
|
-
"2024-10-07"
|
|
462
|
-
];
|
|
463
|
-
const RELATED_TASK_META_KEY = "io.modelcontextprotocol/related-task";
|
|
464
|
-
const JSONRPC_VERSION = "2.0";
|
|
465
|
-
const PARSE_ERROR = -32700;
|
|
466
|
-
const INVALID_REQUEST = -32600;
|
|
467
|
-
const METHOD_NOT_FOUND = -32601;
|
|
468
|
-
const INVALID_PARAMS = -32602;
|
|
469
|
-
const INTERNAL_ERROR = -32603;
|
|
470
|
-
|
|
471
|
-
//#endregion
|
|
472
|
-
//#region ../core/src/types/enums.ts
|
|
473
|
-
/**
|
|
474
|
-
* Error codes for protocol errors that cross the wire as JSON-RPC error responses.
|
|
475
|
-
* These follow the JSON-RPC specification and MCP-specific extensions.
|
|
476
|
-
*/
|
|
477
|
-
let ProtocolErrorCode = /* @__PURE__ */ function(ProtocolErrorCode$1) {
|
|
478
|
-
ProtocolErrorCode$1[ProtocolErrorCode$1["ParseError"] = -32700] = "ParseError";
|
|
479
|
-
ProtocolErrorCode$1[ProtocolErrorCode$1["InvalidRequest"] = -32600] = "InvalidRequest";
|
|
480
|
-
ProtocolErrorCode$1[ProtocolErrorCode$1["MethodNotFound"] = -32601] = "MethodNotFound";
|
|
481
|
-
ProtocolErrorCode$1[ProtocolErrorCode$1["InvalidParams"] = -32602] = "InvalidParams";
|
|
482
|
-
ProtocolErrorCode$1[ProtocolErrorCode$1["InternalError"] = -32603] = "InternalError";
|
|
483
|
-
ProtocolErrorCode$1[ProtocolErrorCode$1["ResourceNotFound"] = -32002] = "ResourceNotFound";
|
|
484
|
-
ProtocolErrorCode$1[ProtocolErrorCode$1["UrlElicitationRequired"] = -32042] = "UrlElicitationRequired";
|
|
485
|
-
return ProtocolErrorCode$1;
|
|
486
|
-
}({});
|
|
487
|
-
|
|
488
|
-
//#endregion
|
|
489
|
-
//#region ../core/src/types/errors.ts
|
|
490
|
-
/**
|
|
491
|
-
* Protocol errors are JSON-RPC errors that cross the wire as error responses.
|
|
492
|
-
* They use numeric error codes from the {@linkcode ProtocolErrorCode} enum.
|
|
493
|
-
*/
|
|
494
|
-
var ProtocolError = class ProtocolError extends Error {
|
|
495
|
-
constructor(code, message, data) {
|
|
496
|
-
super(message);
|
|
497
|
-
this.code = code;
|
|
498
|
-
this.data = data;
|
|
499
|
-
this.name = "ProtocolError";
|
|
500
|
-
}
|
|
501
|
-
/**
|
|
502
|
-
* Factory method to create the appropriate error type based on the error code and data
|
|
503
|
-
*/
|
|
504
|
-
static fromError(code, message, data) {
|
|
505
|
-
if (code === ProtocolErrorCode.UrlElicitationRequired && data) {
|
|
506
|
-
const errorData = data;
|
|
507
|
-
if (errorData.elicitations) return new UrlElicitationRequiredError(errorData.elicitations, message);
|
|
508
|
-
}
|
|
509
|
-
return new ProtocolError(code, message, data);
|
|
510
|
-
}
|
|
511
|
-
};
|
|
512
|
-
/**
|
|
513
|
-
* Specialized error type when a tool requires a URL mode elicitation.
|
|
514
|
-
* This makes it nicer for the client to handle since there is specific data to work with instead of just a code to check against.
|
|
515
|
-
*/
|
|
516
|
-
var UrlElicitationRequiredError = class extends ProtocolError {
|
|
517
|
-
constructor(elicitations, message = `URL elicitation${elicitations.length > 1 ? "s" : ""} required`) {
|
|
518
|
-
super(ProtocolErrorCode.UrlElicitationRequired, message, { elicitations });
|
|
519
|
-
}
|
|
520
|
-
get elicitations() {
|
|
521
|
-
return this.data?.elicitations ?? [];
|
|
522
|
-
}
|
|
523
|
-
};
|
|
524
|
-
|
|
525
|
-
//#endregion
|
|
526
|
-
//#region ../core/src/types/schemas.ts
|
|
527
|
-
const JSONValueSchema = z.lazy(() => z.union([
|
|
528
|
-
z.string(),
|
|
529
|
-
z.number(),
|
|
530
|
-
z.boolean(),
|
|
531
|
-
z.null(),
|
|
532
|
-
z.record(z.string(), JSONValueSchema),
|
|
533
|
-
z.array(JSONValueSchema)
|
|
534
|
-
]));
|
|
535
|
-
const JSONObjectSchema = z.record(z.string(), JSONValueSchema);
|
|
536
|
-
const JSONArraySchema = z.array(JSONValueSchema);
|
|
537
|
-
/**
|
|
538
|
-
* A progress token, used to associate progress notifications with the original request.
|
|
539
|
-
*/
|
|
540
|
-
const ProgressTokenSchema = z.union([z.string(), z.number().int()]);
|
|
541
|
-
/**
|
|
542
|
-
* An opaque token used to represent a cursor for pagination.
|
|
543
|
-
*/
|
|
544
|
-
const CursorSchema = z.string();
|
|
545
|
-
/**
|
|
546
|
-
* Task creation parameters, used to ask that the server create a task to represent a request.
|
|
547
|
-
*/
|
|
548
|
-
const TaskCreationParamsSchema = z.looseObject({
|
|
549
|
-
ttl: z.number().optional(),
|
|
550
|
-
pollInterval: z.number().optional()
|
|
551
|
-
});
|
|
552
|
-
const TaskMetadataSchema = z.object({ ttl: z.number().optional() });
|
|
553
|
-
/**
|
|
554
|
-
* Metadata for associating messages with a task.
|
|
555
|
-
* Include this in the `_meta` field under the key `io.modelcontextprotocol/related-task`.
|
|
556
|
-
*/
|
|
557
|
-
const RelatedTaskMetadataSchema = z.object({ taskId: z.string() });
|
|
558
|
-
const RequestMetaSchema = z.looseObject({
|
|
559
|
-
progressToken: ProgressTokenSchema.optional(),
|
|
560
|
-
[RELATED_TASK_META_KEY]: RelatedTaskMetadataSchema.optional()
|
|
561
|
-
});
|
|
562
|
-
/**
|
|
563
|
-
* Common params for any request.
|
|
564
|
-
*/
|
|
565
|
-
const BaseRequestParamsSchema = z.object({ _meta: RequestMetaSchema.optional() });
|
|
566
|
-
/**
|
|
567
|
-
* Common params for any task-augmented request.
|
|
568
|
-
*/
|
|
569
|
-
const TaskAugmentedRequestParamsSchema = BaseRequestParamsSchema.extend({ task: TaskMetadataSchema.optional() });
|
|
570
|
-
const RequestSchema = z.object({
|
|
571
|
-
method: z.string(),
|
|
572
|
-
params: BaseRequestParamsSchema.loose().optional()
|
|
573
|
-
});
|
|
574
|
-
const NotificationsParamsSchema = z.object({ _meta: RequestMetaSchema.optional() });
|
|
575
|
-
const NotificationSchema = z.object({
|
|
576
|
-
method: z.string(),
|
|
577
|
-
params: NotificationsParamsSchema.loose().optional()
|
|
578
|
-
});
|
|
579
|
-
const ResultSchema = z.looseObject({ _meta: RequestMetaSchema.optional() });
|
|
580
|
-
/**
|
|
581
|
-
* A uniquely identifying ID for a request in JSON-RPC.
|
|
582
|
-
*/
|
|
583
|
-
const RequestIdSchema = z.union([z.string(), z.number().int()]);
|
|
584
|
-
/**
|
|
585
|
-
* A request that expects a response.
|
|
586
|
-
*/
|
|
587
|
-
const JSONRPCRequestSchema = z.object({
|
|
588
|
-
jsonrpc: z.literal(JSONRPC_VERSION),
|
|
589
|
-
id: RequestIdSchema,
|
|
590
|
-
...RequestSchema.shape
|
|
591
|
-
}).strict();
|
|
592
|
-
/**
|
|
593
|
-
* A notification which does not expect a response.
|
|
594
|
-
*/
|
|
595
|
-
const JSONRPCNotificationSchema = z.object({
|
|
596
|
-
jsonrpc: z.literal(JSONRPC_VERSION),
|
|
597
|
-
...NotificationSchema.shape
|
|
598
|
-
}).strict();
|
|
599
|
-
/**
|
|
600
|
-
* A successful (non-error) response to a request.
|
|
601
|
-
*/
|
|
602
|
-
const JSONRPCResultResponseSchema = z.object({
|
|
603
|
-
jsonrpc: z.literal(JSONRPC_VERSION),
|
|
604
|
-
id: RequestIdSchema,
|
|
605
|
-
result: ResultSchema
|
|
606
|
-
}).strict();
|
|
607
|
-
/**
|
|
608
|
-
* A response to a request that indicates an error occurred.
|
|
609
|
-
*/
|
|
610
|
-
const JSONRPCErrorResponseSchema = z.object({
|
|
611
|
-
jsonrpc: z.literal(JSONRPC_VERSION),
|
|
612
|
-
id: RequestIdSchema.optional(),
|
|
613
|
-
error: z.object({
|
|
614
|
-
code: z.number().int(),
|
|
615
|
-
message: z.string(),
|
|
616
|
-
data: z.unknown().optional()
|
|
617
|
-
})
|
|
618
|
-
}).strict();
|
|
619
|
-
const JSONRPCMessageSchema = z.union([
|
|
620
|
-
JSONRPCRequestSchema,
|
|
621
|
-
JSONRPCNotificationSchema,
|
|
622
|
-
JSONRPCResultResponseSchema,
|
|
623
|
-
JSONRPCErrorResponseSchema
|
|
624
|
-
]);
|
|
625
|
-
const JSONRPCResponseSchema = z.union([JSONRPCResultResponseSchema, JSONRPCErrorResponseSchema]);
|
|
626
|
-
/**
|
|
627
|
-
* A response that indicates success but carries no data.
|
|
628
|
-
*/
|
|
629
|
-
const EmptyResultSchema = ResultSchema.strict();
|
|
630
|
-
const CancelledNotificationParamsSchema = NotificationsParamsSchema.extend({
|
|
631
|
-
requestId: RequestIdSchema.optional(),
|
|
632
|
-
reason: z.string().optional()
|
|
633
|
-
});
|
|
634
|
-
/**
|
|
635
|
-
* This notification can be sent by either side to indicate that it is cancelling a previously-issued request.
|
|
636
|
-
*
|
|
637
|
-
* The request SHOULD still be in-flight, but due to communication latency, it is always possible that this notification MAY arrive after the request has already finished.
|
|
638
|
-
*
|
|
639
|
-
* This notification indicates that the result will be unused, so any associated processing SHOULD cease.
|
|
640
|
-
*
|
|
641
|
-
* A client MUST NOT attempt to cancel its {@linkcode InitializeRequest | initialize} request.
|
|
642
|
-
*/
|
|
643
|
-
const CancelledNotificationSchema = NotificationSchema.extend({
|
|
644
|
-
method: z.literal("notifications/cancelled"),
|
|
645
|
-
params: CancelledNotificationParamsSchema
|
|
646
|
-
});
|
|
647
|
-
/**
|
|
648
|
-
* Icon schema for use in {@link Tool | tools}, {@link Prompt | prompts}, {@link Resource | resources}, and {@link Implementation | implementations}.
|
|
649
|
-
*/
|
|
650
|
-
const IconSchema = z.object({
|
|
651
|
-
src: z.string(),
|
|
652
|
-
mimeType: z.string().optional(),
|
|
653
|
-
sizes: z.array(z.string()).optional(),
|
|
654
|
-
theme: z.enum(["light", "dark"]).optional()
|
|
655
|
-
});
|
|
656
|
-
/**
|
|
657
|
-
* Base schema to add `icons` property.
|
|
658
|
-
*
|
|
659
|
-
*/
|
|
660
|
-
const IconsSchema = z.object({ icons: z.array(IconSchema).optional() });
|
|
661
|
-
/**
|
|
662
|
-
* Base metadata interface for common properties across {@link Resource | resources}, {@link Tool | tools}, {@link Prompt | prompts}, and {@link Implementation | implementations}.
|
|
663
|
-
*/
|
|
664
|
-
const BaseMetadataSchema = z.object({
|
|
665
|
-
name: z.string(),
|
|
666
|
-
title: z.string().optional()
|
|
667
|
-
});
|
|
668
|
-
/**
|
|
669
|
-
* Describes the name and version of an MCP implementation.
|
|
670
|
-
*/
|
|
671
|
-
const ImplementationSchema = BaseMetadataSchema.extend({
|
|
672
|
-
...BaseMetadataSchema.shape,
|
|
673
|
-
...IconsSchema.shape,
|
|
674
|
-
version: z.string(),
|
|
675
|
-
websiteUrl: z.string().optional(),
|
|
676
|
-
description: z.string().optional()
|
|
677
|
-
});
|
|
678
|
-
const FormElicitationCapabilitySchema = z.intersection(z.object({ applyDefaults: z.boolean().optional() }), JSONObjectSchema);
|
|
679
|
-
const ElicitationCapabilitySchema = z.preprocess((value) => {
|
|
680
|
-
if (value && typeof value === "object" && !Array.isArray(value) && Object.keys(value).length === 0) return { form: {} };
|
|
681
|
-
return value;
|
|
682
|
-
}, z.intersection(z.object({
|
|
683
|
-
form: FormElicitationCapabilitySchema.optional(),
|
|
684
|
-
url: JSONObjectSchema.optional()
|
|
685
|
-
}), JSONObjectSchema.optional()));
|
|
686
|
-
/**
|
|
687
|
-
* Task capabilities for clients, indicating which request types support task creation.
|
|
688
|
-
*/
|
|
689
|
-
const ClientTasksCapabilitySchema = z.looseObject({
|
|
690
|
-
list: JSONObjectSchema.optional(),
|
|
691
|
-
cancel: JSONObjectSchema.optional(),
|
|
692
|
-
requests: z.looseObject({
|
|
693
|
-
sampling: z.looseObject({ createMessage: JSONObjectSchema.optional() }).optional(),
|
|
694
|
-
elicitation: z.looseObject({ create: JSONObjectSchema.optional() }).optional()
|
|
695
|
-
}).optional()
|
|
696
|
-
});
|
|
697
|
-
/**
|
|
698
|
-
* Task capabilities for servers, indicating which request types support task creation.
|
|
699
|
-
*/
|
|
700
|
-
const ServerTasksCapabilitySchema = z.looseObject({
|
|
701
|
-
list: JSONObjectSchema.optional(),
|
|
702
|
-
cancel: JSONObjectSchema.optional(),
|
|
703
|
-
requests: z.looseObject({ tools: z.looseObject({ call: JSONObjectSchema.optional() }).optional() }).optional()
|
|
704
|
-
});
|
|
705
|
-
/**
|
|
706
|
-
* Capabilities a client may support. Known capabilities are defined here, in this schema, but this is not a closed set: any client can define its own, additional capabilities.
|
|
707
|
-
*/
|
|
708
|
-
const ClientCapabilitiesSchema = z.object({
|
|
709
|
-
experimental: z.record(z.string(), JSONObjectSchema).optional(),
|
|
710
|
-
sampling: z.object({
|
|
711
|
-
context: JSONObjectSchema.optional(),
|
|
712
|
-
tools: JSONObjectSchema.optional()
|
|
713
|
-
}).optional(),
|
|
714
|
-
elicitation: ElicitationCapabilitySchema.optional(),
|
|
715
|
-
roots: z.object({ listChanged: z.boolean().optional() }).optional(),
|
|
716
|
-
tasks: ClientTasksCapabilitySchema.optional(),
|
|
717
|
-
extensions: z.record(z.string(), JSONObjectSchema).optional()
|
|
718
|
-
});
|
|
719
|
-
const InitializeRequestParamsSchema = BaseRequestParamsSchema.extend({
|
|
720
|
-
protocolVersion: z.string(),
|
|
721
|
-
capabilities: ClientCapabilitiesSchema,
|
|
722
|
-
clientInfo: ImplementationSchema
|
|
723
|
-
});
|
|
724
|
-
/**
|
|
725
|
-
* This request is sent from the client to the server when it first connects, asking it to begin initialization.
|
|
726
|
-
*/
|
|
727
|
-
const InitializeRequestSchema = RequestSchema.extend({
|
|
728
|
-
method: z.literal("initialize"),
|
|
729
|
-
params: InitializeRequestParamsSchema
|
|
730
|
-
});
|
|
731
|
-
/**
|
|
732
|
-
* Capabilities that a server may support. Known capabilities are defined here, in this schema, but this is not a closed set: any server can define its own, additional capabilities.
|
|
733
|
-
*/
|
|
734
|
-
const ServerCapabilitiesSchema = z.object({
|
|
735
|
-
experimental: z.record(z.string(), JSONObjectSchema).optional(),
|
|
736
|
-
logging: JSONObjectSchema.optional(),
|
|
737
|
-
completions: JSONObjectSchema.optional(),
|
|
738
|
-
prompts: z.object({ listChanged: z.boolean().optional() }).optional(),
|
|
739
|
-
resources: z.object({
|
|
740
|
-
subscribe: z.boolean().optional(),
|
|
741
|
-
listChanged: z.boolean().optional()
|
|
742
|
-
}).optional(),
|
|
743
|
-
tools: z.object({ listChanged: z.boolean().optional() }).optional(),
|
|
744
|
-
tasks: ServerTasksCapabilitySchema.optional(),
|
|
745
|
-
extensions: z.record(z.string(), JSONObjectSchema).optional()
|
|
746
|
-
});
|
|
747
|
-
/**
|
|
748
|
-
* After receiving an initialize request from the client, the server sends this response.
|
|
749
|
-
*/
|
|
750
|
-
const InitializeResultSchema = ResultSchema.extend({
|
|
751
|
-
protocolVersion: z.string(),
|
|
752
|
-
capabilities: ServerCapabilitiesSchema,
|
|
753
|
-
serverInfo: ImplementationSchema,
|
|
754
|
-
instructions: z.string().optional()
|
|
755
|
-
});
|
|
756
|
-
/**
|
|
757
|
-
* This notification is sent from the client to the server after initialization has finished.
|
|
758
|
-
*/
|
|
759
|
-
const InitializedNotificationSchema = NotificationSchema.extend({
|
|
760
|
-
method: z.literal("notifications/initialized"),
|
|
761
|
-
params: NotificationsParamsSchema.optional()
|
|
762
|
-
});
|
|
763
|
-
/**
|
|
764
|
-
* A ping, issued by either the server or the client, to check that the other party is still alive. The receiver must promptly respond, or else may be disconnected.
|
|
765
|
-
*/
|
|
766
|
-
const PingRequestSchema = RequestSchema.extend({
|
|
767
|
-
method: z.literal("ping"),
|
|
768
|
-
params: BaseRequestParamsSchema.optional()
|
|
769
|
-
});
|
|
770
|
-
const ProgressSchema = z.object({
|
|
771
|
-
progress: z.number(),
|
|
772
|
-
total: z.optional(z.number()),
|
|
773
|
-
message: z.optional(z.string())
|
|
774
|
-
});
|
|
775
|
-
const ProgressNotificationParamsSchema = z.object({
|
|
776
|
-
...NotificationsParamsSchema.shape,
|
|
777
|
-
...ProgressSchema.shape,
|
|
778
|
-
progressToken: ProgressTokenSchema
|
|
779
|
-
});
|
|
780
|
-
/**
|
|
781
|
-
* An out-of-band notification used to inform the receiver of a progress update for a long-running request.
|
|
782
|
-
*
|
|
783
|
-
* @category notifications/progress
|
|
784
|
-
*/
|
|
785
|
-
const ProgressNotificationSchema = NotificationSchema.extend({
|
|
786
|
-
method: z.literal("notifications/progress"),
|
|
787
|
-
params: ProgressNotificationParamsSchema
|
|
788
|
-
});
|
|
789
|
-
const PaginatedRequestParamsSchema = BaseRequestParamsSchema.extend({ cursor: CursorSchema.optional() });
|
|
790
|
-
const PaginatedRequestSchema = RequestSchema.extend({ params: PaginatedRequestParamsSchema.optional() });
|
|
791
|
-
const PaginatedResultSchema = ResultSchema.extend({ nextCursor: CursorSchema.optional() });
|
|
792
|
-
/**
|
|
793
|
-
* The status of a task.
|
|
794
|
-
* */
|
|
795
|
-
const TaskStatusSchema = z.enum([
|
|
796
|
-
"working",
|
|
797
|
-
"input_required",
|
|
798
|
-
"completed",
|
|
799
|
-
"failed",
|
|
800
|
-
"cancelled"
|
|
801
|
-
]);
|
|
802
|
-
/**
|
|
803
|
-
* A pollable state object associated with a request.
|
|
804
|
-
*/
|
|
805
|
-
const TaskSchema = z.object({
|
|
806
|
-
taskId: z.string(),
|
|
807
|
-
status: TaskStatusSchema,
|
|
808
|
-
ttl: z.union([z.number(), z.null()]),
|
|
809
|
-
createdAt: z.string(),
|
|
810
|
-
lastUpdatedAt: z.string(),
|
|
811
|
-
pollInterval: z.optional(z.number()),
|
|
812
|
-
statusMessage: z.optional(z.string())
|
|
813
|
-
});
|
|
814
|
-
/**
|
|
815
|
-
* Result returned when a task is created, containing the task data wrapped in a `task` field.
|
|
816
|
-
*/
|
|
817
|
-
const CreateTaskResultSchema = ResultSchema.extend({ task: TaskSchema });
|
|
818
|
-
/**
|
|
819
|
-
* Parameters for task status notification.
|
|
820
|
-
*/
|
|
821
|
-
const TaskStatusNotificationParamsSchema = NotificationsParamsSchema.merge(TaskSchema);
|
|
822
|
-
/**
|
|
823
|
-
* A notification sent when a task's status changes.
|
|
824
|
-
*/
|
|
825
|
-
const TaskStatusNotificationSchema = NotificationSchema.extend({
|
|
826
|
-
method: z.literal("notifications/tasks/status"),
|
|
827
|
-
params: TaskStatusNotificationParamsSchema
|
|
828
|
-
});
|
|
829
|
-
/**
|
|
830
|
-
* A request to get the state of a specific task.
|
|
831
|
-
*/
|
|
832
|
-
const GetTaskRequestSchema = RequestSchema.extend({
|
|
833
|
-
method: z.literal("tasks/get"),
|
|
834
|
-
params: BaseRequestParamsSchema.extend({ taskId: z.string() })
|
|
835
|
-
});
|
|
836
|
-
/**
|
|
837
|
-
* The response to a {@linkcode GetTaskRequest | tasks/get} request.
|
|
838
|
-
*/
|
|
839
|
-
const GetTaskResultSchema = ResultSchema.merge(TaskSchema);
|
|
840
|
-
/**
|
|
841
|
-
* A request to get the result of a specific task.
|
|
842
|
-
*/
|
|
843
|
-
const GetTaskPayloadRequestSchema = RequestSchema.extend({
|
|
844
|
-
method: z.literal("tasks/result"),
|
|
845
|
-
params: BaseRequestParamsSchema.extend({ taskId: z.string() })
|
|
846
|
-
});
|
|
847
|
-
/**
|
|
848
|
-
* The response to a `tasks/result` request.
|
|
849
|
-
* The structure matches the result type of the original request.
|
|
850
|
-
* For example, a {@linkcode CallToolRequest | tools/call} task would return the `CallToolResult` structure.
|
|
851
|
-
*
|
|
852
|
-
*/
|
|
853
|
-
const GetTaskPayloadResultSchema = ResultSchema.loose();
|
|
854
|
-
/**
|
|
855
|
-
* A request to list tasks.
|
|
856
|
-
*/
|
|
857
|
-
const ListTasksRequestSchema = PaginatedRequestSchema.extend({ method: z.literal("tasks/list") });
|
|
858
|
-
/**
|
|
859
|
-
* The response to a {@linkcode ListTasksRequest | tasks/list} request.
|
|
860
|
-
*/
|
|
861
|
-
const ListTasksResultSchema = PaginatedResultSchema.extend({ tasks: z.array(TaskSchema) });
|
|
862
|
-
/**
|
|
863
|
-
* A request to cancel a specific task.
|
|
864
|
-
*/
|
|
865
|
-
const CancelTaskRequestSchema = RequestSchema.extend({
|
|
866
|
-
method: z.literal("tasks/cancel"),
|
|
867
|
-
params: BaseRequestParamsSchema.extend({ taskId: z.string() })
|
|
868
|
-
});
|
|
869
|
-
/**
|
|
870
|
-
* The response to a {@linkcode CancelTaskRequest | tasks/cancel} request.
|
|
871
|
-
*/
|
|
872
|
-
const CancelTaskResultSchema = ResultSchema.merge(TaskSchema);
|
|
873
|
-
/**
|
|
874
|
-
* The contents of a specific resource or sub-resource.
|
|
875
|
-
*/
|
|
876
|
-
const ResourceContentsSchema = z.object({
|
|
877
|
-
uri: z.string(),
|
|
878
|
-
mimeType: z.optional(z.string()),
|
|
879
|
-
_meta: z.record(z.string(), z.unknown()).optional()
|
|
880
|
-
});
|
|
881
|
-
const TextResourceContentsSchema = ResourceContentsSchema.extend({ text: z.string() });
|
|
882
|
-
/**
|
|
883
|
-
* A Zod schema for validating Base64 strings that is more performant and
|
|
884
|
-
* robust for very large inputs than the default regex-based check. It avoids
|
|
885
|
-
* stack overflows by using the native `atob` function for validation.
|
|
886
|
-
*/
|
|
887
|
-
const Base64Schema = z.string().refine((val) => {
|
|
888
|
-
try {
|
|
889
|
-
atob(val);
|
|
890
|
-
return true;
|
|
891
|
-
} catch {
|
|
892
|
-
return false;
|
|
893
|
-
}
|
|
894
|
-
}, { message: "Invalid Base64 string" });
|
|
895
|
-
const BlobResourceContentsSchema = ResourceContentsSchema.extend({ blob: Base64Schema });
|
|
896
|
-
/**
|
|
897
|
-
* The sender or recipient of messages and data in a conversation.
|
|
898
|
-
*/
|
|
899
|
-
const RoleSchema = z.enum(["user", "assistant"]);
|
|
900
|
-
/**
|
|
901
|
-
* Optional annotations providing clients additional context about a resource.
|
|
902
|
-
*/
|
|
903
|
-
const AnnotationsSchema = z.object({
|
|
904
|
-
audience: z.array(RoleSchema).optional(),
|
|
905
|
-
priority: z.number().min(0).max(1).optional(),
|
|
906
|
-
lastModified: z.iso.datetime({ offset: true }).optional()
|
|
907
|
-
});
|
|
908
|
-
/**
|
|
909
|
-
* A known resource that the server is capable of reading.
|
|
910
|
-
*/
|
|
911
|
-
const ResourceSchema = z.object({
|
|
912
|
-
...BaseMetadataSchema.shape,
|
|
913
|
-
...IconsSchema.shape,
|
|
914
|
-
uri: z.string(),
|
|
915
|
-
description: z.optional(z.string()),
|
|
916
|
-
mimeType: z.optional(z.string()),
|
|
917
|
-
size: z.optional(z.number()),
|
|
918
|
-
annotations: AnnotationsSchema.optional(),
|
|
919
|
-
_meta: z.optional(z.looseObject({}))
|
|
920
|
-
});
|
|
921
|
-
/**
|
|
922
|
-
* A template description for resources available on the server.
|
|
923
|
-
*/
|
|
924
|
-
const ResourceTemplateSchema = z.object({
|
|
925
|
-
...BaseMetadataSchema.shape,
|
|
926
|
-
...IconsSchema.shape,
|
|
927
|
-
uriTemplate: z.string(),
|
|
928
|
-
description: z.optional(z.string()),
|
|
929
|
-
mimeType: z.optional(z.string()),
|
|
930
|
-
annotations: AnnotationsSchema.optional(),
|
|
931
|
-
_meta: z.optional(z.looseObject({}))
|
|
932
|
-
});
|
|
933
|
-
/**
|
|
934
|
-
* Sent from the client to request a list of resources the server has.
|
|
935
|
-
*/
|
|
936
|
-
const ListResourcesRequestSchema = PaginatedRequestSchema.extend({ method: z.literal("resources/list") });
|
|
937
|
-
/**
|
|
938
|
-
* The server's response to a {@linkcode ListResourcesRequest | resources/list} request from the client.
|
|
939
|
-
*/
|
|
940
|
-
const ListResourcesResultSchema = PaginatedResultSchema.extend({ resources: z.array(ResourceSchema) });
|
|
941
|
-
/**
|
|
942
|
-
* Sent from the client to request a list of resource templates the server has.
|
|
943
|
-
*/
|
|
944
|
-
const ListResourceTemplatesRequestSchema = PaginatedRequestSchema.extend({ method: z.literal("resources/templates/list") });
|
|
945
|
-
/**
|
|
946
|
-
* The server's response to a {@linkcode ListResourceTemplatesRequest | resources/templates/list} request from the client.
|
|
947
|
-
*/
|
|
948
|
-
const ListResourceTemplatesResultSchema = PaginatedResultSchema.extend({ resourceTemplates: z.array(ResourceTemplateSchema) });
|
|
949
|
-
const ResourceRequestParamsSchema = BaseRequestParamsSchema.extend({ uri: z.string() });
|
|
950
|
-
/**
|
|
951
|
-
* Parameters for a {@linkcode ReadResourceRequest | resources/read} request.
|
|
952
|
-
*/
|
|
953
|
-
const ReadResourceRequestParamsSchema = ResourceRequestParamsSchema;
|
|
954
|
-
/**
|
|
955
|
-
* Sent from the client to the server, to read a specific resource URI.
|
|
956
|
-
*/
|
|
957
|
-
const ReadResourceRequestSchema = RequestSchema.extend({
|
|
958
|
-
method: z.literal("resources/read"),
|
|
959
|
-
params: ReadResourceRequestParamsSchema
|
|
960
|
-
});
|
|
961
|
-
/**
|
|
962
|
-
* The server's response to a {@linkcode ReadResourceRequest | resources/read} request from the client.
|
|
963
|
-
*/
|
|
964
|
-
const ReadResourceResultSchema = ResultSchema.extend({ contents: z.array(z.union([TextResourceContentsSchema, BlobResourceContentsSchema])) });
|
|
965
|
-
/**
|
|
966
|
-
* An optional notification from the server to the client, informing it that the list of resources it can read from has changed. This may be issued by servers without any previous subscription from the client.
|
|
967
|
-
*/
|
|
968
|
-
const ResourceListChangedNotificationSchema = NotificationSchema.extend({
|
|
969
|
-
method: z.literal("notifications/resources/list_changed"),
|
|
970
|
-
params: NotificationsParamsSchema.optional()
|
|
971
|
-
});
|
|
972
|
-
const SubscribeRequestParamsSchema = ResourceRequestParamsSchema;
|
|
973
|
-
/**
|
|
974
|
-
* Sent from the client to request `resources/updated` notifications from the server whenever a particular resource changes.
|
|
975
|
-
*/
|
|
976
|
-
const SubscribeRequestSchema = RequestSchema.extend({
|
|
977
|
-
method: z.literal("resources/subscribe"),
|
|
978
|
-
params: SubscribeRequestParamsSchema
|
|
979
|
-
});
|
|
980
|
-
const UnsubscribeRequestParamsSchema = ResourceRequestParamsSchema;
|
|
981
|
-
/**
|
|
982
|
-
* Sent from the client to request cancellation of {@linkcode ResourceUpdatedNotification | resources/updated} notifications from the server. This should follow a previous {@linkcode SubscribeRequest | resources/subscribe} request.
|
|
983
|
-
*/
|
|
984
|
-
const UnsubscribeRequestSchema = RequestSchema.extend({
|
|
985
|
-
method: z.literal("resources/unsubscribe"),
|
|
986
|
-
params: UnsubscribeRequestParamsSchema
|
|
987
|
-
});
|
|
988
|
-
/**
|
|
989
|
-
* Parameters for a {@linkcode ResourceUpdatedNotification | notifications/resources/updated} notification.
|
|
990
|
-
*/
|
|
991
|
-
const ResourceUpdatedNotificationParamsSchema = NotificationsParamsSchema.extend({ uri: z.string() });
|
|
992
|
-
/**
|
|
993
|
-
* A notification from the server to the client, informing it that a resource has changed and may need to be read again. This should only be sent if the client previously sent a {@linkcode SubscribeRequest | resources/subscribe} request.
|
|
994
|
-
*/
|
|
995
|
-
const ResourceUpdatedNotificationSchema = NotificationSchema.extend({
|
|
996
|
-
method: z.literal("notifications/resources/updated"),
|
|
997
|
-
params: ResourceUpdatedNotificationParamsSchema
|
|
998
|
-
});
|
|
999
|
-
/**
|
|
1000
|
-
* Describes an argument that a prompt can accept.
|
|
1001
|
-
*/
|
|
1002
|
-
const PromptArgumentSchema = z.object({
|
|
1003
|
-
name: z.string(),
|
|
1004
|
-
description: z.optional(z.string()),
|
|
1005
|
-
required: z.optional(z.boolean())
|
|
1006
|
-
});
|
|
1007
|
-
/**
|
|
1008
|
-
* A prompt or prompt template that the server offers.
|
|
1009
|
-
*/
|
|
1010
|
-
const PromptSchema = z.object({
|
|
1011
|
-
...BaseMetadataSchema.shape,
|
|
1012
|
-
...IconsSchema.shape,
|
|
1013
|
-
description: z.optional(z.string()),
|
|
1014
|
-
arguments: z.optional(z.array(PromptArgumentSchema)),
|
|
1015
|
-
_meta: z.optional(z.looseObject({}))
|
|
1016
|
-
});
|
|
1017
|
-
/**
|
|
1018
|
-
* Sent from the client to request a list of prompts and prompt templates the server has.
|
|
1019
|
-
*/
|
|
1020
|
-
const ListPromptsRequestSchema = PaginatedRequestSchema.extend({ method: z.literal("prompts/list") });
|
|
1021
|
-
/**
|
|
1022
|
-
* The server's response to a {@linkcode ListPromptsRequest | prompts/list} request from the client.
|
|
1023
|
-
*/
|
|
1024
|
-
const ListPromptsResultSchema = PaginatedResultSchema.extend({ prompts: z.array(PromptSchema) });
|
|
1025
|
-
/**
|
|
1026
|
-
* Parameters for a {@linkcode GetPromptRequest | prompts/get} request.
|
|
1027
|
-
*/
|
|
1028
|
-
const GetPromptRequestParamsSchema = BaseRequestParamsSchema.extend({
|
|
1029
|
-
name: z.string(),
|
|
1030
|
-
arguments: z.record(z.string(), z.string()).optional()
|
|
1031
|
-
});
|
|
1032
|
-
/**
|
|
1033
|
-
* Used by the client to get a prompt provided by the server.
|
|
1034
|
-
*/
|
|
1035
|
-
const GetPromptRequestSchema = RequestSchema.extend({
|
|
1036
|
-
method: z.literal("prompts/get"),
|
|
1037
|
-
params: GetPromptRequestParamsSchema
|
|
1038
|
-
});
|
|
1039
|
-
/**
|
|
1040
|
-
* Text provided to or from an LLM.
|
|
1041
|
-
*/
|
|
1042
|
-
const TextContentSchema = z.object({
|
|
1043
|
-
type: z.literal("text"),
|
|
1044
|
-
text: z.string(),
|
|
1045
|
-
annotations: AnnotationsSchema.optional(),
|
|
1046
|
-
_meta: z.record(z.string(), z.unknown()).optional()
|
|
1047
|
-
});
|
|
1048
|
-
/**
|
|
1049
|
-
* An image provided to or from an LLM.
|
|
1050
|
-
*/
|
|
1051
|
-
const ImageContentSchema = z.object({
|
|
1052
|
-
type: z.literal("image"),
|
|
1053
|
-
data: Base64Schema,
|
|
1054
|
-
mimeType: z.string(),
|
|
1055
|
-
annotations: AnnotationsSchema.optional(),
|
|
1056
|
-
_meta: z.record(z.string(), z.unknown()).optional()
|
|
1057
|
-
});
|
|
1058
|
-
/**
|
|
1059
|
-
* Audio content provided to or from an LLM.
|
|
1060
|
-
*/
|
|
1061
|
-
const AudioContentSchema = z.object({
|
|
1062
|
-
type: z.literal("audio"),
|
|
1063
|
-
data: Base64Schema,
|
|
1064
|
-
mimeType: z.string(),
|
|
1065
|
-
annotations: AnnotationsSchema.optional(),
|
|
1066
|
-
_meta: z.record(z.string(), z.unknown()).optional()
|
|
1067
|
-
});
|
|
1068
|
-
/**
|
|
1069
|
-
* A tool call request from an assistant (LLM).
|
|
1070
|
-
* Represents the assistant's request to use a tool.
|
|
1071
|
-
*/
|
|
1072
|
-
const ToolUseContentSchema = z.object({
|
|
1073
|
-
type: z.literal("tool_use"),
|
|
1074
|
-
name: z.string(),
|
|
1075
|
-
id: z.string(),
|
|
1076
|
-
input: z.record(z.string(), z.unknown()),
|
|
1077
|
-
_meta: z.record(z.string(), z.unknown()).optional()
|
|
1078
|
-
});
|
|
1079
|
-
/**
|
|
1080
|
-
* The contents of a resource, embedded into a prompt or tool call result.
|
|
1081
|
-
*/
|
|
1082
|
-
const EmbeddedResourceSchema = z.object({
|
|
1083
|
-
type: z.literal("resource"),
|
|
1084
|
-
resource: z.union([TextResourceContentsSchema, BlobResourceContentsSchema]),
|
|
1085
|
-
annotations: AnnotationsSchema.optional(),
|
|
1086
|
-
_meta: z.record(z.string(), z.unknown()).optional()
|
|
1087
|
-
});
|
|
1088
|
-
/**
|
|
1089
|
-
* A resource that the server is capable of reading, included in a prompt or tool call result.
|
|
1090
|
-
*
|
|
1091
|
-
* Note: resource links returned by tools are not guaranteed to appear in the results of {@linkcode ListResourcesRequest | resources/list} requests.
|
|
1092
|
-
*/
|
|
1093
|
-
const ResourceLinkSchema = ResourceSchema.extend({ type: z.literal("resource_link") });
|
|
1094
|
-
/**
|
|
1095
|
-
* A content block that can be used in prompts and tool results.
|
|
1096
|
-
*/
|
|
1097
|
-
const ContentBlockSchema = z.union([
|
|
1098
|
-
TextContentSchema,
|
|
1099
|
-
ImageContentSchema,
|
|
1100
|
-
AudioContentSchema,
|
|
1101
|
-
ResourceLinkSchema,
|
|
1102
|
-
EmbeddedResourceSchema
|
|
1103
|
-
]);
|
|
1104
|
-
/**
|
|
1105
|
-
* Describes a message returned as part of a prompt.
|
|
1106
|
-
*/
|
|
1107
|
-
const PromptMessageSchema = z.object({
|
|
1108
|
-
role: RoleSchema,
|
|
1109
|
-
content: ContentBlockSchema
|
|
1110
|
-
});
|
|
1111
|
-
/**
|
|
1112
|
-
* The server's response to a {@linkcode GetPromptRequest | prompts/get} request from the client.
|
|
1113
|
-
*/
|
|
1114
|
-
const GetPromptResultSchema = ResultSchema.extend({
|
|
1115
|
-
description: z.string().optional(),
|
|
1116
|
-
messages: z.array(PromptMessageSchema)
|
|
1117
|
-
});
|
|
1118
|
-
/**
|
|
1119
|
-
* An optional notification from the server to the client, informing it that the list of prompts it offers has changed. This may be issued by servers without any previous subscription from the client.
|
|
1120
|
-
*/
|
|
1121
|
-
const PromptListChangedNotificationSchema = NotificationSchema.extend({
|
|
1122
|
-
method: z.literal("notifications/prompts/list_changed"),
|
|
1123
|
-
params: NotificationsParamsSchema.optional()
|
|
1124
|
-
});
|
|
1125
|
-
/**
|
|
1126
|
-
* Additional properties describing a `Tool` to clients.
|
|
1127
|
-
*
|
|
1128
|
-
* NOTE: all properties in {@linkcode ToolAnnotations} are **hints**.
|
|
1129
|
-
* They are not guaranteed to provide a faithful description of
|
|
1130
|
-
* tool behavior (including descriptive properties like `title`).
|
|
1131
|
-
*
|
|
1132
|
-
* Clients should never make tool use decisions based on `ToolAnnotations`
|
|
1133
|
-
* received from untrusted servers.
|
|
1134
|
-
*/
|
|
1135
|
-
const ToolAnnotationsSchema = z.object({
|
|
1136
|
-
title: z.string().optional(),
|
|
1137
|
-
readOnlyHint: z.boolean().optional(),
|
|
1138
|
-
destructiveHint: z.boolean().optional(),
|
|
1139
|
-
idempotentHint: z.boolean().optional(),
|
|
1140
|
-
openWorldHint: z.boolean().optional()
|
|
1141
|
-
});
|
|
1142
|
-
/**
|
|
1143
|
-
* Execution-related properties for a tool.
|
|
1144
|
-
*/
|
|
1145
|
-
const ToolExecutionSchema = z.object({ taskSupport: z.enum([
|
|
1146
|
-
"required",
|
|
1147
|
-
"optional",
|
|
1148
|
-
"forbidden"
|
|
1149
|
-
]).optional() });
|
|
1150
|
-
/**
|
|
1151
|
-
* Definition for a tool the client can call.
|
|
1152
|
-
*/
|
|
1153
|
-
const ToolSchema = z.object({
|
|
1154
|
-
...BaseMetadataSchema.shape,
|
|
1155
|
-
...IconsSchema.shape,
|
|
1156
|
-
description: z.string().optional(),
|
|
1157
|
-
inputSchema: z.object({
|
|
1158
|
-
type: z.literal("object"),
|
|
1159
|
-
properties: z.record(z.string(), JSONValueSchema).optional(),
|
|
1160
|
-
required: z.array(z.string()).optional()
|
|
1161
|
-
}).catchall(z.unknown()),
|
|
1162
|
-
outputSchema: z.object({
|
|
1163
|
-
type: z.literal("object"),
|
|
1164
|
-
properties: z.record(z.string(), JSONValueSchema).optional(),
|
|
1165
|
-
required: z.array(z.string()).optional()
|
|
1166
|
-
}).catchall(z.unknown()).optional(),
|
|
1167
|
-
annotations: ToolAnnotationsSchema.optional(),
|
|
1168
|
-
execution: ToolExecutionSchema.optional(),
|
|
1169
|
-
_meta: z.record(z.string(), z.unknown()).optional()
|
|
1170
|
-
});
|
|
1171
|
-
/**
|
|
1172
|
-
* Sent from the client to request a list of tools the server has.
|
|
1173
|
-
*/
|
|
1174
|
-
const ListToolsRequestSchema = PaginatedRequestSchema.extend({ method: z.literal("tools/list") });
|
|
1175
|
-
/**
|
|
1176
|
-
* The server's response to a {@linkcode ListToolsRequest | tools/list} request from the client.
|
|
1177
|
-
*/
|
|
1178
|
-
const ListToolsResultSchema = PaginatedResultSchema.extend({ tools: z.array(ToolSchema) });
|
|
1179
|
-
/**
|
|
1180
|
-
* The server's response to a tool call.
|
|
1181
|
-
*/
|
|
1182
|
-
const CallToolResultSchema = ResultSchema.extend({
|
|
1183
|
-
content: z.array(ContentBlockSchema).default([]),
|
|
1184
|
-
structuredContent: z.record(z.string(), z.unknown()).optional(),
|
|
1185
|
-
isError: z.boolean().optional()
|
|
1186
|
-
});
|
|
1187
|
-
/**
|
|
1188
|
-
* {@linkcode CallToolResultSchema} extended with backwards compatibility to protocol version 2024-10-07.
|
|
1189
|
-
*/
|
|
1190
|
-
const CompatibilityCallToolResultSchema = CallToolResultSchema.or(ResultSchema.extend({ toolResult: z.unknown() }));
|
|
1191
|
-
/**
|
|
1192
|
-
* Parameters for a `tools/call` request.
|
|
1193
|
-
*/
|
|
1194
|
-
const CallToolRequestParamsSchema = TaskAugmentedRequestParamsSchema.extend({
|
|
1195
|
-
name: z.string(),
|
|
1196
|
-
arguments: z.record(z.string(), z.unknown()).optional()
|
|
1197
|
-
});
|
|
1198
|
-
/**
|
|
1199
|
-
* Used by the client to invoke a tool provided by the server.
|
|
1200
|
-
*/
|
|
1201
|
-
const CallToolRequestSchema = RequestSchema.extend({
|
|
1202
|
-
method: z.literal("tools/call"),
|
|
1203
|
-
params: CallToolRequestParamsSchema
|
|
1204
|
-
});
|
|
1205
|
-
/**
|
|
1206
|
-
* An optional notification from the server to the client, informing it that the list of tools it offers has changed. This may be issued by servers without any previous subscription from the client.
|
|
1207
|
-
*/
|
|
1208
|
-
const ToolListChangedNotificationSchema = NotificationSchema.extend({
|
|
1209
|
-
method: z.literal("notifications/tools/list_changed"),
|
|
1210
|
-
params: NotificationsParamsSchema.optional()
|
|
1211
|
-
});
|
|
1212
|
-
/**
|
|
1213
|
-
* Base schema for list changed subscription options (without callback).
|
|
1214
|
-
* Used internally for Zod validation of `autoRefresh` and `debounceMs`.
|
|
1215
|
-
*/
|
|
1216
|
-
const ListChangedOptionsBaseSchema = z.object({
|
|
1217
|
-
autoRefresh: z.boolean().default(true),
|
|
1218
|
-
debounceMs: z.number().int().nonnegative().default(300)
|
|
1219
|
-
});
|
|
1220
|
-
/**
|
|
1221
|
-
* The severity of a log message.
|
|
1222
|
-
*/
|
|
1223
|
-
const LoggingLevelSchema = z.enum([
|
|
1224
|
-
"debug",
|
|
1225
|
-
"info",
|
|
1226
|
-
"notice",
|
|
1227
|
-
"warning",
|
|
1228
|
-
"error",
|
|
1229
|
-
"critical",
|
|
1230
|
-
"alert",
|
|
1231
|
-
"emergency"
|
|
1232
|
-
]);
|
|
1233
|
-
/**
|
|
1234
|
-
* Parameters for a `logging/setLevel` request.
|
|
1235
|
-
*/
|
|
1236
|
-
const SetLevelRequestParamsSchema = BaseRequestParamsSchema.extend({ level: LoggingLevelSchema });
|
|
1237
|
-
/**
|
|
1238
|
-
* A request from the client to the server, to enable or adjust logging.
|
|
1239
|
-
*/
|
|
1240
|
-
const SetLevelRequestSchema = RequestSchema.extend({
|
|
1241
|
-
method: z.literal("logging/setLevel"),
|
|
1242
|
-
params: SetLevelRequestParamsSchema
|
|
1243
|
-
});
|
|
1244
|
-
/**
|
|
1245
|
-
* Parameters for a `notifications/message` notification.
|
|
1246
|
-
*/
|
|
1247
|
-
const LoggingMessageNotificationParamsSchema = NotificationsParamsSchema.extend({
|
|
1248
|
-
level: LoggingLevelSchema,
|
|
1249
|
-
logger: z.string().optional(),
|
|
1250
|
-
data: z.unknown()
|
|
1251
|
-
});
|
|
1252
|
-
/**
|
|
1253
|
-
* Notification of a log message passed from server to client. If no `logging/setLevel` request has been sent from the client, the server MAY decide which messages to send automatically.
|
|
1254
|
-
*/
|
|
1255
|
-
const LoggingMessageNotificationSchema = NotificationSchema.extend({
|
|
1256
|
-
method: z.literal("notifications/message"),
|
|
1257
|
-
params: LoggingMessageNotificationParamsSchema
|
|
1258
|
-
});
|
|
1259
|
-
/**
|
|
1260
|
-
* Hints to use for model selection.
|
|
1261
|
-
*/
|
|
1262
|
-
const ModelHintSchema = z.object({ name: z.string().optional() });
|
|
1263
|
-
/**
|
|
1264
|
-
* The server's preferences for model selection, requested of the client during sampling.
|
|
1265
|
-
*/
|
|
1266
|
-
const ModelPreferencesSchema = z.object({
|
|
1267
|
-
hints: z.array(ModelHintSchema).optional(),
|
|
1268
|
-
costPriority: z.number().min(0).max(1).optional(),
|
|
1269
|
-
speedPriority: z.number().min(0).max(1).optional(),
|
|
1270
|
-
intelligencePriority: z.number().min(0).max(1).optional()
|
|
1271
|
-
});
|
|
1272
|
-
/**
|
|
1273
|
-
* Controls tool usage behavior in sampling requests.
|
|
1274
|
-
*/
|
|
1275
|
-
const ToolChoiceSchema = z.object({ mode: z.enum([
|
|
1276
|
-
"auto",
|
|
1277
|
-
"required",
|
|
1278
|
-
"none"
|
|
1279
|
-
]).optional() });
|
|
1280
|
-
/**
|
|
1281
|
-
* The result of a tool execution, provided by the user (server).
|
|
1282
|
-
* Represents the outcome of invoking a tool requested via `ToolUseContent`.
|
|
1283
|
-
*/
|
|
1284
|
-
const ToolResultContentSchema = z.object({
|
|
1285
|
-
type: z.literal("tool_result"),
|
|
1286
|
-
toolUseId: z.string().describe("The unique identifier for the corresponding tool call."),
|
|
1287
|
-
content: z.array(ContentBlockSchema).default([]),
|
|
1288
|
-
structuredContent: z.object({}).loose().optional(),
|
|
1289
|
-
isError: z.boolean().optional(),
|
|
1290
|
-
_meta: z.record(z.string(), z.unknown()).optional()
|
|
1291
|
-
});
|
|
1292
|
-
/**
|
|
1293
|
-
* Basic content types for sampling responses (without tool use).
|
|
1294
|
-
* Used for backwards-compatible {@linkcode CreateMessageResult} when tools are not used.
|
|
1295
|
-
*/
|
|
1296
|
-
const SamplingContentSchema = z.discriminatedUnion("type", [
|
|
1297
|
-
TextContentSchema,
|
|
1298
|
-
ImageContentSchema,
|
|
1299
|
-
AudioContentSchema
|
|
1300
|
-
]);
|
|
1301
|
-
/**
|
|
1302
|
-
* Content block types allowed in sampling messages.
|
|
1303
|
-
* This includes text, image, audio, tool use requests, and tool results.
|
|
1304
|
-
*/
|
|
1305
|
-
const SamplingMessageContentBlockSchema = z.discriminatedUnion("type", [
|
|
1306
|
-
TextContentSchema,
|
|
1307
|
-
ImageContentSchema,
|
|
1308
|
-
AudioContentSchema,
|
|
1309
|
-
ToolUseContentSchema,
|
|
1310
|
-
ToolResultContentSchema
|
|
1311
|
-
]);
|
|
1312
|
-
/**
|
|
1313
|
-
* Describes a message issued to or received from an LLM API.
|
|
1314
|
-
*/
|
|
1315
|
-
const SamplingMessageSchema = z.object({
|
|
1316
|
-
role: RoleSchema,
|
|
1317
|
-
content: z.union([SamplingMessageContentBlockSchema, z.array(SamplingMessageContentBlockSchema)]),
|
|
1318
|
-
_meta: z.record(z.string(), z.unknown()).optional()
|
|
1319
|
-
});
|
|
1320
|
-
/**
|
|
1321
|
-
* Parameters for a `sampling/createMessage` request.
|
|
1322
|
-
*/
|
|
1323
|
-
const CreateMessageRequestParamsSchema = TaskAugmentedRequestParamsSchema.extend({
|
|
1324
|
-
messages: z.array(SamplingMessageSchema),
|
|
1325
|
-
modelPreferences: ModelPreferencesSchema.optional(),
|
|
1326
|
-
systemPrompt: z.string().optional(),
|
|
1327
|
-
includeContext: z.enum([
|
|
1328
|
-
"none",
|
|
1329
|
-
"thisServer",
|
|
1330
|
-
"allServers"
|
|
1331
|
-
]).optional(),
|
|
1332
|
-
temperature: z.number().optional(),
|
|
1333
|
-
maxTokens: z.number().int(),
|
|
1334
|
-
stopSequences: z.array(z.string()).optional(),
|
|
1335
|
-
metadata: JSONObjectSchema.optional(),
|
|
1336
|
-
tools: z.array(ToolSchema).optional(),
|
|
1337
|
-
toolChoice: ToolChoiceSchema.optional()
|
|
1338
|
-
});
|
|
1339
|
-
/**
|
|
1340
|
-
* A request from the server to sample an LLM via the client. The client has full discretion over which model to select. The client should also inform the user before beginning sampling, to allow them to inspect the request (human in the loop) and decide whether to approve it.
|
|
1341
|
-
*/
|
|
1342
|
-
const CreateMessageRequestSchema = RequestSchema.extend({
|
|
1343
|
-
method: z.literal("sampling/createMessage"),
|
|
1344
|
-
params: CreateMessageRequestParamsSchema
|
|
1345
|
-
});
|
|
1346
|
-
/**
|
|
1347
|
-
* The client's response to a `sampling/create_message` request from the server.
|
|
1348
|
-
* This is the backwards-compatible version that returns single content (no arrays).
|
|
1349
|
-
* Used when the request does not include tools.
|
|
1350
|
-
*/
|
|
1351
|
-
const CreateMessageResultSchema = ResultSchema.extend({
|
|
1352
|
-
model: z.string(),
|
|
1353
|
-
stopReason: z.optional(z.enum([
|
|
1354
|
-
"endTurn",
|
|
1355
|
-
"stopSequence",
|
|
1356
|
-
"maxTokens"
|
|
1357
|
-
]).or(z.string())),
|
|
1358
|
-
role: RoleSchema,
|
|
1359
|
-
content: SamplingContentSchema
|
|
1360
|
-
});
|
|
1361
|
-
/**
|
|
1362
|
-
* The client's response to a `sampling/create_message` request when tools were provided.
|
|
1363
|
-
* This version supports array content for tool use flows.
|
|
1364
|
-
*/
|
|
1365
|
-
const CreateMessageResultWithToolsSchema = ResultSchema.extend({
|
|
1366
|
-
model: z.string(),
|
|
1367
|
-
stopReason: z.optional(z.enum([
|
|
1368
|
-
"endTurn",
|
|
1369
|
-
"stopSequence",
|
|
1370
|
-
"maxTokens",
|
|
1371
|
-
"toolUse"
|
|
1372
|
-
]).or(z.string())),
|
|
1373
|
-
role: RoleSchema,
|
|
1374
|
-
content: z.union([SamplingMessageContentBlockSchema, z.array(SamplingMessageContentBlockSchema)])
|
|
1375
|
-
});
|
|
1376
|
-
/**
|
|
1377
|
-
* Primitive schema definition for boolean fields.
|
|
1378
|
-
*/
|
|
1379
|
-
const BooleanSchemaSchema = z.object({
|
|
1380
|
-
type: z.literal("boolean"),
|
|
1381
|
-
title: z.string().optional(),
|
|
1382
|
-
description: z.string().optional(),
|
|
1383
|
-
default: z.boolean().optional()
|
|
1384
|
-
});
|
|
1385
|
-
/**
|
|
1386
|
-
* Primitive schema definition for string fields.
|
|
1387
|
-
*/
|
|
1388
|
-
const StringSchemaSchema = z.object({
|
|
1389
|
-
type: z.literal("string"),
|
|
1390
|
-
title: z.string().optional(),
|
|
1391
|
-
description: z.string().optional(),
|
|
1392
|
-
minLength: z.number().optional(),
|
|
1393
|
-
maxLength: z.number().optional(),
|
|
1394
|
-
format: z.enum([
|
|
1395
|
-
"email",
|
|
1396
|
-
"uri",
|
|
1397
|
-
"date",
|
|
1398
|
-
"date-time"
|
|
1399
|
-
]).optional(),
|
|
1400
|
-
default: z.string().optional()
|
|
1401
|
-
});
|
|
1402
|
-
/**
|
|
1403
|
-
* Primitive schema definition for number fields.
|
|
1404
|
-
*/
|
|
1405
|
-
const NumberSchemaSchema = z.object({
|
|
1406
|
-
type: z.enum(["number", "integer"]),
|
|
1407
|
-
title: z.string().optional(),
|
|
1408
|
-
description: z.string().optional(),
|
|
1409
|
-
minimum: z.number().optional(),
|
|
1410
|
-
maximum: z.number().optional(),
|
|
1411
|
-
default: z.number().optional()
|
|
1412
|
-
});
|
|
1413
|
-
/**
|
|
1414
|
-
* Schema for single-selection enumeration without display titles for options.
|
|
1415
|
-
*/
|
|
1416
|
-
const UntitledSingleSelectEnumSchemaSchema = z.object({
|
|
1417
|
-
type: z.literal("string"),
|
|
1418
|
-
title: z.string().optional(),
|
|
1419
|
-
description: z.string().optional(),
|
|
1420
|
-
enum: z.array(z.string()),
|
|
1421
|
-
default: z.string().optional()
|
|
1422
|
-
});
|
|
1423
|
-
/**
|
|
1424
|
-
* Schema for single-selection enumeration with display titles for each option.
|
|
1425
|
-
*/
|
|
1426
|
-
const TitledSingleSelectEnumSchemaSchema = z.object({
|
|
1427
|
-
type: z.literal("string"),
|
|
1428
|
-
title: z.string().optional(),
|
|
1429
|
-
description: z.string().optional(),
|
|
1430
|
-
oneOf: z.array(z.object({
|
|
1431
|
-
const: z.string(),
|
|
1432
|
-
title: z.string()
|
|
1433
|
-
})),
|
|
1434
|
-
default: z.string().optional()
|
|
1435
|
-
});
|
|
1436
|
-
/**
|
|
1437
|
-
* Use {@linkcode TitledSingleSelectEnumSchema} instead.
|
|
1438
|
-
* This interface will be removed in a future version.
|
|
1439
|
-
*/
|
|
1440
|
-
const LegacyTitledEnumSchemaSchema = z.object({
|
|
1441
|
-
type: z.literal("string"),
|
|
1442
|
-
title: z.string().optional(),
|
|
1443
|
-
description: z.string().optional(),
|
|
1444
|
-
enum: z.array(z.string()),
|
|
1445
|
-
enumNames: z.array(z.string()).optional(),
|
|
1446
|
-
default: z.string().optional()
|
|
1447
|
-
});
|
|
1448
|
-
const SingleSelectEnumSchemaSchema = z.union([UntitledSingleSelectEnumSchemaSchema, TitledSingleSelectEnumSchemaSchema]);
|
|
1449
|
-
/**
|
|
1450
|
-
* Schema for multiple-selection enumeration without display titles for options.
|
|
1451
|
-
*/
|
|
1452
|
-
const UntitledMultiSelectEnumSchemaSchema = z.object({
|
|
1453
|
-
type: z.literal("array"),
|
|
1454
|
-
title: z.string().optional(),
|
|
1455
|
-
description: z.string().optional(),
|
|
1456
|
-
minItems: z.number().optional(),
|
|
1457
|
-
maxItems: z.number().optional(),
|
|
1458
|
-
items: z.object({
|
|
1459
|
-
type: z.literal("string"),
|
|
1460
|
-
enum: z.array(z.string())
|
|
1461
|
-
}),
|
|
1462
|
-
default: z.array(z.string()).optional()
|
|
1463
|
-
});
|
|
1464
|
-
/**
|
|
1465
|
-
* Schema for multiple-selection enumeration with display titles for each option.
|
|
1466
|
-
*/
|
|
1467
|
-
const TitledMultiSelectEnumSchemaSchema = z.object({
|
|
1468
|
-
type: z.literal("array"),
|
|
1469
|
-
title: z.string().optional(),
|
|
1470
|
-
description: z.string().optional(),
|
|
1471
|
-
minItems: z.number().optional(),
|
|
1472
|
-
maxItems: z.number().optional(),
|
|
1473
|
-
items: z.object({ anyOf: z.array(z.object({
|
|
1474
|
-
const: z.string(),
|
|
1475
|
-
title: z.string()
|
|
1476
|
-
})) }),
|
|
1477
|
-
default: z.array(z.string()).optional()
|
|
1478
|
-
});
|
|
1479
|
-
/**
|
|
1480
|
-
* Combined schema for multiple-selection enumeration
|
|
1481
|
-
*/
|
|
1482
|
-
const MultiSelectEnumSchemaSchema = z.union([UntitledMultiSelectEnumSchemaSchema, TitledMultiSelectEnumSchemaSchema]);
|
|
1483
|
-
/**
|
|
1484
|
-
* Primitive schema definition for enum fields.
|
|
1485
|
-
*/
|
|
1486
|
-
const EnumSchemaSchema = z.union([
|
|
1487
|
-
LegacyTitledEnumSchemaSchema,
|
|
1488
|
-
SingleSelectEnumSchemaSchema,
|
|
1489
|
-
MultiSelectEnumSchemaSchema
|
|
1490
|
-
]);
|
|
1491
|
-
/**
|
|
1492
|
-
* Union of all primitive schema definitions.
|
|
1493
|
-
*/
|
|
1494
|
-
const PrimitiveSchemaDefinitionSchema = z.union([
|
|
1495
|
-
EnumSchemaSchema,
|
|
1496
|
-
BooleanSchemaSchema,
|
|
1497
|
-
StringSchemaSchema,
|
|
1498
|
-
NumberSchemaSchema
|
|
1499
|
-
]);
|
|
1500
|
-
/**
|
|
1501
|
-
* Parameters for an `elicitation/create` request for form-based elicitation.
|
|
1502
|
-
*/
|
|
1503
|
-
const ElicitRequestFormParamsSchema = TaskAugmentedRequestParamsSchema.extend({
|
|
1504
|
-
mode: z.literal("form").optional(),
|
|
1505
|
-
message: z.string(),
|
|
1506
|
-
requestedSchema: z.object({
|
|
1507
|
-
type: z.literal("object"),
|
|
1508
|
-
properties: z.record(z.string(), PrimitiveSchemaDefinitionSchema),
|
|
1509
|
-
required: z.array(z.string()).optional()
|
|
1510
|
-
})
|
|
1511
|
-
});
|
|
1512
|
-
/**
|
|
1513
|
-
* Parameters for an {@linkcode ElicitRequest | elicitation/create} request for URL-based elicitation.
|
|
1514
|
-
*/
|
|
1515
|
-
const ElicitRequestURLParamsSchema = TaskAugmentedRequestParamsSchema.extend({
|
|
1516
|
-
mode: z.literal("url"),
|
|
1517
|
-
message: z.string(),
|
|
1518
|
-
elicitationId: z.string(),
|
|
1519
|
-
url: z.string().url()
|
|
1520
|
-
});
|
|
1521
|
-
/**
|
|
1522
|
-
* The parameters for a request to elicit additional information from the user via the client.
|
|
1523
|
-
*/
|
|
1524
|
-
const ElicitRequestParamsSchema = z.union([ElicitRequestFormParamsSchema, ElicitRequestURLParamsSchema]);
|
|
1525
|
-
/**
|
|
1526
|
-
* A request from the server to elicit user input via the client.
|
|
1527
|
-
* The client should present the message and form fields to the user (form mode)
|
|
1528
|
-
* or navigate to a URL (URL mode).
|
|
1529
|
-
*/
|
|
1530
|
-
const ElicitRequestSchema = RequestSchema.extend({
|
|
1531
|
-
method: z.literal("elicitation/create"),
|
|
1532
|
-
params: ElicitRequestParamsSchema
|
|
1533
|
-
});
|
|
1534
|
-
/**
|
|
1535
|
-
* Parameters for a {@linkcode ElicitationCompleteNotification | notifications/elicitation/complete} notification.
|
|
1536
|
-
*
|
|
1537
|
-
* @category notifications/elicitation/complete
|
|
1538
|
-
*/
|
|
1539
|
-
const ElicitationCompleteNotificationParamsSchema = NotificationsParamsSchema.extend({ elicitationId: z.string() });
|
|
1540
|
-
/**
|
|
1541
|
-
* A notification from the server to the client, informing it of a completion of an out-of-band elicitation request.
|
|
1542
|
-
*
|
|
1543
|
-
* @category notifications/elicitation/complete
|
|
1544
|
-
*/
|
|
1545
|
-
const ElicitationCompleteNotificationSchema = NotificationSchema.extend({
|
|
1546
|
-
method: z.literal("notifications/elicitation/complete"),
|
|
1547
|
-
params: ElicitationCompleteNotificationParamsSchema
|
|
1548
|
-
});
|
|
1549
|
-
/**
|
|
1550
|
-
* The client's response to an {@linkcode ElicitRequest | elicitation/create} request from the server.
|
|
1551
|
-
*/
|
|
1552
|
-
const ElicitResultSchema = ResultSchema.extend({
|
|
1553
|
-
action: z.enum([
|
|
1554
|
-
"accept",
|
|
1555
|
-
"decline",
|
|
1556
|
-
"cancel"
|
|
1557
|
-
]),
|
|
1558
|
-
content: z.preprocess((val) => val === null ? void 0 : val, z.record(z.string(), z.union([
|
|
1559
|
-
z.string(),
|
|
1560
|
-
z.number(),
|
|
1561
|
-
z.boolean(),
|
|
1562
|
-
z.array(z.string())
|
|
1563
|
-
])).optional())
|
|
1564
|
-
});
|
|
1565
|
-
/**
|
|
1566
|
-
* A reference to a resource or resource template definition.
|
|
1567
|
-
*/
|
|
1568
|
-
const ResourceTemplateReferenceSchema = z.object({
|
|
1569
|
-
type: z.literal("ref/resource"),
|
|
1570
|
-
uri: z.string()
|
|
1571
|
-
});
|
|
1572
|
-
/**
|
|
1573
|
-
* Identifies a prompt.
|
|
1574
|
-
*/
|
|
1575
|
-
const PromptReferenceSchema = z.object({
|
|
1576
|
-
type: z.literal("ref/prompt"),
|
|
1577
|
-
name: z.string()
|
|
1578
|
-
});
|
|
1579
|
-
/**
|
|
1580
|
-
* Parameters for a {@linkcode CompleteRequest | completion/complete} request.
|
|
1581
|
-
*/
|
|
1582
|
-
const CompleteRequestParamsSchema = BaseRequestParamsSchema.extend({
|
|
1583
|
-
ref: z.union([PromptReferenceSchema, ResourceTemplateReferenceSchema]),
|
|
1584
|
-
argument: z.object({
|
|
1585
|
-
name: z.string(),
|
|
1586
|
-
value: z.string()
|
|
1587
|
-
}),
|
|
1588
|
-
context: z.object({ arguments: z.record(z.string(), z.string()).optional() }).optional()
|
|
1589
|
-
});
|
|
1590
|
-
/**
|
|
1591
|
-
* A request from the client to the server, to ask for completion options.
|
|
1592
|
-
*/
|
|
1593
|
-
const CompleteRequestSchema = RequestSchema.extend({
|
|
1594
|
-
method: z.literal("completion/complete"),
|
|
1595
|
-
params: CompleteRequestParamsSchema
|
|
1596
|
-
});
|
|
1597
|
-
/**
|
|
1598
|
-
* The server's response to a {@linkcode CompleteRequest | completion/complete} request
|
|
1599
|
-
*/
|
|
1600
|
-
const CompleteResultSchema = ResultSchema.extend({ completion: z.looseObject({
|
|
1601
|
-
values: z.array(z.string()).max(100),
|
|
1602
|
-
total: z.optional(z.number().int()),
|
|
1603
|
-
hasMore: z.optional(z.boolean())
|
|
1604
|
-
}) });
|
|
1605
|
-
/**
|
|
1606
|
-
* Represents a root directory or file that the server can operate on.
|
|
1607
|
-
*/
|
|
1608
|
-
const RootSchema = z.object({
|
|
1609
|
-
uri: z.string().startsWith("file://"),
|
|
1610
|
-
name: z.string().optional(),
|
|
1611
|
-
_meta: z.record(z.string(), z.unknown()).optional()
|
|
1612
|
-
});
|
|
1613
|
-
/**
|
|
1614
|
-
* Sent from the server to request a list of root URIs from the client.
|
|
1615
|
-
*/
|
|
1616
|
-
const ListRootsRequestSchema = RequestSchema.extend({
|
|
1617
|
-
method: z.literal("roots/list"),
|
|
1618
|
-
params: BaseRequestParamsSchema.optional()
|
|
1619
|
-
});
|
|
1620
|
-
/**
|
|
1621
|
-
* The client's response to a `roots/list` request from the server.
|
|
1622
|
-
*/
|
|
1623
|
-
const ListRootsResultSchema = ResultSchema.extend({ roots: z.array(RootSchema) });
|
|
1624
|
-
/**
|
|
1625
|
-
* A notification from the client to the server, informing it that the list of roots has changed.
|
|
1626
|
-
*/
|
|
1627
|
-
const RootsListChangedNotificationSchema = NotificationSchema.extend({
|
|
1628
|
-
method: z.literal("notifications/roots/list_changed"),
|
|
1629
|
-
params: NotificationsParamsSchema.optional()
|
|
1630
|
-
});
|
|
1631
|
-
const ClientRequestSchema = z.union([
|
|
1632
|
-
PingRequestSchema,
|
|
1633
|
-
InitializeRequestSchema,
|
|
1634
|
-
CompleteRequestSchema,
|
|
1635
|
-
SetLevelRequestSchema,
|
|
1636
|
-
GetPromptRequestSchema,
|
|
1637
|
-
ListPromptsRequestSchema,
|
|
1638
|
-
ListResourcesRequestSchema,
|
|
1639
|
-
ListResourceTemplatesRequestSchema,
|
|
1640
|
-
ReadResourceRequestSchema,
|
|
1641
|
-
SubscribeRequestSchema,
|
|
1642
|
-
UnsubscribeRequestSchema,
|
|
1643
|
-
CallToolRequestSchema,
|
|
1644
|
-
ListToolsRequestSchema,
|
|
1645
|
-
GetTaskRequestSchema,
|
|
1646
|
-
GetTaskPayloadRequestSchema,
|
|
1647
|
-
ListTasksRequestSchema,
|
|
1648
|
-
CancelTaskRequestSchema
|
|
1649
|
-
]);
|
|
1650
|
-
const ClientNotificationSchema = z.union([
|
|
1651
|
-
CancelledNotificationSchema,
|
|
1652
|
-
ProgressNotificationSchema,
|
|
1653
|
-
InitializedNotificationSchema,
|
|
1654
|
-
RootsListChangedNotificationSchema,
|
|
1655
|
-
TaskStatusNotificationSchema
|
|
1656
|
-
]);
|
|
1657
|
-
const ClientResultSchema = z.union([
|
|
1658
|
-
EmptyResultSchema,
|
|
1659
|
-
CreateMessageResultSchema,
|
|
1660
|
-
CreateMessageResultWithToolsSchema,
|
|
1661
|
-
ElicitResultSchema,
|
|
1662
|
-
ListRootsResultSchema,
|
|
1663
|
-
GetTaskResultSchema,
|
|
1664
|
-
ListTasksResultSchema,
|
|
1665
|
-
CreateTaskResultSchema
|
|
1666
|
-
]);
|
|
1667
|
-
const ServerRequestSchema = z.union([
|
|
1668
|
-
PingRequestSchema,
|
|
1669
|
-
CreateMessageRequestSchema,
|
|
1670
|
-
ElicitRequestSchema,
|
|
1671
|
-
ListRootsRequestSchema,
|
|
1672
|
-
GetTaskRequestSchema,
|
|
1673
|
-
GetTaskPayloadRequestSchema,
|
|
1674
|
-
ListTasksRequestSchema,
|
|
1675
|
-
CancelTaskRequestSchema
|
|
1676
|
-
]);
|
|
1677
|
-
const ServerNotificationSchema = z.union([
|
|
1678
|
-
CancelledNotificationSchema,
|
|
1679
|
-
ProgressNotificationSchema,
|
|
1680
|
-
LoggingMessageNotificationSchema,
|
|
1681
|
-
ResourceUpdatedNotificationSchema,
|
|
1682
|
-
ResourceListChangedNotificationSchema,
|
|
1683
|
-
ToolListChangedNotificationSchema,
|
|
1684
|
-
PromptListChangedNotificationSchema,
|
|
1685
|
-
TaskStatusNotificationSchema,
|
|
1686
|
-
ElicitationCompleteNotificationSchema
|
|
1687
|
-
]);
|
|
1688
|
-
const ServerResultSchema = z.union([
|
|
1689
|
-
EmptyResultSchema,
|
|
1690
|
-
InitializeResultSchema,
|
|
1691
|
-
CompleteResultSchema,
|
|
1692
|
-
GetPromptResultSchema,
|
|
1693
|
-
ListPromptsResultSchema,
|
|
1694
|
-
ListResourcesResultSchema,
|
|
1695
|
-
ListResourceTemplatesResultSchema,
|
|
1696
|
-
ReadResourceResultSchema,
|
|
1697
|
-
CallToolResultSchema,
|
|
1698
|
-
ListToolsResultSchema,
|
|
1699
|
-
GetTaskResultSchema,
|
|
1700
|
-
ListTasksResultSchema,
|
|
1701
|
-
CreateTaskResultSchema
|
|
1702
|
-
]);
|
|
1703
|
-
const resultSchemas = {
|
|
1704
|
-
ping: EmptyResultSchema,
|
|
1705
|
-
initialize: InitializeResultSchema,
|
|
1706
|
-
"completion/complete": CompleteResultSchema,
|
|
1707
|
-
"logging/setLevel": EmptyResultSchema,
|
|
1708
|
-
"prompts/get": GetPromptResultSchema,
|
|
1709
|
-
"prompts/list": ListPromptsResultSchema,
|
|
1710
|
-
"resources/list": ListResourcesResultSchema,
|
|
1711
|
-
"resources/templates/list": ListResourceTemplatesResultSchema,
|
|
1712
|
-
"resources/read": ReadResourceResultSchema,
|
|
1713
|
-
"resources/subscribe": EmptyResultSchema,
|
|
1714
|
-
"resources/unsubscribe": EmptyResultSchema,
|
|
1715
|
-
"tools/call": z.union([CallToolResultSchema, CreateTaskResultSchema]),
|
|
1716
|
-
"tools/list": ListToolsResultSchema,
|
|
1717
|
-
"sampling/createMessage": z.union([CreateMessageResultWithToolsSchema, CreateTaskResultSchema]),
|
|
1718
|
-
"elicitation/create": z.union([ElicitResultSchema, CreateTaskResultSchema]),
|
|
1719
|
-
"roots/list": ListRootsResultSchema,
|
|
1720
|
-
"tasks/get": GetTaskResultSchema,
|
|
1721
|
-
"tasks/result": ResultSchema,
|
|
1722
|
-
"tasks/list": ListTasksResultSchema,
|
|
1723
|
-
"tasks/cancel": CancelTaskResultSchema
|
|
1724
|
-
};
|
|
1725
|
-
/**
|
|
1726
|
-
* Gets the Zod schema for validating results of a given request method.
|
|
1727
|
-
* @see getRequestSchema for explanation of the internal type assertion.
|
|
1728
|
-
*/
|
|
1729
|
-
function getResultSchema(method) {
|
|
1730
|
-
return resultSchemas[method];
|
|
1731
|
-
}
|
|
1732
|
-
function buildSchemaMap(schemas) {
|
|
1733
|
-
const map = {};
|
|
1734
|
-
for (const schema of schemas) {
|
|
1735
|
-
const method = schema.shape.method.value;
|
|
1736
|
-
map[method] = schema;
|
|
1737
|
-
}
|
|
1738
|
-
return map;
|
|
1739
|
-
}
|
|
1740
|
-
const requestSchemas = buildSchemaMap([...ClientRequestSchema.options, ...ServerRequestSchema.options]);
|
|
1741
|
-
const notificationSchemas = buildSchemaMap([...ClientNotificationSchema.options, ...ServerNotificationSchema.options]);
|
|
1742
|
-
/**
|
|
1743
|
-
* Gets the Zod schema for a given request method.
|
|
1744
|
-
* The return type is a ZodType that parses to RequestTypeMap[M], allowing callers
|
|
1745
|
-
* to use schema.parse() without needing additional type assertions.
|
|
1746
|
-
*
|
|
1747
|
-
* Note: The internal cast is necessary because TypeScript can't correlate the
|
|
1748
|
-
* Record-based schema lookup with the MethodToTypeMap-based RequestTypeMap
|
|
1749
|
-
* when M is a generic type parameter. Both compute to the same type at
|
|
1750
|
-
* instantiation, but TypeScript can't prove this statically.
|
|
1751
|
-
*/
|
|
1752
|
-
function getRequestSchema(method) {
|
|
1753
|
-
return requestSchemas[method];
|
|
1754
|
-
}
|
|
1755
|
-
/**
|
|
1756
|
-
* Gets the Zod schema for a given notification method.
|
|
1757
|
-
* @see getRequestSchema for explanation of the internal type assertion.
|
|
1758
|
-
*/
|
|
1759
|
-
function getNotificationSchema(method) {
|
|
1760
|
-
return notificationSchemas[method];
|
|
1761
|
-
}
|
|
1762
|
-
|
|
1763
|
-
//#endregion
|
|
1764
|
-
//#region ../core/src/types/guards.ts
|
|
1765
|
-
/**
|
|
1766
|
-
* Validates and parses an unknown value as a JSON-RPC message.
|
|
1767
|
-
*
|
|
1768
|
-
* Use this to validate incoming messages in custom transport implementations.
|
|
1769
|
-
* Throws if the value does not conform to the JSON-RPC message schema.
|
|
1770
|
-
*
|
|
1771
|
-
* @param value - The value to validate (typically a parsed JSON object).
|
|
1772
|
-
* @returns The validated {@linkcode JSONRPCMessage}.
|
|
1773
|
-
* @throws If validation fails.
|
|
1774
|
-
*/
|
|
1775
|
-
function parseJSONRPCMessage(value) {
|
|
1776
|
-
return JSONRPCMessageSchema.parse(value);
|
|
1777
|
-
}
|
|
1778
|
-
const isJSONRPCRequest = (value) => JSONRPCRequestSchema.safeParse(value).success;
|
|
1779
|
-
const isJSONRPCNotification = (value) => JSONRPCNotificationSchema.safeParse(value).success;
|
|
1780
|
-
/**
|
|
1781
|
-
* Checks if a value is a valid {@linkcode JSONRPCResultResponse}.
|
|
1782
|
-
* @param value - The value to check.
|
|
1783
|
-
*
|
|
1784
|
-
* @returns True if the value is a valid {@linkcode JSONRPCResultResponse}, false otherwise.
|
|
1785
|
-
*/
|
|
1786
|
-
const isJSONRPCResultResponse = (value) => JSONRPCResultResponseSchema.safeParse(value).success;
|
|
1787
|
-
/**
|
|
1788
|
-
* Checks if a value is a valid {@linkcode JSONRPCErrorResponse}.
|
|
1789
|
-
* @param value - The value to check.
|
|
1790
|
-
*
|
|
1791
|
-
* @returns True if the value is a valid {@linkcode JSONRPCErrorResponse}, false otherwise.
|
|
1792
|
-
*/
|
|
1793
|
-
const isJSONRPCErrorResponse = (value) => JSONRPCErrorResponseSchema.safeParse(value).success;
|
|
1794
|
-
/**
|
|
1795
|
-
* Checks if a value is a valid {@linkcode TaskAugmentedRequestParams}.
|
|
1796
|
-
* @param value - The value to check.
|
|
1797
|
-
*
|
|
1798
|
-
* @returns True if the value is a valid {@linkcode TaskAugmentedRequestParams}, false otherwise.
|
|
1799
|
-
*/
|
|
1800
|
-
const isTaskAugmentedRequestParams = (value) => TaskAugmentedRequestParamsSchema.safeParse(value).success;
|
|
1801
|
-
const isInitializeRequest = (value) => InitializeRequestSchema.safeParse(value).success;
|
|
1802
|
-
const isInitializedNotification = (value) => InitializedNotificationSchema.safeParse(value).success;
|
|
1803
|
-
function assertCompleteRequestPrompt(request) {
|
|
1804
|
-
if (request.params.ref.type !== "ref/prompt") throw new TypeError(`Expected CompleteRequestPrompt, but got ${request.params.ref.type}`);
|
|
1805
|
-
}
|
|
1806
|
-
function assertCompleteRequestResourceTemplate(request) {
|
|
1807
|
-
if (request.params.ref.type !== "ref/resource") throw new TypeError(`Expected CompleteRequestResourceTemplate, but got ${request.params.ref.type}`);
|
|
1808
|
-
}
|
|
1809
|
-
|
|
1810
|
-
//#endregion
|
|
1811
|
-
//#region ../core/src/util/schema.ts
|
|
1812
|
-
/**
|
|
1813
|
-
* Internal Zod schema utilities for protocol handling.
|
|
1814
|
-
* These are used internally by the SDK for protocol message validation.
|
|
1815
|
-
*/
|
|
1816
|
-
/**
|
|
1817
|
-
* Parses data against a Zod schema (synchronous).
|
|
1818
|
-
* Returns a discriminated union with success/error.
|
|
1819
|
-
*/
|
|
1820
|
-
function parseSchema(schema, data) {
|
|
1821
|
-
return z.safeParse(schema, data);
|
|
1822
|
-
}
|
|
1823
|
-
|
|
1824
|
-
//#endregion
|
|
1825
|
-
//#region ../core/src/experimental/tasks/interfaces.ts
|
|
1826
|
-
/**
|
|
1827
|
-
* Checks if a task status represents a terminal state.
|
|
1828
|
-
* Terminal states are those where the task has finished and will not change.
|
|
1829
|
-
*
|
|
1830
|
-
* @param status - The task status to check
|
|
1831
|
-
* @returns `true` if the status is terminal (`completed`, `failed`, or `cancelled`)
|
|
1832
|
-
* @experimental
|
|
1833
|
-
*/
|
|
1834
|
-
function isTerminal(status) {
|
|
1835
|
-
return status === "completed" || status === "failed" || status === "cancelled";
|
|
1836
|
-
}
|
|
1837
|
-
|
|
1838
|
-
//#endregion
|
|
1839
|
-
//#region ../core/src/shared/taskManager.ts
|
|
1840
|
-
/**
|
|
1841
|
-
* Extracts {@linkcode TaskManagerOptions} from a capability object that mixes in runtime fields.
|
|
1842
|
-
* Returns `undefined` when no task capability is configured.
|
|
1843
|
-
*/
|
|
1844
|
-
function extractTaskManagerOptions(tasksCapability) {
|
|
1845
|
-
if (!tasksCapability) return void 0;
|
|
1846
|
-
const { taskStore, taskMessageQueue, defaultTaskPollInterval, maxTaskQueueSize } = tasksCapability;
|
|
1847
|
-
return {
|
|
1848
|
-
taskStore,
|
|
1849
|
-
taskMessageQueue,
|
|
1850
|
-
defaultTaskPollInterval,
|
|
1851
|
-
maxTaskQueueSize
|
|
1852
|
-
};
|
|
1853
|
-
}
|
|
1854
|
-
/**
|
|
1855
|
-
* Manages task orchestration: state, message queuing, and polling.
|
|
1856
|
-
* Capability checking is delegated to the Protocol host.
|
|
1857
|
-
* @internal
|
|
1858
|
-
*/
|
|
1859
|
-
var TaskManager = class {
|
|
1860
|
-
_taskStore;
|
|
1861
|
-
_taskMessageQueue;
|
|
1862
|
-
_taskProgressTokens = /* @__PURE__ */ new Map();
|
|
1863
|
-
_requestResolvers = /* @__PURE__ */ new Map();
|
|
1864
|
-
_options;
|
|
1865
|
-
_host;
|
|
1866
|
-
constructor(options) {
|
|
1867
|
-
this._options = options;
|
|
1868
|
-
this._taskStore = options.taskStore;
|
|
1869
|
-
this._taskMessageQueue = options.taskMessageQueue;
|
|
1870
|
-
}
|
|
1871
|
-
bind(host) {
|
|
1872
|
-
this._host = host;
|
|
1873
|
-
if (this._taskStore) {
|
|
1874
|
-
host.registerHandler("tasks/get", async (request, ctx) => {
|
|
1875
|
-
const params = request.params;
|
|
1876
|
-
return { ...await this.handleGetTask(params.taskId, ctx.sessionId) };
|
|
1877
|
-
});
|
|
1878
|
-
host.registerHandler("tasks/result", async (request, ctx) => {
|
|
1879
|
-
const params = request.params;
|
|
1880
|
-
return await this.handleGetTaskPayload(params.taskId, ctx.sessionId, ctx.mcpReq.signal, async (message) => {
|
|
1881
|
-
await host.sendOnResponseStream(message, ctx.mcpReq.id);
|
|
1882
|
-
});
|
|
1883
|
-
});
|
|
1884
|
-
host.registerHandler("tasks/list", async (request, ctx) => {
|
|
1885
|
-
const params = request.params;
|
|
1886
|
-
return await this.handleListTasks(params?.cursor, ctx.sessionId);
|
|
1887
|
-
});
|
|
1888
|
-
host.registerHandler("tasks/cancel", async (request, ctx) => {
|
|
1889
|
-
const params = request.params;
|
|
1890
|
-
return await this.handleCancelTask(params.taskId, ctx.sessionId);
|
|
1891
|
-
});
|
|
1892
|
-
}
|
|
1893
|
-
}
|
|
1894
|
-
get _requireHost() {
|
|
1895
|
-
if (!this._host) throw new ProtocolError(ProtocolErrorCode.InternalError, "TaskManager is not bound to a Protocol host — call bind() first");
|
|
1896
|
-
return this._host;
|
|
1897
|
-
}
|
|
1898
|
-
get taskStore() {
|
|
1899
|
-
return this._taskStore;
|
|
1900
|
-
}
|
|
1901
|
-
get _requireTaskStore() {
|
|
1902
|
-
if (!this._taskStore) throw new ProtocolError(ProtocolErrorCode.InternalError, "TaskStore is not configured");
|
|
1903
|
-
return this._taskStore;
|
|
1904
|
-
}
|
|
1905
|
-
get taskMessageQueue() {
|
|
1906
|
-
return this._taskMessageQueue;
|
|
1907
|
-
}
|
|
1908
|
-
async *requestStream(request, resultSchema, options) {
|
|
1909
|
-
const host = this._requireHost;
|
|
1910
|
-
const { task } = options ?? {};
|
|
1911
|
-
if (!task) {
|
|
1912
|
-
try {
|
|
1913
|
-
yield {
|
|
1914
|
-
type: "result",
|
|
1915
|
-
result: await host.request(request, resultSchema, options)
|
|
1916
|
-
};
|
|
1917
|
-
} catch (error) {
|
|
1918
|
-
yield {
|
|
1919
|
-
type: "error",
|
|
1920
|
-
error: error instanceof Error ? error : new Error(String(error))
|
|
1921
|
-
};
|
|
1922
|
-
}
|
|
1923
|
-
return;
|
|
1924
|
-
}
|
|
1925
|
-
let taskId;
|
|
1926
|
-
try {
|
|
1927
|
-
const createResult = await host.request(request, CreateTaskResultSchema, options);
|
|
1928
|
-
if (createResult.task) {
|
|
1929
|
-
taskId = createResult.task.taskId;
|
|
1930
|
-
yield {
|
|
1931
|
-
type: "taskCreated",
|
|
1932
|
-
task: createResult.task
|
|
1933
|
-
};
|
|
1934
|
-
} else throw new ProtocolError(ProtocolErrorCode.InternalError, "Task creation did not return a task");
|
|
1935
|
-
while (true) {
|
|
1936
|
-
const task$1 = await this.getTask({ taskId }, options);
|
|
1937
|
-
yield {
|
|
1938
|
-
type: "taskStatus",
|
|
1939
|
-
task: task$1
|
|
1940
|
-
};
|
|
1941
|
-
if (isTerminal(task$1.status)) {
|
|
1942
|
-
switch (task$1.status) {
|
|
1943
|
-
case "completed":
|
|
1944
|
-
yield {
|
|
1945
|
-
type: "result",
|
|
1946
|
-
result: await this.getTaskResult({ taskId }, resultSchema, options)
|
|
1947
|
-
};
|
|
1948
|
-
break;
|
|
1949
|
-
case "failed":
|
|
1950
|
-
yield {
|
|
1951
|
-
type: "error",
|
|
1952
|
-
error: new ProtocolError(ProtocolErrorCode.InternalError, `Task ${taskId} failed`)
|
|
1953
|
-
};
|
|
1954
|
-
break;
|
|
1955
|
-
case "cancelled":
|
|
1956
|
-
yield {
|
|
1957
|
-
type: "error",
|
|
1958
|
-
error: new ProtocolError(ProtocolErrorCode.InternalError, `Task ${taskId} was cancelled`)
|
|
1959
|
-
};
|
|
1960
|
-
break;
|
|
1961
|
-
}
|
|
1962
|
-
return;
|
|
1963
|
-
}
|
|
1964
|
-
if (task$1.status === "input_required") {
|
|
1965
|
-
yield {
|
|
1966
|
-
type: "result",
|
|
1967
|
-
result: await this.getTaskResult({ taskId }, resultSchema, options)
|
|
1968
|
-
};
|
|
1969
|
-
return;
|
|
1970
|
-
}
|
|
1971
|
-
const pollInterval = task$1.pollInterval ?? this._options.defaultTaskPollInterval ?? 1e3;
|
|
1972
|
-
await new Promise((resolve) => setTimeout(resolve, pollInterval));
|
|
1973
|
-
options?.signal?.throwIfAborted();
|
|
1974
|
-
}
|
|
1975
|
-
} catch (error) {
|
|
1976
|
-
yield {
|
|
1977
|
-
type: "error",
|
|
1978
|
-
error: error instanceof Error ? error : new Error(String(error))
|
|
1979
|
-
};
|
|
1980
|
-
}
|
|
1981
|
-
}
|
|
1982
|
-
async getTask(params, options) {
|
|
1983
|
-
return this._requireHost.request({
|
|
1984
|
-
method: "tasks/get",
|
|
1985
|
-
params
|
|
1986
|
-
}, GetTaskResultSchema, options);
|
|
1987
|
-
}
|
|
1988
|
-
async getTaskResult(params, resultSchema, options) {
|
|
1989
|
-
return this._requireHost.request({
|
|
1990
|
-
method: "tasks/result",
|
|
1991
|
-
params
|
|
1992
|
-
}, resultSchema, options);
|
|
1993
|
-
}
|
|
1994
|
-
async listTasks(params, options) {
|
|
1995
|
-
return this._requireHost.request({
|
|
1996
|
-
method: "tasks/list",
|
|
1997
|
-
params
|
|
1998
|
-
}, ListTasksResultSchema, options);
|
|
1999
|
-
}
|
|
2000
|
-
async cancelTask(params, options) {
|
|
2001
|
-
return this._requireHost.request({
|
|
2002
|
-
method: "tasks/cancel",
|
|
2003
|
-
params
|
|
2004
|
-
}, CancelTaskResultSchema, options);
|
|
2005
|
-
}
|
|
2006
|
-
async handleGetTask(taskId, sessionId) {
|
|
2007
|
-
const task = await this._requireTaskStore.getTask(taskId, sessionId);
|
|
2008
|
-
if (!task) throw new ProtocolError(ProtocolErrorCode.InvalidParams, "Failed to retrieve task: Task not found");
|
|
2009
|
-
return task;
|
|
2010
|
-
}
|
|
2011
|
-
async handleGetTaskPayload(taskId, sessionId, signal, sendOnResponseStream) {
|
|
2012
|
-
const handleTaskResult = async () => {
|
|
2013
|
-
if (this._taskMessageQueue) {
|
|
2014
|
-
let queuedMessage;
|
|
2015
|
-
while (queuedMessage = await this._taskMessageQueue.dequeue(taskId, sessionId)) {
|
|
2016
|
-
if (queuedMessage.type === "response" || queuedMessage.type === "error") {
|
|
2017
|
-
const message = queuedMessage.message;
|
|
2018
|
-
const requestId = message.id;
|
|
2019
|
-
const resolver = this._requestResolvers.get(requestId);
|
|
2020
|
-
if (resolver) {
|
|
2021
|
-
this._requestResolvers.delete(requestId);
|
|
2022
|
-
if (queuedMessage.type === "response") resolver(message);
|
|
2023
|
-
else {
|
|
2024
|
-
const errorMessage = message;
|
|
2025
|
-
resolver(new ProtocolError(errorMessage.error.code, errorMessage.error.message, errorMessage.error.data));
|
|
2026
|
-
}
|
|
2027
|
-
} else {
|
|
2028
|
-
const messageType = queuedMessage.type === "response" ? "Response" : "Error";
|
|
2029
|
-
this._host?.reportError(/* @__PURE__ */ new Error(`${messageType} handler missing for request ${requestId}`));
|
|
2030
|
-
}
|
|
2031
|
-
continue;
|
|
2032
|
-
}
|
|
2033
|
-
await sendOnResponseStream(queuedMessage.message);
|
|
2034
|
-
}
|
|
2035
|
-
}
|
|
2036
|
-
const task = await this._requireTaskStore.getTask(taskId, sessionId);
|
|
2037
|
-
if (!task) throw new ProtocolError(ProtocolErrorCode.InvalidParams, `Task not found: ${taskId}`);
|
|
2038
|
-
if (!isTerminal(task.status)) {
|
|
2039
|
-
await this._waitForTaskUpdate(task.pollInterval, signal);
|
|
2040
|
-
return await handleTaskResult();
|
|
2041
|
-
}
|
|
2042
|
-
const result = await this._requireTaskStore.getTaskResult(taskId, sessionId);
|
|
2043
|
-
await this._clearTaskQueue(taskId);
|
|
2044
|
-
return {
|
|
2045
|
-
...result,
|
|
2046
|
-
_meta: {
|
|
2047
|
-
...result._meta,
|
|
2048
|
-
[RELATED_TASK_META_KEY]: { taskId }
|
|
2049
|
-
}
|
|
2050
|
-
};
|
|
2051
|
-
};
|
|
2052
|
-
return await handleTaskResult();
|
|
2053
|
-
}
|
|
2054
|
-
async handleListTasks(cursor, sessionId) {
|
|
2055
|
-
try {
|
|
2056
|
-
const { tasks, nextCursor } = await this._requireTaskStore.listTasks(cursor, sessionId);
|
|
2057
|
-
return {
|
|
2058
|
-
tasks,
|
|
2059
|
-
nextCursor,
|
|
2060
|
-
_meta: {}
|
|
2061
|
-
};
|
|
2062
|
-
} catch (error) {
|
|
2063
|
-
throw new ProtocolError(ProtocolErrorCode.InvalidParams, `Failed to list tasks: ${error instanceof Error ? error.message : String(error)}`);
|
|
2064
|
-
}
|
|
2065
|
-
}
|
|
2066
|
-
async handleCancelTask(taskId, sessionId) {
|
|
2067
|
-
try {
|
|
2068
|
-
const task = await this._requireTaskStore.getTask(taskId, sessionId);
|
|
2069
|
-
if (!task) throw new ProtocolError(ProtocolErrorCode.InvalidParams, `Task not found: ${taskId}`);
|
|
2070
|
-
if (isTerminal(task.status)) throw new ProtocolError(ProtocolErrorCode.InvalidParams, `Cannot cancel task in terminal status: ${task.status}`);
|
|
2071
|
-
await this._requireTaskStore.updateTaskStatus(taskId, "cancelled", "Client cancelled task execution.", sessionId);
|
|
2072
|
-
await this._clearTaskQueue(taskId);
|
|
2073
|
-
const cancelledTask = await this._requireTaskStore.getTask(taskId, sessionId);
|
|
2074
|
-
if (!cancelledTask) throw new ProtocolError(ProtocolErrorCode.InvalidParams, `Task not found after cancellation: ${taskId}`);
|
|
2075
|
-
return {
|
|
2076
|
-
_meta: {},
|
|
2077
|
-
...cancelledTask
|
|
2078
|
-
};
|
|
2079
|
-
} catch (error) {
|
|
2080
|
-
if (error instanceof ProtocolError) throw error;
|
|
2081
|
-
throw new ProtocolError(ProtocolErrorCode.InvalidRequest, `Failed to cancel task: ${error instanceof Error ? error.message : String(error)}`);
|
|
2082
|
-
}
|
|
2083
|
-
}
|
|
2084
|
-
prepareOutboundRequest(jsonrpcRequest, options, messageId, responseHandler, onError) {
|
|
2085
|
-
const { task, relatedTask } = options ?? {};
|
|
2086
|
-
if (task) jsonrpcRequest.params = {
|
|
2087
|
-
...jsonrpcRequest.params,
|
|
2088
|
-
task
|
|
2089
|
-
};
|
|
2090
|
-
if (relatedTask) jsonrpcRequest.params = {
|
|
2091
|
-
...jsonrpcRequest.params,
|
|
2092
|
-
_meta: {
|
|
2093
|
-
...jsonrpcRequest.params?._meta,
|
|
2094
|
-
[RELATED_TASK_META_KEY]: relatedTask
|
|
2095
|
-
}
|
|
2096
|
-
};
|
|
2097
|
-
const relatedTaskId = relatedTask?.taskId;
|
|
2098
|
-
if (relatedTaskId) {
|
|
2099
|
-
this._requestResolvers.set(messageId, responseHandler);
|
|
2100
|
-
this._enqueueTaskMessage(relatedTaskId, {
|
|
2101
|
-
type: "request",
|
|
2102
|
-
message: jsonrpcRequest,
|
|
2103
|
-
timestamp: Date.now()
|
|
2104
|
-
}).catch((error) => {
|
|
2105
|
-
onError(error);
|
|
2106
|
-
});
|
|
2107
|
-
return true;
|
|
2108
|
-
}
|
|
2109
|
-
return false;
|
|
2110
|
-
}
|
|
2111
|
-
extractInboundTaskContext(request, sessionId) {
|
|
2112
|
-
const relatedTaskId = (request.params?._meta)?.[RELATED_TASK_META_KEY]?.taskId;
|
|
2113
|
-
const taskCreationParams = isTaskAugmentedRequestParams(request.params) ? request.params.task : void 0;
|
|
2114
|
-
let taskContext;
|
|
2115
|
-
if (this._taskStore) taskContext = {
|
|
2116
|
-
id: relatedTaskId,
|
|
2117
|
-
store: this.createRequestTaskStore(request, sessionId),
|
|
2118
|
-
requestedTtl: taskCreationParams?.ttl
|
|
2119
|
-
};
|
|
2120
|
-
if (!relatedTaskId && !taskCreationParams && !taskContext) return {};
|
|
2121
|
-
return {
|
|
2122
|
-
relatedTaskId,
|
|
2123
|
-
taskCreationParams,
|
|
2124
|
-
taskContext
|
|
2125
|
-
};
|
|
2126
|
-
}
|
|
2127
|
-
wrapSendNotification(relatedTaskId, originalSendNotification) {
|
|
2128
|
-
return async (notification) => {
|
|
2129
|
-
await originalSendNotification(notification, { relatedTask: { taskId: relatedTaskId } });
|
|
2130
|
-
};
|
|
2131
|
-
}
|
|
2132
|
-
wrapSendRequest(relatedTaskId, taskStore, originalSendRequest) {
|
|
2133
|
-
return async (request, resultSchema, options) => {
|
|
2134
|
-
const requestOptions = { ...options };
|
|
2135
|
-
if (relatedTaskId && !requestOptions.relatedTask) requestOptions.relatedTask = { taskId: relatedTaskId };
|
|
2136
|
-
const effectiveTaskId = requestOptions.relatedTask?.taskId ?? relatedTaskId;
|
|
2137
|
-
if (effectiveTaskId && taskStore) await taskStore.updateTaskStatus(effectiveTaskId, "input_required");
|
|
2138
|
-
return await originalSendRequest(request, resultSchema, requestOptions);
|
|
2139
|
-
};
|
|
2140
|
-
}
|
|
2141
|
-
handleResponse(response) {
|
|
2142
|
-
const messageId = Number(response.id);
|
|
2143
|
-
const resolver = this._requestResolvers.get(messageId);
|
|
2144
|
-
if (resolver) {
|
|
2145
|
-
this._requestResolvers.delete(messageId);
|
|
2146
|
-
if (isJSONRPCResultResponse(response)) resolver(response);
|
|
2147
|
-
else resolver(new ProtocolError(response.error.code, response.error.message, response.error.data));
|
|
2148
|
-
return true;
|
|
2149
|
-
}
|
|
2150
|
-
return false;
|
|
2151
|
-
}
|
|
2152
|
-
shouldPreserveProgressHandler(response, messageId) {
|
|
2153
|
-
if (isJSONRPCResultResponse(response) && response.result && typeof response.result === "object") {
|
|
2154
|
-
const result = response.result;
|
|
2155
|
-
if (result.task && typeof result.task === "object") {
|
|
2156
|
-
const task = result.task;
|
|
2157
|
-
if (typeof task.taskId === "string") {
|
|
2158
|
-
this._taskProgressTokens.set(task.taskId, messageId);
|
|
2159
|
-
return true;
|
|
2160
|
-
}
|
|
2161
|
-
}
|
|
2162
|
-
}
|
|
2163
|
-
return false;
|
|
2164
|
-
}
|
|
2165
|
-
async routeNotification(notification, options) {
|
|
2166
|
-
const relatedTaskId = options?.relatedTask?.taskId;
|
|
2167
|
-
if (!relatedTaskId) return false;
|
|
2168
|
-
const jsonrpcNotification = {
|
|
2169
|
-
...notification,
|
|
2170
|
-
jsonrpc: "2.0",
|
|
2171
|
-
params: {
|
|
2172
|
-
...notification.params,
|
|
2173
|
-
_meta: {
|
|
2174
|
-
...notification.params?._meta,
|
|
2175
|
-
[RELATED_TASK_META_KEY]: options.relatedTask
|
|
2176
|
-
}
|
|
2177
|
-
}
|
|
2178
|
-
};
|
|
2179
|
-
await this._enqueueTaskMessage(relatedTaskId, {
|
|
2180
|
-
type: "notification",
|
|
2181
|
-
message: jsonrpcNotification,
|
|
2182
|
-
timestamp: Date.now()
|
|
2183
|
-
});
|
|
2184
|
-
return true;
|
|
2185
|
-
}
|
|
2186
|
-
async routeResponse(relatedTaskId, message, sessionId) {
|
|
2187
|
-
if (!relatedTaskId || !this._taskMessageQueue) return false;
|
|
2188
|
-
await (isJSONRPCErrorResponse(message) ? this._enqueueTaskMessage(relatedTaskId, {
|
|
2189
|
-
type: "error",
|
|
2190
|
-
message,
|
|
2191
|
-
timestamp: Date.now()
|
|
2192
|
-
}, sessionId) : this._enqueueTaskMessage(relatedTaskId, {
|
|
2193
|
-
type: "response",
|
|
2194
|
-
message,
|
|
2195
|
-
timestamp: Date.now()
|
|
2196
|
-
}, sessionId));
|
|
2197
|
-
return true;
|
|
2198
|
-
}
|
|
2199
|
-
createRequestTaskStore(request, sessionId) {
|
|
2200
|
-
const taskStore = this._requireTaskStore;
|
|
2201
|
-
const host = this._host;
|
|
2202
|
-
return {
|
|
2203
|
-
createTask: async (taskParams) => {
|
|
2204
|
-
if (!request) throw new Error("No request provided");
|
|
2205
|
-
return await taskStore.createTask(taskParams, request.id, {
|
|
2206
|
-
method: request.method,
|
|
2207
|
-
params: request.params
|
|
2208
|
-
}, sessionId);
|
|
2209
|
-
},
|
|
2210
|
-
getTask: async (taskId) => {
|
|
2211
|
-
const task = await taskStore.getTask(taskId, sessionId);
|
|
2212
|
-
if (!task) throw new ProtocolError(ProtocolErrorCode.InvalidParams, "Failed to retrieve task: Task not found");
|
|
2213
|
-
return task;
|
|
2214
|
-
},
|
|
2215
|
-
storeTaskResult: async (taskId, status, result) => {
|
|
2216
|
-
await taskStore.storeTaskResult(taskId, status, result, sessionId);
|
|
2217
|
-
const task = await taskStore.getTask(taskId, sessionId);
|
|
2218
|
-
if (task) {
|
|
2219
|
-
const notification = TaskStatusNotificationSchema.parse({
|
|
2220
|
-
method: "notifications/tasks/status",
|
|
2221
|
-
params: task
|
|
2222
|
-
});
|
|
2223
|
-
await host?.notification(notification);
|
|
2224
|
-
if (isTerminal(task.status)) this._cleanupTaskProgressHandler(taskId);
|
|
2225
|
-
}
|
|
2226
|
-
},
|
|
2227
|
-
getTaskResult: (taskId) => taskStore.getTaskResult(taskId, sessionId),
|
|
2228
|
-
updateTaskStatus: async (taskId, status, statusMessage) => {
|
|
2229
|
-
const task = await taskStore.getTask(taskId, sessionId);
|
|
2230
|
-
if (!task) throw new ProtocolError(ProtocolErrorCode.InvalidParams, `Task "${taskId}" not found - it may have been cleaned up`);
|
|
2231
|
-
if (isTerminal(task.status)) throw new ProtocolError(ProtocolErrorCode.InvalidParams, `Cannot update task "${taskId}" from terminal status "${task.status}" to "${status}". Terminal states (completed, failed, cancelled) cannot transition to other states.`);
|
|
2232
|
-
await taskStore.updateTaskStatus(taskId, status, statusMessage, sessionId);
|
|
2233
|
-
const updatedTask = await taskStore.getTask(taskId, sessionId);
|
|
2234
|
-
if (updatedTask) {
|
|
2235
|
-
const notification = TaskStatusNotificationSchema.parse({
|
|
2236
|
-
method: "notifications/tasks/status",
|
|
2237
|
-
params: updatedTask
|
|
2238
|
-
});
|
|
2239
|
-
await host?.notification(notification);
|
|
2240
|
-
if (isTerminal(updatedTask.status)) this._cleanupTaskProgressHandler(taskId);
|
|
2241
|
-
}
|
|
2242
|
-
},
|
|
2243
|
-
listTasks: (cursor) => taskStore.listTasks(cursor, sessionId)
|
|
2244
|
-
};
|
|
2245
|
-
}
|
|
2246
|
-
processInboundRequest(request, ctx) {
|
|
2247
|
-
const taskInfo = this.extractInboundTaskContext(request, ctx.sessionId);
|
|
2248
|
-
const relatedTaskId = taskInfo?.relatedTaskId;
|
|
2249
|
-
const sendNotification = relatedTaskId ? this.wrapSendNotification(relatedTaskId, ctx.sendNotification) : (notification) => ctx.sendNotification(notification);
|
|
2250
|
-
const sendRequest = relatedTaskId ? this.wrapSendRequest(relatedTaskId, taskInfo?.taskContext?.store, ctx.sendRequest) : taskInfo?.taskContext ? this.wrapSendRequest("", taskInfo.taskContext.store, ctx.sendRequest) : ctx.sendRequest;
|
|
2251
|
-
const hasTaskCreationParams = !!taskInfo?.taskCreationParams;
|
|
2252
|
-
return {
|
|
2253
|
-
taskContext: taskInfo?.taskContext,
|
|
2254
|
-
sendNotification,
|
|
2255
|
-
sendRequest,
|
|
2256
|
-
routeResponse: async (message) => {
|
|
2257
|
-
if (relatedTaskId) return this.routeResponse(relatedTaskId, message, ctx.sessionId);
|
|
2258
|
-
return false;
|
|
2259
|
-
},
|
|
2260
|
-
hasTaskCreationParams,
|
|
2261
|
-
validateInbound: hasTaskCreationParams ? () => this._requireHost.assertTaskHandlerCapability(request.method) : void 0
|
|
2262
|
-
};
|
|
2263
|
-
}
|
|
2264
|
-
processOutboundRequest(jsonrpcRequest, options, messageId, responseHandler, onError) {
|
|
2265
|
-
if (this._requireHost.enforceStrictCapabilities && options?.task) this._requireHost.assertTaskCapability(jsonrpcRequest.method);
|
|
2266
|
-
return { queued: this.prepareOutboundRequest(jsonrpcRequest, options, messageId, responseHandler, onError) };
|
|
2267
|
-
}
|
|
2268
|
-
processInboundResponse(response, messageId) {
|
|
2269
|
-
if (this.handleResponse(response)) return {
|
|
2270
|
-
consumed: true,
|
|
2271
|
-
preserveProgress: false
|
|
2272
|
-
};
|
|
2273
|
-
return {
|
|
2274
|
-
consumed: false,
|
|
2275
|
-
preserveProgress: this.shouldPreserveProgressHandler(response, messageId)
|
|
2276
|
-
};
|
|
2277
|
-
}
|
|
2278
|
-
async processOutboundNotification(notification, options) {
|
|
2279
|
-
if (await this.routeNotification(notification, options)) return { queued: true };
|
|
2280
|
-
let jsonrpcNotification = {
|
|
2281
|
-
...notification,
|
|
2282
|
-
jsonrpc: "2.0"
|
|
2283
|
-
};
|
|
2284
|
-
if (options?.relatedTask) jsonrpcNotification = {
|
|
2285
|
-
...jsonrpcNotification,
|
|
2286
|
-
params: {
|
|
2287
|
-
...jsonrpcNotification.params,
|
|
2288
|
-
_meta: {
|
|
2289
|
-
...jsonrpcNotification.params?._meta,
|
|
2290
|
-
[RELATED_TASK_META_KEY]: options.relatedTask
|
|
2291
|
-
}
|
|
2292
|
-
}
|
|
2293
|
-
};
|
|
2294
|
-
return {
|
|
2295
|
-
queued: false,
|
|
2296
|
-
jsonrpcNotification
|
|
2297
|
-
};
|
|
2298
|
-
}
|
|
2299
|
-
onClose() {
|
|
2300
|
-
this._taskProgressTokens.clear();
|
|
2301
|
-
this._requestResolvers.clear();
|
|
2302
|
-
}
|
|
2303
|
-
async _enqueueTaskMessage(taskId, message, sessionId) {
|
|
2304
|
-
if (!this._taskStore || !this._taskMessageQueue) throw new Error("Cannot enqueue task message: taskStore and taskMessageQueue are not configured");
|
|
2305
|
-
await this._taskMessageQueue.enqueue(taskId, message, sessionId, this._options.maxTaskQueueSize);
|
|
2306
|
-
}
|
|
2307
|
-
async _clearTaskQueue(taskId, sessionId) {
|
|
2308
|
-
if (this._taskMessageQueue) {
|
|
2309
|
-
const messages = await this._taskMessageQueue.dequeueAll(taskId, sessionId);
|
|
2310
|
-
for (const message of messages) if (message.type === "request" && isJSONRPCRequest(message.message)) {
|
|
2311
|
-
const requestId = message.message.id;
|
|
2312
|
-
const resolver = this._requestResolvers.get(requestId);
|
|
2313
|
-
if (resolver) {
|
|
2314
|
-
resolver(new ProtocolError(ProtocolErrorCode.InternalError, "Task cancelled or completed"));
|
|
2315
|
-
this._requestResolvers.delete(requestId);
|
|
2316
|
-
} else this._host?.reportError(/* @__PURE__ */ new Error(`Resolver missing for request ${requestId} during task ${taskId} cleanup`));
|
|
2317
|
-
}
|
|
2318
|
-
}
|
|
2319
|
-
}
|
|
2320
|
-
async _waitForTaskUpdate(pollInterval, signal) {
|
|
2321
|
-
const interval = pollInterval ?? this._options.defaultTaskPollInterval ?? 1e3;
|
|
2322
|
-
return new Promise((resolve, reject) => {
|
|
2323
|
-
if (signal.aborted) {
|
|
2324
|
-
reject(new ProtocolError(ProtocolErrorCode.InvalidRequest, "Request cancelled"));
|
|
2325
|
-
return;
|
|
2326
|
-
}
|
|
2327
|
-
const timeoutId = setTimeout(resolve, interval);
|
|
2328
|
-
signal.addEventListener("abort", () => {
|
|
2329
|
-
clearTimeout(timeoutId);
|
|
2330
|
-
reject(new ProtocolError(ProtocolErrorCode.InvalidRequest, "Request cancelled"));
|
|
2331
|
-
}, { once: true });
|
|
2332
|
-
});
|
|
2333
|
-
}
|
|
2334
|
-
_cleanupTaskProgressHandler(taskId) {
|
|
2335
|
-
const progressToken = this._taskProgressTokens.get(taskId);
|
|
2336
|
-
if (progressToken !== void 0) {
|
|
2337
|
-
this._host?.removeProgressHandler(progressToken);
|
|
2338
|
-
this._taskProgressTokens.delete(taskId);
|
|
2339
|
-
}
|
|
2340
|
-
}
|
|
2341
|
-
};
|
|
2342
|
-
/**
|
|
2343
|
-
* No-op TaskManager used when tasks capability is not configured.
|
|
2344
|
-
* Provides passthrough implementations for the hot paths, avoiding
|
|
2345
|
-
* unnecessary task extraction logic on every request.
|
|
2346
|
-
*/
|
|
2347
|
-
var NullTaskManager = class extends TaskManager {
|
|
2348
|
-
constructor() {
|
|
2349
|
-
super({});
|
|
2350
|
-
}
|
|
2351
|
-
processInboundRequest(request, ctx) {
|
|
2352
|
-
const hasTaskCreationParams = isTaskAugmentedRequestParams(request.params) && !!request.params.task;
|
|
2353
|
-
return {
|
|
2354
|
-
taskContext: void 0,
|
|
2355
|
-
sendNotification: (notification) => ctx.sendNotification(notification),
|
|
2356
|
-
sendRequest: ctx.sendRequest,
|
|
2357
|
-
routeResponse: async () => false,
|
|
2358
|
-
hasTaskCreationParams,
|
|
2359
|
-
validateInbound: hasTaskCreationParams ? () => this._requireHost.assertTaskHandlerCapability(request.method) : void 0
|
|
2360
|
-
};
|
|
2361
|
-
}
|
|
2362
|
-
async processOutboundNotification(notification, _options) {
|
|
2363
|
-
return {
|
|
2364
|
-
queued: false,
|
|
2365
|
-
jsonrpcNotification: {
|
|
2366
|
-
...notification,
|
|
2367
|
-
jsonrpc: "2.0"
|
|
2368
|
-
}
|
|
2369
|
-
};
|
|
2370
|
-
}
|
|
2371
|
-
};
|
|
2372
|
-
|
|
2373
|
-
//#endregion
|
|
2374
|
-
//#region ../core/src/shared/protocol.ts
|
|
2375
|
-
/**
|
|
2376
|
-
* The default request timeout, in milliseconds.
|
|
2377
|
-
*/
|
|
2378
|
-
const DEFAULT_REQUEST_TIMEOUT_MSEC = 6e4;
|
|
2379
|
-
/**
|
|
2380
|
-
* Implements MCP protocol framing on top of a pluggable transport, including
|
|
2381
|
-
* features like request/response linking, notifications, and progress.
|
|
2382
|
-
*/
|
|
2383
|
-
var Protocol = class {
|
|
2384
|
-
_transport;
|
|
2385
|
-
_requestMessageId = 0;
|
|
2386
|
-
_requestHandlers = /* @__PURE__ */ new Map();
|
|
2387
|
-
_requestHandlerAbortControllers = /* @__PURE__ */ new Map();
|
|
2388
|
-
_notificationHandlers = /* @__PURE__ */ new Map();
|
|
2389
|
-
_responseHandlers = /* @__PURE__ */ new Map();
|
|
2390
|
-
_progressHandlers = /* @__PURE__ */ new Map();
|
|
2391
|
-
_timeoutInfo = /* @__PURE__ */ new Map();
|
|
2392
|
-
_pendingDebouncedNotifications = /* @__PURE__ */ new Set();
|
|
2393
|
-
_taskManager;
|
|
2394
|
-
_supportedProtocolVersions;
|
|
2395
|
-
/**
|
|
2396
|
-
* Callback for when the connection is closed for any reason.
|
|
2397
|
-
*
|
|
2398
|
-
* This is invoked when {@linkcode Protocol.close | close()} is called as well.
|
|
2399
|
-
*/
|
|
2400
|
-
onclose;
|
|
2401
|
-
/**
|
|
2402
|
-
* Callback for when an error occurs.
|
|
2403
|
-
*
|
|
2404
|
-
* Note that errors are not necessarily fatal; they are used for reporting any kind of exceptional condition out of band.
|
|
2405
|
-
*/
|
|
2406
|
-
onerror;
|
|
2407
|
-
/**
|
|
2408
|
-
* A handler to invoke for any request types that do not have their own handler installed.
|
|
2409
|
-
*/
|
|
2410
|
-
fallbackRequestHandler;
|
|
2411
|
-
/**
|
|
2412
|
-
* A handler to invoke for any notification types that do not have their own handler installed.
|
|
2413
|
-
*/
|
|
2414
|
-
fallbackNotificationHandler;
|
|
2415
|
-
constructor(_options) {
|
|
2416
|
-
this._options = _options;
|
|
2417
|
-
this._supportedProtocolVersions = _options?.supportedProtocolVersions ?? SUPPORTED_PROTOCOL_VERSIONS;
|
|
2418
|
-
this._taskManager = _options?.tasks ? new TaskManager(_options.tasks) : new NullTaskManager();
|
|
2419
|
-
this._bindTaskManager();
|
|
2420
|
-
this.setNotificationHandler("notifications/cancelled", (notification) => {
|
|
2421
|
-
this._oncancel(notification);
|
|
2422
|
-
});
|
|
2423
|
-
this.setNotificationHandler("notifications/progress", (notification) => {
|
|
2424
|
-
this._onprogress(notification);
|
|
2425
|
-
});
|
|
2426
|
-
this.setRequestHandler("ping", (_request) => ({}));
|
|
2427
|
-
}
|
|
2428
|
-
/**
|
|
2429
|
-
* Access the TaskManager for task orchestration.
|
|
2430
|
-
* Always available; returns a NullTaskManager when no task store is configured.
|
|
2431
|
-
*/
|
|
2432
|
-
get taskManager() {
|
|
2433
|
-
return this._taskManager;
|
|
2434
|
-
}
|
|
2435
|
-
_bindTaskManager() {
|
|
2436
|
-
const taskManager = this._taskManager;
|
|
2437
|
-
const host = {
|
|
2438
|
-
request: (request, resultSchema, options) => this._requestWithSchema(request, resultSchema, options),
|
|
2439
|
-
notification: (notification, options) => this.notification(notification, options),
|
|
2440
|
-
reportError: (error) => this._onerror(error),
|
|
2441
|
-
removeProgressHandler: (token) => this._progressHandlers.delete(token),
|
|
2442
|
-
registerHandler: (method, handler) => {
|
|
2443
|
-
const schema = getRequestSchema(method);
|
|
2444
|
-
this._requestHandlers.set(method, (request, ctx) => {
|
|
2445
|
-
schema.parse(request);
|
|
2446
|
-
return handler(request, ctx);
|
|
2447
|
-
});
|
|
2448
|
-
},
|
|
2449
|
-
sendOnResponseStream: async (message, relatedRequestId) => {
|
|
2450
|
-
await this._transport?.send(message, { relatedRequestId });
|
|
2451
|
-
},
|
|
2452
|
-
enforceStrictCapabilities: this._options?.enforceStrictCapabilities === true,
|
|
2453
|
-
assertTaskCapability: (method) => this.assertTaskCapability(method),
|
|
2454
|
-
assertTaskHandlerCapability: (method) => this.assertTaskHandlerCapability(method)
|
|
2455
|
-
};
|
|
2456
|
-
taskManager.bind(host);
|
|
2457
|
-
}
|
|
2458
|
-
async _oncancel(notification) {
|
|
2459
|
-
if (!notification.params.requestId) return;
|
|
2460
|
-
this._requestHandlerAbortControllers.get(notification.params.requestId)?.abort(notification.params.reason);
|
|
2461
|
-
}
|
|
2462
|
-
_setupTimeout(messageId, timeout, maxTotalTimeout, onTimeout, resetTimeoutOnProgress = false) {
|
|
2463
|
-
this._timeoutInfo.set(messageId, {
|
|
2464
|
-
timeoutId: setTimeout(onTimeout, timeout),
|
|
2465
|
-
startTime: Date.now(),
|
|
2466
|
-
timeout,
|
|
2467
|
-
maxTotalTimeout,
|
|
2468
|
-
resetTimeoutOnProgress,
|
|
2469
|
-
onTimeout
|
|
2470
|
-
});
|
|
2471
|
-
}
|
|
2472
|
-
_resetTimeout(messageId) {
|
|
2473
|
-
const info = this._timeoutInfo.get(messageId);
|
|
2474
|
-
if (!info) return false;
|
|
2475
|
-
const totalElapsed = Date.now() - info.startTime;
|
|
2476
|
-
if (info.maxTotalTimeout && totalElapsed >= info.maxTotalTimeout) {
|
|
2477
|
-
this._timeoutInfo.delete(messageId);
|
|
2478
|
-
throw new SdkError(SdkErrorCode.RequestTimeout, "Maximum total timeout exceeded", {
|
|
2479
|
-
maxTotalTimeout: info.maxTotalTimeout,
|
|
2480
|
-
totalElapsed
|
|
2481
|
-
});
|
|
2482
|
-
}
|
|
2483
|
-
clearTimeout(info.timeoutId);
|
|
2484
|
-
info.timeoutId = setTimeout(info.onTimeout, info.timeout);
|
|
2485
|
-
return true;
|
|
2486
|
-
}
|
|
2487
|
-
_cleanupTimeout(messageId) {
|
|
2488
|
-
const info = this._timeoutInfo.get(messageId);
|
|
2489
|
-
if (info) {
|
|
2490
|
-
clearTimeout(info.timeoutId);
|
|
2491
|
-
this._timeoutInfo.delete(messageId);
|
|
2492
|
-
}
|
|
2493
|
-
}
|
|
2494
|
-
/**
|
|
2495
|
-
* Attaches to the given transport, starts it, and starts listening for messages.
|
|
2496
|
-
*
|
|
2497
|
-
* The caller assumes ownership of the {@linkcode Transport}, replacing any callbacks that have already been set, and expects that it is the only user of the {@linkcode Transport} instance going forward.
|
|
2498
|
-
*/
|
|
2499
|
-
async connect(transport) {
|
|
2500
|
-
this._transport = transport;
|
|
2501
|
-
const _onclose = this.transport?.onclose;
|
|
2502
|
-
this._transport.onclose = () => {
|
|
2503
|
-
try {
|
|
2504
|
-
_onclose?.();
|
|
2505
|
-
} finally {
|
|
2506
|
-
this._onclose();
|
|
2507
|
-
}
|
|
2508
|
-
};
|
|
2509
|
-
const _onerror = this.transport?.onerror;
|
|
2510
|
-
this._transport.onerror = (error) => {
|
|
2511
|
-
_onerror?.(error);
|
|
2512
|
-
this._onerror(error);
|
|
2513
|
-
};
|
|
2514
|
-
const _onmessage = this._transport?.onmessage;
|
|
2515
|
-
this._transport.onmessage = (message, extra) => {
|
|
2516
|
-
_onmessage?.(message, extra);
|
|
2517
|
-
if (isJSONRPCResultResponse(message) || isJSONRPCErrorResponse(message)) this._onresponse(message);
|
|
2518
|
-
else if (isJSONRPCRequest(message)) this._onrequest(message, extra);
|
|
2519
|
-
else if (isJSONRPCNotification(message)) this._onnotification(message);
|
|
2520
|
-
else this._onerror(/* @__PURE__ */ new Error(`Unknown message type: ${JSON.stringify(message)}`));
|
|
2521
|
-
};
|
|
2522
|
-
transport.setSupportedProtocolVersions?.(this._supportedProtocolVersions);
|
|
2523
|
-
await this._transport.start();
|
|
2524
|
-
}
|
|
2525
|
-
_onclose() {
|
|
2526
|
-
const responseHandlers = this._responseHandlers;
|
|
2527
|
-
this._responseHandlers = /* @__PURE__ */ new Map();
|
|
2528
|
-
this._progressHandlers.clear();
|
|
2529
|
-
this._taskManager.onClose();
|
|
2530
|
-
this._pendingDebouncedNotifications.clear();
|
|
2531
|
-
for (const info of this._timeoutInfo.values()) clearTimeout(info.timeoutId);
|
|
2532
|
-
this._timeoutInfo.clear();
|
|
2533
|
-
const requestHandlerAbortControllers = this._requestHandlerAbortControllers;
|
|
2534
|
-
this._requestHandlerAbortControllers = /* @__PURE__ */ new Map();
|
|
2535
|
-
const error = new SdkError(SdkErrorCode.ConnectionClosed, "Connection closed");
|
|
2536
|
-
this._transport = void 0;
|
|
2537
|
-
try {
|
|
2538
|
-
this.onclose?.();
|
|
2539
|
-
} finally {
|
|
2540
|
-
for (const handler of responseHandlers.values()) handler(error);
|
|
2541
|
-
for (const controller of requestHandlerAbortControllers.values()) controller.abort(error);
|
|
2542
|
-
}
|
|
2543
|
-
}
|
|
2544
|
-
_onerror(error) {
|
|
2545
|
-
this.onerror?.(error);
|
|
2546
|
-
}
|
|
2547
|
-
_onnotification(notification) {
|
|
2548
|
-
const handler = this._notificationHandlers.get(notification.method) ?? this.fallbackNotificationHandler;
|
|
2549
|
-
if (handler === void 0) return;
|
|
2550
|
-
Promise.resolve().then(() => handler(notification)).catch((error) => this._onerror(/* @__PURE__ */ new Error(`Uncaught error in notification handler: ${error}`)));
|
|
2551
|
-
}
|
|
2552
|
-
_onrequest(request, extra) {
|
|
2553
|
-
const handler = this._requestHandlers.get(request.method) ?? this.fallbackRequestHandler;
|
|
2554
|
-
const capturedTransport = this._transport;
|
|
2555
|
-
const inboundCtx = {
|
|
2556
|
-
sessionId: capturedTransport?.sessionId,
|
|
2557
|
-
sendNotification: (notification, options) => this.notification(notification, {
|
|
2558
|
-
...options,
|
|
2559
|
-
relatedRequestId: request.id
|
|
2560
|
-
}),
|
|
2561
|
-
sendRequest: (r, resultSchema, options) => this._requestWithSchema(r, resultSchema, {
|
|
2562
|
-
...options,
|
|
2563
|
-
relatedRequestId: request.id
|
|
2564
|
-
})
|
|
2565
|
-
};
|
|
2566
|
-
const taskResult = this._taskManager.processInboundRequest(request, inboundCtx);
|
|
2567
|
-
const sendNotification = taskResult.sendNotification;
|
|
2568
|
-
const sendRequest = taskResult.sendRequest;
|
|
2569
|
-
const taskContext = taskResult.taskContext;
|
|
2570
|
-
const routeResponse = taskResult.routeResponse;
|
|
2571
|
-
const validators = [];
|
|
2572
|
-
if (taskResult.validateInbound) validators.push(taskResult.validateInbound);
|
|
2573
|
-
if (handler === void 0) {
|
|
2574
|
-
const errorResponse = {
|
|
2575
|
-
jsonrpc: "2.0",
|
|
2576
|
-
id: request.id,
|
|
2577
|
-
error: {
|
|
2578
|
-
code: ProtocolErrorCode.MethodNotFound,
|
|
2579
|
-
message: "Method not found"
|
|
2580
|
-
}
|
|
2581
|
-
};
|
|
2582
|
-
routeResponse(errorResponse).then((routed) => {
|
|
2583
|
-
if (!routed) capturedTransport?.send(errorResponse).catch((error) => this._onerror(/* @__PURE__ */ new Error(`Failed to send an error response: ${error}`)));
|
|
2584
|
-
}).catch((error) => this._onerror(/* @__PURE__ */ new Error(`Failed to enqueue error response: ${error}`)));
|
|
2585
|
-
return;
|
|
2586
|
-
}
|
|
2587
|
-
const abortController = new AbortController();
|
|
2588
|
-
this._requestHandlerAbortControllers.set(request.id, abortController);
|
|
2589
|
-
const baseCtx = {
|
|
2590
|
-
sessionId: capturedTransport?.sessionId,
|
|
2591
|
-
mcpReq: {
|
|
2592
|
-
id: request.id,
|
|
2593
|
-
method: request.method,
|
|
2594
|
-
_meta: request.params?._meta,
|
|
2595
|
-
signal: abortController.signal,
|
|
2596
|
-
send: (r, options) => {
|
|
2597
|
-
return sendRequest(r, getResultSchema(r.method), options);
|
|
2598
|
-
},
|
|
2599
|
-
notify: sendNotification
|
|
2600
|
-
},
|
|
2601
|
-
http: extra?.authInfo ? { authInfo: extra.authInfo } : void 0,
|
|
2602
|
-
task: taskContext
|
|
2603
|
-
};
|
|
2604
|
-
const ctx = this.buildContext(baseCtx, extra);
|
|
2605
|
-
Promise.resolve().then(() => {
|
|
2606
|
-
for (const validate of validators) validate();
|
|
2607
|
-
}).then(() => handler(request, ctx)).then(async (result) => {
|
|
2608
|
-
if (abortController.signal.aborted) return;
|
|
2609
|
-
const response = {
|
|
2610
|
-
result,
|
|
2611
|
-
jsonrpc: "2.0",
|
|
2612
|
-
id: request.id
|
|
2613
|
-
};
|
|
2614
|
-
if (!await routeResponse(response)) await capturedTransport?.send(response);
|
|
2615
|
-
}, async (error) => {
|
|
2616
|
-
if (abortController.signal.aborted) return;
|
|
2617
|
-
const errorResponse = {
|
|
2618
|
-
jsonrpc: "2.0",
|
|
2619
|
-
id: request.id,
|
|
2620
|
-
error: {
|
|
2621
|
-
code: Number.isSafeInteger(error["code"]) ? error["code"] : ProtocolErrorCode.InternalError,
|
|
2622
|
-
message: error.message ?? "Internal error",
|
|
2623
|
-
...error["data"] !== void 0 && { data: error["data"] }
|
|
2624
|
-
}
|
|
2625
|
-
};
|
|
2626
|
-
if (!await routeResponse(errorResponse)) await capturedTransport?.send(errorResponse);
|
|
2627
|
-
}).catch((error) => this._onerror(/* @__PURE__ */ new Error(`Failed to send response: ${error}`))).finally(() => {
|
|
2628
|
-
if (this._requestHandlerAbortControllers.get(request.id) === abortController) this._requestHandlerAbortControllers.delete(request.id);
|
|
2629
|
-
});
|
|
2630
|
-
}
|
|
2631
|
-
_onprogress(notification) {
|
|
2632
|
-
const { progressToken, ...params } = notification.params;
|
|
2633
|
-
const messageId = Number(progressToken);
|
|
2634
|
-
const handler = this._progressHandlers.get(messageId);
|
|
2635
|
-
if (!handler) {
|
|
2636
|
-
this._onerror(/* @__PURE__ */ new Error(`Received a progress notification for an unknown token: ${JSON.stringify(notification)}`));
|
|
2637
|
-
return;
|
|
2638
|
-
}
|
|
2639
|
-
const responseHandler = this._responseHandlers.get(messageId);
|
|
2640
|
-
const timeoutInfo = this._timeoutInfo.get(messageId);
|
|
2641
|
-
if (timeoutInfo && responseHandler && timeoutInfo.resetTimeoutOnProgress) try {
|
|
2642
|
-
this._resetTimeout(messageId);
|
|
2643
|
-
} catch (error) {
|
|
2644
|
-
this._responseHandlers.delete(messageId);
|
|
2645
|
-
this._progressHandlers.delete(messageId);
|
|
2646
|
-
this._cleanupTimeout(messageId);
|
|
2647
|
-
responseHandler(error);
|
|
2648
|
-
return;
|
|
2649
|
-
}
|
|
2650
|
-
handler(params);
|
|
2651
|
-
}
|
|
2652
|
-
_onresponse(response) {
|
|
2653
|
-
const messageId = Number(response.id);
|
|
2654
|
-
const taskResult = this._taskManager.processInboundResponse(response, messageId);
|
|
2655
|
-
if (taskResult.consumed) return;
|
|
2656
|
-
const preserveProgress = taskResult.preserveProgress;
|
|
2657
|
-
const handler = this._responseHandlers.get(messageId);
|
|
2658
|
-
if (handler === void 0) {
|
|
2659
|
-
this._onerror(/* @__PURE__ */ new Error(`Received a response for an unknown message ID: ${JSON.stringify(response)}`));
|
|
2660
|
-
return;
|
|
2661
|
-
}
|
|
2662
|
-
this._responseHandlers.delete(messageId);
|
|
2663
|
-
this._cleanupTimeout(messageId);
|
|
2664
|
-
if (!preserveProgress) this._progressHandlers.delete(messageId);
|
|
2665
|
-
if (isJSONRPCResultResponse(response)) handler(response);
|
|
2666
|
-
else handler(ProtocolError.fromError(response.error.code, response.error.message, response.error.data));
|
|
2667
|
-
}
|
|
2668
|
-
get transport() {
|
|
2669
|
-
return this._transport;
|
|
2670
|
-
}
|
|
2671
|
-
/**
|
|
2672
|
-
* Closes the connection.
|
|
2673
|
-
*/
|
|
2674
|
-
async close() {
|
|
2675
|
-
await this._transport?.close();
|
|
2676
|
-
}
|
|
2677
|
-
/**
|
|
2678
|
-
* Sends a request and waits for a response, resolving the result schema
|
|
2679
|
-
* automatically from the method name.
|
|
2680
|
-
*
|
|
2681
|
-
* Do not use this method to emit notifications! Use {@linkcode Protocol.notification | notification()} instead.
|
|
2682
|
-
*/
|
|
2683
|
-
request(request, options) {
|
|
2684
|
-
const resultSchema = getResultSchema(request.method);
|
|
2685
|
-
return this._requestWithSchema(request, resultSchema, options);
|
|
2686
|
-
}
|
|
2687
|
-
/**
|
|
2688
|
-
* Sends a request and waits for a response, using the provided schema for validation.
|
|
2689
|
-
*
|
|
2690
|
-
* This is the internal implementation used by SDK methods that need to specify
|
|
2691
|
-
* a particular result schema (e.g., for compatibility or task-specific schemas).
|
|
2692
|
-
*/
|
|
2693
|
-
_requestWithSchema(request, resultSchema, options) {
|
|
2694
|
-
const { relatedRequestId, resumptionToken, onresumptiontoken } = options ?? {};
|
|
2695
|
-
let onAbort;
|
|
2696
|
-
let cleanupMessageId;
|
|
2697
|
-
return new Promise((resolve, reject) => {
|
|
2698
|
-
const earlyReject = (error) => {
|
|
2699
|
-
reject(error);
|
|
2700
|
-
};
|
|
2701
|
-
if (!this._transport) {
|
|
2702
|
-
earlyReject(/* @__PURE__ */ new Error("Not connected"));
|
|
2703
|
-
return;
|
|
2704
|
-
}
|
|
2705
|
-
if (this._options?.enforceStrictCapabilities === true) try {
|
|
2706
|
-
this.assertCapabilityForMethod(request.method);
|
|
2707
|
-
} catch (error) {
|
|
2708
|
-
earlyReject(error);
|
|
2709
|
-
return;
|
|
2710
|
-
}
|
|
2711
|
-
options?.signal?.throwIfAborted();
|
|
2712
|
-
const messageId = this._requestMessageId++;
|
|
2713
|
-
cleanupMessageId = messageId;
|
|
2714
|
-
const jsonrpcRequest = {
|
|
2715
|
-
...request,
|
|
2716
|
-
jsonrpc: "2.0",
|
|
2717
|
-
id: messageId
|
|
2718
|
-
};
|
|
2719
|
-
if (options?.onprogress) {
|
|
2720
|
-
this._progressHandlers.set(messageId, options.onprogress);
|
|
2721
|
-
jsonrpcRequest.params = {
|
|
2722
|
-
...request.params,
|
|
2723
|
-
_meta: {
|
|
2724
|
-
...request.params?._meta,
|
|
2725
|
-
progressToken: messageId
|
|
2726
|
-
}
|
|
2727
|
-
};
|
|
2728
|
-
}
|
|
2729
|
-
const cancel = (reason) => {
|
|
2730
|
-
this._progressHandlers.delete(messageId);
|
|
2731
|
-
this._transport?.send({
|
|
2732
|
-
jsonrpc: "2.0",
|
|
2733
|
-
method: "notifications/cancelled",
|
|
2734
|
-
params: {
|
|
2735
|
-
requestId: messageId,
|
|
2736
|
-
reason: String(reason)
|
|
2737
|
-
}
|
|
2738
|
-
}, {
|
|
2739
|
-
relatedRequestId,
|
|
2740
|
-
resumptionToken,
|
|
2741
|
-
onresumptiontoken
|
|
2742
|
-
}).catch((error) => this._onerror(/* @__PURE__ */ new Error(`Failed to send cancellation: ${error}`)));
|
|
2743
|
-
reject(reason instanceof SdkError ? reason : new SdkError(SdkErrorCode.RequestTimeout, String(reason)));
|
|
2744
|
-
};
|
|
2745
|
-
this._responseHandlers.set(messageId, (response) => {
|
|
2746
|
-
if (options?.signal?.aborted) return;
|
|
2747
|
-
if (response instanceof Error) return reject(response);
|
|
2748
|
-
try {
|
|
2749
|
-
const parseResult = parseSchema(resultSchema, response.result);
|
|
2750
|
-
if (parseResult.success) resolve(parseResult.data);
|
|
2751
|
-
else reject(parseResult.error);
|
|
2752
|
-
} catch (error) {
|
|
2753
|
-
reject(error);
|
|
2754
|
-
}
|
|
2755
|
-
});
|
|
2756
|
-
onAbort = () => cancel(options?.signal?.reason);
|
|
2757
|
-
options?.signal?.addEventListener("abort", onAbort, { once: true });
|
|
2758
|
-
const timeout = options?.timeout ?? DEFAULT_REQUEST_TIMEOUT_MSEC;
|
|
2759
|
-
const timeoutHandler = () => cancel(new SdkError(SdkErrorCode.RequestTimeout, "Request timed out", { timeout }));
|
|
2760
|
-
this._setupTimeout(messageId, timeout, options?.maxTotalTimeout, timeoutHandler, options?.resetTimeoutOnProgress ?? false);
|
|
2761
|
-
const responseHandler = (response) => {
|
|
2762
|
-
const handler = this._responseHandlers.get(messageId);
|
|
2763
|
-
if (handler) handler(response);
|
|
2764
|
-
else this._onerror(/* @__PURE__ */ new Error(`Response handler missing for side-channeled request ${messageId}`));
|
|
2765
|
-
};
|
|
2766
|
-
let outboundQueued = false;
|
|
2767
|
-
try {
|
|
2768
|
-
if (this._taskManager.processOutboundRequest(jsonrpcRequest, options, messageId, responseHandler, (error) => {
|
|
2769
|
-
this._progressHandlers.delete(messageId);
|
|
2770
|
-
reject(error);
|
|
2771
|
-
}).queued) outboundQueued = true;
|
|
2772
|
-
} catch (error) {
|
|
2773
|
-
this._progressHandlers.delete(messageId);
|
|
2774
|
-
reject(error);
|
|
2775
|
-
return;
|
|
2776
|
-
}
|
|
2777
|
-
if (!outboundQueued) this._transport.send(jsonrpcRequest, {
|
|
2778
|
-
relatedRequestId,
|
|
2779
|
-
resumptionToken,
|
|
2780
|
-
onresumptiontoken
|
|
2781
|
-
}).catch((error) => {
|
|
2782
|
-
this._progressHandlers.delete(messageId);
|
|
2783
|
-
reject(error);
|
|
2784
|
-
});
|
|
2785
|
-
}).finally(() => {
|
|
2786
|
-
if (onAbort) options?.signal?.removeEventListener("abort", onAbort);
|
|
2787
|
-
if (cleanupMessageId !== void 0) {
|
|
2788
|
-
this._responseHandlers.delete(cleanupMessageId);
|
|
2789
|
-
this._cleanupTimeout(cleanupMessageId);
|
|
2790
|
-
}
|
|
2791
|
-
});
|
|
2792
|
-
}
|
|
2793
|
-
/**
|
|
2794
|
-
* Emits a notification, which is a one-way message that does not expect a response.
|
|
2795
|
-
*/
|
|
2796
|
-
async notification(notification, options) {
|
|
2797
|
-
if (!this._transport) throw new SdkError(SdkErrorCode.NotConnected, "Not connected");
|
|
2798
|
-
this.assertNotificationCapability(notification.method);
|
|
2799
|
-
const taskResult = await this._taskManager.processOutboundNotification(notification, options);
|
|
2800
|
-
const queued = taskResult.queued;
|
|
2801
|
-
const jsonrpcNotification = taskResult.queued ? void 0 : taskResult.jsonrpcNotification;
|
|
2802
|
-
if (queued) return;
|
|
2803
|
-
if ((this._options?.debouncedNotificationMethods ?? []).includes(notification.method) && !notification.params && !options?.relatedRequestId && !options?.relatedTask) {
|
|
2804
|
-
if (this._pendingDebouncedNotifications.has(notification.method)) return;
|
|
2805
|
-
this._pendingDebouncedNotifications.add(notification.method);
|
|
2806
|
-
Promise.resolve().then(() => {
|
|
2807
|
-
this._pendingDebouncedNotifications.delete(notification.method);
|
|
2808
|
-
if (!this._transport) return;
|
|
2809
|
-
this._transport?.send(jsonrpcNotification, options).catch((error) => this._onerror(error));
|
|
2810
|
-
});
|
|
2811
|
-
return;
|
|
2812
|
-
}
|
|
2813
|
-
await this._transport.send(jsonrpcNotification, options);
|
|
2814
|
-
}
|
|
2815
|
-
/**
|
|
2816
|
-
* Registers a handler to invoke when this protocol object receives a request with the given method.
|
|
2817
|
-
*
|
|
2818
|
-
* Note that this will replace any previous request handler for the same method.
|
|
2819
|
-
*/
|
|
2820
|
-
setRequestHandler(method, handler) {
|
|
2821
|
-
this.assertRequestHandlerCapability(method);
|
|
2822
|
-
const schema = getRequestSchema(method);
|
|
2823
|
-
this._requestHandlers.set(method, (request, ctx) => {
|
|
2824
|
-
const parsed = schema.parse(request);
|
|
2825
|
-
return Promise.resolve(handler(parsed, ctx));
|
|
2826
|
-
});
|
|
2827
|
-
}
|
|
2828
|
-
/**
|
|
2829
|
-
* Removes the request handler for the given method.
|
|
2830
|
-
*/
|
|
2831
|
-
removeRequestHandler(method) {
|
|
2832
|
-
this._requestHandlers.delete(method);
|
|
2833
|
-
}
|
|
2834
|
-
/**
|
|
2835
|
-
* Asserts that a request handler has not already been set for the given method, in preparation for a new one being automatically installed.
|
|
2836
|
-
*/
|
|
2837
|
-
assertCanSetRequestHandler(method) {
|
|
2838
|
-
if (this._requestHandlers.has(method)) throw new Error(`A request handler for ${method} already exists, which would be overridden`);
|
|
2839
|
-
}
|
|
2840
|
-
/**
|
|
2841
|
-
* Registers a handler to invoke when this protocol object receives a notification with the given method.
|
|
2842
|
-
*
|
|
2843
|
-
* Note that this will replace any previous notification handler for the same method.
|
|
2844
|
-
*/
|
|
2845
|
-
setNotificationHandler(method, handler) {
|
|
2846
|
-
const schema = getNotificationSchema(method);
|
|
2847
|
-
this._notificationHandlers.set(method, (notification) => {
|
|
2848
|
-
const parsed = schema.parse(notification);
|
|
2849
|
-
return Promise.resolve(handler(parsed));
|
|
2850
|
-
});
|
|
2851
|
-
}
|
|
2852
|
-
/**
|
|
2853
|
-
* Removes the notification handler for the given method.
|
|
2854
|
-
*/
|
|
2855
|
-
removeNotificationHandler(method) {
|
|
2856
|
-
this._notificationHandlers.delete(method);
|
|
2857
|
-
}
|
|
2858
|
-
};
|
|
2859
|
-
function isPlainObject(value) {
|
|
2860
|
-
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
2861
|
-
}
|
|
2862
|
-
function mergeCapabilities(base, additional) {
|
|
2863
|
-
const result = { ...base };
|
|
2864
|
-
for (const key in additional) {
|
|
2865
|
-
const k = key;
|
|
2866
|
-
const addValue = additional[k];
|
|
2867
|
-
if (addValue === void 0) continue;
|
|
2868
|
-
const baseValue = result[k];
|
|
2869
|
-
result[k] = isPlainObject(baseValue) && isPlainObject(addValue) ? {
|
|
2870
|
-
...baseValue,
|
|
2871
|
-
...addValue
|
|
2872
|
-
} : addValue;
|
|
2873
|
-
}
|
|
2874
|
-
return result;
|
|
2875
|
-
}
|
|
2876
|
-
|
|
2877
|
-
//#endregion
|
|
2878
|
-
//#region ../core/src/shared/responseMessage.ts
|
|
2879
|
-
/**
|
|
2880
|
-
* Collects all values from an async generator into an array.
|
|
2881
|
-
*/
|
|
2882
|
-
async function toArrayAsync(it) {
|
|
2883
|
-
const arr = [];
|
|
2884
|
-
for await (const o of it) arr.push(o);
|
|
2885
|
-
return arr;
|
|
2886
|
-
}
|
|
2887
|
-
/**
|
|
2888
|
-
* Consumes a {@linkcode ResponseMessage} stream and returns the final result,
|
|
2889
|
-
* discarding intermediate `taskCreated` and `taskStatus` messages. Throws
|
|
2890
|
-
* if an `error` message is received or the stream ends without a result.
|
|
2891
|
-
*/
|
|
2892
|
-
async function takeResult(it) {
|
|
2893
|
-
for await (const o of it) if (o.type === "result") return o.result;
|
|
2894
|
-
else if (o.type === "error") throw o.error;
|
|
2895
|
-
throw new Error("No result in stream.");
|
|
2896
|
-
}
|
|
2897
|
-
|
|
2898
|
-
//#endregion
|
|
2899
|
-
//#region ../core/src/shared/stdio.ts
|
|
2900
|
-
/**
|
|
2901
|
-
* Buffers a continuous stdio stream into discrete JSON-RPC messages.
|
|
2902
|
-
*/
|
|
2903
|
-
var ReadBuffer = class {
|
|
2904
|
-
_buffer;
|
|
2905
|
-
append(chunk) {
|
|
2906
|
-
this._buffer = this._buffer ? Buffer.concat([this._buffer, chunk]) : chunk;
|
|
2907
|
-
}
|
|
2908
|
-
readMessage() {
|
|
2909
|
-
while (this._buffer) {
|
|
2910
|
-
const index = this._buffer.indexOf("\n");
|
|
2911
|
-
if (index === -1) return null;
|
|
2912
|
-
const line = this._buffer.toString("utf8", 0, index).replace(/\r$/, "");
|
|
2913
|
-
this._buffer = this._buffer.subarray(index + 1);
|
|
2914
|
-
try {
|
|
2915
|
-
return deserializeMessage(line);
|
|
2916
|
-
} catch (error) {
|
|
2917
|
-
if (error instanceof SyntaxError) continue;
|
|
2918
|
-
throw error;
|
|
2919
|
-
}
|
|
2920
|
-
}
|
|
2921
|
-
return null;
|
|
2922
|
-
}
|
|
2923
|
-
clear() {
|
|
2924
|
-
this._buffer = void 0;
|
|
2925
|
-
}
|
|
2926
|
-
};
|
|
2927
|
-
function deserializeMessage(line) {
|
|
2928
|
-
return JSONRPCMessageSchema.parse(JSON.parse(line));
|
|
2929
|
-
}
|
|
2930
|
-
function serializeMessage(message) {
|
|
2931
|
-
return JSON.stringify(message) + "\n";
|
|
2932
|
-
}
|
|
2933
|
-
|
|
2934
|
-
//#endregion
|
|
2935
|
-
//#region ../core/src/shared/toolNameValidation.ts
|
|
2936
|
-
/**
|
|
2937
|
-
* Tool name validation utilities according to SEP: Specify Format for Tool Names
|
|
2938
|
-
*
|
|
2939
|
-
* Tool names SHOULD be between 1 and 128 characters in length (inclusive).
|
|
2940
|
-
* Tool names are case-sensitive.
|
|
2941
|
-
* Allowed characters: uppercase and lowercase ASCII letters (`A-Z`, `a-z`), digits
|
|
2942
|
-
* (`0-9`), underscore (`_`), dash (`-`), and dot (`.`).
|
|
2943
|
-
* Tool names SHOULD NOT contain spaces, commas, or other special characters.
|
|
2944
|
-
*
|
|
2945
|
-
* @see {@link https://github.com/modelcontextprotocol/modelcontextprotocol/issues/986 | SEP-986: Specify Format for Tool Names}
|
|
2946
|
-
*/
|
|
2947
|
-
/**
|
|
2948
|
-
* Regular expression for valid tool names according to SEP-986 specification
|
|
2949
|
-
*/
|
|
2950
|
-
const TOOL_NAME_REGEX = /^[A-Za-z0-9._-]{1,128}$/;
|
|
2951
|
-
/**
|
|
2952
|
-
* Validates a tool name according to the SEP specification
|
|
2953
|
-
* @param name - The tool name to validate
|
|
2954
|
-
* @returns An object containing validation result and any warnings
|
|
2955
|
-
*/
|
|
2956
|
-
function validateToolName(name) {
|
|
2957
|
-
const warnings = [];
|
|
2958
|
-
if (name.length === 0) return {
|
|
2959
|
-
isValid: false,
|
|
2960
|
-
warnings: ["Tool name cannot be empty"]
|
|
2961
|
-
};
|
|
2962
|
-
if (name.length > 128) return {
|
|
2963
|
-
isValid: false,
|
|
2964
|
-
warnings: [`Tool name exceeds maximum length of 128 characters (current: ${name.length})`]
|
|
2965
|
-
};
|
|
2966
|
-
if (name.includes(" ")) warnings.push("Tool name contains spaces, which may cause parsing issues");
|
|
2967
|
-
if (name.includes(",")) warnings.push("Tool name contains commas, which may cause parsing issues");
|
|
2968
|
-
if (name.startsWith("-") || name.endsWith("-")) warnings.push("Tool name starts or ends with a dash, which may cause parsing issues in some contexts");
|
|
2969
|
-
if (name.startsWith(".") || name.endsWith(".")) warnings.push("Tool name starts or ends with a dot, which may cause parsing issues in some contexts");
|
|
2970
|
-
if (!TOOL_NAME_REGEX.test(name)) {
|
|
2971
|
-
const invalidChars = [...name].filter((char) => !/[A-Za-z0-9._-]/.test(char)).filter((char, index, arr) => arr.indexOf(char) === index);
|
|
2972
|
-
warnings.push(`Tool name contains invalid characters: ${invalidChars.map((c) => `"${c}"`).join(", ")}`, "Allowed characters are: A-Z, a-z, 0-9, underscore (_), dash (-), and dot (.)");
|
|
2973
|
-
return {
|
|
2974
|
-
isValid: false,
|
|
2975
|
-
warnings
|
|
2976
|
-
};
|
|
2977
|
-
}
|
|
2978
|
-
return {
|
|
2979
|
-
isValid: true,
|
|
2980
|
-
warnings
|
|
2981
|
-
};
|
|
2982
|
-
}
|
|
2983
|
-
/**
|
|
2984
|
-
* Issues warnings for non-conforming tool names
|
|
2985
|
-
* @param name - The tool name that triggered the warnings
|
|
2986
|
-
* @param warnings - Array of warning messages
|
|
2987
|
-
*/
|
|
2988
|
-
function issueToolNameWarning(name, warnings) {
|
|
2989
|
-
if (warnings.length > 0) {
|
|
2990
|
-
console.warn(`Tool name validation warning for "${name}":`);
|
|
2991
|
-
for (const warning of warnings) console.warn(` - ${warning}`);
|
|
2992
|
-
console.warn("Tool registration will proceed, but this may cause compatibility issues.");
|
|
2993
|
-
console.warn("Consider updating the tool name to conform to the MCP tool naming standard.");
|
|
2994
|
-
console.warn("See SEP: Specify Format for Tool Names (https://github.com/modelcontextprotocol/modelcontextprotocol/issues/986) for more details.");
|
|
2995
|
-
}
|
|
2996
|
-
}
|
|
2997
|
-
/**
|
|
2998
|
-
* Validates a tool name and issues warnings for non-conforming names
|
|
2999
|
-
* @param name - The tool name to validate
|
|
3000
|
-
* @returns `true` if the name is valid, `false` otherwise
|
|
3001
|
-
*/
|
|
3002
|
-
function validateAndWarnToolName(name) {
|
|
3003
|
-
const result = validateToolName(name);
|
|
3004
|
-
issueToolNameWarning(name, result.warnings);
|
|
3005
|
-
return result.isValid;
|
|
3006
|
-
}
|
|
3007
|
-
|
|
3008
|
-
//#endregion
|
|
3009
|
-
//#region ../core/src/shared/transport.ts
|
|
3010
|
-
/**
|
|
3011
|
-
* Normalizes `HeadersInit` to a plain `Record<string, string>` for manipulation.
|
|
3012
|
-
* Handles `Headers` objects, arrays of tuples, and plain objects.
|
|
3013
|
-
*/
|
|
3014
|
-
function normalizeHeaders(headers) {
|
|
3015
|
-
if (!headers) return {};
|
|
3016
|
-
if (headers instanceof Headers) return Object.fromEntries(headers.entries());
|
|
3017
|
-
if (Array.isArray(headers)) return Object.fromEntries(headers);
|
|
3018
|
-
return { ...headers };
|
|
3019
|
-
}
|
|
3020
|
-
/**
|
|
3021
|
-
* Creates a fetch function that includes base `RequestInit` options.
|
|
3022
|
-
* This ensures requests inherit settings like credentials, mode, headers, etc. from the base init.
|
|
3023
|
-
*
|
|
3024
|
-
* @param baseFetch - The base fetch function to wrap (defaults to global `fetch`)
|
|
3025
|
-
* @param baseInit - The base `RequestInit` to merge with each request
|
|
3026
|
-
* @returns A wrapped fetch function that merges base options with call-specific options
|
|
3027
|
-
*/
|
|
3028
|
-
function createFetchWithInit(baseFetch = fetch, baseInit) {
|
|
3029
|
-
if (!baseInit) return baseFetch;
|
|
3030
|
-
return async (url, init) => {
|
|
3031
|
-
return baseFetch(url, {
|
|
3032
|
-
...baseInit,
|
|
3033
|
-
...init,
|
|
3034
|
-
headers: init?.headers ? {
|
|
3035
|
-
...normalizeHeaders(baseInit.headers),
|
|
3036
|
-
...normalizeHeaders(init.headers)
|
|
3037
|
-
} : baseInit.headers
|
|
3038
|
-
});
|
|
3039
|
-
};
|
|
3040
|
-
}
|
|
3041
|
-
|
|
3042
|
-
//#endregion
|
|
3043
|
-
//#region ../core/src/shared/uriTemplate.ts
|
|
3044
|
-
const MAX_TEMPLATE_LENGTH = 1e6;
|
|
3045
|
-
const MAX_VARIABLE_LENGTH = 1e6;
|
|
3046
|
-
const MAX_TEMPLATE_EXPRESSIONS = 1e4;
|
|
3047
|
-
const MAX_REGEX_LENGTH = 1e6;
|
|
3048
|
-
var UriTemplate = class UriTemplate {
|
|
3049
|
-
/**
|
|
3050
|
-
* Returns true if the given string contains any URI template expressions.
|
|
3051
|
-
* A template expression is a sequence of characters enclosed in curly braces,
|
|
3052
|
-
* like `{foo}` or `{?bar}`.
|
|
3053
|
-
*/
|
|
3054
|
-
static isTemplate(str) {
|
|
3055
|
-
return /\{[^}\s]+\}/.test(str);
|
|
3056
|
-
}
|
|
3057
|
-
static validateLength(str, max, context) {
|
|
3058
|
-
if (str.length > max) throw new Error(`${context} exceeds maximum length of ${max} characters (got ${str.length})`);
|
|
3059
|
-
}
|
|
3060
|
-
template;
|
|
3061
|
-
parts;
|
|
3062
|
-
get variableNames() {
|
|
3063
|
-
return this.parts.flatMap((part) => typeof part === "string" ? [] : part.names);
|
|
3064
|
-
}
|
|
3065
|
-
constructor(template) {
|
|
3066
|
-
UriTemplate.validateLength(template, MAX_TEMPLATE_LENGTH, "Template");
|
|
3067
|
-
this.template = template;
|
|
3068
|
-
this.parts = this.parse(template);
|
|
3069
|
-
}
|
|
3070
|
-
toString() {
|
|
3071
|
-
return this.template;
|
|
3072
|
-
}
|
|
3073
|
-
parse(template) {
|
|
3074
|
-
const parts = [];
|
|
3075
|
-
let currentText = "";
|
|
3076
|
-
let i = 0;
|
|
3077
|
-
let expressionCount = 0;
|
|
3078
|
-
while (i < template.length) if (template[i] === "{") {
|
|
3079
|
-
if (currentText) {
|
|
3080
|
-
parts.push(currentText);
|
|
3081
|
-
currentText = "";
|
|
3082
|
-
}
|
|
3083
|
-
const end = template.indexOf("}", i);
|
|
3084
|
-
if (end === -1) throw new Error("Unclosed template expression");
|
|
3085
|
-
expressionCount++;
|
|
3086
|
-
if (expressionCount > MAX_TEMPLATE_EXPRESSIONS) throw new Error(`Template contains too many expressions (max ${MAX_TEMPLATE_EXPRESSIONS})`);
|
|
3087
|
-
const expr = template.slice(i + 1, end);
|
|
3088
|
-
const operator = this.getOperator(expr);
|
|
3089
|
-
const exploded = expr.includes("*");
|
|
3090
|
-
const names = this.getNames(expr);
|
|
3091
|
-
const name = names[0];
|
|
3092
|
-
for (const name$1 of names) UriTemplate.validateLength(name$1, MAX_VARIABLE_LENGTH, "Variable name");
|
|
3093
|
-
parts.push({
|
|
3094
|
-
name,
|
|
3095
|
-
operator,
|
|
3096
|
-
names,
|
|
3097
|
-
exploded
|
|
3098
|
-
});
|
|
3099
|
-
i = end + 1;
|
|
3100
|
-
} else {
|
|
3101
|
-
currentText += template[i];
|
|
3102
|
-
i++;
|
|
3103
|
-
}
|
|
3104
|
-
if (currentText) parts.push(currentText);
|
|
3105
|
-
return parts;
|
|
3106
|
-
}
|
|
3107
|
-
getOperator(expr) {
|
|
3108
|
-
return [
|
|
3109
|
-
"+",
|
|
3110
|
-
"#",
|
|
3111
|
-
".",
|
|
3112
|
-
"/",
|
|
3113
|
-
"?",
|
|
3114
|
-
"&"
|
|
3115
|
-
].find((op) => expr.startsWith(op)) || "";
|
|
3116
|
-
}
|
|
3117
|
-
getNames(expr) {
|
|
3118
|
-
const operator = this.getOperator(expr);
|
|
3119
|
-
return expr.slice(operator.length).split(",").map((name) => name.replace("*", "").trim()).filter((name) => name.length > 0);
|
|
3120
|
-
}
|
|
3121
|
-
encodeValue(value, operator) {
|
|
3122
|
-
UriTemplate.validateLength(value, MAX_VARIABLE_LENGTH, "Variable value");
|
|
3123
|
-
if (operator === "+" || operator === "#") return encodeURI(value);
|
|
3124
|
-
return encodeURIComponent(value);
|
|
3125
|
-
}
|
|
3126
|
-
expandPart(part, variables) {
|
|
3127
|
-
if (part.operator === "?" || part.operator === "&") {
|
|
3128
|
-
const pairs = part.names.map((name) => {
|
|
3129
|
-
const value$1 = variables[name];
|
|
3130
|
-
if (value$1 === void 0) return "";
|
|
3131
|
-
return `${name}=${Array.isArray(value$1) ? value$1.map((v) => this.encodeValue(v, part.operator)).join(",") : this.encodeValue(value$1.toString(), part.operator)}`;
|
|
3132
|
-
}).filter((pair) => pair.length > 0);
|
|
3133
|
-
if (pairs.length === 0) return "";
|
|
3134
|
-
return (part.operator === "?" ? "?" : "&") + pairs.join("&");
|
|
3135
|
-
}
|
|
3136
|
-
if (part.names.length > 1) {
|
|
3137
|
-
const values = part.names.map((name) => variables[name]).filter((v) => v !== void 0);
|
|
3138
|
-
if (values.length === 0) return "";
|
|
3139
|
-
return values.map((v) => Array.isArray(v) ? v[0] : v).join(",");
|
|
3140
|
-
}
|
|
3141
|
-
const value = variables[part.name];
|
|
3142
|
-
if (value === void 0) return "";
|
|
3143
|
-
const encoded = (Array.isArray(value) ? value : [value]).map((v) => this.encodeValue(v, part.operator));
|
|
3144
|
-
switch (part.operator) {
|
|
3145
|
-
case "": return encoded.join(",");
|
|
3146
|
-
case "+": return encoded.join(",");
|
|
3147
|
-
case "#": return "#" + encoded.join(",");
|
|
3148
|
-
case ".": return "." + encoded.join(".");
|
|
3149
|
-
case "/": return "/" + encoded.join("/");
|
|
3150
|
-
default: return encoded.join(",");
|
|
3151
|
-
}
|
|
3152
|
-
}
|
|
3153
|
-
expand(variables) {
|
|
3154
|
-
let result = "";
|
|
3155
|
-
let hasQueryParam = false;
|
|
3156
|
-
for (const part of this.parts) {
|
|
3157
|
-
if (typeof part === "string") {
|
|
3158
|
-
result += part;
|
|
3159
|
-
continue;
|
|
3160
|
-
}
|
|
3161
|
-
const expanded = this.expandPart(part, variables);
|
|
3162
|
-
if (!expanded) continue;
|
|
3163
|
-
result += (part.operator === "?" || part.operator === "&") && hasQueryParam ? expanded.replace("?", "&") : expanded;
|
|
3164
|
-
if (part.operator === "?" || part.operator === "&") hasQueryParam = true;
|
|
3165
|
-
}
|
|
3166
|
-
return result;
|
|
3167
|
-
}
|
|
3168
|
-
escapeRegExp(str) {
|
|
3169
|
-
return str.replaceAll(/[.*+?^${}()|[\]\\]/g, String.raw`\$&`);
|
|
3170
|
-
}
|
|
3171
|
-
partToRegExp(part) {
|
|
3172
|
-
const patterns = [];
|
|
3173
|
-
for (const name$1 of part.names) UriTemplate.validateLength(name$1, MAX_VARIABLE_LENGTH, "Variable name");
|
|
3174
|
-
if (part.operator === "?" || part.operator === "&") {
|
|
3175
|
-
for (let i = 0; i < part.names.length; i++) {
|
|
3176
|
-
const name$1 = part.names[i];
|
|
3177
|
-
const prefix = i === 0 ? "\\" + part.operator : "&";
|
|
3178
|
-
patterns.push({
|
|
3179
|
-
pattern: prefix + this.escapeRegExp(name$1) + "=([^&]+)",
|
|
3180
|
-
name: name$1
|
|
3181
|
-
});
|
|
3182
|
-
}
|
|
3183
|
-
return patterns;
|
|
3184
|
-
}
|
|
3185
|
-
let pattern;
|
|
3186
|
-
const name = part.name;
|
|
3187
|
-
switch (part.operator) {
|
|
3188
|
-
case "":
|
|
3189
|
-
pattern = part.exploded ? "([^/,]+(?:,[^/,]+)*)" : "([^/,]+)";
|
|
3190
|
-
break;
|
|
3191
|
-
case "+":
|
|
3192
|
-
case "#":
|
|
3193
|
-
pattern = "(.+)";
|
|
3194
|
-
break;
|
|
3195
|
-
case ".":
|
|
3196
|
-
pattern = String.raw`\.([^/,]+)`;
|
|
3197
|
-
break;
|
|
3198
|
-
case "/":
|
|
3199
|
-
pattern = "/" + (part.exploded ? "([^/,]+(?:,[^/,]+)*)" : "([^/,]+)");
|
|
3200
|
-
break;
|
|
3201
|
-
default: pattern = "([^/]+)";
|
|
3202
|
-
}
|
|
3203
|
-
patterns.push({
|
|
3204
|
-
pattern,
|
|
3205
|
-
name
|
|
3206
|
-
});
|
|
3207
|
-
return patterns;
|
|
3208
|
-
}
|
|
3209
|
-
match(uri) {
|
|
3210
|
-
UriTemplate.validateLength(uri, MAX_TEMPLATE_LENGTH, "URI");
|
|
3211
|
-
let pattern = "^";
|
|
3212
|
-
const names = [];
|
|
3213
|
-
for (const part of this.parts) if (typeof part === "string") pattern += this.escapeRegExp(part);
|
|
3214
|
-
else {
|
|
3215
|
-
const patterns = this.partToRegExp(part);
|
|
3216
|
-
for (const { pattern: partPattern, name } of patterns) {
|
|
3217
|
-
pattern += partPattern;
|
|
3218
|
-
names.push({
|
|
3219
|
-
name,
|
|
3220
|
-
exploded: part.exploded
|
|
3221
|
-
});
|
|
3222
|
-
}
|
|
3223
|
-
}
|
|
3224
|
-
pattern += "$";
|
|
3225
|
-
UriTemplate.validateLength(pattern, MAX_REGEX_LENGTH, "Generated regex pattern");
|
|
3226
|
-
const regex = new RegExp(pattern);
|
|
3227
|
-
const match = uri.match(regex);
|
|
3228
|
-
if (!match) return null;
|
|
3229
|
-
const result = {};
|
|
3230
|
-
for (const [i, name_] of names.entries()) {
|
|
3231
|
-
const { name, exploded } = name_;
|
|
3232
|
-
const value = match[i + 1];
|
|
3233
|
-
const cleanName = name.replace("*", "");
|
|
3234
|
-
result[cleanName] = exploded && value.includes(",") ? value.split(",") : value;
|
|
3235
|
-
}
|
|
3236
|
-
return result;
|
|
3237
|
-
}
|
|
3238
|
-
};
|
|
3239
|
-
|
|
3240
|
-
//#endregion
|
|
3241
|
-
//#region ../core/src/util/standardSchema.ts
|
|
3242
|
-
/**
|
|
3243
|
-
* Converts a StandardSchema to JSON Schema for use as an MCP tool/prompt schema.
|
|
3244
|
-
*
|
|
3245
|
-
* MCP requires `type: "object"` at the root of tool inputSchema/outputSchema and
|
|
3246
|
-
* prompt argument schemas. Zod's discriminated unions emit `{oneOf: [...]}` without
|
|
3247
|
-
* a top-level `type`, so this function defaults `type` to `"object"` when absent.
|
|
3248
|
-
*
|
|
3249
|
-
* Throws if the schema has an explicit non-object `type` (e.g. `z.string()`),
|
|
3250
|
-
* since that cannot satisfy the MCP spec.
|
|
3251
|
-
*/
|
|
3252
|
-
function standardSchemaToJsonSchema(schema, io = "input") {
|
|
3253
|
-
const result = schema["~standard"].jsonSchema[io]({ target: "draft-2020-12" });
|
|
3254
|
-
if (result.type !== void 0 && result.type !== "object") throw new Error(`MCP tool and prompt schemas must describe objects (got type: ${JSON.stringify(result.type)}). Wrap your schema in z.object({...}) or equivalent.`);
|
|
3255
|
-
return {
|
|
3256
|
-
type: "object",
|
|
3257
|
-
...result
|
|
3258
|
-
};
|
|
3259
|
-
}
|
|
3260
|
-
function formatIssue(issue) {
|
|
3261
|
-
if (!issue.path?.length) return issue.message;
|
|
3262
|
-
return `${issue.path.map((p) => String(typeof p === "object" ? p.key : p)).join(".")}: ${issue.message}`;
|
|
3263
|
-
}
|
|
3264
|
-
async function validateStandardSchema(schema, data) {
|
|
3265
|
-
const result = await schema["~standard"].validate(data);
|
|
3266
|
-
if (result.issues && result.issues.length > 0) return {
|
|
3267
|
-
success: false,
|
|
3268
|
-
error: result.issues.map((i) => formatIssue(i)).join(", ")
|
|
3269
|
-
};
|
|
3270
|
-
return {
|
|
3271
|
-
success: true,
|
|
3272
|
-
data: result.value
|
|
3273
|
-
};
|
|
3274
|
-
}
|
|
3275
|
-
function promptArgumentsFromStandardSchema(schema) {
|
|
3276
|
-
const jsonSchema = standardSchemaToJsonSchema(schema, "input");
|
|
3277
|
-
const properties = jsonSchema.properties || {};
|
|
3278
|
-
const required = jsonSchema.required || [];
|
|
3279
|
-
return Object.entries(properties).map(([name, prop]) => ({
|
|
3280
|
-
name,
|
|
3281
|
-
description: prop?.description,
|
|
3282
|
-
required: required.includes(name)
|
|
3283
|
-
}));
|
|
3284
|
-
}
|
|
3285
|
-
|
|
3286
|
-
//#endregion
|
|
3287
3
|
//#region ../../node_modules/.pnpm/ajv@8.18.0/node_modules/ajv/dist/compile/codegen/code.js
|
|
3288
4
|
var require_code$1 = /* @__PURE__ */ __commonJSMin(((exports) => {
|
|
3289
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
@@ -9459,7 +6175,7 @@ var require_dist = /* @__PURE__ */ __commonJSMin(((exports, module) => {
|
|
|
9459
6175
|
}));
|
|
9460
6176
|
|
|
9461
6177
|
//#endregion
|
|
9462
|
-
//#region ../core/src/validators/ajvProvider.ts
|
|
6178
|
+
//#region ../core-internal/src/validators/ajvProvider.ts
|
|
9463
6179
|
var import_ajv = require_ajv();
|
|
9464
6180
|
var import_dist = /* @__PURE__ */ __toESM(require_dist(), 1);
|
|
9465
6181
|
function createDefaultAjvInstance() {
|
|
@@ -9473,50 +6189,38 @@ function createDefaultAjvInstance() {
|
|
|
9473
6189
|
return ajv;
|
|
9474
6190
|
}
|
|
9475
6191
|
/**
|
|
9476
|
-
*
|
|
6192
|
+
* AJV-backed JSON Schema validator. See `@modelcontextprotocol/{client,server}/validators/ajv`
|
|
6193
|
+
* for the customisation entry point (re-exports `Ajv` and `addFormats` from the bundled copy).
|
|
6194
|
+
*
|
|
6195
|
+
* @example Use with default configuration
|
|
9477
6196
|
* ```ts source="./ajvProvider.examples.ts#AjvJsonSchemaValidator_default"
|
|
9478
6197
|
* const validator = new AjvJsonSchemaValidator();
|
|
9479
6198
|
* ```
|
|
9480
6199
|
*
|
|
9481
|
-
* @example Use with custom AJV instance
|
|
6200
|
+
* @example Use with a custom AJV instance
|
|
9482
6201
|
* ```ts source="./ajvProvider.examples.ts#AjvJsonSchemaValidator_customInstance"
|
|
9483
6202
|
* const ajv = new Ajv({ strict: true, allErrors: true });
|
|
9484
6203
|
* const validator = new AjvJsonSchemaValidator(ajv);
|
|
9485
6204
|
* ```
|
|
9486
6205
|
*
|
|
9487
|
-
* @
|
|
6206
|
+
* @example Register ajv-formats
|
|
6207
|
+
* ```ts source="./ajvProvider.examples.ts#AjvJsonSchemaValidator_withFormats"
|
|
6208
|
+
* const ajv = new Ajv({ strict: true, allErrors: true });
|
|
6209
|
+
* addFormats(ajv);
|
|
6210
|
+
* const validator = new AjvJsonSchemaValidator(ajv);
|
|
6211
|
+
* ```
|
|
9488
6212
|
*/
|
|
9489
6213
|
var AjvJsonSchemaValidator = class {
|
|
9490
6214
|
_ajv;
|
|
9491
6215
|
/**
|
|
9492
|
-
*
|
|
9493
|
-
*
|
|
9494
|
-
*
|
|
9495
|
-
*
|
|
9496
|
-
* @example Use default configuration (recommended for most cases)
|
|
9497
|
-
* ```ts source="./ajvProvider.examples.ts#AjvJsonSchemaValidator_default"
|
|
9498
|
-
* const validator = new AjvJsonSchemaValidator();
|
|
9499
|
-
* ```
|
|
9500
|
-
*
|
|
9501
|
-
* @example Provide custom AJV instance for advanced configuration
|
|
9502
|
-
* ```ts source="./ajvProvider.examples.ts#AjvJsonSchemaValidator_constructor_withFormats"
|
|
9503
|
-
* const ajv = new Ajv({ validateFormats: true });
|
|
9504
|
-
* addFormats(ajv);
|
|
9505
|
-
* const validator = new AjvJsonSchemaValidator(ajv);
|
|
9506
|
-
* ```
|
|
6216
|
+
* @param ajv - Optional pre-configured AJV-compatible instance. If omitted, a default instance is
|
|
6217
|
+
* created with `strict: false`, `validateFormats: true`, `validateSchema: false`, `allErrors: true`,
|
|
6218
|
+
* and `ajv-formats` registered. The parameter is typed structurally so consumers who don't pass
|
|
6219
|
+
* an instance need not have `ajv` installed.
|
|
9507
6220
|
*/
|
|
9508
6221
|
constructor(ajv) {
|
|
9509
6222
|
this._ajv = ajv ?? createDefaultAjvInstance();
|
|
9510
6223
|
}
|
|
9511
|
-
/**
|
|
9512
|
-
* Create a validator for the given JSON Schema
|
|
9513
|
-
*
|
|
9514
|
-
* The validator is compiled once and can be reused multiple times.
|
|
9515
|
-
* If the schema has an `$id`, it will be cached by AJV automatically.
|
|
9516
|
-
*
|
|
9517
|
-
* @param schema - Standard JSON Schema object
|
|
9518
|
-
* @returns A validator function that validates input data
|
|
9519
|
-
*/
|
|
9520
6224
|
getValidator(schema) {
|
|
9521
6225
|
const ajvValidator = "$id" in schema && typeof schema.$id === "string" ? this._ajv.getSchema(schema.$id) ?? this._ajv.compile(schema) : this._ajv.compile(schema);
|
|
9522
6226
|
return (input) => {
|
|
@@ -9532,113 +6236,9 @@ var AjvJsonSchemaValidator = class {
|
|
|
9532
6236
|
};
|
|
9533
6237
|
}
|
|
9534
6238
|
};
|
|
6239
|
+
/** `ajv-formats` default export, normalised through the CJS/ESM interop wrapper. */
|
|
6240
|
+
const addFormats = import_dist.default;
|
|
9535
6241
|
|
|
9536
6242
|
//#endregion
|
|
9537
|
-
|
|
9538
|
-
|
|
9539
|
-
* Cloudflare Worker-compatible JSON Schema validator provider
|
|
9540
|
-
*
|
|
9541
|
-
* This provider uses @cfworker/json-schema for validation without code generation,
|
|
9542
|
-
* making it compatible with edge runtimes like Cloudflare Workers that restrict
|
|
9543
|
-
* eval and new Function.
|
|
9544
|
-
*
|
|
9545
|
-
* @see {@linkcode AjvJsonSchemaValidator} for the Node.js alternative
|
|
9546
|
-
*/
|
|
9547
|
-
/**
|
|
9548
|
-
*
|
|
9549
|
-
* @example Use with default configuration (2020-12, shortcircuit)
|
|
9550
|
-
* ```ts source="./cfWorkerProvider.examples.ts#CfWorkerJsonSchemaValidator_default"
|
|
9551
|
-
* const validator = new CfWorkerJsonSchemaValidator();
|
|
9552
|
-
* ```
|
|
9553
|
-
*
|
|
9554
|
-
* @example Use with custom configuration
|
|
9555
|
-
* ```ts source="./cfWorkerProvider.examples.ts#CfWorkerJsonSchemaValidator_customConfig"
|
|
9556
|
-
* const validator = new CfWorkerJsonSchemaValidator({
|
|
9557
|
-
* draft: '2020-12',
|
|
9558
|
-
* shortcircuit: false // Report all errors
|
|
9559
|
-
* });
|
|
9560
|
-
* ```
|
|
9561
|
-
*/
|
|
9562
|
-
var CfWorkerJsonSchemaValidator = class {
|
|
9563
|
-
shortcircuit;
|
|
9564
|
-
draft;
|
|
9565
|
-
/**
|
|
9566
|
-
* Create a validator
|
|
9567
|
-
*
|
|
9568
|
-
* @param options - Configuration options
|
|
9569
|
-
* @param options.shortcircuit - If `true`, stop validation after first error (default: `true`)
|
|
9570
|
-
* @param options.draft - JSON Schema draft version to use (default: `'2020-12'`)
|
|
9571
|
-
*/
|
|
9572
|
-
constructor(options) {
|
|
9573
|
-
this.shortcircuit = options?.shortcircuit ?? true;
|
|
9574
|
-
this.draft = options?.draft ?? "2020-12";
|
|
9575
|
-
}
|
|
9576
|
-
/**
|
|
9577
|
-
* Create a validator for the given JSON Schema
|
|
9578
|
-
*
|
|
9579
|
-
* Unlike AJV, this validator is not cached internally
|
|
9580
|
-
*
|
|
9581
|
-
* @param schema - Standard JSON Schema object
|
|
9582
|
-
* @returns A validator function that validates input data
|
|
9583
|
-
*/
|
|
9584
|
-
getValidator(schema) {
|
|
9585
|
-
const validator = new Validator(schema, this.draft, this.shortcircuit);
|
|
9586
|
-
return (input) => {
|
|
9587
|
-
const result = validator.validate(input);
|
|
9588
|
-
return result.valid ? {
|
|
9589
|
-
valid: true,
|
|
9590
|
-
data: input,
|
|
9591
|
-
errorMessage: void 0
|
|
9592
|
-
} : {
|
|
9593
|
-
valid: false,
|
|
9594
|
-
data: void 0,
|
|
9595
|
-
errorMessage: result.errors.map((err) => `${err.instanceLocation}: ${err.error}`).join("; ")
|
|
9596
|
-
};
|
|
9597
|
-
};
|
|
9598
|
-
}
|
|
9599
|
-
};
|
|
9600
|
-
|
|
9601
|
-
//#endregion
|
|
9602
|
-
//#region ../core/src/validators/fromJsonSchema.ts
|
|
9603
|
-
/**
|
|
9604
|
-
* Wrap a raw JSON Schema object as a {@linkcode StandardSchemaWithJSON} so it can be
|
|
9605
|
-
* passed to `registerTool` / `registerPrompt`. Use this when you already have JSON
|
|
9606
|
-
* Schema (e.g. from TypeBox, or hand-written) and want to register it without going
|
|
9607
|
-
* through a Standard Schema library.
|
|
9608
|
-
*
|
|
9609
|
-
* The callback arguments will be typed `unknown` (raw JSON Schema has no TypeScript
|
|
9610
|
-
* types attached). Cast at the call site, or use the generic `fromJsonSchema<MyType>(...)`.
|
|
9611
|
-
*
|
|
9612
|
-
* @param schema - A JSON Schema object describing the expected shape
|
|
9613
|
-
* @param validator - A validator provider. When importing `fromJsonSchema` from
|
|
9614
|
-
* `@modelcontextprotocol/server` or `@modelcontextprotocol/client`, a runtime-appropriate
|
|
9615
|
-
* default is provided automatically (AJV on Node.js, CfWorker on edge runtimes).
|
|
9616
|
-
*
|
|
9617
|
-
* @example
|
|
9618
|
-
* ```ts source="./fromJsonSchema.examples.ts#fromJsonSchema_basicUsage"
|
|
9619
|
-
* const inputSchema = fromJsonSchema<{ name: string }>(
|
|
9620
|
-
* { type: 'object', properties: { name: { type: 'string' } }, required: ['name'] },
|
|
9621
|
-
* new AjvJsonSchemaValidator()
|
|
9622
|
-
* );
|
|
9623
|
-
* // Use with server.registerTool('greet', { inputSchema }, handler)
|
|
9624
|
-
* ```
|
|
9625
|
-
*/
|
|
9626
|
-
function fromJsonSchema(schema, validator) {
|
|
9627
|
-
const check = validator.getValidator(schema);
|
|
9628
|
-
return { "~standard": {
|
|
9629
|
-
version: 1,
|
|
9630
|
-
vendor: "mcp",
|
|
9631
|
-
jsonSchema: {
|
|
9632
|
-
input: () => schema,
|
|
9633
|
-
output: () => schema
|
|
9634
|
-
},
|
|
9635
|
-
validate: (data) => {
|
|
9636
|
-
const result = check(data);
|
|
9637
|
-
return result.valid ? { value: result.data } : { issues: [{ message: result.errorMessage }] };
|
|
9638
|
-
}
|
|
9639
|
-
} };
|
|
9640
|
-
}
|
|
9641
|
-
|
|
9642
|
-
//#endregion
|
|
9643
|
-
export { PARSE_ERROR as $, parseJSONRPCMessage as A, ListRootsResultSchema as B, isInitializeRequest as C, isJSONRPCRequest as D, isJSONRPCNotification as E, CreateTaskResultSchema as F, ProtocolErrorCode as G, getResultSchema as H, ElicitResultSchema as I, INVALID_PARAMS as J, DEFAULT_NEGOTIATED_PROTOCOL_VERSION as K, EmptyResultSchema as L, CallToolResultSchema as M, CreateMessageResultSchema as N, isJSONRPCResultResponse as O, CreateMessageResultWithToolsSchema as P, METHOD_NOT_FOUND as Q, GetTaskPayloadResultSchema as R, assertCompleteRequestResourceTemplate as S, isJSONRPCErrorResponse as T, ProtocolError as U, LoggingLevelSchema as V, UrlElicitationRequiredError as W, JSONRPC_VERSION as X, INVALID_REQUEST as Y, LATEST_PROTOCOL_VERSION as Z, mergeCapabilities as _, standardSchemaToJsonSchema as a, SdkError as at, parseSchema as b, createFetchWithInit as c, OAuthErrorCode as ct, deserializeMessage as d, RELATED_TASK_META_KEY as et, serializeMessage as f, Protocol as g, DEFAULT_REQUEST_TIMEOUT_MSEC as h, promptArgumentsFromStandardSchema as i, resourceUrlFromServerUrl as it, CallToolRequestSchema as j, isTaskAugmentedRequestParams as k, validateAndWarnToolName as l, toArrayAsync as m, CfWorkerJsonSchemaValidator as n, getDisplayName as nt, validateStandardSchema as o, SdkErrorCode as ot, takeResult as p, INTERNAL_ERROR as q, AjvJsonSchemaValidator as r, checkResourceAllowed as rt, UriTemplate as s, OAuthError as st, fromJsonSchema as t, SUPPORTED_PROTOCOL_VERSIONS as tt, ReadBuffer as u, extractTaskManagerOptions as v, isInitializedNotification as w, assertCompleteRequestPrompt as x, isTerminal as y, JSONRPCMessageSchema as z };
|
|
9644
|
-
//# sourceMappingURL=src-IKPjmxu7.mjs.map
|
|
6243
|
+
export { addFormats as n, import_ajv as r, AjvJsonSchemaValidator as t };
|
|
6244
|
+
//# sourceMappingURL=ajvProvider-Birb50r-.mjs.map
|