@mastra/mcp 1.17.0-alpha.0 → 1.17.0-alpha.1

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 CHANGED
@@ -1,5 +1,37 @@
1
1
  # @mastra/mcp
2
2
 
3
+ ## 1.17.0-alpha.1
4
+
5
+ ### Minor Changes
6
+
7
+ - MCP tools served over HTTP now see the authenticated caller. When an MCP server runs behind a Mastra server with `server.auth` configured, the resolved user is bridged into `extra.authInfo` automatically, on both the streamable HTTP and SSE transports. Previously `extra.authInfo` was always undefined because the request handed to the MCP transport was rebuilt without the auth data. ([#21689](https://github.com/mastra-ai/mastra/pull/21689))
8
+
9
+ **Custom verification**
10
+
11
+ If your own middleware verifies the caller, build the auth info yourself with the new `server.mcpOptions.setRequestAuth` hook:
12
+
13
+ ```ts
14
+ export const mastra = new Mastra({
15
+ mcpServers: { myServer },
16
+ server: {
17
+ middleware: [verifyBearerToken],
18
+ mcpOptions: {
19
+ setRequestAuth: (req, requestContext) => {
20
+ const payload = requestContext.get('bearerPayload');
21
+ req.auth = { token: payload.token, clientId: payload.sub, scopes: payload.scope.split(' ') };
22
+ },
23
+ },
24
+ },
25
+ });
26
+ ```
27
+
28
+ Fixes #17291
29
+
30
+ ### Patch Changes
31
+
32
+ - Updated dependencies [[`4e7a421`](https://github.com/mastra-ai/mastra/commit/4e7a421dce8a48742f785d1e93ad2f43a572b282), [`242e324`](https://github.com/mastra-ai/mastra/commit/242e3241e73cbd5c9bb86a31ebb49ca0256488d4), [`217e967`](https://github.com/mastra-ai/mastra/commit/217e9672d8b3160eb729d8e9f0044949e88da239), [`d774e89`](https://github.com/mastra-ai/mastra/commit/d774e8930c781df8c9effe3763e6b501c099b6cc), [`9c27a53`](https://github.com/mastra-ai/mastra/commit/9c27a53cd9d3de4f3f025bc387d94ce371c33f95), [`dff25a1`](https://github.com/mastra-ai/mastra/commit/dff25a1103fa72ee082a9b6f805ebeb5ce400753), [`217e967`](https://github.com/mastra-ai/mastra/commit/217e9672d8b3160eb729d8e9f0044949e88da239), [`7f78585`](https://github.com/mastra-ai/mastra/commit/7f785857e401570e2ffb316911f126ed363aa537), [`f2a4afd`](https://github.com/mastra-ai/mastra/commit/f2a4afd7e37e809669001ed17724b341a5c1f45e), [`d438148`](https://github.com/mastra-ai/mastra/commit/d438148e222c1e2fb3c652725ce75680962ebec4), [`ba05fe0`](https://github.com/mastra-ai/mastra/commit/ba05fe0738f70cb686777546e968237d09269142), [`d26a8d4`](https://github.com/mastra-ai/mastra/commit/d26a8d4281f28414715b333c85bedaf70d0b2890), [`677cdc6`](https://github.com/mastra-ai/mastra/commit/677cdc6af564dec29a13464d12b7ab2a4efc22e9), [`a318490`](https://github.com/mastra-ai/mastra/commit/a318490e17da32f338d50929c770d901a9b3dd72), [`763e0c6`](https://github.com/mastra-ai/mastra/commit/763e0c61e04d76ad9a9efd301aa57525ca0cbea9), [`23e0be2`](https://github.com/mastra-ai/mastra/commit/23e0be261381e49534b4ff3101c60ee64a946cbf), [`7fc8806`](https://github.com/mastra-ai/mastra/commit/7fc880627d3cbf995d31ea0e8b807bf15417e651), [`0e02eac`](https://github.com/mastra-ai/mastra/commit/0e02eacdb2e30e1697a41910b41163742a181dc1), [`4df174c`](https://github.com/mastra-ai/mastra/commit/4df174c32bddf093a82f273070b8380aef7c9e90), [`f7c25b5`](https://github.com/mastra-ai/mastra/commit/f7c25b5106ddfb48e591f98df7a51e0f2dd01dba), [`dc09cc1`](https://github.com/mastra-ai/mastra/commit/dc09cc1083d861cde192c1cd235324dc75b8c731), [`36b4649`](https://github.com/mastra-ai/mastra/commit/36b4649045a3a380cbab8ceca866db4086223aff), [`377eb81`](https://github.com/mastra-ai/mastra/commit/377eb81ce43b964e3a6b541df172da74a8ff3716)]:
33
+ - @mastra/core@1.60.0-alpha.8
34
+
3
35
  ## 1.17.0-alpha.0
4
36
 
5
37
  ### Minor Changes
@@ -3,7 +3,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.17.0-alpha.0"
6
+ version: "1.17.0-alpha.1"
7
7
  ---
8
8
 
9
9
  ## When to use
@@ -16,9 +16,9 @@ Read the individual reference documents for detailed explanations and code examp
16
16
 
17
17
  ### Docs
18
18
 
19
+ - [Fine-Grained Authorization (FGA)](references/docs-auth-fga.md) - Add resource-level authorization to your Mastra application with FGA providers.
20
+ - [MCP](references/docs-connections-mcp.md) - Connect Mastra agents to external MCP servers and expose Mastra tools through MCPServer. You can also build interactive MCP Apps for Studio.
19
21
  - [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
- - [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
24
24
 
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.17.0-alpha.0",
2
+ "version": "1.17.0-alpha.1",
3
3
  "package": "@mastra/mcp",
4
4
  "exports": {},
5
5
  "modules": {}
@@ -299,5 +299,5 @@ The actor signal is trusted input, so construct it server-side:
299
299
  ## Related
300
300
 
301
301
  - [`IFGAProvider` reference](https://mastra.ai/reference/auth/fga)
302
- - [Authentication overview](https://mastra.ai/docs/server/auth)
302
+ - [Authentication overview](https://mastra.ai/docs/auth/overview)
303
303
  - [WorkOS authentication](https://mastra.ai/integrations/auth/workos)
@@ -406,7 +406,7 @@ App iframes allow scripts, forms, and popups. They can't access the parent page'
406
406
 
407
407
  ## Next steps
408
408
 
409
- - [Use tools with agents](https://mastra.ai/docs/agents/using-tools)
409
+ - [Use tools with agents](https://mastra.ai/docs/agents/tools)
410
410
  - [`MCPClient` reference](https://mastra.ai/reference/tools/mcp-client)
411
411
  - [`MCPServer` reference](https://mastra.ai/reference/tools/mcp-server)
412
412
  - [MCP Apps extension specification](https://github.com/modelcontextprotocol/ext-apps)
@@ -4,10 +4,10 @@
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
 
7
- - [**Agent-to-Agent (A2A)**](https://mastra.ai/docs/agents/a2a): Expose or consume remote agents across service, framework, vendor, and language boundaries.
8
- - [**Agent Client Protocol (ACP)**](https://mastra.ai/docs/agents/acp): Run compatible coding-agent processes as Mastra tools or subagents.
9
- - [**SDK agents**](https://mastra.ai/docs/agents/sdk-agents): Register Claude, Cursor, or OpenAI SDK-backed agents while the provider SDK retains control of the runtime, tools, permissions, and agent loop.
10
- - [**Model Context Protocol (MCP)**](https://mastra.ai/docs/mcp/overview): Connect agents to external tools and resources, or expose Mastra agents, tools, workflows, prompts, and resources to MCP-compatible systems.
7
+ - [**Model Context Protocol (MCP)**](https://mastra.ai/docs/connections/mcp): Connect agents to external tools and resources, or expose Mastra agents, tools, workflows, prompts, and resources to MCP-compatible systems.
8
+ - [**Agent-to-Agent (A2A)**](https://mastra.ai/docs/connections/a2a): Expose or consume remote agents across service, framework, vendor, and language boundaries.
9
+ - [**Agent Client Protocol (ACP)**](https://mastra.ai/docs/connections/acp): Run compatible coding-agent processes as Mastra tools or subagents.
10
+ - [**SDK agents**](https://mastra.ai/docs/connections/sdk-agents): Register Claude, Cursor, or OpenAI SDK-backed agents while the provider SDK retains control of the runtime, tools, permissions, and agent loop.
11
11
 
12
12
  ## When to use connections
13
13
 
@@ -20,7 +20,7 @@ Use connections when you need to:
20
20
 
21
21
  ## Get started
22
22
 
23
- Start with the boundary you need to cross. Use [A2A](https://mastra.ai/docs/agents/a2a) for remote agent endpoints, [ACP](https://mastra.ai/docs/agents/acp) for coding-agent processes, [SDK agents](https://mastra.ai/docs/agents/sdk-agents) for provider-owned runtimes, or [MCP](https://mastra.ai/docs/mcp/overview) for tools and resources.
23
+ Start with the boundary you need to cross. Use [A2A](https://mastra.ai/docs/connections/a2a) for remote agent endpoints, [ACP](https://mastra.ai/docs/connections/acp) for coding-agent processes, [SDK agents](https://mastra.ai/docs/connections/sdk-agents) for provider-owned runtimes, or [MCP](https://mastra.ai/docs/connections/mcp) for tools and resources.
24
24
 
25
25
  **A2A**:
26
26
 
@@ -4,7 +4,7 @@
4
4
 
5
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
- See [Editor tools](https://mastra.ai/docs/editor/overview) for the Studio workflow and common uses.
7
+ See [Editor tools](https://mastra.ai/docs/studio/editor) for the Studio workflow and common uses.
8
8
 
9
9
  ## Tool sources
10
10
 
@@ -168,6 +168,51 @@ const fetchUserData = createTool({
168
168
  })
169
169
  ```
170
170
 
171
+ #### Where `authInfo` comes from
172
+
173
+ When an MCP server is served by a Mastra server, `extra.authInfo` is populated from the principal resolved by `server.auth`. Nothing extra is required:
174
+
175
+ ```typescript
176
+ export const mastra = new Mastra({
177
+ mcpServers: { myServer },
178
+ server: {
179
+ auth: new MastraJwtAuth({ secret: process.env.JWT_SECRET! }),
180
+ },
181
+ })
182
+ ```
183
+
184
+ The authenticated user is mapped to `authInfo` as:
185
+
186
+ | `authInfo` field | Value |
187
+ | ---------------- | -------------------------------------------------------------------------- |
188
+ | `token` | The bearer token or session cookie value used for the request |
189
+ | `clientId` | The first present of `user.id`, `user.sub`, `user.userId`, `user.email` |
190
+ | `scopes` | `user.scopes`, `user.scope`, or `user.permissions`, normalized to an array |
191
+ | `extra.user` | The full user object returned by the auth provider |
192
+
193
+ If your own middleware performs the verification, set `server.mcpOptions.setRequestAuth` to build `authInfo` yourself. The hook replaces the default mapping and applies to both the streamable HTTP and SSE transports:
194
+
195
+ ```typescript
196
+ export const mastra = new Mastra({
197
+ mcpServers: { myServer },
198
+ server: {
199
+ middleware: [verifyBearerToken], // stores the payload on the request context
200
+ mcpOptions: {
201
+ setRequestAuth: (req, requestContext) => {
202
+ const payload = requestContext.get('bearerPayload')
203
+ req.auth = {
204
+ token: payload.token,
205
+ clientId: payload.sub,
206
+ scopes: payload.scope.split(' '),
207
+ }
208
+ },
209
+ },
210
+ },
211
+ })
212
+ ```
213
+
214
+ Leaving `req.auth` unset inside the hook opts the request out of auth info entirely.
215
+
171
216
  ## Methods
172
217
 
173
218
  These are the functions you can call on an `MCPServer` instance to control its behavior and get information.
@@ -929,7 +974,7 @@ Notification methods (`resources.notifyListChanged()`, `prompts.notifyListChange
929
974
 
930
975
  ## Examples
931
976
 
932
- For a practical example of packaging a stdio server, see [Publish a stdio server package](https://mastra.ai/docs/mcp/overview).
977
+ For a practical example of packaging a stdio server, see [Publish a stdio server package](https://mastra.ai/docs/connections/mcp).
933
978
 
934
979
  The example at the beginning of this page also demonstrates how to instantiate `MCPServer` with both tools and agents.
935
980
 
@@ -1663,7 +1708,7 @@ const server = new MCPServer({
1663
1708
  })
1664
1709
  ```
1665
1710
 
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.
1711
+ 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/connections/mcp) for the full app bridge API and usage patterns.
1667
1712
 
1668
1713
  ## Related information
1669
1714
 
package/dist/index.cjs CHANGED
@@ -26555,6 +26555,8 @@ var MCPServer = class extends _mastra_core_mcp.MCPServerBase {
26555
26555
  stdioTransport;
26556
26556
  sseTransport;
26557
26557
  sseHonoTransports;
26558
+ /** Auth info for the in-flight Hono SSE message POST, keyed by session id. */
26559
+ sseHonoAuthInfo = /* @__PURE__ */ new Map();
26558
26560
  streamableHTTPTransports = /* @__PURE__ */ new Map();
26559
26561
  httpServerInstances = /* @__PURE__ */ new Map();
26560
26562
  resourceOptions;
@@ -27731,7 +27733,7 @@ var MCPServer = class extends _mastra_core_mcp.MCPServerBase {
27731
27733
  * export default app;
27732
27734
  * ```
27733
27735
  */
27734
- async startHonoSSE({ url, ssePath, messagePath, context }) {
27736
+ async startHonoSSE({ url, ssePath, messagePath, context, authInfo }) {
27735
27737
  const honoContext = context;
27736
27738
  try {
27737
27739
  if (url.pathname === ssePath) return streamSSE(honoContext, async (stream) => {
@@ -27746,6 +27748,8 @@ var MCPServer = class extends _mastra_core_mcp.MCPServerBase {
27746
27748
  this.logger.debug("Received message for sessionId", { sessionId });
27747
27749
  if (!sessionId) return honoContext.text("No sessionId provided", 400);
27748
27750
  if (!this.sseHonoTransports.has(sessionId)) return honoContext.text(`No transport found for sessionId ${sessionId}`, 400);
27751
+ if (authInfo) this.sseHonoAuthInfo.set(sessionId, authInfo);
27752
+ else this.sseHonoAuthInfo.delete(sessionId);
27749
27753
  const message = await this.sseHonoTransports.get(sessionId)?.handlePostMessage(honoContext);
27750
27754
  if (!message) return honoContext.text("Transport not found", 400);
27751
27755
  return message;
@@ -28098,12 +28102,22 @@ var MCPServer = class extends _mastra_core_mcp.MCPServerBase {
28098
28102
  stream.onAbort(() => {
28099
28103
  this.logger.debug("SSE Transport aborted with sessionId:", { sessionId });
28100
28104
  this.sseHonoTransports.delete(sessionId);
28105
+ this.sseHonoAuthInfo.delete(sessionId);
28101
28106
  });
28102
28107
  try {
28103
28108
  await this.server.connect(sseTransport);
28109
+ const onmessage = sseTransport.onmessage?.bind(sseTransport);
28110
+ if (onmessage) sseTransport.onmessage = (message, extra) => {
28111
+ const authInfo = this.sseHonoAuthInfo.get(sessionId);
28112
+ onmessage(message, authInfo ? {
28113
+ ...extra,
28114
+ authInfo
28115
+ } : extra);
28116
+ };
28104
28117
  this.server.onclose = async () => {
28105
28118
  this.logger.debug("SSE Transport closed with sessionId:", { sessionId });
28106
28119
  this.sseHonoTransports.delete(sessionId);
28120
+ this.sseHonoAuthInfo.delete(sessionId);
28107
28121
  await this.server.close();
28108
28122
  };
28109
28123
  while (true) {