@mastra/mcp 1.0.0-beta.4 → 1.0.0-beta.5
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 +41 -0
- package/README.md +61 -4
- 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 +22 -9
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +22 -9
- 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 +3 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,46 @@
|
|
|
1
1
|
# @mastra/mcp
|
|
2
2
|
|
|
3
|
+
## 1.0.0-beta.5
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- Add support for RequestOptions in elicitation requests to allow custom timeouts and request cancellation. ([#10849](https://github.com/mastra-ai/mastra/pull/10849))
|
|
8
|
+
|
|
9
|
+
You can now pass RequestOptions when sending elicitation requests:
|
|
10
|
+
|
|
11
|
+
```typescript
|
|
12
|
+
// Within a tool's execute function
|
|
13
|
+
const result = await options.mcp.elicitation.sendRequest(
|
|
14
|
+
{
|
|
15
|
+
message: 'Please provide your email',
|
|
16
|
+
requestedSchema: {
|
|
17
|
+
type: 'object',
|
|
18
|
+
properties: { email: { type: 'string' } },
|
|
19
|
+
},
|
|
20
|
+
},
|
|
21
|
+
{ timeout: 120000 }, // Custom 2-minute timeout
|
|
22
|
+
);
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
The RequestOptions parameter supports:
|
|
26
|
+
- `timeout`: Custom timeout in milliseconds (default: 60000ms)
|
|
27
|
+
- `signal`: AbortSignal for request cancellation
|
|
28
|
+
|
|
29
|
+
Fixes #10834
|
|
30
|
+
|
|
31
|
+
### Patch Changes
|
|
32
|
+
|
|
33
|
+
- Fix HTTP SSE fallback to only trigger for 400/404/405 per MCP spec ([#10803](https://github.com/mastra-ai/mastra/pull/10803))
|
|
34
|
+
|
|
35
|
+
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.
|
|
36
|
+
|
|
37
|
+
Older SDK versions maintain the existing behavior (always fallback to SSE).
|
|
38
|
+
|
|
39
|
+
- 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))
|
|
40
|
+
|
|
41
|
+
- 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)]:
|
|
42
|
+
- @mastra/core@1.0.0-beta.7
|
|
43
|
+
|
|
3
44
|
## 1.0.0-beta.4
|
|
4
45
|
|
|
5
46
|
### 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.
|
|
@@ -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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ClB,mBAAmB;IA2C3B,KAAK,IAAI,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,CAAC,CAAC;CA+FjE"}
|
package/dist/client/types.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { RequestContext } from '@mastra/core/di';
|
|
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,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EAAE,MAAM,CAAC;IACb,sCAAsC;IACtC,MAAM,EAAE,yBAAyB,CAAC;IAClC,8DAA8D;IAC9D,YAAY,CAAC,EAAE,kBAAkB,CAAC;IAClC,8BAA8B;IAC9B,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,uCAAuC;IACvC,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB,CAAC"}
|
package/dist/index.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.");
|
|
@@ -2390,8 +2402,8 @@ var MCPServer = class extends mcp.MCPServerBase {
|
|
|
2390
2402
|
}
|
|
2391
2403
|
});
|
|
2392
2404
|
this.elicitation = {
|
|
2393
|
-
sendRequest: async (request) => {
|
|
2394
|
-
return this.handleElicitationRequest(request);
|
|
2405
|
+
sendRequest: async (request, options) => {
|
|
2406
|
+
return this.handleElicitationRequest(request, void 0, options);
|
|
2395
2407
|
}
|
|
2396
2408
|
};
|
|
2397
2409
|
}
|
|
@@ -2401,12 +2413,13 @@ var MCPServer = class extends mcp.MCPServerBase {
|
|
|
2401
2413
|
*
|
|
2402
2414
|
* @param request - The elicitation request containing message and schema
|
|
2403
2415
|
* @param serverInstance - Optional server instance to use; defaults to main server for backward compatibility
|
|
2416
|
+
* @param options - Optional request options (timeout, signal, etc.)
|
|
2404
2417
|
* @returns Promise that resolves to the client's response
|
|
2405
2418
|
*/
|
|
2406
|
-
async handleElicitationRequest(request, serverInstance) {
|
|
2419
|
+
async handleElicitationRequest(request, serverInstance, options) {
|
|
2407
2420
|
this.logger.debug(`Sending elicitation request: ${request.message}`);
|
|
2408
2421
|
const server = serverInstance || this.server;
|
|
2409
|
-
const response = await server.elicitInput(request);
|
|
2422
|
+
const response = await server.elicitInput(request, options);
|
|
2410
2423
|
this.logger.debug(`Received elicitation response: ${JSON.stringify(response)}`);
|
|
2411
2424
|
return response;
|
|
2412
2425
|
}
|
|
@@ -2530,8 +2543,8 @@ Provided arguments: ${JSON.stringify(request.params.arguments, null, 2)}`
|
|
|
2530
2543
|
};
|
|
2531
2544
|
}
|
|
2532
2545
|
const sessionElicitation = {
|
|
2533
|
-
sendRequest: async (request2) => {
|
|
2534
|
-
return this.handleElicitationRequest(request2, serverInstance);
|
|
2546
|
+
sendRequest: async (request2, options) => {
|
|
2547
|
+
return this.handleElicitationRequest(request2, serverInstance, options);
|
|
2535
2548
|
}
|
|
2536
2549
|
};
|
|
2537
2550
|
const mcpOptions = {
|