@mastra/mcp 0.4.1-alpha.1 → 0.4.1-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/.turbo/turbo-build.log +7 -7
- package/CHANGELOG.md +17 -0
- package/README.md +92 -63
- package/dist/_tsup-dts-rollup.d.cts +44 -11
- package/dist/_tsup-dts-rollup.d.ts +44 -11
- package/dist/index.cjs +128 -45
- package/dist/index.js +128 -45
- package/package.json +3 -3
- package/src/client.test.ts +121 -33
- package/src/client.ts +158 -55
- package/src/configuration.test.ts +4 -19
- package/src/configuration.ts +17 -1
package/.turbo/turbo-build.log
CHANGED
|
@@ -1,23 +1,23 @@
|
|
|
1
1
|
|
|
2
|
-
> @mastra/mcp@0.4.1-alpha.
|
|
2
|
+
> @mastra/mcp@0.4.1-alpha.3 build /home/runner/work/mastra/mastra/packages/mcp
|
|
3
3
|
> tsup src/index.ts --format esm,cjs --experimental-dts --clean --treeshake=smallest --splitting
|
|
4
4
|
|
|
5
5
|
[34mCLI[39m Building entry: src/index.ts
|
|
6
6
|
[34mCLI[39m Using tsconfig: tsconfig.json
|
|
7
7
|
[34mCLI[39m tsup v8.4.0
|
|
8
8
|
[34mTSC[39m Build start
|
|
9
|
-
[32mTSC[39m ⚡️ Build success in
|
|
9
|
+
[32mTSC[39m ⚡️ Build success in 16742ms
|
|
10
10
|
[34mDTS[39m Build start
|
|
11
11
|
[34mCLI[39m Target: es2022
|
|
12
12
|
Analysis will use the bundled TypeScript version 5.8.2
|
|
13
13
|
[36mWriting package typings: /home/runner/work/mastra/mastra/packages/mcp/dist/_tsup-dts-rollup.d.ts[39m
|
|
14
14
|
Analysis will use the bundled TypeScript version 5.8.2
|
|
15
15
|
[36mWriting package typings: /home/runner/work/mastra/mastra/packages/mcp/dist/_tsup-dts-rollup.d.cts[39m
|
|
16
|
-
[32mDTS[39m ⚡️ Build success in
|
|
16
|
+
[32mDTS[39m ⚡️ Build success in 13642ms
|
|
17
17
|
[34mCLI[39m Cleaning output folder
|
|
18
18
|
[34mESM[39m Build start
|
|
19
19
|
[34mCJS[39m Build start
|
|
20
|
-
[
|
|
21
|
-
[
|
|
22
|
-
[
|
|
23
|
-
[
|
|
20
|
+
[32mCJS[39m [1mdist/index.cjs [22m[32m203.57 KB[39m
|
|
21
|
+
[32mCJS[39m ⚡️ Build success in 2723ms
|
|
22
|
+
[32mESM[39m [1mdist/index.js [22m[32m202.88 KB[39m
|
|
23
|
+
[32mESM[39m ⚡️ Build success in 2724ms
|
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,22 @@
|
|
|
1
1
|
# @mastra/mcp
|
|
2
2
|
|
|
3
|
+
## 0.4.1-alpha.3
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- ba1f4f3: Added Streamable HTTP MCP support to MCPConfiguration and MastraMCPClient
|
|
8
|
+
- Updated dependencies [6262bd5]
|
|
9
|
+
- @mastra/core@0.9.1-alpha.3
|
|
10
|
+
|
|
11
|
+
## 0.4.1-alpha.2
|
|
12
|
+
|
|
13
|
+
### Patch Changes
|
|
14
|
+
|
|
15
|
+
- Updated dependencies [405b63d]
|
|
16
|
+
- Updated dependencies [61e92f5]
|
|
17
|
+
- Updated dependencies [c71013a]
|
|
18
|
+
- @mastra/core@0.9.1-alpha.2
|
|
19
|
+
|
|
3
20
|
## 0.4.1-alpha.1
|
|
4
21
|
|
|
5
22
|
### Patch Changes
|
package/README.md
CHANGED
|
@@ -5,68 +5,70 @@ Model Context Protocol (MCP) client implementation for Mastra, providing seamles
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
7
7
|
```bash
|
|
8
|
-
npm install @mastra/mcp
|
|
8
|
+
npm install @mastra/mcp@latest
|
|
9
9
|
```
|
|
10
10
|
|
|
11
11
|
## Overview
|
|
12
12
|
|
|
13
13
|
The `@mastra/mcp` package provides a client implementation for the Model Context Protocol (MCP), enabling Mastra to communicate with MCP-compatible AI models and tools. It wraps the official `@modelcontextprotocol/sdk` and provides Mastra-specific functionality.
|
|
14
14
|
|
|
15
|
+
The client automatically detects the transport type based on your server configuration:
|
|
16
|
+
|
|
17
|
+
- If you provide a `command`, it uses the Stdio transport.
|
|
18
|
+
- If you provide a `url`, it first attempts to use the Streamable HTTP transport (protocol version 2025-03-26) and falls back to the legacy SSE transport (protocol version 2024-11-05) if the initial connection fails.
|
|
19
|
+
|
|
15
20
|
## Usage
|
|
16
21
|
|
|
17
22
|
```typescript
|
|
18
23
|
import { MastraMCPClient } from '@mastra/mcp';
|
|
19
24
|
|
|
20
|
-
// Create a client with
|
|
25
|
+
// Create a client with a Stdio server
|
|
21
26
|
const stdioClient = new MastraMCPClient({
|
|
22
27
|
name: 'my-stdio-client',
|
|
23
28
|
version: '1.0.0', // optional
|
|
24
29
|
server: {
|
|
25
30
|
command: 'your-mcp-server-command',
|
|
26
31
|
args: ['--your', 'args'],
|
|
32
|
+
env: { API_KEY: 'your-api-key' }, // optional environment variables
|
|
27
33
|
},
|
|
28
34
|
capabilities: {}, // optional ClientCapabilities
|
|
35
|
+
timeout: 60000, // optional timeout for tool calls in milliseconds
|
|
29
36
|
});
|
|
30
37
|
|
|
31
|
-
//
|
|
32
|
-
const
|
|
33
|
-
name: 'my-
|
|
38
|
+
// Create a client with an HTTP server (tries Streamable HTTP, falls back to SSE)
|
|
39
|
+
const httpClient = new MastraMCPClient({
|
|
40
|
+
name: 'my-http-client',
|
|
34
41
|
version: '1.0.0',
|
|
35
42
|
server: {
|
|
36
|
-
url: new URL('https://your-mcp-server.com/
|
|
43
|
+
url: new URL('https://your-mcp-server.com/mcp'), // Use the base URL for Streamable HTTP
|
|
37
44
|
requestInit: {
|
|
45
|
+
// Optional fetch request configuration
|
|
38
46
|
headers: { Authorization: 'Bearer your-token' },
|
|
39
47
|
},
|
|
48
|
+
// eventSourceInit is only needed for custom headers with the legacy SSE fallback
|
|
40
49
|
eventSourceInit: {
|
|
41
|
-
|
|
42
|
-
const headers = new Headers(init?.headers || {});
|
|
43
|
-
headers.set('Authorization', 'Bearer your-token');
|
|
44
|
-
return fetch(input, {
|
|
45
|
-
...init,
|
|
46
|
-
headers,
|
|
47
|
-
});
|
|
48
|
-
},
|
|
50
|
+
/* ... */
|
|
49
51
|
},
|
|
50
52
|
},
|
|
51
53
|
timeout: 60000, // optional timeout for tool calls in milliseconds
|
|
52
54
|
});
|
|
53
55
|
|
|
54
|
-
// Connect to the MCP server
|
|
55
|
-
await
|
|
56
|
+
// Connect to the MCP server (using one of the clients above)
|
|
57
|
+
await httpClient.connect();
|
|
56
58
|
|
|
57
59
|
// List available resources
|
|
58
|
-
const resources = await
|
|
60
|
+
const resources = await httpClient.resources();
|
|
59
61
|
|
|
60
62
|
// Get available tools
|
|
61
|
-
const tools = await
|
|
63
|
+
const tools = await httpClient.tools();
|
|
62
64
|
|
|
63
65
|
// Disconnect when done
|
|
64
|
-
await
|
|
66
|
+
await httpClient.disconnect();
|
|
65
67
|
```
|
|
66
68
|
|
|
67
69
|
## Managing Multiple MCP Servers
|
|
68
70
|
|
|
69
|
-
For applications that need to interact with multiple MCP servers, the `MCPConfiguration` class provides a convenient way to manage multiple server connections and their tools:
|
|
71
|
+
For applications that need to interact with multiple MCP servers, the `MCPConfiguration` class provides a convenient way to manage multiple server connections and their tools. It also uses the automatic transport detection based on the `server` configuration:
|
|
70
72
|
|
|
71
73
|
```typescript
|
|
72
74
|
import { MCPConfiguration } from '@mastra/mcp';
|
|
@@ -81,9 +83,13 @@ const mcp = new MCPConfiguration({
|
|
|
81
83
|
API_KEY: 'your-api-key',
|
|
82
84
|
},
|
|
83
85
|
},
|
|
84
|
-
//
|
|
86
|
+
// HTTP-based server (tries Streamable HTTP, falls back to SSE)
|
|
85
87
|
weather: {
|
|
86
|
-
url: new URL('http://localhost:8080/
|
|
88
|
+
url: new URL('http://localhost:8080/mcp'), // Use the base URL for Streamable HTTP
|
|
89
|
+
requestInit: {
|
|
90
|
+
// Optional fetch request configuration
|
|
91
|
+
headers: { 'X-Api-Key': 'weather-key' },
|
|
92
|
+
},
|
|
87
93
|
},
|
|
88
94
|
},
|
|
89
95
|
});
|
|
@@ -118,14 +124,15 @@ const mcp = new MCPConfiguration({
|
|
|
118
124
|
command: 'npx',
|
|
119
125
|
args: ['tsx', 'weather-mcp.ts'],
|
|
120
126
|
// Attach the logger to this specific server
|
|
121
|
-
|
|
127
|
+
logger: weatherLogger, // Use 'logger' key
|
|
122
128
|
},
|
|
123
129
|
|
|
124
130
|
stockPriceService: {
|
|
125
131
|
command: 'npx',
|
|
126
132
|
args: ['tsx', 'stock-mcp.ts'],
|
|
127
133
|
// Different logger for this service
|
|
128
|
-
|
|
134
|
+
logger: logMessage => {
|
|
135
|
+
// Use 'logger' key
|
|
129
136
|
// Just log errors and critical events for this service
|
|
130
137
|
if (['error', 'critical', 'alert', 'emergency'].includes(logMessage.level)) {
|
|
131
138
|
console.error(`Stock service ${logMessage.level}: ${logMessage.message}`);
|
|
@@ -157,9 +164,11 @@ The `LoggingLevel` type is directly imported from the MCP SDK, ensuring compatib
|
|
|
157
164
|
You can create reusable logger factories for common patterns:
|
|
158
165
|
|
|
159
166
|
```typescript
|
|
167
|
+
import fs from 'node:fs';
|
|
168
|
+
|
|
160
169
|
// File logger factory with color coded output for different severity levels
|
|
161
|
-
const createFileLogger = filePath => {
|
|
162
|
-
return logMessage => {
|
|
170
|
+
const createFileLogger = (filePath: string) => {
|
|
171
|
+
return (logMessage: LogMessage) => {
|
|
163
172
|
// Format the message based on level
|
|
164
173
|
const prefix =
|
|
165
174
|
logMessage.level === 'emergency' ? '!!! EMERGENCY !!! ' : logMessage.level === 'alert' ? '! ALERT ! ' : '';
|
|
@@ -178,7 +187,7 @@ const mcp = new MCPConfiguration({
|
|
|
178
187
|
weatherService: {
|
|
179
188
|
command: 'npx',
|
|
180
189
|
args: ['tsx', 'weather-mcp.ts'],
|
|
181
|
-
|
|
190
|
+
logger: createFileLogger('./logs/weather.log'), // Use 'logger' key
|
|
182
191
|
},
|
|
183
192
|
},
|
|
184
193
|
});
|
|
@@ -200,6 +209,9 @@ Use this when:
|
|
|
200
209
|
- You want to initialize an Agent with a fixed set of tools
|
|
201
210
|
|
|
202
211
|
```typescript
|
|
212
|
+
import { Agent } from '@mastra/core/agent';
|
|
213
|
+
import { openai } from '@ai-sdk/openai';
|
|
214
|
+
|
|
203
215
|
const agent = new Agent({
|
|
204
216
|
name: 'CLI Assistant',
|
|
205
217
|
instructions: 'You help users with CLI tasks',
|
|
@@ -234,22 +246,16 @@ const mcp = new MCPConfiguration({
|
|
|
234
246
|
},
|
|
235
247
|
},
|
|
236
248
|
weather: {
|
|
237
|
-
url: new URL('http://localhost:8080/
|
|
249
|
+
url: new URL('http://localhost:8080/mcp'), // Use the base URL for Streamable HTTP
|
|
238
250
|
requestInit: {
|
|
239
251
|
headers: {
|
|
240
252
|
// These would be different per user
|
|
241
253
|
Authorization: 'Bearer user-1-token',
|
|
242
254
|
},
|
|
243
255
|
},
|
|
256
|
+
// eventSourceInit is only needed for custom headers with the legacy SSE fallback
|
|
244
257
|
eventSourceInit: {
|
|
245
|
-
|
|
246
|
-
const headers = new Headers(init?.headers || {});
|
|
247
|
-
headers.set('Authorization', 'Bearer user-1-token');
|
|
248
|
-
return fetch(input, {
|
|
249
|
-
...init,
|
|
250
|
-
headers,
|
|
251
|
-
});
|
|
252
|
-
},
|
|
258
|
+
/* ... */
|
|
253
259
|
},
|
|
254
260
|
},
|
|
255
261
|
},
|
|
@@ -273,19 +279,19 @@ The `MCPConfiguration` class automatically:
|
|
|
273
279
|
- Handles connection lifecycle and cleanup
|
|
274
280
|
- Provides both flat and grouped access to tools
|
|
275
281
|
|
|
276
|
-
## SSE Authentication and Headers
|
|
282
|
+
## SSE Authentication and Headers (Legacy Fallback)
|
|
277
283
|
|
|
278
|
-
When using SSE (Server-Sent Events)
|
|
284
|
+
When the client falls back to using the legacy SSE (Server-Sent Events) transport and you need to include authentication or custom headers, you need to configure headers in a specific way. The standard `requestInit` headers won't work alone because SSE connections using the browser's `EventSource` API don't support custom headers directly.
|
|
279
285
|
|
|
280
286
|
The `eventSourceInit` configuration allows you to customize the underlying fetch request used for the SSE connection, ensuring your authentication headers are properly included.
|
|
281
287
|
|
|
282
|
-
To properly include authentication headers or other custom headers in SSE connections, you need to use both `requestInit` and `eventSourceInit`:
|
|
288
|
+
To properly include authentication headers or other custom headers in SSE connections when using the legacy fallback, you need to use both `requestInit` and `eventSourceInit`:
|
|
283
289
|
|
|
284
290
|
```typescript
|
|
285
291
|
const sseClient = new MastraMCPClient({
|
|
286
292
|
name: 'authenticated-sse-client',
|
|
287
293
|
server: {
|
|
288
|
-
url: new URL('https://your-mcp-server.com/sse'),
|
|
294
|
+
url: new URL('https://your-mcp-server.com/sse'), // Note the typical /sse path for legacy servers
|
|
289
295
|
// requestInit alone isn't enough for SSE connections
|
|
290
296
|
requestInit: {
|
|
291
297
|
headers: { Authorization: 'Bearer your-token' },
|
|
@@ -311,38 +317,58 @@ This configuration ensures that:
|
|
|
311
317
|
2. The connection can be established with the required credentials
|
|
312
318
|
3. Subsequent messages can be received through the authenticated connection
|
|
313
319
|
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
320
|
+
```typescript
|
|
321
|
+
const sseClient = new MastraMCPClient({
|
|
322
|
+
name: 'authenticated-sse-client',
|
|
323
|
+
server: {
|
|
324
|
+
url: new URL('https://your-mcp-server.com/sse'), // Note the typical /sse path for legacy servers
|
|
325
|
+
// requestInit alone isn't enough for SSE connections
|
|
326
|
+
requestInit: {
|
|
327
|
+
headers: { Authorization: 'Bearer your-token' },
|
|
328
|
+
},
|
|
329
|
+
// eventSourceInit is required to include headers in the SSE connection
|
|
330
|
+
eventSourceInit: {
|
|
331
|
+
fetch(input: Request | URL | string, init?: RequestInit) {
|
|
332
|
+
const headers = new Headers(init?.headers || {});
|
|
333
|
+
headers.set('Authorization', 'Bearer your-token');
|
|
334
|
+
return fetch(input, {
|
|
335
|
+
...init,
|
|
336
|
+
headers,
|
|
337
|
+
});
|
|
338
|
+
},
|
|
339
|
+
},
|
|
340
|
+
},
|
|
341
|
+
});
|
|
342
|
+
```
|
|
322
343
|
|
|
323
|
-
|
|
324
|
-
- `args`: Array of command arguments
|
|
344
|
+
## Configuration (`MastraMCPServerDefinition`)
|
|
325
345
|
|
|
326
|
-
|
|
346
|
+
The `server` parameter for both `MastraMCPClient` and `MCPConfiguration` uses the `MastraMCPServerDefinition` type. The client automatically detects the transport type based on the provided parameters:
|
|
327
347
|
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
- `eventSourceInit`: Optional EventSource configuration
|
|
348
|
+
- If `command` is provided, it uses the Stdio transport.
|
|
349
|
+
- If `url` is provided, it first attempts to use the Streamable HTTP transport and falls back to the legacy SSE transport if the initial connection fails.
|
|
331
350
|
|
|
332
|
-
|
|
351
|
+
Here are the available options within `MastraMCPServerDefinition`:
|
|
333
352
|
|
|
334
|
-
-
|
|
335
|
-
-
|
|
336
|
-
-
|
|
353
|
+
- **`command`**: (Optional, string) For Stdio servers: The command to execute.
|
|
354
|
+
- **`args`**: (Optional, string[]) For Stdio servers: Arguments to pass to the command.
|
|
355
|
+
- **`env`**: (Optional, Record<string, string>) For Stdio servers: Environment variables to set for the command.
|
|
356
|
+
- **`url`**: (Optional, URL) For HTTP servers (Streamable HTTP or SSE): The URL of the server.
|
|
357
|
+
- **`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.
|
|
358
|
+
- **`eventSourceInit`**: (Optional, EventSourceInit) **Only** for the legacy SSE fallback: Custom fetch configuration for SSE connections. Required when using custom headers with SSE.
|
|
359
|
+
- **`logger`**: (Optional, LogHandler) Optional additional handler for logging.
|
|
360
|
+
- **`timeout`**: (Optional, number) Server-specific timeout in milliseconds, overriding the global client/configuration timeout.
|
|
361
|
+
- **`capabilities`**: (Optional, ClientCapabilities) Server-specific capabilities configuration.
|
|
362
|
+
- **`enableServerLogs`**: (Optional, boolean, default: `true`) Whether to enable logging for this server.
|
|
337
363
|
|
|
338
364
|
## Features
|
|
339
365
|
|
|
340
366
|
- Standard MCP client implementation
|
|
341
367
|
- Automatic tool conversion to Mastra format
|
|
342
368
|
- Resource discovery and management
|
|
343
|
-
- Multiple transport layers:
|
|
344
|
-
- Stdio-based for local servers
|
|
345
|
-
-
|
|
369
|
+
- Multiple transport layers with automatic detection:
|
|
370
|
+
- Stdio-based for local servers (`command`)
|
|
371
|
+
- HTTP-based for remote servers (`url`): Tries Streamable HTTP first, falls back to legacy SSE.
|
|
346
372
|
- Per-server logging capability using all standard MCP log levels
|
|
347
373
|
- Automatic error handling and logging
|
|
348
374
|
- Tool execution with context
|
|
@@ -391,5 +417,8 @@ The client includes comprehensive error handling:
|
|
|
391
417
|
|
|
392
418
|
## Related Links
|
|
393
419
|
|
|
394
|
-
- [Model Context Protocol Specification](https://
|
|
395
|
-
- [@modelcontextprotocol/sdk Documentation](https://github.com/modelcontextprotocol/sdk)
|
|
420
|
+
- [Model Context Protocol Specification](https://modelcontextprotocol.io/specification)
|
|
421
|
+
- [@modelcontextprotocol/sdk Documentation](https://github.com/modelcontextprotocol/typescript-sdk)
|
|
422
|
+
- [Mastra Docs: Using MCP With Mastra](/docs/agents/mcp-guide)
|
|
423
|
+
- [Mastra Docs: MCPConfiguration Reference](/reference/tools/mcp-configuration)
|
|
424
|
+
- [Mastra Docs: MastraMCPClient Reference](/reference/tools/client)
|
|
@@ -5,13 +5,20 @@ import type { Protocol } from '@modelcontextprotocol/sdk/shared/protocol.js';
|
|
|
5
5
|
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
|
|
6
6
|
import type { SSEClientTransportOptions } from '@modelcontextprotocol/sdk/client/sse.js';
|
|
7
7
|
import { SSEServerTransport } from '@modelcontextprotocol/sdk/server/sse.js';
|
|
8
|
-
import type { StdioServerParameters } from '@modelcontextprotocol/sdk/client/stdio.js';
|
|
9
8
|
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
|
|
9
|
+
import type { StreamableHTTPClientTransportOptions } from '@modelcontextprotocol/sdk/client/streamableHttp.js';
|
|
10
10
|
import { Tool } from '@mastra/core/tools';
|
|
11
11
|
import { ToolExecutionContext } from '@mastra/core';
|
|
12
12
|
import type { ToolsInput } from '@mastra/core/agent';
|
|
13
13
|
import { z } from 'zod';
|
|
14
14
|
|
|
15
|
+
declare type BaseServerOptions = {
|
|
16
|
+
logger?: LogHandler;
|
|
17
|
+
timeout?: number;
|
|
18
|
+
capabilities?: ClientCapabilities;
|
|
19
|
+
enableServerLogs?: boolean;
|
|
20
|
+
};
|
|
21
|
+
|
|
15
22
|
declare type ConvertedTool = {
|
|
16
23
|
name: string;
|
|
17
24
|
description?: string;
|
|
@@ -22,6 +29,17 @@ declare type ConvertedTool = {
|
|
|
22
29
|
|
|
23
30
|
export declare function createLogger(server?: Server): Logger;
|
|
24
31
|
|
|
32
|
+
declare type HttpServerDefinition = BaseServerOptions & {
|
|
33
|
+
url: URL;
|
|
34
|
+
command?: never;
|
|
35
|
+
args?: never;
|
|
36
|
+
env?: never;
|
|
37
|
+
requestInit?: StreamableHTTPClientTransportOptions['requestInit'];
|
|
38
|
+
eventSourceInit?: SSEClientTransportOptions['eventSourceInit'];
|
|
39
|
+
reconnectionOptions?: StreamableHTTPClientTransportOptions['reconnectionOptions'];
|
|
40
|
+
sessionId?: StreamableHTTPClientTransportOptions['sessionId'];
|
|
41
|
+
};
|
|
42
|
+
|
|
25
43
|
export declare interface Logger {
|
|
26
44
|
info: (message: string, data?: any) => Promise<void>;
|
|
27
45
|
warning: (message: string, data?: any) => Promise<void>;
|
|
@@ -50,11 +68,12 @@ export { LogMessage as LogMessage_alias_1 }
|
|
|
50
68
|
|
|
51
69
|
declare class MastraMCPClient extends MastraBase {
|
|
52
70
|
name: string;
|
|
53
|
-
private transport;
|
|
54
71
|
private client;
|
|
55
72
|
private readonly timeout;
|
|
56
73
|
private logHandler?;
|
|
57
74
|
private enableServerLogs?;
|
|
75
|
+
private serverConfig;
|
|
76
|
+
private transport?;
|
|
58
77
|
constructor({ name, version, server, capabilities, timeout, }: {
|
|
59
78
|
name: string;
|
|
60
79
|
server: MastraMCPServerDefinition;
|
|
@@ -70,8 +89,15 @@ declare class MastraMCPClient extends MastraBase {
|
|
|
70
89
|
*/
|
|
71
90
|
private log;
|
|
72
91
|
private setupLogging;
|
|
92
|
+
private connectStdio;
|
|
93
|
+
private connectHttp;
|
|
73
94
|
private isConnected;
|
|
74
95
|
connect(): Promise<void>;
|
|
96
|
+
/**
|
|
97
|
+
* Get the current session ID if using the Streamable HTTP transport.
|
|
98
|
+
* Returns undefined if not connected or not using Streamable HTTP.
|
|
99
|
+
*/
|
|
100
|
+
get sessionId(): string | undefined;
|
|
75
101
|
disconnect(): Promise<void>;
|
|
76
102
|
resources(): Promise<ReturnType<Protocol<any, any, any>['request']>>;
|
|
77
103
|
tools(): Promise<Record<string, any>>;
|
|
@@ -79,12 +105,7 @@ declare class MastraMCPClient extends MastraBase {
|
|
|
79
105
|
export { MastraMCPClient }
|
|
80
106
|
export { MastraMCPClient as MastraMCPClient_alias_1 }
|
|
81
107
|
|
|
82
|
-
declare type MastraMCPServerDefinition =
|
|
83
|
-
logger?: LogHandler;
|
|
84
|
-
timeout?: number;
|
|
85
|
-
capabilities?: ClientCapabilities;
|
|
86
|
-
enableServerLogs?: boolean;
|
|
87
|
-
};
|
|
108
|
+
declare type MastraMCPServerDefinition = StdioServerDefinition | HttpServerDefinition;
|
|
88
109
|
export { MastraMCPServerDefinition }
|
|
89
110
|
export { MastraMCPServerDefinition as MastraMCPServerDefinition_alias_1 }
|
|
90
111
|
|
|
@@ -98,6 +119,11 @@ declare class MCPConfiguration extends MastraBase {
|
|
|
98
119
|
disconnect(): Promise<void>;
|
|
99
120
|
getTools(): Promise<Record<string, any>>;
|
|
100
121
|
getToolsets(): Promise<Record<string, Record<string, any>>>;
|
|
122
|
+
/**
|
|
123
|
+
* Get the current session IDs for all connected MCP clients using the Streamable HTTP transport.
|
|
124
|
+
* Returns an object mapping server names to their session IDs.
|
|
125
|
+
*/
|
|
126
|
+
get sessionIds(): Record<string, string>;
|
|
101
127
|
private mcpClientsById;
|
|
102
128
|
private getConnectedClient;
|
|
103
129
|
private eachClientTools;
|
|
@@ -228,9 +254,16 @@ export declare const server_alias_1: Server<{
|
|
|
228
254
|
} | undefined;
|
|
229
255
|
}>;
|
|
230
256
|
|
|
231
|
-
declare type
|
|
232
|
-
|
|
233
|
-
|
|
257
|
+
declare type StdioServerDefinition = BaseServerOptions & {
|
|
258
|
+
command: string;
|
|
259
|
+
args?: string[];
|
|
260
|
+
env?: Record<string, string>;
|
|
261
|
+
url?: never;
|
|
262
|
+
requestInit?: never;
|
|
263
|
+
eventSourceInit?: never;
|
|
264
|
+
reconnectionOptions?: never;
|
|
265
|
+
sessionId?: never;
|
|
266
|
+
};
|
|
234
267
|
|
|
235
268
|
export declare const weatherTool: Tool<z.ZodObject<{
|
|
236
269
|
location: z.ZodString;
|
|
@@ -5,13 +5,20 @@ import type { Protocol } from '@modelcontextprotocol/sdk/shared/protocol.js';
|
|
|
5
5
|
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
|
|
6
6
|
import type { SSEClientTransportOptions } from '@modelcontextprotocol/sdk/client/sse.js';
|
|
7
7
|
import { SSEServerTransport } from '@modelcontextprotocol/sdk/server/sse.js';
|
|
8
|
-
import type { StdioServerParameters } from '@modelcontextprotocol/sdk/client/stdio.js';
|
|
9
8
|
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
|
|
9
|
+
import type { StreamableHTTPClientTransportOptions } from '@modelcontextprotocol/sdk/client/streamableHttp.js';
|
|
10
10
|
import { Tool } from '@mastra/core/tools';
|
|
11
11
|
import { ToolExecutionContext } from '@mastra/core';
|
|
12
12
|
import type { ToolsInput } from '@mastra/core/agent';
|
|
13
13
|
import { z } from 'zod';
|
|
14
14
|
|
|
15
|
+
declare type BaseServerOptions = {
|
|
16
|
+
logger?: LogHandler;
|
|
17
|
+
timeout?: number;
|
|
18
|
+
capabilities?: ClientCapabilities;
|
|
19
|
+
enableServerLogs?: boolean;
|
|
20
|
+
};
|
|
21
|
+
|
|
15
22
|
declare type ConvertedTool = {
|
|
16
23
|
name: string;
|
|
17
24
|
description?: string;
|
|
@@ -22,6 +29,17 @@ declare type ConvertedTool = {
|
|
|
22
29
|
|
|
23
30
|
export declare function createLogger(server?: Server): Logger;
|
|
24
31
|
|
|
32
|
+
declare type HttpServerDefinition = BaseServerOptions & {
|
|
33
|
+
url: URL;
|
|
34
|
+
command?: never;
|
|
35
|
+
args?: never;
|
|
36
|
+
env?: never;
|
|
37
|
+
requestInit?: StreamableHTTPClientTransportOptions['requestInit'];
|
|
38
|
+
eventSourceInit?: SSEClientTransportOptions['eventSourceInit'];
|
|
39
|
+
reconnectionOptions?: StreamableHTTPClientTransportOptions['reconnectionOptions'];
|
|
40
|
+
sessionId?: StreamableHTTPClientTransportOptions['sessionId'];
|
|
41
|
+
};
|
|
42
|
+
|
|
25
43
|
export declare interface Logger {
|
|
26
44
|
info: (message: string, data?: any) => Promise<void>;
|
|
27
45
|
warning: (message: string, data?: any) => Promise<void>;
|
|
@@ -50,11 +68,12 @@ export { LogMessage as LogMessage_alias_1 }
|
|
|
50
68
|
|
|
51
69
|
declare class MastraMCPClient extends MastraBase {
|
|
52
70
|
name: string;
|
|
53
|
-
private transport;
|
|
54
71
|
private client;
|
|
55
72
|
private readonly timeout;
|
|
56
73
|
private logHandler?;
|
|
57
74
|
private enableServerLogs?;
|
|
75
|
+
private serverConfig;
|
|
76
|
+
private transport?;
|
|
58
77
|
constructor({ name, version, server, capabilities, timeout, }: {
|
|
59
78
|
name: string;
|
|
60
79
|
server: MastraMCPServerDefinition;
|
|
@@ -70,8 +89,15 @@ declare class MastraMCPClient extends MastraBase {
|
|
|
70
89
|
*/
|
|
71
90
|
private log;
|
|
72
91
|
private setupLogging;
|
|
92
|
+
private connectStdio;
|
|
93
|
+
private connectHttp;
|
|
73
94
|
private isConnected;
|
|
74
95
|
connect(): Promise<void>;
|
|
96
|
+
/**
|
|
97
|
+
* Get the current session ID if using the Streamable HTTP transport.
|
|
98
|
+
* Returns undefined if not connected or not using Streamable HTTP.
|
|
99
|
+
*/
|
|
100
|
+
get sessionId(): string | undefined;
|
|
75
101
|
disconnect(): Promise<void>;
|
|
76
102
|
resources(): Promise<ReturnType<Protocol<any, any, any>['request']>>;
|
|
77
103
|
tools(): Promise<Record<string, any>>;
|
|
@@ -79,12 +105,7 @@ declare class MastraMCPClient extends MastraBase {
|
|
|
79
105
|
export { MastraMCPClient }
|
|
80
106
|
export { MastraMCPClient as MastraMCPClient_alias_1 }
|
|
81
107
|
|
|
82
|
-
declare type MastraMCPServerDefinition =
|
|
83
|
-
logger?: LogHandler;
|
|
84
|
-
timeout?: number;
|
|
85
|
-
capabilities?: ClientCapabilities;
|
|
86
|
-
enableServerLogs?: boolean;
|
|
87
|
-
};
|
|
108
|
+
declare type MastraMCPServerDefinition = StdioServerDefinition | HttpServerDefinition;
|
|
88
109
|
export { MastraMCPServerDefinition }
|
|
89
110
|
export { MastraMCPServerDefinition as MastraMCPServerDefinition_alias_1 }
|
|
90
111
|
|
|
@@ -98,6 +119,11 @@ declare class MCPConfiguration extends MastraBase {
|
|
|
98
119
|
disconnect(): Promise<void>;
|
|
99
120
|
getTools(): Promise<Record<string, any>>;
|
|
100
121
|
getToolsets(): Promise<Record<string, Record<string, any>>>;
|
|
122
|
+
/**
|
|
123
|
+
* Get the current session IDs for all connected MCP clients using the Streamable HTTP transport.
|
|
124
|
+
* Returns an object mapping server names to their session IDs.
|
|
125
|
+
*/
|
|
126
|
+
get sessionIds(): Record<string, string>;
|
|
101
127
|
private mcpClientsById;
|
|
102
128
|
private getConnectedClient;
|
|
103
129
|
private eachClientTools;
|
|
@@ -228,9 +254,16 @@ export declare const server_alias_1: Server<{
|
|
|
228
254
|
} | undefined;
|
|
229
255
|
}>;
|
|
230
256
|
|
|
231
|
-
declare type
|
|
232
|
-
|
|
233
|
-
|
|
257
|
+
declare type StdioServerDefinition = BaseServerOptions & {
|
|
258
|
+
command: string;
|
|
259
|
+
args?: string[];
|
|
260
|
+
env?: Record<string, string>;
|
|
261
|
+
url?: never;
|
|
262
|
+
requestInit?: never;
|
|
263
|
+
eventSourceInit?: never;
|
|
264
|
+
reconnectionOptions?: never;
|
|
265
|
+
sessionId?: never;
|
|
266
|
+
};
|
|
234
267
|
|
|
235
268
|
export declare const weatherTool: Tool<z.ZodObject<{
|
|
236
269
|
location: z.ZodString;
|