@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.
Files changed (65) hide show
  1. package/.docs/docs/agents/a2a.md +1 -1
  2. package/.docs/docs/agents/acp.md +1 -1
  3. package/.docs/docs/agents/agent-approval.md +1 -1
  4. package/.docs/docs/agents/networks.md +2 -2
  5. package/.docs/docs/agents/overview.md +2 -2
  6. package/.docs/docs/agents/using-tools.md +2 -2
  7. package/.docs/docs/capabilities/channels/overview.md +1 -1
  8. package/.docs/docs/{agents/supervisor-agents.md → capabilities/subagents.md} +58 -58
  9. package/.docs/docs/deployment/sandbox.md +95 -1
  10. package/.docs/docs/getting-started/develop.md +2 -0
  11. package/.docs/docs/harness/agent-controller.md +370 -0
  12. package/.docs/docs/long-running-agents/background-tasks.md +1 -1
  13. package/.docs/docs/long-running-agents/goals.md +3 -3
  14. package/.docs/docs/mcp/overview.md +222 -281
  15. package/.docs/docs/memory/overview.md +2 -2
  16. package/.docs/docs/observability/integrations/exporters/confident-ai.md +140 -0
  17. package/.docs/docs/observability/integrations/overview.md +1 -1
  18. package/.docs/docs/observability/overview.md +122 -8
  19. package/.docs/docs/observability/tracing/overview.md +2 -2
  20. package/.docs/docs/server/mastra-client.md +1 -1
  21. package/.docs/docs/server/server-adapters.md +2 -0
  22. package/.docs/docs/storage/overview.md +2 -2
  23. package/.docs/guides/build-your-ui/copilotkit/channels.md +49 -39
  24. package/.docs/guides/concepts/multi-agent-systems.md +7 -7
  25. package/.docs/guides/guide/coding-agent.md +2 -2
  26. package/.docs/guides/guide/research-coordinator.md +1 -1
  27. package/.docs/guides/migrations/network-to-supervisor.md +1 -1
  28. package/.docs/models/index.md +1 -1
  29. package/.docs/models/providers/cortecs.md +2 -1
  30. package/.docs/models/providers/digitalocean.md +9 -9
  31. package/.docs/models/providers/kilo.md +1 -1
  32. package/.docs/models/providers/llmgateway.md +1 -1
  33. package/.docs/models/providers/opencode.md +2 -1
  34. package/.docs/reference/acp/acp-agent.md +1 -1
  35. package/.docs/reference/agent-controller/agent-controller-class.md +195 -506
  36. package/.docs/reference/agent-controller/session.md +274 -111
  37. package/.docs/reference/agents/network.md +1 -1
  38. package/.docs/reference/ai-sdk/handle-network-stream.md +1 -1
  39. package/.docs/reference/ai-sdk/network-route.md +1 -1
  40. package/.docs/reference/cli/create-mastra.md +1 -1
  41. package/.docs/reference/editor/versioning.md +1 -1
  42. package/.docs/reference/evals/rubric.md +1 -1
  43. package/.docs/reference/file-based-agents/observability.md +2 -2
  44. package/.docs/reference/file-based-agents/storage.md +1 -1
  45. package/.docs/reference/file-based-agents/subagents.md +1 -1
  46. package/.docs/reference/file-based-agents/tools.md +1 -1
  47. package/.docs/reference/observability/tracing/configuration.md +1 -1
  48. package/.docs/reference/observability/tracing/exporters/confident-ai.md +138 -0
  49. package/.docs/reference/observability/tracing/interfaces.md +29 -0
  50. package/.docs/reference/signals/signal-provider.md +2 -0
  51. package/.docs/reference/tools/mcp-server.md +1 -1
  52. package/.docs/reference/tools/submit-plan-tool.md +1 -1
  53. package/.docs/reference/workspace/railway-sandbox.md +5 -5
  54. package/CHANGELOG.md +14 -0
  55. package/package.json +3 -3
  56. package/.docs/docs/agent-controller/channels.md +0 -111
  57. package/.docs/docs/agent-controller/modes.md +0 -147
  58. package/.docs/docs/agent-controller/overview.md +0 -136
  59. package/.docs/docs/agent-controller/session.md +0 -161
  60. package/.docs/docs/agent-controller/subagents.md +0 -110
  61. package/.docs/docs/agent-controller/threads-and-state.md +0 -148
  62. package/.docs/docs/agent-controller/tool-approvals.md +0 -147
  63. package/.docs/docs/mcp/mcp-apps.md +0 -306
  64. package/.docs/docs/observability/config.md +0 -140
  65. 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. It's a universal plugin system, enabling agents to call tools regardless of language or hosting environment.
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
- Mastra can also be used to author MCP servers, exposing agents, tools, and other structured resources via the MCP interface. These can then be accessed by any system or agent that supports the protocol.
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
- Mastra currently supports two MCP classes:
9
+ ## Connect to MCP servers
10
10
 
