@mastra/mcp 1.14.0 → 1.15.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,22 @@
1
1
  # @mastra/mcp
2
2
 
3
+ ## 1.15.0-alpha.1
4
+
5
+ ### Patch Changes
6
+
7
+ - Fixed MCP clients getting stuck after a failed reconnect to streamable-HTTP-only servers. `listTools()`, `callTool()`, and `forceReconnect()` now work once the server is reachable again. Fixes https://github.com/mastra-ai/mastra/issues/19862 ([#19894](https://github.com/mastra-ai/mastra/pull/19894))
8
+
9
+ ## 1.15.0-alpha.0
10
+
11
+ ### Minor Changes
12
+
13
+ - Complete the OAuth authorization-code loop in MCPClient for HTTP MCP servers. Connections rejected with an authorization error now surface a `needs-auth` state (readable via `getServerAuthState()`), and the new `authenticate(serverName)` method runs the interactive flow end to end: it captures the authorization code on a local loopback callback server (exported as `createOAuthCallbackServer` for hosts with custom redirect handling), exchanges it for tokens, and reconnects. ([#19466](https://github.com/mastra-ai/mastra/pull/19466))
14
+
15
+ ### Patch Changes
16
+
17
+ - Updated dependencies [[`8b20926`](https://github.com/mastra-ai/mastra/commit/8b20926cd59e2ba3d66458e062fa0e6e2ada3e68), [`74faf8b`](https://github.com/mastra-ai/mastra/commit/74faf8bd9c1018f2492653c06b1e25fc8300e9e6), [`1fadac4`](https://github.com/mastra-ai/mastra/commit/1fadac44537caeefe81f9f775ae2f2f3d94e9069), [`792ec9a`](https://github.com/mastra-ai/mastra/commit/792ec9a0869bab8274cf5e0ed2840738737a1607), [`712b864`](https://github.com/mastra-ai/mastra/commit/712b864aa1ed12b14c54390ec17b69de163c37f7), [`8f7a5de`](https://github.com/mastra-ai/mastra/commit/8f7a5dedc246cdc938bb65516703cf9b27b03756), [`c0bec73`](https://github.com/mastra-ai/mastra/commit/c0bec732c93d1a22ae5e51ed66cf8cacca8bd6a6)]:
18
+ - @mastra/core@1.52.0-alpha.2
19
+
3
20
  ## 1.14.0
4
21
 
5
22
  ### Minor Changes
@@ -8,6 +8,16 @@ import { PromptClientActions } from './actions/prompt.js';
8
8
  import { ResourceClientActions } from './actions/resource.js';
9
9
  import type { ElicitationHandler, ProgressHandler, InternalMastraMCPClientOptions, Root } from './types.js';
10
10
  export type { LoggingLevel, LogMessage, LogHandler, ElicitationHandler, ProgressHandler, MastraFetchLike, MastraMCPServerDefinition, InternalMastraMCPClientOptions, Root, RequireToolApproval, RequireToolApprovalFn, RequireToolApprovalContext, } from './types.js';
11
+ /**
12
+ * OAuth authorization state of an MCP server connection.
13
+ *
14
+ * - `needs-auth`: the server rejected the connection with a 401 and interactive
15
+ * authorization is required (see MCPClient.authenticate)
16
+ * - `authorized`: the server accepted the configured authProvider's credentials
17
+ *
18
+ * Servers without an authProvider never carry an auth state.
19
+ */
20
+ export type MCPServerAuthState = 'needs-auth' | 'authorized';
11
21
  /**
12
22
  * Internal MCP client implementation for connecting to a single MCP server.
13
23
  *
@@ -25,6 +35,10 @@ export declare class InternalMastraMCPClient extends MastraBase {
25
35
  private enableProgressTracking?;
26
36
  private serverConfig;
27
37
  private transport?;
38
+ private pendingAuthTransport?;
39
+ private clientBaseOnClose?;
40
+ private clientConnectionOnClose?;
41
+ private _authState?;
28
42
  private operationContextStore;
29
43
  private exitHookUnsubscribe?;
30
44
  private sigTermHandler?;
@@ -94,6 +108,56 @@ export declare class InternalMastraMCPClient extends MastraBase {
94
108
  sendRootsListChanged(): Promise<void>;
95
109
  private connectStdio;
96
110
  private connectHttp;
111
+ /**
112
+ * Detaches whatever transport is still attached to the underlying SDK Client.
113
+ *
114
+ * The SDK assigns its internal `_transport` before `transport.start()` and never
115
+ * clears it when `start()` throws, and its cleanup after a failed initialize is a
116
+ * fire-and-forget `void this.close()`. Either way a stale transport can remain
117
+ * attached, making every subsequent `client.connect()` throw "Already connected
118
+ * to a transport" and permanently wedging this client (issue #19862). Mastra's
119
+ * own `this.transport` is only assigned after a successful connect, so
120
+ * disconnect/forceReconnect never see the stale one. Calling this before every
121
+ * connect attempt restores the invariant that a connect starts from a detached
122
+ * SDK client.
123
+ */
124
+ private detachStaleClientTransport;
125
+ /**
126
+ * Severs the mutual references between the SDK client and a stale transport
127
+ * without closing it. Clearing the transport's callbacks ensures a later
128
+ * close() of the stale transport cannot reach into the SDK client and clear
129
+ * the state of a newer live connection.
130
+ */
131
+ private severClientTransportLink;
132
+ /**
133
+ * Closes and clears any transport retained from an unfinished authorization
134
+ * flow. Safe to call when nothing is pending. Centralizes the cleanup so
135
+ * success, disconnect, and forceReconnect all release the same resource.
136
+ */
137
+ private closePendingAuthTransport;
138
+ /**
139
+ * Records that the server rejected the connection with a 401 and keeps the
140
+ * transport that started the authorization flow so finishAuth can complete it.
141
+ */
142
+ private markNeedsAuth;
143
+ /**
144
+ * OAuth authorization state of this server connection, when it has an authProvider.
145
+ *
146
+ * @internal
147
+ */
148
+ get authState(): MCPServerAuthState | undefined;
149
+ /**
150
+ * Completes a pending OAuth authorization-code flow.
151
+ *
152
+ * Exchanges the authorization code captured at the redirect URI on the same
153
+ * transport that started the flow, then leaves the client ready to connect().
154
+ *
155
+ * @param authorizationCode - The authorization code captured at the redirect URI
156
+ * @throws {Error} If no authorization flow is pending for this server
157
+ *
158
+ * @internal
159
+ */
160
+ finishAuth(authorizationCode: string): Promise<void>;
97
161
  private isConnected;
98
162
  /**
99
163
  * Connects to the MCP server using the configured transport.
@@ -1 +1 @@
1
- {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../src/client/client.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAI/C,OAAO,KAAK,EAAmB,IAAI,EAAE,MAAM,oBAAoB,CAAC;AAShE,OAAO,KAAK,EACV,WAAW,EACX,eAAe,EACf,iBAAiB,EACjB,mBAAmB,EACnB,2BAA2B,EAE3B,kBAAkB,EA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AI,GAAG,IAAI;IAOhE,qCAAqC,CAAC,OAAO,EAAE,CAAC,MAAM,EAAE,GAAG,KAAK,IAAI,GAAG,IAAI;IAO3E,yCAAyC,CAAC,OAAO,EAAE,MAAM,IAAI,GAAG,IAAI;IAOpE,4BAA4B,CAAC,OAAO,EAAE,kBAAkB,GAAG,IAAI;IAoB/D,8BAA8B,CAAC,OAAO,EAAE,eAAe,GAAG,IAAI;YAOhD,kBAAkB;IAMhC;;;OAGG;IACH,OAAO,CAAC,mBAAmB;IAcrB,KAAK,IAAI,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,CAAC,CAAC;IA+LhE,OAAO,CAAC,mBAAmB;CAQ5B"}
@@ -3,7 +3,7 @@ import { MastraBase } from '@mastra/core/base';
3
3
  import type { MCPServerBase } from '@mastra/core/mcp';
4
4
  import type { Tool } from '@mastra/core/tools';
5
5
  import type { ElicitRequest, ElicitResult, ProgressNotification, Prompt, Resource, ResourceTemplate } from '@modelcontextprotocol/sdk/types.js';
6
- import type { MastraMCPServerDefinition } from './client.js';
6
+ import type { MastraMCPServerDefinition, MCPServerAuthState } from './client.js';
7
7
  /**
8
8
  * Configuration options for creating an MCPClient instance.
9
9
  */
@@ -55,6 +55,15 @@ export declare class MCPClient extends MastraBase {
55
55
  private defaultTimeout;
56
56
  private mcpClientsById;
57
57
  private disconnectPromise;
58
+ private authFlowsByServer;
59
+ private authCallbackServersByServer;
60
+ /**
61
+ * Per-server abort controllers for in-flight authorization flows. Created
62
+ * synchronously at the start of {@link runAuthorizationFlow} — before the
63
+ * callback server exists — so cancel/disconnect can interrupt the setup phase
64
+ * (discovery, registration, port binding) and not just the waitForCode wait.
65
+ */
66
+ private authAbortControllersByServer;
58
67
  /**
59
68
  * Creates a new MCPClient instance for managing MCP server connections.
60
69
  *
@@ -565,6 +574,67 @@ export declare class MCPClient extends MastraBase {
565
574
  * ```
566
575
  */
567
576
  reconnectServer(serverName: string): Promise<void>;
577
+ /**
578
+ * Runs the interactive OAuth authorization-code flow for a server.
579
+ *
580
+ * Requires the server to be configured with an MCPOAuthClientProvider whose
581
+ * redirect URL points at a loopback address. The flow:
582
+ *
583
+ * 1. Starts a loopback callback server on the redirect URL's port (falling
584
+ * back to the next sequential ports when it is in use)
585
+ * 2. Attempts a connection so the SDK runs discovery and dynamic client
586
+ * registration, delivering the authorization URL through the provider's
587
+ * `onRedirectToAuthorization` callback — the host directs the user there
588
+ * 3. Waits for the browser to deliver the authorization code, validates the
589
+ * OAuth state, exchanges the code for tokens, and reconnects
590
+ *
591
+ * Concurrent calls for the same server join the pending flow (the joiner's
592
+ * options are ignored — the pending flow keeps its own timeout); different
593
+ * servers authenticate independently. Each server needs its own provider
594
+ * instance: the flow pins session state on the provider, so sharing one
595
+ * MCPOAuthClientProvider across servers is not supported. Hosts with custom
596
+ * redirect handling (e.g. a web app with an HTTPS redirect URL) should
597
+ * drive MCPOAuthClientProvider directly instead.
598
+ *
599
+ * @param serverName - The name of the server to authenticate (must match a key in `servers`)
600
+ * @param options.timeoutMs - How long to wait for the browser callback (default 5 minutes)
601
+ * @throws {Error} If the server has no MCPOAuthClientProvider or its redirect URL is not loopback
602
+ *
603
+ * @example
604
+ * ```typescript
605
+ * if (mcp.getServerAuthState('weatherServer') === 'needs-auth') {
606
+ * await mcp.authenticate('weatherServer');
607
+ * }
608
+ * ```
609
+ */
610
+ authenticate(serverName: string, options?: {
611
+ timeoutMs?: number;
612
+ }): Promise<void>;
613
+ /**
614
+ * OAuth authorization state of a configured server.
615
+ *
616
+ * Returns `undefined` for servers without an authProvider and for servers
617
+ * that have not attempted a connection yet.
618
+ */
619
+ getServerAuthState(serverName: string): MCPServerAuthState | undefined;
620
+ private runAuthorizationFlow;
621
+ /**
622
+ * Cancels a pending {@link authenticate} flow for a server.
623
+ *
624
+ * Tears down the loopback callback server immediately — the pending
625
+ * authenticate() call rejects. Useful when the user closed the browser
626
+ * without completing consent, which the host cannot observe.
627
+ *
628
+ * The resulting auth state depends on how far the flow had progressed: a flow
629
+ * cancelled after the server rejected the connection with a 401 stays in the
630
+ * `needs-auth` state so it can be retried right away, while a flow cancelled
631
+ * during the setup phase (before any connection was attempted) leaves the
632
+ * state unchanged — typically `undefined`.
633
+ *
634
+ * @param serverName - The name of the server whose flow to cancel
635
+ * @returns `true` if a pending flow was cancelled, `false` when no flow was pending
636
+ */
637
+ cancelAuthentication(serverName: string): Promise<boolean>;
568
638
  /**
569
639
  * Returns instructions advertised by connected MCP servers during initialize.
570
640
  *
@@ -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,gBAAgB,EACjB,MAAM,oCAAoC,CAAC;AAG5C,OAAO,KAAK,EAAE,yBAAyB,EAAE,MAAM,UAAU,CAAC;AAO1D;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAC/B,wHAAwH;IACxH,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ,mFAAmF;IACnF,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,yBAAyB,CAAC,CAAC;IACnD,iFAAiF;IACjF,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AACH,qBAAa,SAAU,SAAQ,UAAU;IACvC,OAAO,CAAC,aAAa,CAAiD;IACtE,OAAO,CAAC,EAAE,CAAS;IACnB,OAAO,CAAC,cAA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EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,CAAC,CAAC,CAAC;QACnE,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;KAChC,CAAC;IAgCF;;;;;;;;;;;;;;;;;;;;;;OAsBG;IACI,kBAAkB,IAAI,MAAM,CAAC,MAAM,EAAE,aAAa,CAAC;IAU1D;;;;;;;;;;;;;;OAcG;IACH,IAAI,UAAU,IAAI,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAQvC;IAED;;;;;;;;OAQG;IACI,eAAe,CAAC,UAAU,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI;IAMzD,OAAO,CAAC,eAAe;YAQT,iBAAiB;YAiCjB,kBAAkB;IAchC,OAAO,CAAC,kBAAkB;YAyBZ,2BAA2B;YAI3B,kBAAkB;YAIlB,iBAAiB;CAuBhC"}
@@ -2,5 +2,6 @@ export type { LoggingLevel, LogMessage, LogHandler, MastraMCPServerDefinition, E
2
2
  export * from './client.js';
3
3
  export * from './configuration.js';
4
4
  export * from './oauth-provider.js';
5
+ export * from './oauth-callback-server.js';
5
6
  export { MCPClientServerProxy } from './server-proxy.js';
6
7
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/client/index.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,YAAY,EACZ,UAAU,EACV,UAAU,EACV,yBAAyB,EACzB,kBAAkB,EAClB,eAAe,EACf,8BAA8B,EAC9B,mBAAmB,EACnB,qBAAqB,EACrB,0BAA0B,EAC1B,eAAe,GAChB,MAAM,SAAS,CAAC;AACjB,cAAc,UAAU,CAAC;AACzB,cAAc,iBAAiB,CAAC;AAChC,cAAc,kBAAkB,CAAC;AACjC,OAAO,EAAE,oBAAoB,EAAE,MAAM,gBAAgB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/client/index.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,YAAY,EACZ,UAAU,EACV,UAAU,EACV,yBAAyB,EACzB,kBAAkB,EAClB,eAAe,EACf,8BAA8B,EAC9B,mBAAmB,EACnB,qBAAqB,EACrB,0BAA0B,EAC1B,eAAe,GAChB,MAAM,SAAS,CAAC;AACjB,cAAc,UAAU,CAAC;AACzB,cAAc,iBAAiB,CAAC;AAChC,cAAc,kBAAkB,CAAC;AACjC,cAAc,yBAAyB,CAAC;AACxC,OAAO,EAAE,oBAAoB,EAAE,MAAM,gBAAgB,CAAC"}
@@ -0,0 +1,117 @@
1
+ /**
2
+ * Loopback OAuth Callback Server for MCP Client
3
+ *
4
+ * Provides a one-shot local HTTP server that captures the OAuth authorization
5
+ * code delivered to a loopback redirect URL (RFC 8252). Hosts use it together
6
+ * with MCPOAuthClientProvider to complete the authorization-code flow for
7
+ * OAuth-protected MCP servers.
8
+ *
9
+ * @see https://modelcontextprotocol.io/specification/2025-06-18/basic/authorization
10
+ */
11
+ /**
12
+ * Options for creating an OAuth callback server.
13
+ */
14
+ export interface OAuthCallbackServerOptions {
15
+ /**
16
+ * The redirect URL the authorization server will send the browser back to.
17
+ * Its port is the preferred port to bind; if that port is in use, the next
18
+ * sequential ports are tried (see getCallbackUrlCandidates). The server
19
+ * binds the URL's hostname — prefer the literal `127.0.0.1` over
20
+ * `localhost` (RFC 8252 §8.3) so the browser and server agree on the
21
+ * address family.
22
+ *
23
+ * @example 'http://127.0.0.1:5533/oauth/callback'
24
+ */
25
+ redirectUrl: string | URL;
26
+ /**
27
+ * The OAuth state parameter issued for this authorization request.
28
+ * The state authenticates the redirect (CSRF protection per OAuth 2.1):
29
+ * callback requests without a matching state receive an error response and
30
+ * never settle the flow, so a stray local request cannot abort or spoof a
31
+ * pending authorization.
32
+ */
33
+ state: string;
34
+ }
35
+ /**
36
+ * The authorization code captured from the OAuth callback.
37
+ */
38
+ export interface OAuthCallbackResult {
39
+ /**
40
+ * The authorization code to exchange for tokens.
41
+ */
42
+ code: string;
43
+ /**
44
+ * The state parameter echoed back by the authorization server.
45
+ */
46
+ state: string;
47
+ }
48
+ /**
49
+ * A running loopback OAuth callback server.
50
+ */
51
+ export interface OAuthCallbackServer {
52
+ /**
53
+ * The callback URL that is actually bound (reflects any port fallback).
54
+ * Use this — not the preferred redirect URL — as the redirect_uri for the
55
+ * authorization request.
56
+ */
57
+ url: URL;
58
+ /**
59
+ * The port the server is listening on.
60
+ */
61
+ port: number;
62
+ /**
63
+ * Waits for the browser to deliver the authorization code.
64
+ *
65
+ * Resolves once with the code and state from the first state-matching
66
+ * callback request. Rejects on timeout, on a state-matching OAuth error
67
+ * response (`error` / `error_description` query params), or when the
68
+ * server is closed before a code arrives.
69
+ */
70
+ waitForCode(options?: {
71
+ timeoutMs?: number;
72
+ }): Promise<OAuthCallbackResult>;
73
+ /**
74
+ * Stops the server and releases the port. Rejects any pending waitForCode.
75
+ * Idempotent: closing an already-closed server resolves immediately.
76
+ */
77
+ close(): Promise<void>;
78
+ }
79
+ /**
80
+ * Returns the candidate callback URLs for a redirect URL: the URL itself on
81
+ * its preferred port, followed by the sequential fallback-port variants that
82
+ * createOAuthCallbackServer will try when the preferred port is in use.
83
+ *
84
+ * This is the single source of the candidate list: register all of these as
85
+ * redirect_uris during dynamic client registration so a fallback-bound
86
+ * callback URL always matches a registered URI.
87
+ */
88
+ export declare function getCallbackUrlCandidates(redirectUrl: string | URL): URL[];
89
+ /**
90
+ * Starts a one-shot loopback HTTP server that captures the OAuth
91
+ * authorization code.
92
+ *
93
+ * The server binds the redirect URL's hostname on its port, falling back to
94
+ * the next sequential ports when it is in use (see getCallbackUrlCandidates).
95
+ * The first state-matching request on the callback path settles the outcome;
96
+ * subsequent requests receive 410 Gone. Requests whose state does not match
97
+ * receive an error response without settling, so only the genuine redirect
98
+ * can complete or abort the flow. The response pages never echo the
99
+ * authorization code.
100
+ *
101
+ * @example
102
+ * ```typescript
103
+ * const callbackServer = await createOAuthCallbackServer({
104
+ * redirectUrl: 'http://127.0.0.1:5533/oauth/callback',
105
+ * state: expectedState,
106
+ * });
107
+ * try {
108
+ * // Direct the user to the authorization URL, then:
109
+ * const { code } = await callbackServer.waitForCode();
110
+ * await transport.finishAuth(code);
111
+ * } finally {
112
+ * await callbackServer.close();
113
+ * }
114
+ * ```
115
+ */
116
+ export declare function createOAuthCallbackServer(options: OAuthCallbackServerOptions): Promise<OAuthCallbackServer>;
117
+ //# sourceMappingURL=oauth-callback-server.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"oauth-callback-server.d.ts","sourceRoot":"","sources":["../../src/client/oauth-callback-server.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAkCH;;GAEG;AACH,MAAM,WAAW,0BAA0B;IACzC;;;;;;;;;OASG;IACH,WAAW,EAAE,MAAM,GAAG,GAAG,CAAC;IAE1B;;;;;;OAMG;IACH,KAAK,EAAE,MAAM,CAAC;CACf;AAED;;GAEG;AACH,MAAM,WAAW,mBAAmB;IAClC;;OAEG;IACH,IAAI,EAAE,MAAM,CAAC;IAEb;;OAEG;IACH,KAAK,EAAE,MAAM,CAAC;CACf;AAED;;GAEG;AACH,MAAM,WAAW,mBAAmB;IAClC;;;;OAIG;IACH,GAAG,EAAE,GAAG,CAAC;IAET;;OAEG;IACH,IAAI,EAAE,MAAM,CAAC;IAEb;;;;;;;OAOG;IACH,WAAW,CAAC,OAAO,CAAC,EAAE;QAAE,SAAS,CAAC,EAAE,MAAM,CAAA;KAAE,GAAG,OAAO,CAAC,mBAAmB,CAAC,CAAC;IAE5E;;;OAGG;IACH,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CACxB;AAED;;;;;;;;GAQG;AACH,wBAAgB,wBAAwB,CAAC,WAAW,EAAE,MAAM,GAAG,GAAG,GAAG,GAAG,EAAE,CAezE;AAmCD;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,wBAAsB,yBAAyB,CAAC,OAAO,EAAE,0BAA0B,GAAG,OAAO,CAAC,mBAAmB,CAAC,CA8IjH"}
@@ -125,12 +125,14 @@ export interface MCPOAuthClientProviderOptions {
125
125
  * ```
126
126
  */
127
127
  export declare class MCPOAuthClientProvider implements OAuthClientProvider {
128
- private readonly _redirectUrl;
129
- private readonly _clientMetadata;
128
+ private _redirectUrl;
129
+ private _clientMetadata;
130
130
  private readonly storage;
131
131
  private readonly onRedirect?;
132
132
  private readonly generateState;
133
133
  private _clientInfo?;
134
+ private _sessionState?;
135
+ private _sessionRedirectUrl?;
134
136
  constructor(options: MCPOAuthClientProviderOptions);
135
137
  /**
136
138
  * The URL to redirect the user agent to after authorization.
@@ -142,8 +144,42 @@ export declare class MCPOAuthClientProvider implements OAuthClientProvider {
142
144
  get clientMetadata(): OAuthClientMetadata;
143
145
  /**
144
146
  * Returns a OAuth2 state parameter.
147
+ *
148
+ * While an authorization session is active (see beginAuthorizationSession),
149
+ * the pinned session state is returned so a callback server can validate
150
+ * the redirect against a known value.
145
151
  */
146
152
  state(): Promise<string>;
153
+ /**
154
+ * Pins the OAuth state parameter for the next authorization request.
155
+ *
156
+ * Hosts driving an interactive authorization flow (e.g. MCPClient.authenticate)
157
+ * call this before triggering the flow so the loopback callback server knows
158
+ * which state value to expect. Call endAuthorizationSession once the flow settles.
159
+ *
160
+ * @returns The pinned state value, generated with the configured stateGenerator
161
+ */
162
+ beginAuthorizationSession(): Promise<string>;
163
+ /**
164
+ * Clears the pinned authorization state (see beginAuthorizationSession) and
165
+ * restores the configured redirect URL if applyResolvedRedirectUrl rebased
166
+ * it to a fallback port during the session, so the next flow starts from
167
+ * the preferred port again.
168
+ */
169
+ endAuthorizationSession(): void;
170
+ /**
171
+ * Points the provider at the callback URL that is actually bound.
172
+ *
173
+ * Loopback callback servers may bind a fallback port when the preferred one
174
+ * is in use. Call this before triggering authorization so the authorization
175
+ * request's redirect_uri matches the listening server, and so dynamic client
176
+ * registration registers every candidate callback URL (see
177
+ * getCallbackUrlCandidates) rather than only the preferred one.
178
+ *
179
+ * @param redirectUrl - The callback URL that is actually bound
180
+ * @param registeredRedirectUris - The redirect URIs to register during dynamic client registration
181
+ */
182
+ applyResolvedRedirectUrl(redirectUrl: string | URL, registeredRedirectUris: (string | URL)[]): void;
147
183
  /**
148
184
  * Loads information about this OAuth client.
149
185
  */
@@ -1 +1 @@
1
- {"version":3,"file":"oauth-provider.d.ts","sourceRoot":"","sources":["../../src/client/oauth-provider.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EACV,mBAAmB,EACnB,mBAAmB,EACnB,sBAAsB,EACtB,0BAA0B,EAC1B,WAAW,EACZ,MAAM,0BAA0B,CAAC;AAElC;;;;;GAKG;AACH,MAAM,WAAW,YAAY;IAC3B;;OAEG;IACH,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC;IAEtD;;OAEG;IACH,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,GAAG,MAAM,GAAG,SAAS,CAAC;IAEnE;;OAEG;IACH,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC;CAC3C;AAED;;;;;GAKG;AACH,qBAAa,oBAAqB,YAAW,YAAY;IACvD,OAAO,CAAC,IAAI,CAA6B;IAEzC,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,IAAI;IAIrC,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS;IAIpC,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI;IAIzB,KAAK,IAAI,IAAI;CAGd;AAED;;GAEG;AACH,MAAM,WAAW,6BAA6B;IAC5C;;;;;;OAMG;IACH,WAAW,EAAE,MAAM,GAAG,GAAG,CAA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AO,CAAC,IAAI,CAAC;IAqB3F;;;OAGG;IACG,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAI5B;;OAEG;IACG,cAAc,IAAI,OAAO,CAAC,OAAO,CAAC;CAYzC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,wBAAgB,yBAAyB,CACvC,WAAW,EAAE,MAAM,EACnB,OAAO,EAAE;IACP,WAAW,EAAE,MAAM,GAAG,GAAG,CAAC;IAC1B,cAAc,EAAE,mBAAmB,CAAC;IACpC,iBAAiB,CAAC,EAAE,sBAAsB,CAAC;IAC3C,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB,GACA,mBAAmB,CAsBrB"}
@@ -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.14.0"
6
+ version: "1.15.0-alpha.1"
7
7
  ---
8
8
 
9
9
  ## When to use
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.14.0",
2
+ "version": "1.15.0-alpha.1",
3
3
  "package": "@mastra/mcp",
4
4
  "exports": {
5
5
  "UnauthorizedError": {
@@ -64,7 +64,7 @@ export const testMcpClient = new MCPClient({
64
64
 
65
65
  Visit [MCPClient](https://mastra.ai/reference/tools/mcp-client) for a full list of configuration options.
66
66
 
67
- > **Authentication:** For connecting to OAuth-protected MCP servers, see the [OAuth Authentication](https://mastra.ai/reference/tools/mcp-client) section.
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
68
 
69
69
  ## Using `MCPClient` with an agent
70
70
 
@@ -254,6 +254,46 @@ class MyFGAProvider implements IFGAProvider {
254
254
  }
255
255
  ```
256
256
 
257
+ ## System actors
258
+
259
+ Autonomous and scheduled agents run without an end user. Mark these calls with an actor signal so FGA can tell them apart from user requests:
260
+
261
+ - `true` or `{ actorKind: 'system' }` identifies an anonymous system actor.
262
+ - The object form can also carry `agentId`, `permissions`, and `scope` to identify and constrain the acting agent.
263
+
264
+ By default, a trusted actor skips the user-centric `require()` check after a tenant-scope check. To enforce per-agent least privilege, implement the optional `requireActor` method on your provider. It receives the actor and the same `FGACheckParams` as `require`, and throws `FGADeniedError` to deny. When your provider doesn't implement `requireActor`, the trusted-actor bypass is preserved, so adding it is backward compatible.
265
+
266
+ ```typescript
267
+ import { FGADeniedError } from '@mastra/core/auth/ee'
268
+ import type { ActorSignal, FGACheckParams, IFGAProvider } from '@mastra/core/auth/ee'
269
+
270
+ class MyFGAProvider implements IFGAProvider {
271
+ // ...check, require, filterAccessible...
272
+
273
+ async requireActor(actor: ActorSignal, params: FGACheckParams): Promise<void> {
274
+ const agentId = actor === true ? undefined : actor.agentId
275
+ // Resolve the agent's real grants from a trusted source keyed by agentId.
276
+ const granted = await this.grantsForAgent(agentId)
277
+ const required = Array.isArray(params.permission) ? params.permission : [params.permission]
278
+ if (!required.some(permission => granted.includes(permission))) {
279
+ throw new FGADeniedError(null, params.resource, params.permission)
280
+ }
281
+ }
282
+ }
283
+ ```
284
+
285
+ ### Trust requirements
286
+
287
+ The actor signal is trusted input, so construct it server-side:
288
+
289
+ - Treat `actor` as a per-call signal. Durable workflows forward it when a run starts, but don't restore the initial actor on resume. Pass it explicitly on every trusted resume, or resolve a fresh actor through agent `defaultOptions`. Without a current actor, user authorization applies and a missing user fails closed.
290
+ - Mastra strips `actor` from execution options handled by its built-in agent HTTP routes. Set it in server-side code, for example a scheduled job or a workflow, never from client input.
291
+ - Establish tenant scope server-side. Built-in agent HTTP routes ignore a client-supplied `organizationId` in the request context, and the trusted-actor path requires an `organizationId` to be set.
292
+ - Durable resume keeps its existing request-context recovery and merge behavior. This doesn't make a persisted actor trusted for a later workflow segment.
293
+ - The tenant-scope check confirms that a trusted `organizationId` exists. It doesn't verify that `actor.agentId` belongs to that organization. When that relationship matters, verify it in `requireActor` using authoritative provider data.
294
+ - Treat `actor.permissions` as a claim, not a grant. A provider that enforces least privilege resolves the agent's authoritative permissions from a trusted source, for example a manifest or your FGA backend keyed by `agentId`, rather than trusting the inline values.
295
+ - Once a provider implements `requireActor`, errors from that method stop execution. Mastra doesn't fall back to organization-only authorization.
296
+
257
297
  ## Related
258
298
 
259
299
  - [Authentication overview](https://mastra.ai/docs/server/auth)