@ai-sdk/mcp 1.0.46 → 1.0.48
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/CHANGELOG.md +15 -0
- package/dist/index.d.mts +20 -1
- package/dist/index.d.ts +20 -1
- package/dist/index.js +197 -24
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +197 -24
- package/dist/index.mjs.map +1 -1
- package/dist/mcp-stdio/index.d.mts +4 -0
- package/dist/mcp-stdio/index.d.ts +4 -0
- package/package.json +4 -4
- package/src/index.ts +4 -1
- package/src/tool/mcp-client.ts +5 -1
- package/src/tool/mcp-http-transport.ts +4 -0
- package/src/tool/mcp-sse-transport.ts +4 -0
- package/src/tool/mcp-transport.ts +5 -0
- package/src/tool/oauth-types.ts +18 -14
- package/src/tool/oauth.ts +265 -19
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,20 @@
|
|
|
1
1
|
# @ai-sdk/mcp
|
|
2
2
|
|
|
3
|
+
## 1.0.48
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 26d93a4: fix(mcp): add optional hook to validate authorization servers
|
|
8
|
+
- 3c9ad04: fix(mcp): support official sdk protocol version negotiation
|
|
9
|
+
- Updated dependencies [942f2f8]
|
|
10
|
+
- @ai-sdk/provider-utils@4.0.28
|
|
11
|
+
|
|
12
|
+
## 1.0.47
|
|
13
|
+
|
|
14
|
+
### Patch Changes
|
|
15
|
+
|
|
16
|
+
- bf1d6bd: fix(mcp): prevent mcp oauth credential exfiltration during rediscovery
|
|
17
|
+
|
|
3
18
|
## 1.0.46
|
|
4
19
|
|
|
5
20
|
### Patch Changes
|
package/dist/index.d.mts
CHANGED
|
@@ -77,6 +77,8 @@ declare const OAuthTokensSchema: z.ZodObject<{
|
|
|
77
77
|
expires_in: z.ZodOptional<z.ZodNumber>;
|
|
78
78
|
scope: z.ZodOptional<z.ZodString>;
|
|
79
79
|
refresh_token: z.ZodOptional<z.ZodString>;
|
|
80
|
+
authorization_server: z.ZodOptional<z.ZodString>;
|
|
81
|
+
token_endpoint: z.ZodOptional<z.ZodString>;
|
|
80
82
|
}, z.core.$strip>;
|
|
81
83
|
declare const OAuthMetadataSchema: z.ZodObject<{
|
|
82
84
|
issuer: z.ZodString;
|
|
@@ -116,6 +118,8 @@ declare const OAuthClientInformationSchema: z.ZodObject<{
|
|
|
116
118
|
client_secret: z.ZodOptional<z.ZodString>;
|
|
117
119
|
client_id_issued_at: z.ZodOptional<z.ZodNumber>;
|
|
118
120
|
client_secret_expires_at: z.ZodOptional<z.ZodNumber>;
|
|
121
|
+
authorization_server: z.ZodOptional<z.ZodString>;
|
|
122
|
+
token_endpoint: z.ZodOptional<z.ZodString>;
|
|
119
123
|
}, z.core.$strip>;
|
|
120
124
|
declare const OAuthClientMetadataSchema: z.ZodObject<{
|
|
121
125
|
redirect_uris: z.ZodArray<z.ZodString>;
|
|
@@ -143,6 +147,10 @@ type AuthorizationServerMetadata = OAuthMetadata | OpenIdProviderDiscoveryMetada
|
|
|
143
147
|
type OAuthClientMetadata = z.infer<typeof OAuthClientMetadataSchema>;
|
|
144
148
|
|
|
145
149
|
type AuthResult = 'AUTHORIZED' | 'REDIRECT';
|
|
150
|
+
interface OAuthAuthorizationServerInformation {
|
|
151
|
+
authorizationServerUrl: string;
|
|
152
|
+
tokenEndpoint: string;
|
|
153
|
+
}
|
|
146
154
|
interface OAuthClientProvider {
|
|
147
155
|
/**
|
|
148
156
|
* Returns current access token if present; undefined otherwise.
|
|
@@ -181,6 +189,13 @@ interface OAuthClientProvider {
|
|
|
181
189
|
get clientMetadata(): OAuthClientMetadata;
|
|
182
190
|
clientInformation(): OAuthClientInformation | undefined | Promise<OAuthClientInformation | undefined>;
|
|
183
191
|
saveClientInformation?(clientInformation: OAuthClientInformation): void | Promise<void>;
|
|
192
|
+
authorizationServerInformation?(): OAuthAuthorizationServerInformation | undefined | Promise<OAuthAuthorizationServerInformation | undefined>;
|
|
193
|
+
saveAuthorizationServerInformation?(authorizationServerInformation: OAuthAuthorizationServerInformation): void | Promise<void>;
|
|
194
|
+
/**
|
|
195
|
+
* Validates an authorization server URL discovered from MCP protected resource
|
|
196
|
+
* metadata before the client fetches its OAuth metadata.
|
|
197
|
+
*/
|
|
198
|
+
validateAuthorizationServerURL?(serverUrl: string | URL, authorizationServerUrl: string | URL): void | Promise<void>;
|
|
184
199
|
state?(): string | Promise<string>;
|
|
185
200
|
saveState?(state: string): void | Promise<void>;
|
|
186
201
|
storedState?(): string | undefined | Promise<string | undefined>;
|
|
@@ -232,6 +247,10 @@ interface MCPTransport {
|
|
|
232
247
|
* The protocol version negotiated during initialization.
|
|
233
248
|
*/
|
|
234
249
|
protocolVersion?: string;
|
|
250
|
+
/**
|
|
251
|
+
* Set the protocol version negotiated during initialization.
|
|
252
|
+
*/
|
|
253
|
+
setProtocolVersion?(version: string): void;
|
|
235
254
|
}
|
|
236
255
|
type MCPTransportConfig = {
|
|
237
256
|
type: 'sse' | 'http';
|
|
@@ -561,4 +580,4 @@ interface MCPClient {
|
|
|
561
580
|
close: () => Promise<void>;
|
|
562
581
|
}
|
|
563
582
|
|
|
564
|
-
export { type Configuration, type ElicitResult, ElicitResultSchema, type ElicitationRequest, ElicitationRequestSchema, type JSONRPCError, type JSONRPCMessage, type JSONRPCNotification, type JSONRPCRequest, type JSONRPCResponse, type ListToolsResult, type MCPClient, type ClientCapabilities as MCPClientCapabilities, type MCPClientConfig, type MCPTransport, type OAuthClientInformation, type OAuthClientMetadata, type OAuthClientProvider, type OAuthTokens, UnauthorizedError, auth, createMCPClient, type MCPClient as experimental_MCPClient, type ClientCapabilities as experimental_MCPClientCapabilities, type MCPClientConfig as experimental_MCPClientConfig, createMCPClient as experimental_createMCPClient };
|
|
583
|
+
export { type Configuration, type ElicitResult, ElicitResultSchema, type ElicitationRequest, ElicitationRequestSchema, type JSONRPCError, type JSONRPCMessage, type JSONRPCNotification, type JSONRPCRequest, type JSONRPCResponse, type ListToolsResult, type MCPClient, type ClientCapabilities as MCPClientCapabilities, type MCPClientConfig, type MCPTransport, type OAuthAuthorizationServerInformation, type OAuthClientInformation, type OAuthClientMetadata, type OAuthClientProvider, type OAuthTokens, UnauthorizedError, auth, createMCPClient, type MCPClient as experimental_MCPClient, type ClientCapabilities as experimental_MCPClientCapabilities, type MCPClientConfig as experimental_MCPClientConfig, createMCPClient as experimental_createMCPClient };
|
package/dist/index.d.ts
CHANGED
|
@@ -77,6 +77,8 @@ declare const OAuthTokensSchema: z.ZodObject<{
|
|
|
77
77
|
expires_in: z.ZodOptional<z.ZodNumber>;
|
|
78
78
|
scope: z.ZodOptional<z.ZodString>;
|
|
79
79
|
refresh_token: z.ZodOptional<z.ZodString>;
|
|
80
|
+
authorization_server: z.ZodOptional<z.ZodString>;
|
|
81
|
+
token_endpoint: z.ZodOptional<z.ZodString>;
|
|
80
82
|
}, z.core.$strip>;
|
|
81
83
|
declare const OAuthMetadataSchema: z.ZodObject<{
|
|
82
84
|
issuer: z.ZodString;
|
|
@@ -116,6 +118,8 @@ declare const OAuthClientInformationSchema: z.ZodObject<{
|
|
|
116
118
|
client_secret: z.ZodOptional<z.ZodString>;
|
|
117
119
|
client_id_issued_at: z.ZodOptional<z.ZodNumber>;
|
|
118
120
|
client_secret_expires_at: z.ZodOptional<z.ZodNumber>;
|
|
121
|
+
authorization_server: z.ZodOptional<z.ZodString>;
|
|
122
|
+
token_endpoint: z.ZodOptional<z.ZodString>;
|
|
119
123
|
}, z.core.$strip>;
|
|
120
124
|
declare const OAuthClientMetadataSchema: z.ZodObject<{
|
|
121
125
|
redirect_uris: z.ZodArray<z.ZodString>;
|
|
@@ -143,6 +147,10 @@ type AuthorizationServerMetadata = OAuthMetadata | OpenIdProviderDiscoveryMetada
|
|
|
143
147
|
type OAuthClientMetadata = z.infer<typeof OAuthClientMetadataSchema>;
|
|
144
148
|
|
|
145
149
|
type AuthResult = 'AUTHORIZED' | 'REDIRECT';
|
|
150
|
+
interface OAuthAuthorizationServerInformation {
|
|
151
|
+
authorizationServerUrl: string;
|
|
152
|
+
tokenEndpoint: string;
|
|
153
|
+
}
|
|
146
154
|
interface OAuthClientProvider {
|
|
147
155
|
/**
|
|
148
156
|
* Returns current access token if present; undefined otherwise.
|
|
@@ -181,6 +189,13 @@ interface OAuthClientProvider {
|
|
|
181
189
|
get clientMetadata(): OAuthClientMetadata;
|
|
182
190
|
clientInformation(): OAuthClientInformation | undefined | Promise<OAuthClientInformation | undefined>;
|
|
183
191
|
saveClientInformation?(clientInformation: OAuthClientInformation): void | Promise<void>;
|
|
192
|
+
authorizationServerInformation?(): OAuthAuthorizationServerInformation | undefined | Promise<OAuthAuthorizationServerInformation | undefined>;
|
|
193
|
+
saveAuthorizationServerInformation?(authorizationServerInformation: OAuthAuthorizationServerInformation): void | Promise<void>;
|
|
194
|
+
/**
|
|
195
|
+
* Validates an authorization server URL discovered from MCP protected resource
|
|
196
|
+
* metadata before the client fetches its OAuth metadata.
|
|
197
|
+
*/
|
|
198
|
+
validateAuthorizationServerURL?(serverUrl: string | URL, authorizationServerUrl: string | URL): void | Promise<void>;
|
|
184
199
|
state?(): string | Promise<string>;
|
|
185
200
|
saveState?(state: string): void | Promise<void>;
|
|
186
201
|
storedState?(): string | undefined | Promise<string | undefined>;
|
|
@@ -232,6 +247,10 @@ interface MCPTransport {
|
|
|
232
247
|
* The protocol version negotiated during initialization.
|
|
233
248
|
*/
|
|
234
249
|
protocolVersion?: string;
|
|
250
|
+
/**
|
|
251
|
+
* Set the protocol version negotiated during initialization.
|
|
252
|
+
*/
|
|
253
|
+
setProtocolVersion?(version: string): void;
|
|
235
254
|
}
|
|
236
255
|
type MCPTransportConfig = {
|
|
237
256
|
type: 'sse' | 'http';
|
|
@@ -561,4 +580,4 @@ interface MCPClient {
|
|
|
561
580
|
close: () => Promise<void>;
|
|
562
581
|
}
|
|
563
582
|
|
|
564
|
-
export { type Configuration, type ElicitResult, ElicitResultSchema, type ElicitationRequest, ElicitationRequestSchema, type JSONRPCError, type JSONRPCMessage, type JSONRPCNotification, type JSONRPCRequest, type JSONRPCResponse, type ListToolsResult, type MCPClient, type ClientCapabilities as MCPClientCapabilities, type MCPClientConfig, type MCPTransport, type OAuthClientInformation, type OAuthClientMetadata, type OAuthClientProvider, type OAuthTokens, UnauthorizedError, auth, createMCPClient, type MCPClient as experimental_MCPClient, type ClientCapabilities as experimental_MCPClientCapabilities, type MCPClientConfig as experimental_MCPClientConfig, createMCPClient as experimental_createMCPClient };
|
|
583
|
+
export { type Configuration, type ElicitResult, ElicitResultSchema, type ElicitationRequest, ElicitationRequestSchema, type JSONRPCError, type JSONRPCMessage, type JSONRPCNotification, type JSONRPCRequest, type JSONRPCResponse, type ListToolsResult, type MCPClient, type ClientCapabilities as MCPClientCapabilities, type MCPClientConfig, type MCPTransport, type OAuthAuthorizationServerInformation, type OAuthClientInformation, type OAuthClientMetadata, type OAuthClientProvider, type OAuthTokens, UnauthorizedError, auth, createMCPClient, type MCPClient as experimental_MCPClient, type ClientCapabilities as experimental_MCPClientCapabilities, type MCPClientConfig as experimental_MCPClientConfig, createMCPClient as experimental_createMCPClient };
|
package/dist/index.js
CHANGED
|
@@ -342,15 +342,6 @@ var import_pkce_challenge = __toESM(require("pkce-challenge"));
|
|
|
342
342
|
|
|
343
343
|
// src/tool/oauth-types.ts
|
|
344
344
|
var import_v43 = require("zod/v4");
|
|
345
|
-
var OAuthTokensSchema = import_v43.z.object({
|
|
346
|
-
access_token: import_v43.z.string(),
|
|
347
|
-
id_token: import_v43.z.string().optional(),
|
|
348
|
-
// Optional for OAuth 2.1, but necessary in OpenID Connect
|
|
349
|
-
token_type: import_v43.z.string(),
|
|
350
|
-
expires_in: import_v43.z.number().optional(),
|
|
351
|
-
scope: import_v43.z.string().optional(),
|
|
352
|
-
refresh_token: import_v43.z.string().optional()
|
|
353
|
-
}).strip();
|
|
354
345
|
var SafeUrlSchema = import_v43.z.string().url().superRefine((val, ctx) => {
|
|
355
346
|
if (!URL.canParse(val)) {
|
|
356
347
|
ctx.addIssue({
|
|
@@ -367,6 +358,17 @@ var SafeUrlSchema = import_v43.z.string().url().superRefine((val, ctx) => {
|
|
|
367
358
|
},
|
|
368
359
|
{ message: "URL cannot use javascript:, data:, or vbscript: scheme" }
|
|
369
360
|
);
|
|
361
|
+
var OAuthTokensSchema = import_v43.z.object({
|
|
362
|
+
access_token: import_v43.z.string(),
|
|
363
|
+
id_token: import_v43.z.string().optional(),
|
|
364
|
+
// Optional for OAuth 2.1, but necessary in OpenID Connect
|
|
365
|
+
token_type: import_v43.z.string(),
|
|
366
|
+
expires_in: import_v43.z.number().optional(),
|
|
367
|
+
scope: import_v43.z.string().optional(),
|
|
368
|
+
refresh_token: import_v43.z.string().optional(),
|
|
369
|
+
authorization_server: SafeUrlSchema.optional(),
|
|
370
|
+
token_endpoint: SafeUrlSchema.optional()
|
|
371
|
+
}).strip();
|
|
370
372
|
var OAuthProtectedResourceMetadataSchema = import_v43.z.object({
|
|
371
373
|
resource: import_v43.z.string().url(),
|
|
372
374
|
authorization_servers: import_v43.z.array(SafeUrlSchema).optional(),
|
|
@@ -419,7 +421,9 @@ var OAuthClientInformationSchema = import_v43.z.object({
|
|
|
419
421
|
client_id: import_v43.z.string(),
|
|
420
422
|
client_secret: import_v43.z.string().optional(),
|
|
421
423
|
client_id_issued_at: import_v43.z.number().optional(),
|
|
422
|
-
client_secret_expires_at: import_v43.z.number().optional()
|
|
424
|
+
client_secret_expires_at: import_v43.z.number().optional(),
|
|
425
|
+
authorization_server: SafeUrlSchema.optional(),
|
|
426
|
+
token_endpoint: SafeUrlSchema.optional()
|
|
423
427
|
}).strip();
|
|
424
428
|
var OAuthClientMetadataSchema = import_v43.z.object({
|
|
425
429
|
redirect_uris: import_v43.z.array(SafeUrlSchema),
|
|
@@ -524,6 +528,106 @@ var UnauthorizedError = class extends Error {
|
|
|
524
528
|
this.name = "UnauthorizedError";
|
|
525
529
|
}
|
|
526
530
|
};
|
|
531
|
+
function normalizeUrl(url) {
|
|
532
|
+
return new URL(url).href;
|
|
533
|
+
}
|
|
534
|
+
function createAuthorizationServerInformation(authorizationServerUrl, metadata) {
|
|
535
|
+
return {
|
|
536
|
+
authorizationServerUrl: normalizeUrl(authorizationServerUrl),
|
|
537
|
+
tokenEndpoint: normalizeUrl(
|
|
538
|
+
(metadata == null ? void 0 : metadata.token_endpoint) ? new URL(metadata.token_endpoint) : new URL("/token", authorizationServerUrl)
|
|
539
|
+
)
|
|
540
|
+
};
|
|
541
|
+
}
|
|
542
|
+
function addAuthorizationServerInformationToTokens(tokens, authorizationServerInformation) {
|
|
543
|
+
return {
|
|
544
|
+
...tokens,
|
|
545
|
+
authorization_server: authorizationServerInformation.authorizationServerUrl,
|
|
546
|
+
token_endpoint: authorizationServerInformation.tokenEndpoint
|
|
547
|
+
};
|
|
548
|
+
}
|
|
549
|
+
function addAuthorizationServerInformationToClientInformation(clientInformation, authorizationServerInformation) {
|
|
550
|
+
return {
|
|
551
|
+
...clientInformation,
|
|
552
|
+
authorization_server: authorizationServerInformation.authorizationServerUrl,
|
|
553
|
+
token_endpoint: authorizationServerInformation.tokenEndpoint
|
|
554
|
+
};
|
|
555
|
+
}
|
|
556
|
+
function getAuthorizationServerInformationFromCredentials(credentials) {
|
|
557
|
+
if (!(credentials == null ? void 0 : credentials.authorization_server) || !credentials.token_endpoint) {
|
|
558
|
+
return void 0;
|
|
559
|
+
}
|
|
560
|
+
return {
|
|
561
|
+
authorizationServerUrl: normalizeUrl(credentials.authorization_server),
|
|
562
|
+
tokenEndpoint: normalizeUrl(credentials.token_endpoint)
|
|
563
|
+
};
|
|
564
|
+
}
|
|
565
|
+
async function getStoredAuthorizationServerInformation({
|
|
566
|
+
provider,
|
|
567
|
+
clientInformation,
|
|
568
|
+
tokens
|
|
569
|
+
}) {
|
|
570
|
+
var _a3;
|
|
571
|
+
const tokenAuthorizationServerInformation = getAuthorizationServerInformationFromCredentials(tokens);
|
|
572
|
+
if (tokenAuthorizationServerInformation) {
|
|
573
|
+
return tokenAuthorizationServerInformation;
|
|
574
|
+
}
|
|
575
|
+
const providerAuthorizationServerInformation = await ((_a3 = provider.authorizationServerInformation) == null ? void 0 : _a3.call(provider));
|
|
576
|
+
if (providerAuthorizationServerInformation) {
|
|
577
|
+
return {
|
|
578
|
+
authorizationServerUrl: normalizeUrl(
|
|
579
|
+
providerAuthorizationServerInformation.authorizationServerUrl
|
|
580
|
+
),
|
|
581
|
+
tokenEndpoint: normalizeUrl(
|
|
582
|
+
providerAuthorizationServerInformation.tokenEndpoint
|
|
583
|
+
)
|
|
584
|
+
};
|
|
585
|
+
}
|
|
586
|
+
return getAuthorizationServerInformationFromCredentials(clientInformation);
|
|
587
|
+
}
|
|
588
|
+
async function saveAuthorizationServerInformation({
|
|
589
|
+
provider,
|
|
590
|
+
clientInformation,
|
|
591
|
+
authorizationServerInformation
|
|
592
|
+
}) {
|
|
593
|
+
if (provider.saveAuthorizationServerInformation) {
|
|
594
|
+
await provider.saveAuthorizationServerInformation(
|
|
595
|
+
authorizationServerInformation
|
|
596
|
+
);
|
|
597
|
+
return true;
|
|
598
|
+
}
|
|
599
|
+
if (provider.saveClientInformation) {
|
|
600
|
+
await provider.saveClientInformation(
|
|
601
|
+
addAuthorizationServerInformationToClientInformation(
|
|
602
|
+
clientInformation,
|
|
603
|
+
authorizationServerInformation
|
|
604
|
+
)
|
|
605
|
+
);
|
|
606
|
+
return true;
|
|
607
|
+
}
|
|
608
|
+
return false;
|
|
609
|
+
}
|
|
610
|
+
function assertResourceMetadataUrlSameOrigin(serverUrl, resourceMetadataUrl) {
|
|
611
|
+
if (!resourceMetadataUrl) {
|
|
612
|
+
return;
|
|
613
|
+
}
|
|
614
|
+
const expectedOrigin = new URL(serverUrl).origin;
|
|
615
|
+
if (resourceMetadataUrl.origin !== expectedOrigin) {
|
|
616
|
+
throw new MCPClientOAuthError({
|
|
617
|
+
message: `OAuth protected resource metadata URL ${resourceMetadataUrl.href} must have the same origin as the MCP server URL ${expectedOrigin}`
|
|
618
|
+
});
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
function assertAuthorizationServerInformationMatches({
|
|
622
|
+
storedAuthorizationServerInformation,
|
|
623
|
+
currentAuthorizationServerInformation
|
|
624
|
+
}) {
|
|
625
|
+
if (storedAuthorizationServerInformation.authorizationServerUrl !== currentAuthorizationServerInformation.authorizationServerUrl || storedAuthorizationServerInformation.tokenEndpoint !== currentAuthorizationServerInformation.tokenEndpoint) {
|
|
626
|
+
throw new MCPClientOAuthError({
|
|
627
|
+
message: "OAuth authorization server metadata does not match the metadata that issued the stored credentials"
|
|
628
|
+
});
|
|
629
|
+
}
|
|
630
|
+
}
|
|
527
631
|
function extractResourceMetadataUrl(response) {
|
|
528
632
|
var _a3;
|
|
529
633
|
const header = (_a3 = response.headers.get("www-authenticate")) != null ? _a3 : response.headers.get("WWW-Authenticate");
|
|
@@ -1001,8 +1105,10 @@ async function authInternal(provider, {
|
|
|
1001
1105
|
resourceMetadataUrl,
|
|
1002
1106
|
fetchFn
|
|
1003
1107
|
}) {
|
|
1108
|
+
var _a3, _b3;
|
|
1004
1109
|
let resourceMetadata;
|
|
1005
1110
|
let authorizationServerUrl;
|
|
1111
|
+
assertResourceMetadataUrlSameOrigin(serverUrl, resourceMetadataUrl);
|
|
1006
1112
|
try {
|
|
1007
1113
|
resourceMetadata = await discoverOAuthProtectedResourceMetadata(
|
|
1008
1114
|
serverUrl,
|
|
@@ -1022,12 +1128,18 @@ async function authInternal(provider, {
|
|
|
1022
1128
|
provider,
|
|
1023
1129
|
resourceMetadata
|
|
1024
1130
|
);
|
|
1131
|
+
await ((_a3 = provider.validateAuthorizationServerURL) == null ? void 0 : _a3.call(
|
|
1132
|
+
provider,
|
|
1133
|
+
serverUrl,
|
|
1134
|
+
authorizationServerUrl
|
|
1135
|
+
));
|
|
1025
1136
|
const metadata = await discoverAuthorizationServerMetadata(
|
|
1026
1137
|
authorizationServerUrl,
|
|
1027
1138
|
{
|
|
1028
1139
|
fetchFn
|
|
1029
1140
|
}
|
|
1030
1141
|
);
|
|
1142
|
+
const currentAuthorizationServerInformation = createAuthorizationServerInformation(authorizationServerUrl, metadata);
|
|
1031
1143
|
let clientInformation = await Promise.resolve(provider.clientInformation());
|
|
1032
1144
|
if (!clientInformation) {
|
|
1033
1145
|
if (authorizationCode !== void 0) {
|
|
@@ -1045,8 +1157,11 @@ async function authInternal(provider, {
|
|
|
1045
1157
|
clientMetadata: provider.clientMetadata,
|
|
1046
1158
|
fetchFn
|
|
1047
1159
|
});
|
|
1048
|
-
|
|
1049
|
-
|
|
1160
|
+
clientInformation = addAuthorizationServerInformationToClientInformation(
|
|
1161
|
+
fullInformation,
|
|
1162
|
+
currentAuthorizationServerInformation
|
|
1163
|
+
);
|
|
1164
|
+
await provider.saveClientInformation(clientInformation);
|
|
1050
1165
|
}
|
|
1051
1166
|
if (authorizationCode !== void 0) {
|
|
1052
1167
|
if (provider.storedState) {
|
|
@@ -1057,6 +1172,19 @@ async function authInternal(provider, {
|
|
|
1057
1172
|
);
|
|
1058
1173
|
}
|
|
1059
1174
|
}
|
|
1175
|
+
const storedAuthorizationServerInformation = await getStoredAuthorizationServerInformation({
|
|
1176
|
+
provider,
|
|
1177
|
+
clientInformation
|
|
1178
|
+
});
|
|
1179
|
+
if (!storedAuthorizationServerInformation) {
|
|
1180
|
+
throw new MCPClientOAuthError({
|
|
1181
|
+
message: "Stored OAuth authorization server metadata is required when exchanging an authorization code"
|
|
1182
|
+
});
|
|
1183
|
+
}
|
|
1184
|
+
assertAuthorizationServerInformationMatches({
|
|
1185
|
+
storedAuthorizationServerInformation,
|
|
1186
|
+
currentAuthorizationServerInformation
|
|
1187
|
+
});
|
|
1060
1188
|
const codeVerifier2 = await provider.codeVerifier();
|
|
1061
1189
|
const tokens2 = await exchangeAuthorization(authorizationServerUrl, {
|
|
1062
1190
|
metadata,
|
|
@@ -1068,22 +1196,47 @@ async function authInternal(provider, {
|
|
|
1068
1196
|
addClientAuthentication: provider.addClientAuthentication,
|
|
1069
1197
|
fetchFn
|
|
1070
1198
|
});
|
|
1071
|
-
await provider.saveTokens(
|
|
1199
|
+
await provider.saveTokens(
|
|
1200
|
+
addAuthorizationServerInformationToTokens(
|
|
1201
|
+
tokens2,
|
|
1202
|
+
currentAuthorizationServerInformation
|
|
1203
|
+
)
|
|
1204
|
+
);
|
|
1072
1205
|
return "AUTHORIZED";
|
|
1073
1206
|
}
|
|
1074
1207
|
const tokens = await provider.tokens();
|
|
1075
1208
|
if (tokens == null ? void 0 : tokens.refresh_token) {
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1209
|
+
const storedAuthorizationServerInformation = await getStoredAuthorizationServerInformation({
|
|
1210
|
+
provider,
|
|
1211
|
+
clientInformation,
|
|
1212
|
+
tokens
|
|
1213
|
+
});
|
|
1214
|
+
if (storedAuthorizationServerInformation) {
|
|
1215
|
+
assertAuthorizationServerInformationMatches({
|
|
1216
|
+
storedAuthorizationServerInformation,
|
|
1217
|
+
currentAuthorizationServerInformation
|
|
1084
1218
|
});
|
|
1085
|
-
|
|
1086
|
-
|
|
1219
|
+
} else {
|
|
1220
|
+
await ((_b3 = provider.invalidateCredentials) == null ? void 0 : _b3.call(provider, "tokens"));
|
|
1221
|
+
}
|
|
1222
|
+
try {
|
|
1223
|
+
if (storedAuthorizationServerInformation) {
|
|
1224
|
+
const newTokens = await refreshAuthorization(authorizationServerUrl, {
|
|
1225
|
+
metadata,
|
|
1226
|
+
clientInformation,
|
|
1227
|
+
refreshToken: tokens.refresh_token,
|
|
1228
|
+
resource,
|
|
1229
|
+
addClientAuthentication: provider.addClientAuthentication,
|
|
1230
|
+
fetchFn
|
|
1231
|
+
});
|
|
1232
|
+
await provider.saveTokens(
|
|
1233
|
+
addAuthorizationServerInformationToTokens(
|
|
1234
|
+
newTokens,
|
|
1235
|
+
currentAuthorizationServerInformation
|
|
1236
|
+
)
|
|
1237
|
+
);
|
|
1238
|
+
return "AUTHORIZED";
|
|
1239
|
+
}
|
|
1087
1240
|
} catch (error) {
|
|
1088
1241
|
if (
|
|
1089
1242
|
// If this is a ServerError, or an unknown type, log it out and try to continue. Otherwise, escalate so we can fix things and retry.
|
|
@@ -1109,6 +1262,16 @@ async function authInternal(provider, {
|
|
|
1109
1262
|
resource
|
|
1110
1263
|
}
|
|
1111
1264
|
);
|
|
1265
|
+
const savedAuthorizationServerInformation = await saveAuthorizationServerInformation({
|
|
1266
|
+
provider,
|
|
1267
|
+
clientInformation,
|
|
1268
|
+
authorizationServerInformation: currentAuthorizationServerInformation
|
|
1269
|
+
});
|
|
1270
|
+
if (!savedAuthorizationServerInformation) {
|
|
1271
|
+
throw new MCPClientOAuthError({
|
|
1272
|
+
message: "OAuth authorization server metadata must be saveable before starting authorization"
|
|
1273
|
+
});
|
|
1274
|
+
}
|
|
1112
1275
|
await provider.saveCodeVerifier(codeVerifier);
|
|
1113
1276
|
await provider.redirectToAuthorization(authorizationUrl);
|
|
1114
1277
|
return "REDIRECT";
|
|
@@ -1130,6 +1293,9 @@ var SseMCPTransport = class {
|
|
|
1130
1293
|
this.redirectMode = redirect;
|
|
1131
1294
|
this.fetchFn = fetchFn != null ? fetchFn : globalThis.fetch;
|
|
1132
1295
|
}
|
|
1296
|
+
setProtocolVersion(version) {
|
|
1297
|
+
this.protocolVersion = version;
|
|
1298
|
+
}
|
|
1133
1299
|
async commonHeaders(base) {
|
|
1134
1300
|
var _a3;
|
|
1135
1301
|
const headers = {
|
|
@@ -1345,6 +1511,9 @@ var HttpMCPTransport = class {
|
|
|
1345
1511
|
this.redirectMode = redirect;
|
|
1346
1512
|
this.fetchFn = fetchFn != null ? fetchFn : globalThis.fetch;
|
|
1347
1513
|
}
|
|
1514
|
+
setProtocolVersion(version) {
|
|
1515
|
+
this.protocolVersion = version;
|
|
1516
|
+
}
|
|
1348
1517
|
async commonHeaders(base) {
|
|
1349
1518
|
var _a3;
|
|
1350
1519
|
const headers = {
|
|
@@ -1788,8 +1957,12 @@ var DefaultMCPClient = class {
|
|
|
1788
1957
|
}
|
|
1789
1958
|
this.serverCapabilities = result.capabilities;
|
|
1790
1959
|
this._serverInfo = result.serverInfo;
|
|
1960
|
+
if (this.transport.setProtocolVersion) {
|
|
1961
|
+
this.transport.setProtocolVersion(result.protocolVersion);
|
|
1962
|
+
} else {
|
|
1963
|
+
this.transport.protocolVersion = result.protocolVersion;
|
|
1964
|
+
}
|
|
1791
1965
|
this._serverInstructions = result.instructions;
|
|
1792
|
-
this.transport.protocolVersion = result.protocolVersion;
|
|
1793
1966
|
await this.notification({
|
|
1794
1967
|
method: "notifications/initialized"
|
|
1795
1968
|
});
|