@mastra/mcp 0.4.4-alpha.0 → 0.5.0-alpha.2

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.
@@ -1,23 +1,23 @@
1
1
 
2
- > @mastra/mcp@0.4.4-alpha.0 build /home/runner/work/mastra/mastra/packages/mcp
2
+ > @mastra/mcp@0.5.0-alpha.2 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
  CLI Building entry: src/index.ts
6
6
  CLI Using tsconfig: tsconfig.json
7
7
  CLI tsup v8.4.0
8
8
  TSC Build start
9
- TSC ⚡️ Build success in 15710ms
9
+ TSC ⚡️ Build success in 18478ms
10
10
  DTS Build start
11
11
  CLI Target: es2022
12
12
  Analysis will use the bundled TypeScript version 5.8.3
13
13
  Writing package typings: /home/runner/work/mastra/mastra/packages/mcp/dist/_tsup-dts-rollup.d.ts
14
14
  Analysis will use the bundled TypeScript version 5.8.3
15
15
  Writing package typings: /home/runner/work/mastra/mastra/packages/mcp/dist/_tsup-dts-rollup.d.cts
16
- DTS ⚡️ Build success in 14943ms
16
+ DTS ⚡️ Build success in 12208ms
17
17
  CLI Cleaning output folder
18
18
  ESM Build start
19
19
  CJS Build start
20
- CJS dist/index.cjs 205.43 KB
21
- CJS ⚡️ Build success in 5121ms
22
- ESM dist/index.js 204.63 KB
23
- ESM ⚡️ Build success in 5125ms
20
+ ESM dist/index.js 25.82 KB
21
+ ESM ⚡️ Build success in 1060ms
22
+ CJS dist/index.cjs 26.18 KB
23
+ CJS ⚡️ Build success in 1060ms
package/CHANGELOG.md CHANGED
@@ -1,5 +1,39 @@
1
1
  # @mastra/mcp
2
2
 
3
+ ## 0.5.0-alpha.2
4
+
5
+ ### Patch Changes
6
+
7
+ - Updated dependencies [3171b5b]
8
+ - Updated dependencies [973e5ac]
9
+ - Updated dependencies [9e1eff5]
10
+ - @mastra/core@0.9.4-alpha.2
11
+
12
+ ## 0.5.0-alpha.1
13
+
14
+ ### Minor Changes
15
+
16
+ - e229660: MCPClient: expose connected client resources.
17
+
18
+ Added a new `getResources()` method to the MCPClient class that allows clients to retrieve resources from connected MCP servers. Resources are data or content exposed by MCP servers that can be accessed by clients.
19
+
20
+ The implementation includes:
21
+
22
+ - Direct access to resources from all connected MCP servers, grouped by server name
23
+ - Robust error handling that allows partial results when some servers fail
24
+ - Comprehensive test coverage with real server implementation
25
+
26
+ This feature enables applications to access data and content exposed by MCP servers through the resources capability, such as files, databases, or other content sources.
27
+
28
+ ### Patch Changes
29
+
30
+ - edf1e88: allows ability to pass McpServer into the mastra class and creates an endpoint /api/servers/:serverId/mcp to POST messages to an MCP server
31
+ - Updated dependencies [ab80e7e]
32
+ - Updated dependencies [6fa1ad1]
33
+ - Updated dependencies [c28d7a0]
34
+ - Updated dependencies [edf1e88]
35
+ - @mastra/core@0.9.4-alpha.1
36
+
3
37
  ## 0.4.4-alpha.0
4
38
 
5
39
  ### Patch Changes
package/README.md CHANGED
@@ -301,6 +301,51 @@ The `MCPClient` class automatically:
301
301
  - Handles connection lifecycle and cleanup
302
302
  - Provides both flat and grouped access to tools
303
303
 
