@mastra/mcp 1.16.0 → 1.17.0-alpha.0
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 +17 -0
- package/dist/client/configuration.d.ts +11 -3
- package/dist/client/configuration.d.ts.map +1 -1
- package/dist/docs/SKILL.md +3 -3
- package/dist/docs/assets/SOURCE_MAP.json +1 -1
- package/dist/docs/references/docs-connections-overview.md +1 -1
- package/dist/docs/references/docs-mcp-overview.md +5 -6
- package/dist/docs/references/reference-editor-tools.md +1 -1
- package/dist/docs/references/reference-migrations-upgrade-to-v1-mcp.md +1 -1
- package/dist/docs/references/reference-tools-mcp-client.md +37 -8
- package/dist/docs/references/reference-tools-mcp-server.md +9 -6
- package/dist/index.cjs +44 -16
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +44 -16
- package/dist/index.js.map +1 -1
- package/package.json +9 -9
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,22 @@
|
|
|
1
1
|
# @mastra/mcp
|
|
2
2
|
|
|
3
|
+
## 1.17.0-alpha.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- Added per-server time budgets and duration metrics to MCP discovery methods. ([#21560](https://github.com/mastra-ai/mastra/pull/21560))
|
|
8
|
+
|
|
9
|
+
```typescript
|
|
10
|
+
const { tools, errors, durations } = await mcp.listToolsWithErrors({
|
|
11
|
+
perServerTimeoutMs: 3_000,
|
|
12
|
+
});
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
### Patch Changes
|
|
16
|
+
|
|
17
|
+
- Updated dependencies [[`d7e6745`](https://github.com/mastra-ai/mastra/commit/d7e67456954863c55440ea9c49bc6ceb9949972d), [`9acb50f`](https://github.com/mastra-ai/mastra/commit/9acb50f71cec9c362f06820033f90ae6b1f8282f), [`46e9e3f`](https://github.com/mastra-ai/mastra/commit/46e9e3f73babe1bc70080a596cf2ac0b9da48519), [`3f9a190`](https://github.com/mastra-ai/mastra/commit/3f9a19057c027155867b9317294ee4ca7bd0581a), [`e8808e3`](https://github.com/mastra-ai/mastra/commit/e8808e3d8eb585a2565be53e56a7e0e1477352a4), [`d4be8c1`](https://github.com/mastra-ai/mastra/commit/d4be8c1739d22d621e3f78790e1dd5eb5ecc3589), [`a5d2eb1`](https://github.com/mastra-ai/mastra/commit/a5d2eb10347eade1ae2816d88f466c25186c54a5), [`e81744c`](https://github.com/mastra-ai/mastra/commit/e81744cd13c46619c142dc521dc0baac47607a84)]:
|
|
18
|
+
- @mastra/core@1.60.0-alpha.4
|
|
19
|
+
|
|
3
20
|
## 1.16.0
|
|
4
21
|
|
|
5
22
|
### Minor Changes
|
|
@@ -5,6 +5,11 @@ import type { Tool } from '@mastra/core/tools';
|
|
|
5
5
|
import type { ElicitRequest, ElicitResult, ProgressNotification, Prompt, Resource, ResourceTemplateType } from '@modelcontextprotocol/client';
|
|
6
6
|
import type { MastraMCPServerDefinition, MCPServerAuthState } from './client.js';
|
|
7
7
|
import type { SerializableMCPToolCatalog, SerializableMCPToolDefinition } from './types.js';
|
|
8
|
+
/** Options for aggregate discovery across configured MCP servers. */
|
|
9
|
+
export interface MCPDiscoveryOptions {
|
|
10
|
+
/** Maximum time to wait for each server's discovery operation, in milliseconds. */
|
|
11
|
+
perServerTimeoutMs?: number;
|
|
12
|
+
}
|
|
8
13
|
/**
|
|
9
14
|
* Configuration options for creating an MCPClient instance.
|
|
10
15
|
*/
|
|
@@ -719,9 +724,10 @@ export declare class MCPClient extends MastraBase {
|
|
|
719
724
|
* }
|
|
720
725
|
* ```
|
|
721
726
|
*/
|
|
722
|
-
listToolsWithErrors(): Promise<{
|
|
727
|
+
listToolsWithErrors(options?: MCPDiscoveryOptions): Promise<{
|
|
723
728
|
tools: Record<string, Tool<any, any, any, any>>;
|
|
724
729
|
errors: Record<string, string>;
|
|
730
|
+
durations?: Record<string, number>;
|
|
725
731
|
}>;
|
|
726
732
|
/**
|
|
727
733
|
* Returns toolsets organized by server name for dynamic tool injection.
|
|
@@ -764,9 +770,10 @@ export declare class MCPClient extends MastraBase {
|
|
|
764
770
|
* }
|
|
765
771
|
* ```
|
|
766
772
|
*/
|
|
767
|
-
listToolsetsWithErrors(): Promise<{
|
|
773
|
+
listToolsetsWithErrors(options?: MCPDiscoveryOptions): Promise<{
|
|
768
774
|
toolsets: Record<string, Record<string, Tool<any, any, any, any>>>;
|
|
769
775
|
errors: Record<string, string>;
|
|
776
|
+
durations?: Record<string, number>;
|
|
770
777
|
}>;
|
|
771
778
|
/**
|
|
772
779
|
* Discovers every configured server's tools as plain, serializable definitions.
|
|
@@ -798,9 +805,10 @@ export declare class MCPClient extends MastraBase {
|
|
|
798
805
|
* Useful when caching a catalog, since it lets you avoid persisting a partial manifest that
|
|
799
806
|
* silently omits a server which happened to be down at discovery time.
|
|
800
807
|
*/
|
|
801
|
-
listToolDefinitionsWithErrors(): Promise<{
|
|
808
|
+
listToolDefinitionsWithErrors(options?: MCPDiscoveryOptions): Promise<{
|
|
802
809
|
definitions: SerializableMCPToolCatalog;
|
|
803
810
|
errors: Record<string, string>;
|
|
811
|
+
durations?: Record<string, number>;
|
|
804
812
|
}>;
|
|
805
813
|
/**
|
|
806
814
|
* Rebuilds an executable Mastra tool from a cached {@link SerializableMCPToolDefinition}.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"configuration.d.ts","sourceRoot":"","sources":["../../src/client/configuration.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAE/C,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AACtD,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,oBAAoB,CAAC;AAE/C,OAAO,KAAK,EACV,aAAa,EACb,YAAY,EACZ,oBAAoB,EACpB,MAAM,EACN,QAAQ,EACR,oBAAoB,EACrB,MAAM,8B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7 @@ name: mastra-mcp
|
|
|
3
3
|
description: Documentation for @mastra/mcp. Use when working with @mastra/mcp APIs, configuration, or implementation.
|
|
4
4
|
metadata:
|
|
5
5
|
package: "@mastra/mcp"
|
|
6
|
-
version: "1.
|
|
6
|
+
version: "1.17.0-alpha.0"
|
|
7
7
|
---
|
|
8
8
|
|
|
9
9
|
## When to use
|
|
@@ -16,8 +16,8 @@ Read the individual reference documents for detailed explanations and code examp
|
|
|
16
16
|
|
|
17
17
|
### Docs
|
|
18
18
|
|
|
19
|
-
- [Connections
|
|
20
|
-
- [MCP
|
|
19
|
+
- [Connections](references/docs-connections-overview.md) - Connect Mastra to remote agents, coding agents, provider software development kit runtimes, and external tools and resources.
|
|
20
|
+
- [MCP](references/docs-mcp-overview.md) - Connect Mastra agents to external MCP servers and expose Mastra tools through MCPServer. You can also build interactive MCP Apps for Studio.
|
|
21
21
|
- [Fine-Grained Authorization (FGA)](references/docs-server-auth-fga.md) - Add resource-level authorization to your Mastra application with FGA providers.
|
|
22
22
|
|
|
23
23
|
### Reference
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
> Discover all available pages from the documentation index: https://mastra.ai/llms.txt
|
|
2
2
|
|
|
3
|
-
# Connections
|
|
3
|
+
# Connections
|
|
4
4
|
|
|
5
5
|
Connections let Mastra work with remote agents, coding agents, provider software development kit (SDK) runtimes, and external tools and resources. Choose a connection type based on which system owns the agent runtime and what you need to exchange.
|
|
6
6
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
> Discover all available pages from the documentation index: https://mastra.ai/llms.txt
|
|
2
2
|
|
|
3
|
-
# MCP
|
|
3
|
+
# MCP
|
|
4
4
|
|
|
5
5
|
Mastra supports the [Model Context Protocol (MCP)](https://modelcontextprotocol.io/introduction), an open standard for connecting AI agents to external tools and resources.
|
|
6
6
|
|
|
@@ -252,7 +252,7 @@ Use an MCP App when a tool result benefits from interaction, such as a form, cal
|
|
|
252
252
|
|
|
253
253
|
### Define an app resource
|
|
254
254
|
|
|
255
|
-
Return a short `content` summary for the model and place UI data in `structuredContent`. Link the tool to its app by setting `_meta.ui.resourceUri` to the same `ui://` URI used in `appResources`:
|
|
255
|
+
Return a short `content` summary for the model and place UI data in `structuredContent`. Link the tool to its app by setting `mcp._meta.ui.resourceUri` to the same `ui://` URI used in `appResources`:
|
|
256
256
|
|
|
257
257
|
```typescript
|
|
258
258
|
import { MCPServer } from '@mastra/mcp'
|
|
@@ -266,16 +266,15 @@ export const calculatorTool = createTool({
|
|
|
266
266
|
num1: z.number(),
|
|
267
267
|
num2: z.number(),
|
|
268
268
|
}),
|
|
269
|
+
mcp: {
|
|
270
|
+
_meta: { ui: { resourceUri: 'ui://calculator/main' } },
|
|
271
|
+
},
|
|
269
272
|
execute: async ({ num1, num2 }) => ({
|
|
270
273
|
content: [{ type: 'text', text: 'The result is displayed in the calculator app.' }],
|
|
271
274
|
structuredContent: { result: num1 + num2 },
|
|
272
275
|
}),
|
|
273
276
|
})
|
|
274
277
|
|
|
275
|
-
calculatorTool._meta = {
|
|
276
|
-
ui: { resourceUri: 'ui://calculator/main' },
|
|
277
|
-
}
|
|
278
|
-
|
|
279
278
|
export const calculatorMcpServer = new MCPServer({
|
|
280
279
|
id: 'calculator-app-server',
|
|
281
280
|
name: 'Calculator App Server',
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
# Tool configuration
|
|
4
4
|
|
|
5
|
-
Editor stores tool selections as part of an agent version. A stored configuration can add registered tools
|
|
5
|
+
Editor stores tool selections as part of an agent version. A stored configuration can add registered tools and tools from integration providers, as well as tools from Model Context Protocol (MCP) clients.
|
|
6
6
|
|
|
7
7
|
See [Editor tools](https://mastra.ai/docs/editor/overview) for the Studio workflow and common uses.
|
|
8
8
|
|
|
@@ -59,7 +59,7 @@ To migrate, replace all calls to `mcp.getToolsets()` with `mcp.listToolsets()`.
|
|
|
59
59
|
|
|
60
60
|
### MCP tool context properties organization
|
|
61
61
|
|
|
62
|
-
Context properties in MCP tools are now organized under the `context.mcp` namespace.
|
|
62
|
+
Context properties in MCP tools are now organized under the `context.mcp` namespace. The namespace organizes MCP-specific functionality under a clear API surface.
|
|
63
63
|
|
|
64
64
|
To migrate, access MCP-specific properties like `elicitation` and `extra` through `context.mcp` instead of directly from the context parameter.
|
|
65
65
|
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
# MCPClient
|
|
4
4
|
|
|
5
|
-
The `MCPClient` class provides a way to manage multiple MCP server connections and their tools in a Mastra application. It handles connection lifecycle
|
|
5
|
+
The `MCPClient` class provides a way to manage multiple MCP server connections and their tools in a Mastra application. It handles connection lifecycle and tool namespacing while providing access to tools across all configured servers.
|
|
6
6
|
|
|
7
7
|
## Constructor
|
|
8
8
|
|
|
@@ -223,17 +223,23 @@ Retrieves all tools from all configured servers, with tool names namespaced by t
|
|
|
223
223
|
new Agent({ id: 'agent', tools: await mcp.listTools() })
|
|
224
224
|
```
|
|
225
225
|
|
|
226
|
-
### `listToolsWithErrors()`
|
|
226
|
+
### `listToolsWithErrors(options?)`
|
|
227
227
|
|
|
228
228
|
Retrieves all tools from all configured servers, with tool names namespaced by their server name. Also returns per-server errors for servers that failed to connect or list tools.
|
|
229
229
|
|
|
230
|
+
Set `perServerTimeoutMs` to limit how long discovery waits for each server. Servers that finish within the limit remain in `tools`. Timed-out servers appear in `errors`, and `durations` reports each server's discovery time in milliseconds.
|
|
231
|
+
|
|
230
232
|
```typescript
|
|
231
|
-
const { tools, errors } = await mcp.listToolsWithErrors(
|
|
233
|
+
const { tools, errors, durations } = await mcp.listToolsWithErrors({
|
|
234
|
+
perServerTimeoutMs: 3_000,
|
|
235
|
+
})
|
|
232
236
|
|
|
233
237
|
new Agent({ id: 'agent', tools })
|
|
234
|
-
console.log(errors)
|
|
238
|
+
console.log(errors, durations)
|
|
235
239
|
```
|
|
236
240
|
|
|
241
|
+
When called without options, the method returns only `tools` and `errors`.
|
|
242
|
+
|
|
237
243
|
### `listToolsets()`
|
|
238
244
|
|
|
239
245
|
Returns an object mapping namespaced tool names (in the format `serverName.toolName`) to their tool implementations. Intended to be passed at runtime into the generate or stream method.
|
|
@@ -244,11 +250,26 @@ const res = await agent.stream(prompt, {
|
|
|
244
250
|
})
|
|
245
251
|
```
|
|
246
252
|
|
|
253
|
+
### `listToolsetsWithErrors(options?)`
|
|
254
|
+
|
|
255
|
+
Returns toolsets grouped by server name, along with per-server discovery errors. Set `perServerTimeoutMs` to limit each server independently and include per-server `durations` in milliseconds.
|
|
256
|
+
|
|
257
|
+
```typescript
|
|
258
|
+
const { toolsets, errors, durations } = await mcp.listToolsetsWithErrors({
|
|
259
|
+
perServerTimeoutMs: 3_000,
|
|
260
|
+
})
|
|
261
|
+
|
|
262
|
+
const res = await agent.stream(prompt, { toolsets })
|
|
263
|
+
console.log(errors, durations)
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
When called without options, the method returns only `toolsets` and `errors`.
|
|
267
|
+
|
|
247
268
|
### `listToolDefinitions()`
|
|
248
269
|
|
|
249
270
|
Returns every server's tools as plain, serializable definitions, grouped by server name and keyed by the server's own tool name (without the `serverName_toolName` namespacing that `listTools()` applies).
|
|
250
271
|
|
|
251
|
-
Unlike `listTools()`, the result contains no functions or references to a live client, so it can be passed through `JSON.stringify` and cached in Redis
|
|
272
|
+
Unlike `listTools()`, the result contains no functions or references to a live client, so it can be passed through `JSON.stringify` and cached in Redis or a database, as well as a build artifact. Each definition holds the data from the MCP `tools/list` response (name, description, input schema, output schema, annotations, and `_meta`), plus the server name, version, and instructions captured at discovery time.
|
|
252
273
|
|
|
253
274
|
```typescript
|
|
254
275
|
const definitions = await mcp.listToolDefinitions()
|
|
@@ -256,18 +277,26 @@ const definitions = await mcp.listToolDefinitions()
|
|
|
256
277
|
await cache.set('mcp-tools', JSON.stringify(definitions))
|
|
257
278
|
```
|
|
258
279
|
|
|
259
|
-
### `listToolDefinitionsWithErrors()`
|
|
280
|
+
### `listToolDefinitionsWithErrors(options?)`
|
|
260
281
|
|
|
261
282
|
Like `listToolDefinitions()`, but also returns per-server errors for servers that failed to connect. Use this when caching a catalog, so you don't persist a partial manifest that omits a server that was down at discovery time.
|
|
262
283
|
|
|
284
|
+
Set `perServerTimeoutMs` to limit each server independently. When options are provided, `durations` reports each server's discovery time in milliseconds.
|
|
285
|
+
|
|
263
286
|
```typescript
|
|
264
|
-
const { definitions, errors } = await mcp.listToolDefinitionsWithErrors(
|
|
287
|
+
const { definitions, errors, durations } = await mcp.listToolDefinitionsWithErrors({
|
|
288
|
+
perServerTimeoutMs: 3_000,
|
|
289
|
+
})
|
|
265
290
|
|
|
266
291
|
if (Object.keys(errors).length === 0) {
|
|
267
292
|
await cache.set('mcp-tools', JSON.stringify(definitions))
|
|
268
293
|
}
|
|
294
|
+
|
|
295
|
+
console.log(durations)
|
|
269
296
|
```
|
|
270
297
|
|
|
298
|
+
When called without options, the method returns only `definitions` and `errors`.
|
|
299
|
+
|
|
271
300
|
### `toolFromDefinition()`
|
|
272
301
|
|
|
273
302
|
Rebuilds a single executable tool from a cached definition. No connection is opened here. The client connects lazily, the first time the tool is executed.
|
|
@@ -991,7 +1020,7 @@ try {
|
|
|
991
1020
|
|
|
992
1021
|
Concurrent `authenticate()` calls for the same server join the pending flow. Different servers authenticate independently. With valid stored tokens the call reconnects without opening a browser.
|
|
993
1022
|
|
|
994
|
-
Hosts that drive the flow themselves can capture the authorization code with the exported `createOAuthCallbackServer` helper, which binds a one-shot loopback server
|
|
1023
|
+
Hosts that drive the flow themselves can capture the authorization code with the exported `createOAuthCallbackServer` helper, which binds a one-shot loopback server and validates the OAuth `state` parameter before resolving with the code. It creates a plain HTTP server, so it's only for local loopback redirects. Web applications that use an HTTPS redirect URL must host their own callback endpoint and drive the provider directly rather than using this helper:
|
|
995
1024
|
|
|
996
1025
|
```typescript
|
|
997
1026
|
import { createOAuthCallbackServer, getCallbackUrlCandidates } from '@mastra/mcp'
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
# MCPServer
|
|
4
4
|
|
|
5
|
-
The `MCPServer` class provides the functionality to expose your existing Mastra tools and Agents as a Model Context Protocol (MCP) server.
|
|
5
|
+
The `MCPServer` class provides the functionality to expose your existing Mastra tools and Agents as a Model Context Protocol (MCP) server. Any MCP client, such as Cursor, Windsurf, or Claude Desktop, can connect to these capabilities and make them available to an agent.
|
|
6
6
|
|
|
7
7
|
Note that if you only need to use your tools or agents directly within your Mastra application, you don't necessarily need to create an MCP server. This API is specifically for exposing your Mastra tools and agents to _external_ MCP clients.
|
|
8
8
|
|
|
@@ -95,7 +95,7 @@ The constructor accepts an `MCPServerConfig` object with the following propertie
|
|
|
95
95
|
|
|
96
96
|
A powerful feature of `MCPServer` is its ability to automatically expose your Mastra Agents as callable tools. When you provide agents in the `agents` property of the configuration:
|
|
97
97
|
|
|
98
|
-
- **Tool Naming**: Each agent is converted into a tool
|
|
98
|
+
- **Tool Naming**: Each agent is converted into a tool with the name `ask_<agentKey>`, where `<agentKey>` is the key you used for that agent in the `agents` object. For instance, if you configure `agents: { myAgentKey: myAgentInstance }`, a tool with the name `ask_myAgentKey` will be created.
|
|
99
99
|
|
|
100
100
|
- **Tool Functionality**:
|
|
101
101
|
|
|
@@ -104,7 +104,7 @@ A powerful feature of `MCPServer` is its ability to automatically expose your Ma
|
|
|
104
104
|
- **Execution**: When this tool is called, it invokes the corresponding agent's `generate()` method with the provided `query`.
|
|
105
105
|
- **Output**: The direct result from the agent's `generate()` method is returned as the output of the tool.
|
|
106
106
|
|
|
107
|
-
- **Name collisions.** If an explicit tool defined in the `tools` configuration has the same name as an agent-derived tool (e.g., a tool
|
|
107
|
+
- **Name collisions.** If an explicit tool defined in the `tools` configuration has the same name as an agent-derived tool (e.g., a tool with the name `ask_myAgentKey` alongside an agent keyed as `myAgentKey`), the _explicitly defined tool will take precedence_. The agent won't be converted into a tool in this conflicting case, and a warning will be logged.
|
|
108
108
|
|
|
109
109
|
This makes it straightforward to allow MCP clients to interact with your agents using natural language queries, like any other tool.
|
|
110
110
|
|
|
@@ -116,7 +116,7 @@ The description for this generated tool will be: "Ask agent `<agent.name>` a que
|
|
|
116
116
|
|
|
117
117
|
For an agent to be converted into a tool, it **must** have a non-empty `description` string property set in its configuration when it was instantiated (e.g., `new Agent({ id: 'my-agent', name: 'myAgent', description: 'This agent does X.', ... })`). If an agent is passed to `MCPServer` with a missing or empty `description`, an error will be thrown when the `MCPServer` is instantiated, and server setup will fail.
|
|
118
118
|
|
|
119
|
-
|
|
119
|
+
Clients can use MCP to access your agents' generative capabilities and ask them questions directly.
|
|
120
120
|
|
|
121
121
|
### Accessing MCP Context in Tools
|
|
122
122
|
|
|
@@ -1240,7 +1240,7 @@ const customMiddleware = createOAuthMiddleware({
|
|
|
1240
1240
|
|
|
1241
1241
|
## Authentication context
|
|
1242
1242
|
|
|
1243
|
-
Tools can access request metadata via `context.mcp.extra` when using HTTP-based transports.
|
|
1243
|
+
Tools can access request metadata via `context.mcp.extra` when using HTTP-based transports. You can pass authentication info and user context, as well as custom data from your HTTP middleware to your MCP tools.
|
|
1244
1244
|
|
|
1245
1245
|
### How it works
|
|
1246
1246
|
|
|
@@ -1637,6 +1637,9 @@ const calculatorTool = createTool({
|
|
|
1637
1637
|
num2: z.number(),
|
|
1638
1638
|
operation: z.enum(['add', 'subtract']),
|
|
1639
1639
|
}),
|
|
1640
|
+
mcp: {
|
|
1641
|
+
_meta: { ui: { resourceUri: 'ui://calculator/main' } },
|
|
1642
|
+
},
|
|
1640
1643
|
execute: async ({ num1, num2, operation }) => {
|
|
1641
1644
|
const result = operation === 'add' ? num1 + num2 : num1 - num2
|
|
1642
1645
|
return {
|
|
@@ -1660,7 +1663,7 @@ const server = new MCPServer({
|
|
|
1660
1663
|
})
|
|
1661
1664
|
```
|
|
1662
1665
|
|
|
1663
|
-
Link a tool to its app resource by setting `_meta.ui.resourceUri`
|
|
1666
|
+
Link a tool to its app resource by setting `mcp._meta.ui.resourceUri` in `createTool()` to the matching `ui://` URI. The server normalizes this metadata for older hosts when listing tools. Visit [MCP Apps](https://mastra.ai/docs/mcp/overview) for the full app bridge API and usage patterns.
|
|
1664
1667
|
|
|
1665
1668
|
## Related information
|
|
1666
1669
|
|
package/dist/index.cjs
CHANGED
|
@@ -23762,25 +23762,31 @@ onListChanged: async (serverName, handler) => {
|
|
|
23762
23762
|
* }
|
|
23763
23763
|
* ```
|
|
23764
23764
|
*/
|
|
23765
|
-
async listToolsWithErrors() {
|
|
23765
|
+
async listToolsWithErrors(options) {
|
|
23766
23766
|
this.addToInstanceCache();
|
|
23767
23767
|
const connectedTools = {};
|
|
23768
23768
|
const errors = {};
|
|
23769
|
+
const durations = {};
|
|
23769
23770
|
const settled = await this.discoverAcrossServers((serverName) => this.getToolsForServer(serverName), {
|
|
23770
23771
|
errorId: "MCP_CLIENT_GET_TOOLS_FAILED",
|
|
23771
23772
|
logMessage: "Failed to list tools from server:"
|
|
23772
|
-
});
|
|
23773
|
-
for (const { serverName, value, error } of settled) {
|
|
23773
|
+
}, options);
|
|
23774
|
+
for (const { serverName, value, error, duration } of settled) {
|
|
23775
|
+
durations[serverName] = duration;
|
|
23774
23776
|
if (error !== void 0) {
|
|
23775
23777
|
errors[serverName] = error;
|
|
23776
23778
|
continue;
|
|
23777
23779
|
}
|
|
23778
23780
|
for (const [toolName, toolConfig] of Object.entries(value)) connectedTools[`${serverName}_${toolName}`] = toolConfig;
|
|
23779
23781
|
}
|
|
23780
|
-
|
|
23782
|
+
const result = {
|
|
23781
23783
|
tools: connectedTools,
|
|
23782
23784
|
errors
|
|
23783
23785
|
};
|
|
23786
|
+
return options ? {
|
|
23787
|
+
...result,
|
|
23788
|
+
durations
|
|
23789
|
+
} : result;
|
|
23784
23790
|
}
|
|
23785
23791
|
/**
|
|
23786
23792
|
* Returns toolsets organized by server name for dynamic tool injection.
|
|
@@ -23825,25 +23831,31 @@ onListChanged: async (serverName, handler) => {
|
|
|
23825
23831
|
* }
|
|
23826
23832
|
* ```
|
|
23827
23833
|
*/
|
|
23828
|
-
async listToolsetsWithErrors() {
|
|
23834
|
+
async listToolsetsWithErrors(options) {
|
|
23829
23835
|
this.addToInstanceCache();
|
|
23830
23836
|
const connectedToolsets = {};
|
|
23831
23837
|
const errors = {};
|
|
23838
|
+
const durations = {};
|
|
23832
23839
|
const settled = await this.discoverAcrossServers((serverName) => this.getToolsForServer(serverName), {
|
|
23833
23840
|
errorId: "MCP_CLIENT_GET_TOOLSETS_FAILED",
|
|
23834
23841
|
logMessage: "Failed to list toolsets from server:"
|
|
23835
|
-
});
|
|
23836
|
-
for (const { serverName, value, error } of settled) {
|
|
23842
|
+
}, options);
|
|
23843
|
+
for (const { serverName, value, error, duration } of settled) {
|
|
23844
|
+
durations[serverName] = duration;
|
|
23837
23845
|
if (error !== void 0) {
|
|
23838
23846
|
errors[serverName] = error;
|
|
23839
23847
|
continue;
|
|
23840
23848
|
}
|
|
23841
23849
|
connectedToolsets[serverName] = value;
|
|
23842
23850
|
}
|
|
23843
|
-
|
|
23851
|
+
const result = {
|
|
23844
23852
|
toolsets: connectedToolsets,
|
|
23845
23853
|
errors
|
|
23846
23854
|
};
|
|
23855
|
+
return options ? {
|
|
23856
|
+
...result,
|
|
23857
|
+
durations
|
|
23858
|
+
} : result;
|
|
23847
23859
|
}
|
|
23848
23860
|
/**
|
|
23849
23861
|
* Discovers every configured server's tools as plain, serializable definitions.
|
|
@@ -23877,27 +23889,33 @@ onListChanged: async (serverName, handler) => {
|
|
|
23877
23889
|
* Useful when caching a catalog, since it lets you avoid persisting a partial manifest that
|
|
23878
23890
|
* silently omits a server which happened to be down at discovery time.
|
|
23879
23891
|
*/
|
|
23880
|
-
async listToolDefinitionsWithErrors() {
|
|
23892
|
+
async listToolDefinitionsWithErrors(options) {
|
|
23881
23893
|
this.addToInstanceCache();
|
|
23882
23894
|
const definitions = {};
|
|
23883
23895
|
const errors = {};
|
|
23896
|
+
const durations = {};
|
|
23884
23897
|
const settled = await this.discoverAcrossServers(async (serverName) => {
|
|
23885
23898
|
return (await this.getConnectedClientForServer(serverName)).toolDefinitions();
|
|
23886
23899
|
}, {
|
|
23887
23900
|
errorId: "MCP_CLIENT_GET_TOOL_DEFINITIONS_FAILED",
|
|
23888
23901
|
logMessage: "Failed to list tool definitions from server:"
|
|
23889
|
-
});
|
|
23890
|
-
for (const { serverName, value, error } of settled) {
|
|
23902
|
+
}, options);
|
|
23903
|
+
for (const { serverName, value, error, duration } of settled) {
|
|
23904
|
+
durations[serverName] = duration;
|
|
23891
23905
|
if (error !== void 0) {
|
|
23892
23906
|
errors[serverName] = error;
|
|
23893
23907
|
continue;
|
|
23894
23908
|
}
|
|
23895
23909
|
definitions[serverName] = value;
|
|
23896
23910
|
}
|
|
23897
|
-
|
|
23911
|
+
const result = {
|
|
23898
23912
|
definitions,
|
|
23899
23913
|
errors
|
|
23900
23914
|
};
|
|
23915
|
+
return options ? {
|
|
23916
|
+
...result,
|
|
23917
|
+
durations
|
|
23918
|
+
} : result;
|
|
23901
23919
|
}
|
|
23902
23920
|
/**
|
|
23903
23921
|
* Rebuilds an executable Mastra tool from a cached {@link SerializableMCPToolDefinition}.
|
|
@@ -23961,14 +23979,21 @@ onListChanged: async (serverName, handler) => {
|
|
|
23961
23979
|
* Backs `listTools`/`listToolsets`, `resources.list`/`templates`, and
|
|
23962
23980
|
* `prompts.list`.
|
|
23963
23981
|
*/
|
|
23964
|
-
async discoverAcrossServers(operation, onError) {
|
|
23982
|
+
async discoverAcrossServers(operation, onError, options) {
|
|
23965
23983
|
const serverNames = Object.keys(this.serverConfigs);
|
|
23966
23984
|
return Promise.all(serverNames.map(async (serverName) => {
|
|
23985
|
+
const startedAt = performance.now();
|
|
23986
|
+
let timer;
|
|
23967
23987
|
try {
|
|
23988
|
+
const operationPromise = operation(serverName);
|
|
23968
23989
|
return {
|
|
23969
23990
|
serverName,
|
|
23970
|
-
value: await
|
|
23971
|
-
|
|
23991
|
+
value: options?.perServerTimeoutMs === void 0 ? await operationPromise : await Promise.race([operationPromise, new Promise((_, reject) => {
|
|
23992
|
+
timer = setTimeout(() => reject(/* @__PURE__ */ new Error(`Discovery timed out after ${options.perServerTimeoutMs}ms`)), options.perServerTimeoutMs);
|
|
23993
|
+
timer.unref?.();
|
|
23994
|
+
})]),
|
|
23995
|
+
error: void 0,
|
|
23996
|
+
duration: performance.now() - startedAt
|
|
23972
23997
|
};
|
|
23973
23998
|
} catch (error) {
|
|
23974
23999
|
const mastraError = new _mastra_core_error.MastraError({
|
|
@@ -23982,8 +24007,11 @@ onListChanged: async (serverName, handler) => {
|
|
|
23982
24007
|
return {
|
|
23983
24008
|
serverName,
|
|
23984
24009
|
value: void 0,
|
|
23985
|
-
error: error instanceof Error ? error.message : String(error)
|
|
24010
|
+
error: error instanceof Error ? error.message : String(error),
|
|
24011
|
+
duration: performance.now() - startedAt
|
|
23986
24012
|
};
|
|
24013
|
+
} finally {
|
|
24014
|
+
clearTimeout(timer);
|
|
23987
24015
|
}
|
|
23988
24016
|
}));
|
|
23989
24017
|
}
|