11
- 1. `MCPClient`: Connects to one or many MCP servers to access their tools, resources, prompts, and handle elicitation requests.
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
- ## Configuring `MCPClient`
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 testMcpClient = new MCPClient({
50
- id: 'test-mcp-client',
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
- `https://server.smithery.ai/@smithery-ai/national-weather-service/mcp?api_key=${process.env.SMITHERY_API_KEY}`,
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 [MCPClient](https://mastra.ai/reference/tools/mcp-client) for a full list of configuration options.
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
- To use tools from an MCP server in an agent, import your `MCPClient` and call `.listTools()` in the `tools` parameter. This loads from the defined MCP servers, making them available to the agent.
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 { testMcpClient } from '../mcp/test-mcp-client'
67
+ import { mcpClient } from '../mcp/client'
76
68
 
77
- export const testAgent = new Agent({
78
- id: 'test-agent',
79
- name: 'Test Agent',
80
- description: 'You are a helpful AI assistant',
69
+ export const assistant = new Agent({
70
+ id: 'assistant',
71
+ name: 'Assistant',
81
72
  instructions: `
82
- You are a helpful assistant that has access to the following MCP Servers.
83
- - Wikipedia MCP Server
84
- - US National Weather Service
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 testMcpClient.listTools(),
77
+ tools: await mcpClient.listTools(),
89
78
  })
90
79
  ```
91
80
 
92
- Visit [Agent Class](https://mastra.ai/reference/agents/agent) for a full list of configuration options.
81
+ ### Static and runtime tools
82
+
83
+ Choose how to load tools based on whether the server configuration changes between requests:
93
84
 
94
- ## Tool approval
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
- You can require human approval before MCP tools are executed by setting `requireToolApproval` on a server definition. This integrates with the existing [human-in-the-loop](https://mastra.ai/docs/workflows/human-in-the-loop) approval flow.
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
- export const mcp = new MCPClient({
100
- servers: {
101
- github: {
102
- url: new URL('http://localhost:3000/mcp'),
103
- requireToolApproval: true,
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
- You can also pass a function to decide per runtime-call. See the [MCPClient reference](https://mastra.ai/reference/tools/mcp-client) for the full API.
110
+ const agent = mastra.getAgent('assistant')
111
+ const response = await agent.generate(prompt, {
112
+ toolsets: await userMcpClient.listToolsets(),
113
+ })
110
114
 
111
- ## Configuring `MCPServer`
115
+ await userMcpClient.disconnect()
116
+ return response.text
117
+ }
118
+ ```
112
119
 
113
- To expose agents, tools, and workflows from your Mastra application to external systems over HTTP(S) use the `MCPServer` class. This makes them accessible to any system or agent that supports the protocol.
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
- ```typescript
116
- import { MCPServer } from '@mastra/mcp'
122
+ ### Tool approval
117
123
 
118
- import { testAgent } from '../agents/test-agent'
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
- export const testMcpServer = new MCPServer({
123
- id: 'test-mcp-server',
124
- name: 'Test Server',
125
- version: '1.0.0',
126
- agents: { testAgent },
127
- tools: { testTool },
128
- workflows: { testWorkflow },
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
- Visit [MCPServer](https://mastra.ai/reference/tools/mcp-server) for a full list of configuration options.
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
- import { Mastra } from '@mastra/core/mastra'
140
+ requireToolApproval: ({ toolName }) => toolName.startsWith('delete_')
141
+ ```
142
142
 
143
- import { testMcpServer } from './mcp/test-mcp-server'
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
- export const mastra = new Mastra({
146
- mcpServers: { testMcpServer },
147
- })
148
- ```
145
+ ### MCP registries
149
146
 
150
- ## Static and runtime tools
147
+ Registries provide hosted or packaged MCP servers. The client configuration above works with registry endpoints and commands.
151
148
 
152
- `MCPClient` offers two approaches to retrieving tools from connected servers, suitable for different application architectures:
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
- | Feature | Static Configuration (`await mcp.listTools()`) | Dynamic Configuration (`await mcp.listToolsets()`) |
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
- ### Static tools
160
+ ## Expose a Mastra MCP server
162
161
 
163
- Use the `.listTools()` method to fetch tools from all configured MCP servers. This is suitable when configuration (such as API keys) is static and consistent across users or requests. Call it once and pass the result to the `tools` property when defining your agent. Visit [listTools()](https://mastra.ai/reference/tools/mcp-client) for more information.
162
+ Create an `MCPServer` to expose Mastra primitives to external MCP clients:
164
163
 
165
164
  ```typescript
166
- import { Agent } from '@mastra/core/agent'
167
-
168
- import { testMcpClient } from '../mcp/test-mcp-client'
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 testAgent = new Agent({
171
- id: 'test-agent',
172
- tools: await testMcpClient.listTools(),
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
- ### Dynamic tools
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 { MCPClient } from '@mastra/mcp'
182
- import { mastra } from './mastra'
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
- const response = await agent.generate(userPrompt, {
201
- toolsets: await userMcp.listToolsets(),
202
- })
186
+ export const mastra = new Mastra({
187
+ mcpServers: { mcpServer },
188
+ })
189
+ ```
203
190
 
204
- await userMcp.disconnect()
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
- return Response.json({
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
- Visit [listToolsets()](https://mastra.ai/reference/tools/mcp-client) for more information.
195
+ ## Build MCP Apps
213
196
 
214
- ## Connecting to an MCP registry
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 servers can be discovered through registries. Here's how to connect to some popular ones using `MCPClient`:
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
- **Klavis AI**:
201
+ ### Define an app resource
219
202
 
220
- [Klavis AI](https://klavis.ai) provides hosted, enterprise-authenticated, high-quality MCP servers.
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 { MCPClient } from '@mastra/mcp'
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
- const mcp = new MCPClient({
226
- servers: {
227
- salesforce: {
228
- url: new URL(
229
- 'https://salesforce-mcp-server.klavis.ai/mcp/?instance_id={private-instance-id}',
230
- ),
231
- },
232
- hubspot: {
233
- url: new URL('https://hubspot-mcp-server.klavis.ai/mcp/?instance_id={private-instance-id}'),
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
- Klavis AI offers enterprise-grade authentication and security for production deployments.
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
- **mcp.run**:
243
+ ### Connect the app to Studio
244
244
 
245
- [mcp.run](https://www.mcp.run/) provides pre-authenticated, managed MCP servers. Tools are grouped into Profiles, each with a unique, signed URL.
245
+ Use the `App` class from `@modelcontextprotocol/ext-apps` inside the HTML resource. Register event handlers before calling `connect()`:
246
246
 
247
- ```typescript
248
- import { MCPClient } from '@mastra/mcp'
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
- const mcp = new MCPClient({
251
- servers: {
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
- > **Requirement:** Treat the mcp.run SSE URL like a password. Store it securely, for example, in an environment variable.
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
- **Composio.dev**:
260
+ app.ontoolinput = params => {
261
+ toolInput = params.arguments
262
+ }
267
263
 
268
- [Composio.dev](https://composio.dev) offers a registry of [SSE-based MCP servers](https://mcp.composio.dev). You can use the SSE URL generated for tools like Cursor directly.
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
- ```typescript
271
- import { MCPClient } from '@mastra/mcp'
271
+ await app.sendMessage({
272
+ role: 'user',
273
+ content: [{ type: 'text', text: 'Explain the recalculated result.' }],
274
+ })
275
+ })
272
276
 
273
- const mcp = new MCPClient({
274
- servers: {
275
- googleSheets: {
276
- url: new URL('https://mcp.composio.dev/googlesheets/[private-url-path]'),
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
- Authentication with services like Google Sheets often happens interactively through the agent conversation.
283
+ The guest-side APIs serve different parts of the interaction:
286
284
 
287
- _Note: Composio URLs are typically tied to a single user account, making them best suited for personal automation rather than multi-tenant applications._
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
- **Smithery.ai**:
292
+ The interaction follows this sequence:
290
293
 
291
- [Smithery.ai](https://smithery.ai) provides a registry accessible via their CLI.
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
- ```typescript
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
- const mcp = new MCPClient({
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
- ```typescript
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 { MCPClient } from '@mastra/mcp'
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 mcp = new MCPClient({
343
- servers: {
344
- apify: {
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
- Get your API token from the [Apify Console](https://console.apify.com/settings/integrations) and store it as `APIFY_TOKEN` in your environment.
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
- To pick specific Actors and tools, use the [Apify MCP server configurator](https://mcp.apify.com) and copy the generated URL.
359
-
360
- **Ampersand**:
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
- [Ampersand](https://withampersand.com?utm_source=mastra-docs) offers an [MCP Server](https://docs.withampersand.com/mcp) that allows you to connect your agent to 150+ integrations with SaaS products like Salesforce, Hubspot, and Zendesk.
331
+ const tools = await mcpClient.listTools()
332
+ const mcpServers = mcpClient.toMCPServerProxies()
363
333
 
364
- ```typescript
365
- // MCPClient with Ampersand MCP Server using SSE
366
- export const mcp = new MCPClient({
367
- servers: {
368
- '@amp-labs/mcp-server': {
369
- url: `https://mcp.withampersand.com/v1/sse?${new URLSearchParams({
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
- ```typescript
381
- // If you prefer to run the MCP server locally:
382
- import { MCPClient } from '@mastra/mcp'
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
- As an alternative to MCP, Ampersand's AI SDK also has an adapter for Mastra, so you can [directly import Ampersand tools](https://docs.withampersand.com/ai-sdk#use-with-mastra) for your agent to access.
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
- ## MCP Apps
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
- MCP servers can serve interactive HTML UIs via the MCP Apps extension. Tools with associated `ui://` resources render sandboxed iframes in Studio, both on tool detail pages and inline in agent chat. The app iframe can call server tools and inject messages into the conversation. Visit [MCP Apps](https://mastra.ai/docs/mcp/mcp-apps) for setup instructions and the app bridge API.
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
- ## Related
356
+ ## Next steps
416
357
 
417
- - [MCP Apps](https://mastra.ai/docs/mcp/mcp-apps)
418
- - [Using Tools](https://mastra.ai/docs/agents/using-tools)
419
- - [MCPClient](https://mastra.ai/reference/tools/mcp-client)
420
- - [MCPServer](https://mastra.ai/reference/tools/mcp-server)
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/agents/supervisor-agents) 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.
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/agents/supervisor-agents) callback.
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