304
+ ## Accessing MCP Resources
305
+
306
+ MCP servers can expose resources - data or content that can be retrieved and used in your application. The `MCPClient` class provides methods to access these resources across multiple servers:
307
+
308
+ ```typescript
309
+ import { MCPClient } from '@mastra/mcp';
310
+
311
+ const mcp = new MCPClient({
312
+ servers: {
313
+ weather: {
314
+ url: new URL('http://localhost:8080/mcp'),
315
+ },
316
+ dataService: {
317
+ command: 'npx',
318
+ args: ['tsx', 'data-service.ts'],
319
+ },
320
+ },
321
+ });
322
+
323
+ // Get resources from all connected MCP servers
324
+ const resources = await mcp.getResources();
325
+
326
+ // Resources are grouped by server name
327
+ console.log(Object.keys(resources)); // ['weather', 'dataService']
328
+
329
+ // Each server entry contains an array of resources
330
+ if (resources.weather) {
331
+ // Access resources from the weather server
332
+ const weatherResources = resources.weather;
333
+
334
+ // Each resource has uri, name, description, and mimeType
335
+ weatherResources.forEach(resource => {
336
+ console.log(`${resource.uri}: ${resource.name} (${resource.mimeType})`);
337
+ });
338
+
339
+ // Find a specific resource by URI
340
+ const forecast = weatherResources.find(r => r.uri === 'weather://forecast');
341
+ if (forecast) {
342
+ console.log(`Found forecast resource: ${forecast.description}`);
343
+ }
344
+ }
345
+ ```
346
+
347
+ The `getResources()` method handles errors gracefully - if a server fails or doesn't support resources, it will be omitted from the results without causing the entire operation to fail.
348
+
304
349
  ## SSE Authentication and Headers (Legacy Fallback)
305
350
 
306
351
  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.
@@ -1,12 +1,18 @@
1
1
  import type { ClientCapabilities } from '@modelcontextprotocol/sdk/types.js';
2
+ import type { ConvertedTool } from '@mastra/core/mcp';
3
+ import type * as http from 'node:http';
2
4
  import { LoggingLevel } from '@modelcontextprotocol/sdk/types.js';
3
5
  import { MastraBase } from '@mastra/core/base';
6
+ import { MCPServerBase } from '@mastra/core/mcp';
7
+ import type { MCPServerSSEOptions } from '@mastra/core/mcp';
4
8
  import type { Protocol } from '@modelcontextprotocol/sdk/shared/protocol.js';
5
9
  import { Server } from '@modelcontextprotocol/sdk/server/index.js';
6
10
  import type { SSEClientTransportOptions } from '@modelcontextprotocol/sdk/client/sse.js';
7
11
  import { SSEServerTransport } from '@modelcontextprotocol/sdk/server/sse.js';
8
12
  import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
9
13
  import type { StreamableHTTPClientTransportOptions } from '@modelcontextprotocol/sdk/client/streamableHttp.js';
14
+ import { StreamableHTTPServerTransport } from '@modelcontextprotocol/sdk/server/streamableHttp.js';
15
+ import type { StreamableHTTPServerTransportOptions } from '@modelcontextprotocol/sdk/server/streamableHttp.js';
10
16
  import { Tool } from '@mastra/core/tools';
11
17
  import { ToolExecutionContext } from '@mastra/core';
12
18
  import type { ToolsInput } from '@mastra/core/agent';
@@ -19,14 +25,6 @@ declare type BaseServerOptions = {
19
25
  enableServerLogs?: boolean;
20
26
  };
21
27
 
22
- declare type ConvertedTool = {
23
- name: string;
24
- description?: string;
25
- inputSchema: any;
26
- zodSchema: z.ZodTypeAny;
27
- execute: any;
28
- };
29
-
30
28
  export declare function createLogger(server?: Server): Logger;
31
29
 
