@mastra/mcp 1.0.0-beta.4 → 1.0.0-beta.6
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 +52 -0
- package/README.md +61 -4
- package/dist/client/client.d.ts +1 -0
- package/dist/client/client.d.ts.map +1 -1
- package/dist/client/types.d.ts +53 -4
- package/dist/client/types.d.ts.map +1 -1
- package/dist/index.cjs +24 -10
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +24 -10
- package/dist/index.js.map +1 -1
- package/dist/server/server.d.ts +1 -0
- package/dist/server/server.d.ts.map +1 -1
- package/dist/server/types.d.ts +11 -4
- package/dist/server/types.d.ts.map +1 -1
- package/package.json +4 -4
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,57 @@
|
|
|
1
1
|
# @mastra/mcp
|
|
2
2
|
|
|
3
|
+
## 1.0.0-beta.6
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Add "Not connected" error detection to MCP auto-reconnection ([#10994](https://github.com/mastra-ai/mastra/pull/10994))
|
|
8
|
+
|
|
9
|
+
Enhanced the MCPClient auto-reconnection feature to also detect and handle "Not connected" protocol errors. When the MCP SDK's transport layer throws this error (typically when the connection is in a disconnected state), the client will now automatically reconnect and retry the operation.
|
|
10
|
+
|
|
11
|
+
- Updated dependencies [[`72df8ae`](https://github.com/mastra-ai/mastra/commit/72df8ae595584cdd7747d5c39ffaca45e4507227), [`9198899`](https://github.com/mastra-ai/mastra/commit/91988995c427b185c33714b7f3be955367911324), [`653e65a`](https://github.com/mastra-ai/mastra/commit/653e65ae1f9502c2958a32f47a5a2df11e612a92), [`c6fd6fe`](https://github.com/mastra-ai/mastra/commit/c6fd6fedd09e9cf8004b03a80925f5e94826ad7e), [`0bed332`](https://github.com/mastra-ai/mastra/commit/0bed332843f627202c6520eaf671771313cd20f3)]:
|
|
12
|
+
- @mastra/core@1.0.0-beta.9
|
|
13
|
+
|
|
14
|
+
## 1.0.0-beta.5
|
|
15
|
+
|
|
16
|
+
### Minor Changes
|
|
17
|
+
|
|
18
|
+
- Add support for RequestOptions in elicitation requests to allow custom timeouts and request cancellation. ([#10849](https://github.com/mastra-ai/mastra/pull/10849))
|
|
19
|
+
|
|
20
|
+
You can now pass RequestOptions when sending elicitation requests:
|
|
21
|
+
|
|
22
|
+
```typescript
|
|
23
|
+
// Within a tool's execute function
|
|
24
|
+
const result = await options.mcp.elicitation.sendRequest(
|
|
25
|
+
{
|
|
26
|
+
message: 'Please provide your email',
|
|
27
|
+
requestedSchema: {
|
|
28
|
+
type: 'object',
|
|
29
|
+
properties: { email: { type: 'string' } },
|
|
30
|
+
},
|
|
31
|
+
},
|
|
32
|
+
{ timeout: 120000 }, // Custom 2-minute timeout
|
|
33
|
+
);
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
The RequestOptions parameter supports:
|
|
37
|
+
- `timeout`: Custom timeout in milliseconds (default: 60000ms)
|
|
38
|
+
- `signal`: AbortSignal for request cancellation
|
|
39
|
+
|
|
40
|
+
Fixes #10834
|
|
41
|
+
|
|
42
|
+
### Patch Changes
|
|
43
|
+
|
|
44
|
+
- Fix HTTP SSE fallback to only trigger for 400/404/405 per MCP spec ([#10803](https://github.com/mastra-ai/mastra/pull/10803))
|
|
45
|
+
|
|
46
|
+
With @modelcontextprotocol/sdk 1.24.0+, SSE fallback now only occurs for HTTP status codes 400, 404, and 405. Other errors (like 401 Unauthorized) are re-thrown for proper handling.
|
|
47
|
+
|
|
48
|
+
Older SDK versions maintain the existing behavior (always fallback to SSE).
|
|
49
|
+
|
|
50
|
+
- Add injectable fetch into mcp client integration. Follows from modelcontextprotocol/sdk implementation of Streamable HTTP and SSE transports. ([#10780](https://github.com/mastra-ai/mastra/pull/10780))
|
|
51
|
+
|
|
52
|
+
- Updated dependencies [[`3076c67`](https://github.com/mastra-ai/mastra/commit/3076c6778b18988ae7d5c4c5c466366974b2d63f), [`85d7ee1`](https://github.com/mastra-ai/mastra/commit/85d7ee18ff4e14d625a8a30ec6656bb49804989b), [`c6c1092`](https://github.com/mastra-ai/mastra/commit/c6c1092f8fbf76109303f69e000e96fd1960c4ce), [`81dc110`](https://github.com/mastra-ai/mastra/commit/81dc11008d147cf5bdc8996ead1aa61dbdebb6fc), [`7aedb74`](https://github.com/mastra-ai/mastra/commit/7aedb74883adf66af38e270e4068fd42e7a37036), [`8f02d80`](https://github.com/mastra-ai/mastra/commit/8f02d800777397e4b45d7f1ad041988a8b0c6630), [`d7aad50`](https://github.com/mastra-ai/mastra/commit/d7aad501ce61646b76b4b511e558ac4eea9884d0), [`ce0a73a`](https://github.com/mastra-ai/mastra/commit/ce0a73abeaa75b10ca38f9e40a255a645d50ebfb), [`a02e542`](https://github.com/mastra-ai/mastra/commit/a02e542d23179bad250b044b17ff023caa61739f), [`a372c64`](https://github.com/mastra-ai/mastra/commit/a372c640ad1fd12e8f0613cebdc682fc156b4d95), [`8846867`](https://github.com/mastra-ai/mastra/commit/8846867ffa9a3746767618e314bebac08eb77d87), [`42a42cf`](https://github.com/mastra-ai/mastra/commit/42a42cf3132b9786feecbb8c13c583dce5b0e198), [`ae08bf0`](https://github.com/mastra-ai/mastra/commit/ae08bf0ebc6a4e4da992b711c4a389c32ba84cf4), [`21735a7`](https://github.com/mastra-ai/mastra/commit/21735a7ef306963554a69a89b44f06c3bcd85141), [`1d877b8`](https://github.com/mastra-ai/mastra/commit/1d877b8d7b536a251c1a7a18db7ddcf4f68d6f8b)]:
|
|
53
|
+
- @mastra/core@1.0.0-beta.7
|
|
54
|
+
|
|
3
55
|
## 1.0.0-beta.4
|
|
4
56
|
|
|
5
57
|
### Patch Changes
|
package/README.md
CHANGED
|
@@ -413,9 +413,65 @@ const httpClient = new MCPClient({
|
|
|
413
413
|
|
|
414
414
|
The `authProvider` is automatically passed to both Streamable HTTP and SSE transports.
|
|
415
415
|
|
|
416
|
+
### Custom Fetch for Dynamic Authentication
|
|
417
|
+
|
|
418
|
+
For HTTP servers, you can provide a custom `fetch` function to handle dynamic authentication, request interception, or other custom behavior. This is particularly useful when you need to refresh tokens on each request or customize request behavior.
|
|
419
|
+
|
|
420
|
+
When `fetch` is provided, `requestInit`, `eventSourceInit`, and `authProvider` become optional, as you can handle these concerns within your custom fetch function.
|
|
421
|
+
|
|
422
|
+
```typescript
|
|
423
|
+
const mcpClient = new MCPClient({
|
|
424
|
+
servers: {
|
|
425
|
+
apiServer: {
|
|
426
|
+
url: new URL('https://api.example.com/mcp'),
|
|
427
|
+
fetch: async (url, init) => {
|
|
428
|
+
// Refresh token on each request
|
|
429
|
+
const token = await getAuthToken(); // Your token refresh logic
|
|
430
|
+
|
|
431
|
+
return fetch(url, {
|
|
432
|
+
...init,
|
|
433
|
+
headers: {
|
|
434
|
+
...init?.headers,
|
|
435
|
+
Authorization: `Bearer ${token}`,
|
|
436
|
+
},
|
|
437
|
+
});
|
|
438
|
+
},
|
|
439
|
+
},
|
|
440
|
+
},
|
|
441
|
+
});
|
|
442
|
+
```
|
|
443
|
+
|
|
444
|
+
The custom `fetch` function is automatically used for both Streamable HTTP and SSE transports, making it a simpler alternative to configuring `requestInit` and `eventSourceInit` separately.
|
|
445
|
+
|
|
416
446
|
### SSE Authentication and Headers (Legacy Fallback)
|
|
417
447
|
|
|
418
|
-
When the client falls back to using the legacy SSE (Server-Sent Events) transport and you need to include authentication or custom headers, you
|
|
448
|
+
When the client falls back to using the legacy SSE (Server-Sent Events) transport and you need to include authentication or custom headers, you have two options:
|
|
449
|
+
|
|
450
|
+
**Option 1: Using custom `fetch` (Recommended)**
|
|
451
|
+
|
|
452
|
+
The simplest approach is to provide a custom `fetch` function, which will automatically be used for both POST requests and SSE connections:
|
|
453
|
+
|
|
454
|
+
```typescript
|
|
455
|
+
const sseClient = new MCPClient({
|
|
456
|
+
servers: {
|
|
457
|
+
authenticatedSseClient: {
|
|
458
|
+
url: new URL('https://your-mcp-server.com/sse'),
|
|
459
|
+
fetch: async (url, init) => {
|
|
460
|
+
const headers = new Headers(init?.headers || {});
|
|
461
|
+
headers.set('Authorization', 'Bearer your-token');
|
|
462
|
+
return fetch(url, {
|
|
463
|
+
...init,
|
|
464
|
+
headers,
|
|
465
|
+
});
|
|
466
|
+
},
|
|
467
|
+
},
|
|
468
|
+
},
|
|
469
|
+
});
|
|
470
|
+
```
|
|
471
|
+
|
|
472
|
+
**Option 2: Using `requestInit` and `eventSourceInit`**
|
|
473
|
+
|
|
474
|
+
Alternatively, you can use both `requestInit` and `eventSourceInit`. The standard `requestInit` headers won't work alone because SSE connections using the browser's `EventSource` API don't support custom headers directly.
|
|
419
475
|
|
|
420
476
|
The `eventSourceInit` configuration allows you to customize the underlying fetch request used for the SSE connection, ensuring your authentication headers are properly included.
|
|
421
477
|
|
|
@@ -489,9 +545,10 @@ Here are the available options within `MastraMCPServerDefinition`:
|
|
|
489
545
|
- **`args`**: (Optional, string[]) For Stdio servers: Arguments to pass to the command.
|
|
490
546
|
- **`env`**: (Optional, Record<string, string>) For Stdio servers: Environment variables to set for the command.
|
|
491
547
|
- **`url`**: (Optional, URL) For HTTP servers (Streamable HTTP or SSE): The URL of the server.
|
|
492
|
-
- **`
|
|
493
|
-
- **`
|
|
494
|
-
- **`
|
|
548
|
+
- **`fetch`**: (Optional, FetchLike) For HTTP servers: Custom fetch implementation used for all network requests. When provided, this function will be used for all HTTP requests, allowing you to add dynamic authentication headers (e.g., refreshing bearer tokens), customize request behavior per-request, or intercept and modify requests/responses. When `fetch` is provided, `requestInit`, `eventSourceInit`, and `authProvider` become optional, as you can handle these concerns within your custom fetch function.
|
|
549
|
+
- **`requestInit`**: (Optional, RequestInit) For HTTP servers: Request configuration for the fetch API. Used for the initial Streamable HTTP connection attempt and subsequent POST requests. Also used for the initial SSE connection attempt. Optional when `fetch` is provided.
|
|
550
|
+
- **`eventSourceInit`**: (Optional, EventSourceInit) **Only** for the legacy SSE fallback: Custom fetch configuration for SSE connections. Required when using custom headers with SSE. Optional when `fetch` is provided.
|
|
551
|
+
- **`authProvider`**: (Optional, OAuthClientProvider) For HTTP servers: OAuth authentication provider for automatic token refresh. Automatically passed to both Streamable HTTP and SSE transports. Optional when `fetch` is provided.
|
|
495
552
|
- **`logger`**: (Optional, LogHandler) Optional additional handler for logging.
|
|
496
553
|
- **`timeout`**: (Optional, number) Server-specific timeout in milliseconds, overriding the global client/configuration timeout.
|
|
497
554
|
- **`capabilities`**: (Optional, ClientCapabilities) Server-specific capabilities configuration.
|
package/dist/client/client.d.ts
CHANGED
|
@@ -120,6 +120,7 @@ export declare class InternalMastraMCPClient extends MastraBase {
|
|
|
120
120
|
* Common session-related errors include:
|
|
121
121
|
* - "No valid session ID provided" (HTTP 400)
|
|
122
122
|
* - "Server not initialized" (HTTP 400)
|
|
123
|
+
* - "Not connected" (protocol state error)
|
|
123
124
|
* - Connection refused errors
|
|
124
125
|
*
|
|
125
126
|
* @param error - The error to check
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../src/client/client.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAI/C,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,oBAAoB,CAAC;AAQ/C,OAAO,KAAK,EACV,eAAe,EACf,iBAAiB,EAElB,MAAM,oCAAoC,CAAC;AAC5C,OAAO,EAKL,iCAAiC,EAQlC,MAAM,oCAAoC,CAAC;AAG5C,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAIxB,OAAO,EAAE,wBAAwB,EAAE,MAAM,uBAAuB,CAAC;AACjE,OAAO,EAAE,qBAAqB,EAAE,MAAM,oBAAoB,CAAC;AAC3D,OAAO,EAAE,mBAAmB,EAAE,MAAM,kBAAkB,CAAC;AACvD,OAAO,EAAE,qBAAqB,EAAE,MAAM,oBAAoB,CAAC;AAC3D,OAAO,KAAK,EAEV,kBAAkB,EAClB,eAAe,EAEf,8BAA8B,EAC9B,IAAI,EACL,MAAM,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
2
2
|
import type { SSEClientTransportOptions } from '@modelcontextprotocol/sdk/client/sse.js';
|
|
3
3
|
import type { StreamableHTTPClientTransportOptions } from '@modelcontextprotocol/sdk/client/streamableHttp.js';
|
|
4
|
+
import type { FetchLike } from '@modelcontextprotocol/sdk/shared/transport.js';
|
|
4
5
|
import type { ClientCapabilities, ElicitRequest, ElicitResult, LoggingLevel, ProgressNotification } from '@modelcontextprotocol/sdk/types.js';
|
|
5
6
|
export type { LoggingLevel } from '@modelcontextprotocol/sdk/types.js';
|
|
6
7
|
/**
|
|
@@ -114,12 +115,16 @@ export type StdioServerDefinition = BaseServerOptions & {
|
|
|
114
115
|
reconnectionOptions?: never;
|
|
115
116
|
sessionId?: never;
|
|
116
117
|
connectTimeout?: never;
|
|
118
|
+
fetch?: never;
|
|
117
119
|
};
|
|
118
120
|
/**
|
|
119
121
|
* Configuration for MCP servers using HTTP-based transport (Streamable HTTP or SSE fallback).
|
|
120
122
|
*
|
|
121
123
|
* Used when connecting to remote MCP servers over HTTP. The client will attempt Streamable HTTP
|
|
122
124
|
* transport first and fall back to SSE if that fails.
|
|
125
|
+
*
|
|
126
|
+
* When `fetch` is provided, all other HTTP-related options (`requestInit`, `eventSourceInit`, `authProvider`)
|
|
127
|
+
* become optional, as the custom fetch function can handle authentication and request customization.
|
|
123
128
|
*/
|
|
124
129
|
export type HttpServerDefinition = BaseServerOptions & {
|
|
125
130
|
/** URL of the MCP server endpoint */
|
|
@@ -127,11 +132,40 @@ export type HttpServerDefinition = BaseServerOptions & {
|
|
|
127
132
|
command?: never;
|
|
128
133
|
args?: never;
|
|
129
134
|
env?: never;
|
|
130
|
-
/**
|
|
135
|
+
/**
|
|
136
|
+
* Custom fetch implementation used for all network requests.
|
|
137
|
+
*
|
|
138
|
+
* When provided, this function will be used for all HTTP requests, allowing you to:
|
|
139
|
+
* - Add dynamic authentication headers (e.g., refreshing bearer tokens)
|
|
140
|
+
* - Customize request behavior per-request
|
|
141
|
+
* - Intercept and modify requests/responses
|
|
142
|
+
*
|
|
143
|
+
* When `fetch` is provided, `requestInit`, `eventSourceInit`, and `authProvider` become optional,
|
|
144
|
+
* as you can handle these concerns within your custom fetch function.
|
|
145
|
+
*
|
|
146
|
+
* @example
|
|
147
|
+
* ```typescript
|
|
148
|
+
* {
|
|
149
|
+
* url: new URL('https://api.example.com/mcp'),
|
|
150
|
+
* fetch: async (url, init) => {
|
|
151
|
+
* const token = await getAuthToken(); // Your token refresh logic
|
|
152
|
+
* return fetch(url, {
|
|
153
|
+
* ...init,
|
|
154
|
+
* headers: {
|
|
155
|
+
* ...init?.headers,
|
|
156
|
+
* Authorization: `Bearer ${token}`,
|
|
157
|
+
* },
|
|
158
|
+
* });
|
|
159
|
+
* },
|
|
160
|
+
* }
|
|
161
|
+
* ```
|
|
162
|
+
*/
|
|
163
|
+
fetch?: FetchLike;
|
|
164
|
+
/** Optional request configuration for HTTP requests (optional when `fetch` is provided) */
|
|
131
165
|
requestInit?: StreamableHTTPClientTransportOptions['requestInit'];
|
|
132
|
-
/** Optional configuration for SSE fallback (required when using custom headers with SSE) */
|
|
166
|
+
/** Optional configuration for SSE fallback (required when using custom headers with SSE, optional when `fetch` is provided) */
|
|
133
167
|
eventSourceInit?: SSEClientTransportOptions['eventSourceInit'];
|
|
134
|
-
/** Optional authentication provider for HTTP requests */
|
|
168
|
+
/** Optional authentication provider for HTTP requests (optional when `fetch` is provided) */
|
|
135
169
|
authProvider?: StreamableHTTPClientTransportOptions['authProvider'];
|
|
136
170
|
/** Optional reconnection configuration for Streamable HTTP */
|
|
137
171
|
reconnectionOptions?: StreamableHTTPClientTransportOptions['reconnectionOptions'];
|
|
@@ -158,13 +192,28 @@ export type HttpServerDefinition = BaseServerOptions & {
|
|
|
158
192
|
* env: { API_KEY: 'secret' }
|
|
159
193
|
* };
|
|
160
194
|
*
|
|
161
|
-
* // HTTP server
|
|
195
|
+
* // HTTP server with static headers
|
|
162
196
|
* const httpServer: MastraMCPServerDefinition = {
|
|
163
197
|
* url: new URL('http://localhost:8080/mcp'),
|
|
164
198
|
* requestInit: {
|
|
165
199
|
* headers: { Authorization: 'Bearer token' }
|
|
166
200
|
* }
|
|
167
201
|
* };
|
|
202
|
+
*
|
|
203
|
+
* // HTTP server with custom fetch for dynamic auth
|
|
204
|
+
* const httpServerWithFetch: MastraMCPServerDefinition = {
|
|
205
|
+
* url: new URL('http://localhost:8080/mcp'),
|
|
206
|
+
* fetch: async (url, init) => {
|
|
207
|
+
* const token = await getAuthToken(); // Refresh token on each request
|
|
208
|
+
* return fetch(url, {
|
|
209
|
+
* ...init,
|
|
210
|
+
* headers: {
|
|
211
|
+
* ...init?.headers,
|
|
212
|
+
* Authorization: `Bearer ${token}`,
|
|
213
|
+
* },
|
|
214
|
+
* });
|
|
215
|
+
* },
|
|
216
|
+
* };
|
|
168
217
|
* ```
|
|
169
218
|
*/
|
|
170
219
|
export type MastraMCPServerDefinition = StdioServerDefinition | HttpServerDefinition;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/client/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AACtD,OAAO,KAAK,EAAE,yBAAyB,EAAE,MAAM,yCAAyC,CAAC;AACzF,OAAO,KAAK,EAAE,oCAAoC,EAAE,MAAM,oDAAoD,CAAC;AAC/G,OAAO,KAAK,EACV,kBAAkB,EAClB,aAAa,EACb,YAAY,EACZ,YAAY,EACZ,oBAAoB,EACrB,MAAM,oCAAoC,CAAC;AAG5C,YAAY,EAAE,YAAY,EAAE,MAAM,oCAAoC,CAAC;AAEvE;;GAEG;AACH,MAAM,WAAW,UAAU;IACzB,wDAAwD;IACxD,KAAK,EAAE,YAAY,CAAC;IACpB,0BAA0B;IAC1B,OAAO,EAAE,MAAM,CAAC;IAChB,yCAAyC;IACzC,SAAS,EAAE,IAAI,CAAC;IAChB,oDAAoD;IACpD,UAAU,EAAE,MAAM,C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.cjs
CHANGED
|
@@ -391,6 +391,7 @@ var ResourceClientActions = class {
|
|
|
391
391
|
|
|
392
392
|
// src/client/client.ts
|
|
393
393
|
var DEFAULT_SERVER_CONNECT_TIMEOUT_MSEC = 3e3;
|
|
394
|
+
var SSE_FALLBACK_STATUS_CODES = [400, 404, 405];
|
|
394
395
|
function convertLogLevelToLoggerMethod(level) {
|
|
395
396
|
switch (level) {
|
|
396
397
|
case "debug":
|
|
@@ -583,7 +584,7 @@ var InternalMastraMCPClient = class extends base.MastraBase {
|
|
|
583
584
|
}
|
|
584
585
|
}
|
|
585
586
|
async connectHttp(url) {
|
|
586
|
-
const { requestInit, eventSourceInit, authProvider, connectTimeout } = this.serverConfig;
|
|
587
|
+
const { requestInit, eventSourceInit, authProvider, connectTimeout, fetch } = this.serverConfig;
|
|
587
588
|
this.log("debug", `Attempting to connect to URL: ${url}`);
|
|
588
589
|
let shouldTrySSE = url.pathname.endsWith(`/sse`);
|
|
589
590
|
if (!shouldTrySSE) {
|
|
@@ -592,7 +593,8 @@ var InternalMastraMCPClient = class extends base.MastraBase {
|
|
|
592
593
|
const streamableTransport = new streamableHttp_js.StreamableHTTPClientTransport(url, {
|
|
593
594
|
requestInit,
|
|
594
595
|
reconnectionOptions: this.serverConfig.reconnectionOptions,
|
|
595
|
-
authProvider
|
|
596
|
+
authProvider,
|
|
597
|
+
fetch
|
|
596
598
|
});
|
|
597
599
|
await this.client.connect(streamableTransport, {
|
|
598
600
|
timeout: connectTimeout ?? DEFAULT_SERVER_CONNECT_TIMEOUT_MSEC
|
|
@@ -601,13 +603,23 @@ var InternalMastraMCPClient = class extends base.MastraBase {
|
|
|
601
603
|
this.log("debug", "Successfully connected using Streamable HTTP transport.");
|
|
602
604
|
} catch (error) {
|
|
603
605
|
this.log("debug", `Streamable HTTP transport failed: ${error}`);
|
|
606
|
+
const status = error?.code;
|
|
607
|
+
if (status !== void 0 && !SSE_FALLBACK_STATUS_CODES.includes(status)) {
|
|
608
|
+
throw error;
|
|
609
|
+
}
|
|
604
610
|
shouldTrySSE = true;
|
|
605
611
|
}
|
|
606
612
|
}
|
|
607
613
|
if (shouldTrySSE) {
|
|
608
614
|
this.log("debug", "Falling back to deprecated HTTP+SSE transport...");
|
|
609
615
|
try {
|
|
610
|
-
const
|
|
616
|
+
const sseEventSourceInit = fetch ? { ...eventSourceInit, fetch } : eventSourceInit;
|
|
617
|
+
const sseTransport = new sse_js.SSEClientTransport(url, {
|
|
618
|
+
requestInit,
|
|
619
|
+
eventSourceInit: sseEventSourceInit,
|
|
620
|
+
authProvider,
|
|
621
|
+
fetch
|
|
622
|
+
});
|
|
611
623
|
await this.client.connect(sseTransport, { timeout: this.serverConfig.timeout ?? this.timeout });
|
|
612
624
|
this.transport = sseTransport;
|
|
613
625
|
this.log("debug", "Successfully connected using deprecated HTTP+SSE transport.");
|
|
@@ -724,6 +736,7 @@ var InternalMastraMCPClient = class extends base.MastraBase {
|
|
|
724
736
|
* Common session-related errors include:
|
|
725
737
|
* - "No valid session ID provided" (HTTP 400)
|
|
726
738
|
* - "Server not initialized" (HTTP 400)
|
|
739
|
+
* - "Not connected" (protocol state error)
|
|
727
740
|
* - Connection refused errors
|
|
728
741
|
*
|
|
729
742
|
* @param error - The error to check
|
|
@@ -736,7 +749,7 @@ var InternalMastraMCPClient = class extends base.MastraBase {
|
|
|
736
749
|
return false;
|
|
737
750
|
}
|
|
738
751
|
const errorMessage = error.message.toLowerCase();
|
|
739
|
-
return errorMessage.includes("no valid session") || errorMessage.includes("session") || errorMessage.includes("server not initialized") || errorMessage.includes("http 400") || errorMessage.includes("http 401") || errorMessage.includes("http 403") || errorMessage.includes("econnrefused") || errorMessage.includes("fetch failed") || errorMessage.includes("connection refused");
|
|
752
|
+
return errorMessage.includes("no valid session") || errorMessage.includes("session") || errorMessage.includes("server not initialized") || errorMessage.includes("not connected") || errorMessage.includes("http 400") || errorMessage.includes("http 401") || errorMessage.includes("http 403") || errorMessage.includes("econnrefused") || errorMessage.includes("fetch failed") || errorMessage.includes("connection refused");
|
|
740
753
|
}
|
|
741
754
|
/**
|
|
742
755
|
* Forces a reconnection to the MCP server by disconnecting and reconnecting.
|
|
@@ -2390,8 +2403,8 @@ var MCPServer = class extends mcp.MCPServerBase {
|
|
|
2390
2403
|
}
|
|
2391
2404
|
});
|
|
2392
2405
|
this.elicitation = {
|
|
2393
|
-
sendRequest: async (request) => {
|
|
2394
|
-
return this.handleElicitationRequest(request);
|
|
2406
|
+
sendRequest: async (request, options) => {
|
|
2407
|
+
return this.handleElicitationRequest(request, void 0, options);
|
|
2395
2408
|
}
|
|
2396
2409
|
};
|
|
2397
2410
|
}
|
|
@@ -2401,12 +2414,13 @@ var MCPServer = class extends mcp.MCPServerBase {
|
|
|
2401
2414
|
*
|
|
2402
2415
|
* @param request - The elicitation request containing message and schema
|
|
2403
2416
|
* @param serverInstance - Optional server instance to use; defaults to main server for backward compatibility
|
|
2417
|
+
* @param options - Optional request options (timeout, signal, etc.)
|
|
2404
2418
|
* @returns Promise that resolves to the client's response
|
|
2405
2419
|
*/
|
|
2406
|
-
async handleElicitationRequest(request, serverInstance) {
|
|
2420
|
+
async handleElicitationRequest(request, serverInstance, options) {
|
|
2407
2421
|
this.logger.debug(`Sending elicitation request: ${request.message}`);
|
|
2408
2422
|
const server = serverInstance || this.server;
|
|
2409
|
-
const response = await server.elicitInput(request);
|
|
2423
|
+
const response = await server.elicitInput(request, options);
|
|
2410
2424
|
this.logger.debug(`Received elicitation response: ${JSON.stringify(response)}`);
|
|
2411
2425
|
return response;
|
|
2412
2426
|
}
|
|
@@ -2530,8 +2544,8 @@ Provided arguments: ${JSON.stringify(request.params.arguments, null, 2)}`
|
|
|
2530
2544
|
};
|
|
2531
2545
|
}
|
|
2532
2546
|
const sessionElicitation = {
|
|
2533
|
-
sendRequest: async (request2) => {
|
|
2534
|
-
return this.handleElicitationRequest(request2, serverInstance);
|
|
2547
|
+
sendRequest: async (request2, options) => {
|
|
2548
|
+
return this.handleElicitationRequest(request2, serverInstance, options);
|
|
2535
2549
|
}
|
|
2536
2550
|
};
|
|
2537
2551
|
const mcpOptions = {
|