@mastra/mcp-docs-server 1.2.13 → 1.2.14-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/.docs/docs/agents/a2a.md +1 -1
- package/.docs/docs/agents/acp.md +1 -1
- package/.docs/docs/agents/agent-approval.md +1 -1
- package/.docs/docs/agents/networks.md +2 -2
- package/.docs/docs/agents/overview.md +2 -2
- package/.docs/docs/agents/using-tools.md +2 -2
- package/.docs/docs/capabilities/channels/overview.md +1 -1
- package/.docs/docs/{agents/supervisor-agents.md → capabilities/subagents.md} +58 -58
- package/.docs/docs/deployment/sandbox.md +95 -1
- package/.docs/docs/getting-started/develop.md +2 -0
- package/.docs/docs/harness/agent-controller.md +370 -0
- package/.docs/docs/long-running-agents/background-tasks.md +1 -1
- package/.docs/docs/long-running-agents/goals.md +3 -3
- package/.docs/docs/mcp/overview.md +222 -281
- package/.docs/docs/memory/overview.md +2 -2
- package/.docs/docs/observability/integrations/exporters/confident-ai.md +140 -0
- package/.docs/docs/observability/integrations/overview.md +1 -1
- package/.docs/docs/observability/overview.md +122 -8
- package/.docs/docs/observability/tracing/overview.md +2 -2
- package/.docs/docs/server/mastra-client.md +1 -1
- package/.docs/docs/server/server-adapters.md +2 -0
- package/.docs/docs/storage/overview.md +2 -2
- package/.docs/guides/build-your-ui/copilotkit/channels.md +49 -39
- package/.docs/guides/concepts/multi-agent-systems.md +7 -7
- package/.docs/guides/guide/coding-agent.md +2 -2
- package/.docs/guides/guide/research-coordinator.md +1 -1
- package/.docs/guides/migrations/network-to-supervisor.md +1 -1
- package/.docs/models/index.md +1 -1
- package/.docs/models/providers/cortecs.md +2 -1
- package/.docs/models/providers/digitalocean.md +9 -9
- package/.docs/models/providers/kilo.md +1 -1
- package/.docs/models/providers/llmgateway.md +1 -1
- package/.docs/models/providers/opencode.md +2 -1
- package/.docs/reference/acp/acp-agent.md +1 -1
- package/.docs/reference/agent-controller/agent-controller-class.md +195 -506
- package/.docs/reference/agent-controller/session.md +274 -111
- package/.docs/reference/agents/network.md +1 -1
- package/.docs/reference/ai-sdk/handle-network-stream.md +1 -1
- package/.docs/reference/ai-sdk/network-route.md +1 -1
- package/.docs/reference/cli/create-mastra.md +1 -1
- package/.docs/reference/editor/versioning.md +1 -1
- package/.docs/reference/evals/rubric.md +1 -1
- package/.docs/reference/file-based-agents/observability.md +2 -2
- package/.docs/reference/file-based-agents/storage.md +1 -1
- package/.docs/reference/file-based-agents/subagents.md +1 -1
- package/.docs/reference/file-based-agents/tools.md +1 -1
- package/.docs/reference/observability/tracing/configuration.md +1 -1
- package/.docs/reference/observability/tracing/exporters/confident-ai.md +138 -0
- package/.docs/reference/observability/tracing/interfaces.md +29 -0
- package/.docs/reference/signals/signal-provider.md +2 -0
- package/.docs/reference/tools/mcp-server.md +1 -1
- package/.docs/reference/tools/submit-plan-tool.md +1 -1
- package/.docs/reference/workspace/railway-sandbox.md +5 -5
- package/CHANGELOG.md +14 -0
- package/package.json +3 -3
- package/.docs/docs/agent-controller/channels.md +0 -111
- package/.docs/docs/agent-controller/modes.md +0 -147
- package/.docs/docs/agent-controller/overview.md +0 -136
- package/.docs/docs/agent-controller/session.md +0 -161
- package/.docs/docs/agent-controller/subagents.md +0 -110
- package/.docs/docs/agent-controller/threads-and-state.md +0 -148
- package/.docs/docs/agent-controller/tool-approvals.md +0 -147
- package/.docs/docs/mcp/mcp-apps.md +0 -306
- package/.docs/docs/observability/config.md +0 -140
- package/.docs/docs/observability/storage.md +0 -81
|
@@ -2,18 +2,13 @@
|
|
|
2
2
|
|
|
3
3
|
# MCP overview
|
|
4
4
|
|
|
5
|
-
Mastra supports the [Model Context Protocol (MCP)](https://modelcontextprotocol.io/introduction), an open standard for connecting AI agents to external tools and resources.
|
|
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
|
|
|
7
|
-
|
|
7
|
+
Use [`MCPClient`](https://mastra.ai/reference/tools/mcp-client) to connect to MCP servers. Use [`MCPServer`](https://mastra.ai/reference/tools/mcp-server) to expose Mastra agents, tools, workflows, prompts, and resources to other MCP-compatible systems.
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
## Connect to MCP servers
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
2. `MCPServer`: Exposes Mastra tools, agents, workflows, prompts, and resources to MCP-compatible clients.
|
|
13
|
-
|
|
14
|
-
## Get started
|
|
15
|
-
|
|
16
|
-
To use MCP, install the required dependency:
|
|
11
|
+
Install the MCP package:
|
|
17
12
|
|
|
18
13
|
**npm**:
|
|
19
14
|
|
|
@@ -39,382 +34,328 @@ yarn add @mastra/mcp@latest
|
|
|
39
34
|
bun add @mastra/mcp@latest
|
|
40
35
|
```
|
|
41
36
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
The `MCPClient` connects Mastra primitives to external MCP servers, which can be local packages (invoked using `npx`) or remote HTTP(S) endpoints. Each server must be configured with either a `command` or a `url`, depending on how it's hosted.
|
|
37
|
+
Configure each server with a local command or remote URL:
|
|
45
38
|
|
|
46
39
|
```typescript
|
|
47
40
|
import { MCPClient } from '@mastra/mcp'
|
|
48
41
|
|
|
49
|
-
export const
|
|
50
|
-
id: '
|
|
42
|
+
export const mcpClient = new MCPClient({
|
|
43
|
+
id: 'my-mcp-client',
|
|
51
44
|
servers: {
|
|
52
45
|
wikipedia: {
|
|
53
46
|
command: 'npx',
|
|
54
47
|
args: ['-y', 'wikipedia-mcp'],
|
|
55
48
|
},
|
|
56
49
|
weather: {
|
|
57
|
-
url: new URL(
|
|
58
|
-
|
|
59
|
-
|
|
50
|
+
url: new URL('https://weather.example.com/mcp'),
|
|
51
|
+
requestInit: {
|
|
52
|
+
headers: {
|
|
53
|
+
Authorization: `Bearer ${process.env.WEATHER_API_KEY}`,
|
|
54
|
+
},
|
|
55
|
+
},
|
|
60
56
|
},
|
|
61
57
|
},
|
|
62
58
|
})
|
|
63
59
|
```
|
|
64
60
|
|
|
65
|
-
Visit [
|
|
66
|
-
|
|
67
|
-
> **Authentication:** For connecting to OAuth-protected MCP servers, including completing the browser-based authorization flow with `authenticate()`, see the [OAuth Authentication](https://mastra.ai/reference/tools/mcp-client) section.
|
|
68
|
-
|
|
69
|
-
## Using `MCPClient` with an agent
|
|
61
|
+
> **Authentication:** For OAuth-protected servers, use `authenticate()` to complete the browser-based authorization flow. Visit [OAuth authentication](https://mastra.ai/reference/tools/mcp-client) for configuration details.
|
|
70
62
|
|
|
71
|
-
|
|
63
|
+
Pass tools from the configured servers to an agent:
|
|
72
64
|
|
|
73
65
|
```typescript
|
|
74
66
|
import { Agent } from '@mastra/core/agent'
|
|
75
|
-
import {
|
|
67
|
+
import { mcpClient } from '../mcp/client'
|
|
76
68
|
|
|
77
|
-
export const
|
|
78
|
-
id: '
|
|
79
|
-
name: '
|
|
80
|
-
description: 'You are a helpful AI assistant',
|
|
69
|
+
export const assistant = new Agent({
|
|
70
|
+
id: 'assistant',
|
|
71
|
+
name: 'Assistant',
|
|
81
72
|
instructions: `
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
Answer questions using the information you find using the MCP Servers.`,
|
|
73
|
+
Use the available MCP tools to answer questions.
|
|
74
|
+
Include the source of any information you retrieve.
|
|
75
|
+
`,
|
|
87
76
|
model: 'openai/gpt-5.6-sol',
|
|
88
|
-
tools: await
|
|
77
|
+
tools: await mcpClient.listTools(),
|
|
89
78
|
})
|
|
90
79
|
```
|
|
91
80
|
|
|
92
|
-
|
|
81
|
+
### Static and runtime tools
|
|
82
|
+
|
|
83
|
+
Choose how to load tools based on whether the server configuration changes between requests:
|
|
93
84
|
|
|
94
|
-
|
|
85
|
+
| | Static tools | Runtime toolsets |
|
|
86
|
+
| ----------- | ---------------------------------- | ---------------------------------------- |
|
|
87
|
+
| Method | `await mcpClient.listTools()` | `await mcpClient.listToolsets()` |
|
|
88
|
+
| Use case | Shared, fixed configuration | Per-user or per-request configuration |
|
|
89
|
+
| Credentials | Shared by all requests | Can vary between requests |
|
|
90
|
+
| Agent API | `tools` in the `Agent` constructor | `toolsets` in `generate()` or `stream()` |
|
|
95
91
|
|
|
96
|
-
|
|
92
|
+
The preceding agent example uses static tools. For runtime credentials, create a client for the request and pass its toolsets when calling the agent:
|
|
97
93
|
|
|
98
94
|
```typescript
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
95
|
+
import { MCPClient } from '@mastra/mcp'
|
|
96
|
+
import { mastra } from './mastra'
|
|
97
|
+
|
|
98
|
+
export async function handleRequest(prompt: string, apiKey: string) {
|
|
99
|
+
const userMcpClient = new MCPClient({
|
|
100
|
+
servers: {
|
|
101
|
+
weather: {
|
|
102
|
+
url: new URL('https://weather.example.com/mcp'),
|
|
103
|
+
requestInit: {
|
|
104
|
+
headers: { Authorization: `Bearer ${apiKey}` },
|
|
105
|
+
},
|
|
106
|
+
},
|
|
104
107
|
},
|
|
105
|
-
}
|
|
106
|
-
})
|
|
107
|
-
```
|
|
108
|
+
})
|
|
108
109
|
|
|
109
|
-
|
|
110
|
+
const agent = mastra.getAgent('assistant')
|
|
111
|
+
const response = await agent.generate(prompt, {
|
|
112
|
+
toolsets: await userMcpClient.listToolsets(),
|
|
113
|
+
})
|
|
110
114
|
|
|
111
|
-
|
|
115
|
+
await userMcpClient.disconnect()
|
|
116
|
+
return response.text
|
|
117
|
+
}
|
|
118
|
+
```
|
|
112
119
|
|
|
113
|
-
|
|
120
|
+
Visit [`listTools()`](https://mastra.ai/reference/tools/mcp-client) and [`listToolsets()`](https://mastra.ai/reference/tools/mcp-client) for their full APIs.
|
|
114
121
|
|
|
115
|
-
|
|
116
|
-
import { MCPServer } from '@mastra/mcp'
|
|
122
|
+
### Tool approval
|
|
117
123
|
|
|
118
|
-
|
|
119
|
-
import { testWorkflow } from '../workflows/test-workflow'
|
|
120
|
-
import { testTool } from '../tools/test-tool'
|
|
124
|
+
Set `requireToolApproval` on a server to require approval for all its tools:
|
|
121
125
|
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
126
|
+
```typescript
|
|
127
|
+
const mcpClient = new MCPClient({
|
|
128
|
+
servers: {
|
|
129
|
+
github: {
|
|
130
|
+
url: new URL('https://github.example.com/mcp'),
|
|
131
|
+
requireToolApproval: true,
|
|
132
|
+
},
|
|
133
|
+
},
|
|
129
134
|
})
|
|
130
135
|
```
|
|
131
136
|
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
> **Authentication:** To protect your MCP server with OAuth, see the [OAuth Protection](https://mastra.ai/reference/tools/mcp-server) section.
|
|
135
|
-
|
|
136
|
-
## Registering an `MCPServer`
|
|
137
|
-
|
|
138
|
-
To make an MCP server available to other systems or agents that support the protocol, register it in the main `Mastra` instance using `mcpServers`.
|
|
137
|
+
You can also provide a function that decides based on the tool name, arguments, or annotations:
|
|
139
138
|
|
|
140
139
|
```typescript
|
|
141
|
-
|
|
140
|
+
requireToolApproval: ({ toolName }) => toolName.startsWith('delete_')
|
|
141
|
+
```
|
|
142
142
|
|
|
143
|
-
|
|
143
|
+
Treat tool annotations from servers you don't control as untrusted hints. Visit [tool approval](https://mastra.ai/reference/tools/mcp-client) for the callback context and security guidance.
|
|
144
144
|
|
|
145
|
-
|
|
146
|
-
mcpServers: { testMcpServer },
|
|
147
|
-
})
|
|
148
|
-
```
|
|
145
|
+
### MCP registries
|
|
149
146
|
|
|
150
|
-
|
|
147
|
+
Registries provide hosted or packaged MCP servers. The client configuration above works with registry endpoints and commands.
|
|
151
148
|
|
|
152
|
-
|
|
149
|
+
| Registry | Connection | Notes |
|
|
150
|
+
| ----------------------------------------------- | ----------------- | --------------------------------------------- |
|
|
151
|
+
| [Klavis AI](https://klavis.ai) | Hosted HTTP | Enterprise authentication and managed servers |
|
|
152
|
+
| [mcp.run](https://www.mcp.run/) | Signed SSE URL | Treat the profile URL as a secret |
|
|
153
|
+
| [Composio](https://mcp.composio.dev) | Hosted SSE URL | URLs are often tied to one user account |
|
|
154
|
+
| [Smithery](https://smithery.ai) | CLI or hosted URL | Run local packages through `npx` |
|
|
155
|
+
| [Apify](https://mcp.apify.com) | Hosted HTTP | Authenticate with an Apify API token |
|
|
156
|
+
| [Ampersand](https://docs.withampersand.com/mcp) | SSE or stdio | Connect to configured SaaS integrations |
|
|
153
157
|
|
|
154
|
-
|
|
155
|
-
| ----------------- | ---------------------------------------------- | ---------------------------------------------------- |
|
|
156
|
-
| **Use Case** | Single-user, static config (e.g., CLI tool) | Multi-user, runtime config (e.g., SaaS app) |
|
|
157
|
-
| **Configuration** | Fixed at agent initialization | Per-request, runtime |
|
|
158
|
-
| **Credentials** | Shared across all uses | Can vary per user/request |
|
|
159
|
-
| **Agent Setup** | Tools added in `Agent` constructor | Tools passed in `.generate()` or `.stream()` options |
|
|
158
|
+
Store signed URLs, API keys, and tokens in environment variables. Follow the registry's documentation to obtain the endpoint, command, and credentials for each server.
|
|
160
159
|
|
|
161
|
-
|
|
160
|
+
## Expose a Mastra MCP server
|
|
162
161
|
|
|
163
|
-
|
|
162
|
+
Create an `MCPServer` to expose Mastra primitives to external MCP clients:
|
|
164
163
|
|
|
165
164
|
```typescript
|
|
166
|
-
import {
|
|
167
|
-
|
|
168
|
-
import {
|
|
165
|
+
import { MCPServer } from '@mastra/mcp'
|
|
166
|
+
import { assistant } from '../agents/assistant'
|
|
167
|
+
import { weatherTool } from '../tools/weather'
|
|
168
|
+
import { weatherWorkflow } from '../workflows/weather'
|
|
169
169
|
|
|
170
|
-
export const
|
|
171
|
-
id: '
|
|
172
|
-
|
|
170
|
+
export const mcpServer = new MCPServer({
|
|
171
|
+
id: 'my-mcp-server',
|
|
172
|
+
name: 'My MCP Server',
|
|
173
|
+
version: '1.0.0',
|
|
174
|
+
agents: { assistant },
|
|
175
|
+
tools: { weatherTool },
|
|
176
|
+
workflows: { weatherWorkflow },
|
|
173
177
|
})
|
|
174
178
|
```
|
|
175
179
|
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
Use the `.listToolsets()` method when tool configuration may vary by request or user, such as in a multi-tenant system where each user provides their own API key. This method returns toolsets that can be passed to the `toolsets` option in the agent's `.generate()` or `.stream()` calls.
|
|
180
|
+
Register the server on the main `Mastra` instance:
|
|
179
181
|
|
|
180
182
|
```typescript
|
|
181
|
-
import {
|
|
182
|
-
import {
|
|
183
|
-
|
|
184
|
-
async function handleRequest(userPrompt: string, userApiKey: string) {
|
|
185
|
-
const userMcp = new MCPClient({
|
|
186
|
-
servers: {
|
|
187
|
-
weather: {
|
|
188
|
-
url: new URL('http://localhost:8080/mcp'),
|
|
189
|
-
requestInit: {
|
|
190
|
-
headers: {
|
|
191
|
-
Authorization: `Bearer ${userApiKey}`,
|
|
192
|
-
},
|
|
193
|
-
},
|
|
194
|
-
},
|
|
195
|
-
},
|
|
196
|
-
})
|
|
197
|
-
|
|
198
|
-
const agent = mastra.getAgent('testAgent')
|
|
183
|
+
import { Mastra } from '@mastra/core/mastra'
|
|
184
|
+
import { mcpServer } from './mcp/server'
|
|
199
185
|
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
186
|
+
export const mastra = new Mastra({
|
|
187
|
+
mcpServers: { mcpServer },
|
|
188
|
+
})
|
|
189
|
+
```
|
|
203
190
|
|
|
204
|
-
|
|
191
|
+
> **Authentication:** Protect HTTP MCP servers with OAuth middleware. Visit [OAuth protection](https://mastra.ai/reference/tools/mcp-server) for setup instructions.
|
|
205
192
|
|
|
206
|
-
|
|
207
|
-
data: response.text,
|
|
208
|
-
})
|
|
209
|
-
}
|
|
210
|
-
```
|
|
193
|
+
Visit the [`MCPServer` reference](https://mastra.ai/reference/tools/mcp-server) for prompts, resources, transports, and other server options.
|
|
211
194
|
|
|
212
|
-
|
|
195
|
+
## Build MCP Apps
|
|
213
196
|
|
|
214
|
-
|
|
197
|
+
The [MCP Apps extension](https://github.com/modelcontextprotocol/ext-apps) lets MCP tools serve interactive HTML interfaces through `ui://` resources. Mastra Studio renders these apps in sandboxed iframes on tool pages and in agent chat.
|
|
215
198
|
|
|
216
|
-
MCP
|
|
199
|
+
Use an MCP App when a tool result benefits from interaction, such as a form, calculator, color picker, or data visualization.
|
|
217
200
|
|
|
218
|
-
|
|
201
|
+
### Define an app resource
|
|
219
202
|
|
|
220
|
-
|
|
203
|
+
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`:
|
|
221
204
|
|
|
222
205
|
```typescript
|
|
223
|
-
import {
|
|
206
|
+
import { MCPServer } from '@mastra/mcp'
|
|
207
|
+
import { createTool } from '@mastra/core/tools'
|
|
208
|
+
import { z } from 'zod'
|
|
209
|
+
|
|
210
|
+
export const calculatorTool = createTool({
|
|
211
|
+
id: 'calculatorWithUI',
|
|
212
|
+
description: 'Calculate the sum of two numbers',
|
|
213
|
+
inputSchema: z.object({
|
|
214
|
+
num1: z.number(),
|
|
215
|
+
num2: z.number(),
|
|
216
|
+
}),
|
|
217
|
+
execute: async ({ num1, num2 }) => ({
|
|
218
|
+
content: [{ type: 'text', text: 'The result is displayed in the calculator app.' }],
|
|
219
|
+
structuredContent: { result: num1 + num2 },
|
|
220
|
+
}),
|
|
221
|
+
})
|
|
224
222
|
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
223
|
+
calculatorTool._meta = {
|
|
224
|
+
ui: { resourceUri: 'ui://calculator/main' },
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
export const calculatorMcpServer = new MCPServer({
|
|
228
|
+
id: 'calculator-app-server',
|
|
229
|
+
name: 'Calculator App Server',
|
|
230
|
+
version: '1.0.0',
|
|
231
|
+
tools: { calculatorTool },
|
|
232
|
+
appResources: {
|
|
233
|
+
'ui://calculator/main': {
|
|
234
|
+
name: 'Calculator',
|
|
235
|
+
htmlPath: './src/mastra/mcp/calculator.html',
|
|
234
236
|
},
|
|
235
237
|
},
|
|
236
238
|
})
|
|
237
239
|
```
|
|
238
240
|
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
For more details on how to integrate Mastra with Klavis, check out their [documentation](https://docs.klavis.ai/documentation/ai-platform-integration/mastra).
|
|
241
|
+
The model sees `content`, while the app receives `structuredContent`. Visit [`appResources`](https://mastra.ai/reference/tools/mcp-server) for inline HTML, file paths, metadata, and content security policy options.
|
|
242
242
|
|
|
243
|
-
|
|
243
|
+
### Connect the app to Studio
|
|
244
244
|
|
|
245
|
-
|
|
245
|
+
Use the `App` class from `@modelcontextprotocol/ext-apps` inside the HTML resource. Register event handlers before calling `connect()`:
|
|
246
246
|
|
|
247
|
-
```
|
|
248
|
-
|
|
247
|
+
```html
|
|
248
|
+
<!doctype html>
|
|
249
|
+
<html>
|
|
250
|
+
<body>
|
|
251
|
+
<p id="result">Waiting for input</p>
|
|
252
|
+
<button id="recalculate">Recalculate</button>
|
|
249
253
|
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
marketing: {
|
|
253
|
-
// Example profile name
|
|
254
|
-
url: new URL(process.env.MCP_RUN_SSE_URL!), // Get URL from mcp.run profile
|
|
255
|
-
},
|
|
256
|
-
},
|
|
257
|
-
})
|
|
258
|
-
```
|
|
254
|
+
<script type="module">
|
|
255
|
+
import { App } from 'https://cdn.jsdelivr.net/npm/@modelcontextprotocol/ext-apps/+esm'
|
|
259
256
|
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
> ```bash
|
|
263
|
-
> MCP_RUN_SSE_URL=https://www.mcp.run/api/mcp/sse?nonce=...
|
|
264
|
-
> ```
|
|
257
|
+
const app = new App({ name: 'Calculator', version: '1.0.0' })
|
|
258
|
+
let toolInput
|
|
265
259
|
|
|
266
|
-
|
|
260
|
+
app.ontoolinput = params => {
|
|
261
|
+
toolInput = params.arguments
|
|
262
|
+
}
|
|
267
263
|
|
|
268
|
-
|
|
264
|
+
document.querySelector('#recalculate').addEventListener('click', async () => {
|
|
265
|
+
const result = await app.callServerTool({
|
|
266
|
+
name: 'calculatorWithUI',
|
|
267
|
+
arguments: toolInput,
|
|
268
|
+
})
|
|
269
|
+
document.querySelector('#result').textContent = JSON.stringify(result)
|
|
269
270
|
|
|
270
|
-
|
|
271
|
-
|
|
271
|
+
await app.sendMessage({
|
|
272
|
+
role: 'user',
|
|
273
|
+
content: [{ type: 'text', text: 'Explain the recalculated result.' }],
|
|
274
|
+
})
|
|
275
|
+
})
|
|
272
276
|
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
},
|
|
278
|
-
gmail: {
|
|
279
|
-
url: new URL('https://mcp.composio.dev/gmail/[private-url-path]'),
|
|
280
|
-
},
|
|
281
|
-
},
|
|
282
|
-
})
|
|
277
|
+
await app.connect()
|
|
278
|
+
</script>
|
|
279
|
+
</body>
|
|
280
|
+
</html>
|
|
283
281
|
```
|
|
284
282
|
|
|
285
|
-
|
|
283
|
+
The guest-side APIs serve different parts of the interaction:
|
|
286
284
|
|
|
287
|
-
|
|
285
|
+
| API | Purpose |
|
|
286
|
+
| ---------------------- | ----------------------------------------------------- |
|
|
287
|
+
| `app.ontoolinput` | Receive the arguments from the host tool call |
|
|
288
|
+
| `app.callServerTool()` | Call an MCP tool from inside the iframe |
|
|
289
|
+
| `app.sendMessage()` | Add a user message to chat and start a new model turn |
|
|
290
|
+
| `app.connect()` | Connect to the host after registering event handlers |
|
|
288
291
|
|
|
289
|
-
|
|
292
|
+
The interaction follows this sequence:
|
|
290
293
|
|
|
291
|
-
|
|
294
|
+
1. The agent calls the tool.
|
|
295
|
+
2. The tool returns model-facing `content` and UI-facing `structuredContent`.
|
|
296
|
+
3. Studio renders the associated app resource.
|
|
297
|
+
4. The app receives tool input and can call server tools or send chat messages.
|
|
292
298
|
|
|
293
|
-
|
|
294
|
-
// Unix/Mac
|
|
295
|
-
import { MCPClient } from '@mastra/mcp'
|
|
299
|
+
Visit the external [`App` API reference](https://apps.extensions.modelcontextprotocol.io/api/classes/app.App.html) for all guest-side methods and lifecycle hooks.
|
|
296
300
|
|
|
297
|
-
|
|
298
|
-
servers: {
|
|
299
|
-
sequentialThinking: {
|
|
300
|
-
command: 'npx',
|
|
301
|
-
args: [
|
|
302
|
-
'-y',
|
|
303
|
-
'@smithery/cli@latest',
|
|
304
|
-
'run',
|
|
305
|
-
'@smithery-ai/server-sequential-thinking',
|
|
306
|
-
'--config',
|
|
307
|
-
'{}',
|
|
308
|
-
],
|
|
309
|
-
},
|
|
310
|
-
},
|
|
311
|
-
})
|
|
312
|
-
```
|
|
301
|
+
### Register MCP Apps
|
|
313
302
|
|
|
314
|
-
|
|
315
|
-
// Windows
|
|
316
|
-
import { MCPClient } from '@mastra/mcp'
|
|
317
|
-
|
|
318
|
-
const mcp = new MCPClient({
|
|
319
|
-
servers: {
|
|
320
|
-
sequentialThinking: {
|
|
321
|
-
command: 'npx',
|
|
322
|
-
args: [
|
|
323
|
-
'-y',
|
|
324
|
-
'@smithery/cli@latest',
|
|
325
|
-
'run',
|
|
326
|
-
'@smithery-ai/server-sequential-thinking',
|
|
327
|
-
'--config',
|
|
328
|
-
'{}',
|
|
329
|
-
],
|
|
330
|
-
},
|
|
331
|
-
},
|
|
332
|
-
})
|
|
333
|
-
```
|
|
334
|
-
|
|
335
|
-
**Apify**:
|
|
336
|
-
|
|
337
|
-
[Apify](https://apify.com) is the largest marketplace of tools for AI, with thousands of ready-made [Actors](https://apify.com/store) to extract real-time data from any website, track competitors, generate leads, analyze sentiment, or orchestrate your apps. Connect your agent through the [Apify MCP Server](https://mcp.apify.com).
|
|
303
|
+
For a local app, pass the tool to an agent and register its MCP server on `Mastra`:
|
|
338
304
|
|
|
339
305
|
```typescript
|
|
340
|
-
import {
|
|
306
|
+
import { Agent } from '@mastra/core/agent'
|
|
307
|
+
import { Mastra } from '@mastra/core/mastra'
|
|
308
|
+
import { calculatorMcpServer, calculatorTool } from './mcp/calculator'
|
|
309
|
+
|
|
310
|
+
const calculatorAgent = new Agent({
|
|
311
|
+
id: 'calculator-agent',
|
|
312
|
+
name: 'Calculator Agent',
|
|
313
|
+
instructions: 'Use the calculator tool for arithmetic.',
|
|
314
|
+
model: 'openai/gpt-5-mini',
|
|
315
|
+
tools: { calculatorTool },
|
|
316
|
+
})
|
|
341
317
|
|
|
342
|
-
export const
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
url: new URL('https://mcp.apify.com'),
|
|
346
|
-
requestInit: {
|
|
347
|
-
headers: {
|
|
348
|
-
Authorization: `Bearer ${process.env.APIFY_TOKEN}`,
|
|
349
|
-
},
|
|
350
|
-
},
|
|
351
|
-
},
|
|
352
|
-
},
|
|
318
|
+
export const mastra = new Mastra({
|
|
319
|
+
agents: { calculatorAgent },
|
|
320
|
+
mcpServers: { calculatorMcpServer },
|
|
353
321
|
})
|
|
354
322
|
```
|
|
355
323
|
|
|
356
|
-
|
|
324
|
+
For an external MCP server that implements MCP Apps, load its tools with `MCPClient.listTools()` and register its proxy so Studio can resolve the remote app resources:
|
|
357
325
|
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
326
|
+
```typescript
|
|
327
|
+
import { Agent } from '@mastra/core/agent'
|
|
328
|
+
import { Mastra } from '@mastra/core/mastra'
|
|
329
|
+
import { mcpClient } from './mcp/client'
|
|
361
330
|
|
|
362
|
-
|
|
331
|
+
const tools = await mcpClient.listTools()
|
|
332
|
+
const mcpServers = mcpClient.toMCPServerProxies()
|
|
363
333
|
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
apiKey: process.env.AMPERSAND_API_KEY,
|
|
371
|
-
project: process.env.AMPERSAND_PROJECT_ID,
|
|
372
|
-
integrationName: process.env.AMPERSAND_INTEGRATION_NAME,
|
|
373
|
-
groupRef: process.env.AMPERSAND_GROUP_REF,
|
|
374
|
-
})}`,
|
|
375
|
-
},
|
|
376
|
-
},
|
|
334
|
+
const agent = new Agent({
|
|
335
|
+
id: 'remote-app-agent',
|
|
336
|
+
name: 'Remote App Agent',
|
|
337
|
+
instructions: 'Use the available remote tools.',
|
|
338
|
+
model: 'openai/gpt-5-mini',
|
|
339
|
+
tools,
|
|
377
340
|
})
|
|
378
|
-
```
|
|
379
341
|
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
// MCPClient with Ampersand MCP Server using stdio transport
|
|
385
|
-
export const mcp = new MCPClient({
|
|
386
|
-
servers: {
|
|
387
|
-
'@amp-labs/mcp-server': {
|
|
388
|
-
command: 'npx',
|
|
389
|
-
args: [
|
|
390
|
-
'-y',
|
|
391
|
-
'@amp-labs/mcp-server@latest',
|
|
392
|
-
'--transport',
|
|
393
|
-
'stdio',
|
|
394
|
-
'--project',
|
|
395
|
-
process.env.AMPERSAND_PROJECT_ID,
|
|
396
|
-
'--integrationName',
|
|
397
|
-
process.env.AMPERSAND_INTEGRATION_NAME,
|
|
398
|
-
'--groupRef',
|
|
399
|
-
process.env.AMPERSAND_GROUP_REF, // optional
|
|
400
|
-
],
|
|
401
|
-
env: {
|
|
402
|
-
AMPERSAND_API_KEY: process.env.AMPERSAND_API_KEY,
|
|
403
|
-
},
|
|
404
|
-
},
|
|
405
|
-
},
|
|
342
|
+
export const mastra = new Mastra({
|
|
343
|
+
agents: { agent },
|
|
344
|
+
mcpServers,
|
|
406
345
|
})
|
|
407
346
|
```
|
|
408
347
|
|
|
409
|
-
|
|
348
|
+
Tools loaded through `listTools()` include a `serverId` in `_meta.ui`, allowing Studio to resolve each app resource without scanning every server. Visit [`toMCPServerProxies()`](https://mastra.ai/reference/tools/mcp-client) for proxy configuration details.
|
|
349
|
+
|
|
350
|
+
### Sandbox security
|
|
410
351
|
|
|
411
|
-
|
|
352
|
+
Mastra Studio uses [`@mcp-ui/client`](https://www.npmjs.com/package/@mcp-ui/client) to load app HTML through a sandbox proxy and communicate over JSON-RPC with `postMessage`.
|
|
412
353
|
|
|
413
|
-
|
|
354
|
+
App iframes allow scripts, forms, and popups. They can't access the parent page's DOM, cookies, or storage. The host controls all communication with the guest app.
|
|
414
355
|
|
|
415
|
-
##
|
|
356
|
+
## Next steps
|
|
416
357
|
|
|
417
|
-
- [
|
|
418
|
-
- [
|
|
419
|
-
- [
|
|
420
|
-
- [
|
|
358
|
+
- [Use tools with agents](https://mastra.ai/docs/agents/using-tools)
|
|
359
|
+
- [`MCPClient` reference](https://mastra.ai/reference/tools/mcp-client)
|
|
360
|
+
- [`MCPServer` reference](https://mastra.ai/reference/tools/mcp-server)
|
|
361
|
+
- [MCP Apps extension specification](https://github.com/modelcontextprotocol/ext-apps)
|
|
@@ -188,7 +188,7 @@ Conversation messages are ordered by timestamp and deduplicated by message ID, s
|
|
|
188
188
|
|
|
189
189
|
## Memory in multi-agent systems
|
|
190
190
|
|
|
191
|
-
When a [supervisor agent](https://mastra.ai/docs/
|
|
191
|
+
When a [supervisor agent](https://mastra.ai/docs/capabilities/subagents) delegates to a subagent, Mastra isolates subagent memory automatically. No flag enables this as it happens on every delegation. Understanding how this scoping works lets you decide what stays private and what to share intentionally.
|
|
192
192
|
|
|
193
193
|
### How delegation scopes memory
|
|
194
194
|
|
|
@@ -200,7 +200,7 @@ Each delegation creates a fresh `threadId` and a deterministic `resourceId` for
|
|
|
200
200
|
|
|
201
201
|
> **Note:** Title generation (`generateTitle`) is a top-level thread concern and **isn't** applied to inherited subagent threads. Because each delegation creates an ephemeral thread that no one sees, running title generation for it would waste an LLM call per delegation. To generate titles for a subagent's own threads, give that subagent its own memory configuration.
|
|
202
202
|
|
|
203
|
-
The supervisor forwards its conversation context to the subagent so it has enough background to complete the task. Only the delegation prompt and the subagent's response are saved, the full parent conversation isn't stored. You can control which messages reach the subagent with the [`messageFilter`](https://mastra.ai/docs/
|
|
203
|
+
The supervisor forwards its conversation context to the subagent so it has enough background to complete the task. Only the delegation prompt and the subagent's response are saved, the full parent conversation isn't stored. You can control which messages reach the subagent with the [`messageFilter`](https://mastra.ai/docs/capabilities/subagents) callback.
|
|
204
204
|
|
|
205
205
|
> **Note:** Subagent resource IDs are always suffixed with the agent name (`{parentResourceId}-{agentName}`). Different subagents under the same supervisor never share a resource ID through delegation.
|
|
206
206
|
|