32
30
  declare type HttpServerDefinition = BaseServerOptions & {
@@ -131,6 +129,17 @@ declare class MCPClient extends MastraBase {
131
129
  disconnect(): Promise<void>;
132
130
  getTools(): Promise<Record<string, any>>;
133
131
  getToolsets(): Promise<Record<string, Record<string, any>>>;
132
+ /**
133
+ * Get all resources from connected MCP servers
134
+ * @returns A record of server names to their resources
135
+ */
136
+ getResources(): Promise<Record<string, {
137
+ [x: string]: unknown;
138
+ name: string;
139
+ uri: string;
140
+ mimeType?: string | undefined;
141
+ description?: string | undefined;
142
+ }[]>>;
134
143
  /**
135
144
  * Get the current session IDs for all connected MCP clients using the Streamable HTTP transport.
136
145
  * Returns an object mapping server names to their session IDs.
@@ -138,6 +147,11 @@ declare class MCPClient extends MastraBase {
138
147
  get sessionIds(): Record<string, string>;
139
148
  private getConnectedClient;
140
149
  private eachClientTools;
150
+ /**
151
+ * Helper method to iterate through each connected MCP client and retrieve resources
152
+ * @param cb Callback function to process resources from each server
153
+ */
154
+ private eachClientResources;
141
155
  }
142
156
  export { MCPClient }
143
157
  export { MCPClient as MCPClient_alias_1 }
@@ -170,11 +184,11 @@ declare interface MCPConfigurationOptions {
170
184
  export { MCPConfigurationOptions }
171
185
  export { MCPConfigurationOptions as MCPConfigurationOptions_alias_1 }
172
186
 
173
- declare class MCPServer {
187
+ declare class MCPServer extends MCPServerBase {
174
188
  private server;
175
- private convertedTools;
176
189
  private stdioTransport?;
177
190
  private sseTransport?;
191
+ private streamableHTTPTransport?;
178
192
  /**
179
193
  * Get the current stdio transport.
180
194
  */
@@ -184,9 +198,9 @@ declare class MCPServer {
184
198
  */
185
199
  getSseTransport(): SSEServerTransport | undefined;
186
200
  /**
187
- * Get a read-only view of the registered tools (for testing/introspection).
201
+ * Get the current streamable HTTP transport.
188
202
  */
189
- tools(): Readonly<Record<string, ConvertedTool>>;
203
+ getStreamableHTTPTransport(): StreamableHTTPServerTransport | undefined;
190
204
  /**
191
205
  * Construct a new MCPServer instance.
192
206
  * @param opts.name - Server name
@@ -203,7 +217,7 @@ declare class MCPServer {
203
217
  * @param tools Tool definitions
204
218
  * @returns Converted tools registry
205
219
  */
206
- private convertTools;
220
+ convertTools(tools: ToolsInput): Record<string, ConvertedTool>;
207
221
  /**
208
222
  * Register the ListTools handler for listing all available tools.
209
223
  */
@@ -226,13 +240,33 @@ declare class MCPServer {
226
240
  * @param req Incoming HTTP request
227
241
  * @param res HTTP response (must support .write/.end)
228
242
  */
229
- startSSE({ url, ssePath, messagePath, req, res, }: {
243
+ startSSE({ url, ssePath, messagePath, req, res }: MCPServerSSEOptions): Promise<void>;
244
+ /**
245
+ * Handles MCP-over-StreamableHTTP protocol for user-provided HTTP servers.
246
+ * Call this from your HTTP server for the streamable HTTP endpoint.
247
+ *
248
+ * @param url Parsed URL of the incoming request
249
+ * @param httpPath Path for establishing the streamable HTTP connection (e.g. '/mcp')
250
+ * @param req Incoming HTTP request
251
+ * @param res HTTP response (must support .write/.end)
252
+ * @param options Optional options to pass to the transport (e.g. sessionIdGenerator)
253
+ */
254
+ startHTTP({ url, httpPath, req, res, options, }: {
230
255
  url: URL;
231
- ssePath: string;
256
+ httpPath: string;
257
+ req: http.IncomingMessage;
258
+ res: http.ServerResponse<http.IncomingMessage>;
259
+ options?: StreamableHTTPServerTransportOptions;
260
+ }): Promise<void>;
261
+ handlePostMessage(req: http.IncomingMessage, res: http.ServerResponse<http.IncomingMessage>): Promise<void>;
262
+ connectSSE({ messagePath, res, }: {
232
263
  messagePath: string;
233
- req: any;
234
- res: any;
264
+ res: http.ServerResponse<http.IncomingMessage>;
235
265
  }): Promise<void>;
266
+ /**
267
+ * Close the MCP server and all its connections
268
+ */
269
+ close(): Promise<void>;
236
270
  }
237
271
  export { MCPServer }
238
272
  export { MCPServer as MCPServer_alias_1 }
@@ -1,12 +1,18 @@
1
1
  import type { ClientCapabilities } from '@modelcontextprotocol/sdk/types.js';
2
+ import type { ConvertedTool } from '@mastra/core/mcp';
3
+ import type * as http from 'node:http';
2
4
  import { LoggingLevel } from '@modelcontextprotocol/sdk/types.js';
3
5
  import { MastraBase } from '@mastra/core/base';
6
+ import { MCPServerBase } from '@mastra/core/mcp';
7
+ import type { MCPServerSSEOptions } from '@mastra/core/mcp';
4
8
  import type { Protocol } from '@modelcontextprotocol/sdk/shared/protocol.js';
5
9
  import { Server } from '@modelcontextprotocol/sdk/server/index.js';
6
10
  import type { SSEClientTransportOptions } from '@modelcontextprotocol/sdk/client/sse.js';
7
11
  import { SSEServerTransport } from '@modelcontextprotocol/sdk/server/sse.js';
8
12
  import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
9
13
  import type { StreamableHTTPClientTransportOptions } from '@modelcontextprotocol/sdk/client/streamableHttp.js';
14
+ import { StreamableHTTPServerTransport } from '@modelcontextprotocol/sdk/server/streamableHttp.js';
15
+ import type { StreamableHTTPServerTransportOptions } from '@modelcontextprotocol/sdk/server/streamableHttp.js';
10
16
  import { Tool } from '@mastra/core/tools';
11
17
  import { ToolExecutionContext } from '@mastra/core';
12
18
  import type { ToolsInput } from '@mastra/core/agent';
@@ -19,14 +25,6 @@ declare type BaseServerOptions = {
19
25
  enableServerLogs?: boolean;
20
26
  };
21
27
 
22
- declare type ConvertedTool = {
23
- name: string;
24
- description?: string;
25
- inputSchema: any;
26
- zodSchema: z.ZodTypeAny;
27
- execute: any;
28
- };
29
-
30
28
  export declare function createLogger(server?: Server): Logger;
31
29
 
32
30
  declare type HttpServerDefinition = BaseServerOptions & {
@@ -131,6 +129,17 @@ declare class MCPClient extends MastraBase {
131
129
  disconnect(): Promise<void>;
132
130
  getTools(): Promise<Record<string, any>>;
133
131
  getToolsets(): Promise<Record<string, Record<string, any>>>;
132
+ /**
133
+ * Get all resources from connected MCP servers
134
+ * @returns A record of server names to their resources
135
+ */
136
+ getResources(): Promise<Record<string, {
137
+ [x: string]: unknown;
138
+ name: string;
139
+ uri: string;
140
+ mimeType?: string | undefined;
141
+ description?: string | undefined;
142
+ }[]>>;
134
143
  /**
135
144
  * Get the current session IDs for all connected MCP clients using the Streamable HTTP transport.
136
145
  * Returns an object mapping server names to their session IDs.
@@ -138,6 +147,11 @@ declare class MCPClient extends MastraBase {
138
147
  get sessionIds(): Record<string, string>;
139
148
  private getConnectedClient;
140
149
  private eachClientTools;
150
+ /**
151
+ * Helper method to iterate through each connected MCP client and retrieve resources
152
+ * @param cb Callback function to process resources from each server
153
+ */
154
+ private eachClientResources;
141
155
  }
142
156
  export { MCPClient }
143
157
  export { MCPClient as MCPClient_alias_1 }
@@ -170,11 +184,11 @@ declare interface MCPConfigurationOptions {
170
184
  export { MCPConfigurationOptions }
171
185
  export { MCPConfigurationOptions as MCPConfigurationOptions_alias_1 }
172
186
 
173
- declare class MCPServer {
187
+ declare class MCPServer extends MCPServerBase {
174
188
  private server;
175
- private convertedTools;
176
189
  private stdioTransport?;
177
190
  private sseTransport?;
191
+ private streamableHTTPTransport?;
178
192
  /**
179
193
  * Get the current stdio transport.
180
194
  */
@@ -184,9 +198,9 @@ declare class MCPServer {
184
198
  */
185
199
  getSseTransport(): SSEServerTransport | undefined;
186
200
  /**
187
- * Get a read-only view of the registered tools (for testing/introspection).
201
+ * Get the current streamable HTTP transport.
188
202
  */
189
- tools(): Readonly<Record<string, ConvertedTool>>;
203
+ getStreamableHTTPTransport(): StreamableHTTPServerTransport | undefined;
190
204
  /**
191
205
  * Construct a new MCPServer instance.
192
206
  * @param opts.name - Server name
@@ -203,7 +217,7 @@ declare class MCPServer {
203
217
  * @param tools Tool definitions
204
218
  * @returns Converted tools registry
205
219
  */
206
- private convertTools;
220
+ convertTools(tools: ToolsInput): Record<string, ConvertedTool>;
207
221
  /**
208
222
  * Register the ListTools handler for listing all available tools.
209
223
  */
@@ -226,13 +240,33 @@ declare class MCPServer {
226
240
  * @param req Incoming HTTP request
227
241
  * @param res HTTP response (must support .write/.end)
228
242
  */
229
- startSSE({ url, ssePath, messagePath, req, res, }: {
243
+ startSSE({ url, ssePath, messagePath, req, res }: MCPServerSSEOptions): Promise<void>;
244
+ /**
245
+ * Handles MCP-over-StreamableHTTP protocol for user-provided HTTP servers.
246
+ * Call this from your HTTP server for the streamable HTTP endpoint.
247
+ *
248
+ * @param url Parsed URL of the incoming request
249
+ * @param httpPath Path for establishing the streamable HTTP connection (e.g. '/mcp')
250
+ * @param req Incoming HTTP request
251
+ * @param res HTTP response (must support .write/.end)
252
+ * @param options Optional options to pass to the transport (e.g. sessionIdGenerator)
253
+ */
254
+ startHTTP({ url, httpPath, req, res, options, }: {
230
255
  url: URL;
231
- ssePath: string;
256
+ httpPath: string;
257
+ req: http.IncomingMessage;
258
+ res: http.ServerResponse<http.IncomingMessage>;
259
+ options?: StreamableHTTPServerTransportOptions;
260
+ }): Promise<void>;
261
+ handlePostMessage(req: http.IncomingMessage, res: http.ServerResponse<http.IncomingMessage>): Promise<void>;
262
+ connectSSE({ messagePath, res, }: {
232
263
  messagePath: string;
233
- req: any;
234
- res: any;
264
+ res: http.ServerResponse<http.IncomingMessage>;
235
265
  }): Promise<void>;
266
+ /**
267
+ * Close the MCP server and all its connections
268
+ */
269
+ close(): Promise<void>;
236
270
  }
237
271
  export { MCPServer }
238
272
  export { MCPServer as MCPServer_alias_1 }