@mastra/mcp 1.9.0-alpha.0 → 1.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -2776,6 +2776,7 @@ var MCPServer = class extends MCPServerBase {
2776
2776
  definedPrompts;
2777
2777
  promptOptions;
2778
2778
  jsonSchemaValidator;
2779
+ mapAuthInfoToUser;
2779
2780
  subscriptions = /* @__PURE__ */ new Set();
2780
2781
  currentLoggingLevel;
2781
2782
  /**
@@ -2877,6 +2878,7 @@ var MCPServer = class extends MCPServerBase {
2877
2878
  * @param opts.prompts - Optional prompt configuration for exposing reusable templates
2878
2879
  * @param opts.id - Optional unique identifier (generated if not provided)
2879
2880
  * @param opts.description - Optional description of what the server does
2881
+ * @param opts.mapAuthInfoToUser - Optional mapper from MCP `extra.authInfo` to the FGA user context
2880
2882
  *
2881
2883
  * @example
2882
2884
  * ```typescript
@@ -2913,6 +2915,7 @@ var MCPServer = class extends MCPServerBase {
2913
2915
  this.resourceOptions = this.mergeAppResources(opts.resources, opts.appResources);
2914
2916
  this.promptOptions = opts.prompts;
2915
2917
  this.jsonSchemaValidator = opts.jsonSchemaValidator;
2918
+ this.mapAuthInfoToUser = opts.mapAuthInfoToUser;
2916
2919
  const capabilities = {
2917
2920
  tools: {},
2918
2921
  logging: { enabled: true }
@@ -3149,7 +3152,7 @@ var MCPServer = class extends MCPServerBase {
3149
3152
  */
3150
3153
  registerHandlersOnServer(serverInstance) {
3151
3154
  serverInstance.setRequestHandler(ListToolsRequestSchema, async (_request, extra) => {
3152
- const proxiedContext = this.createProxiedRequestContext(extra);
3155
+ const proxiedContext = await this.createProxiedRequestContext(extra);
3153
3156
  const tools = await this.getAuthorizedConvertedToolEntries(proxiedContext);
3154
3157
  return {
3155
3158
  tools: tools.map(([, tool]) => {
@@ -3229,12 +3232,7 @@ Provided arguments: ${JSON.stringify(request.params.arguments, null, 2)}`
3229
3232
  return this.handleElicitationRequest(request2, serverInstance, options);
3230
3233
  }
3231
3234
  };
3232
- const proxiedContext = new RequestContext();
3233
- if (extra) {
3234
- Object.entries(extra).forEach(([key, value]) => {
3235
- proxiedContext.set(key, value);
3236
- });
3237
- }
3235
+ const proxiedContext = await this.createProxiedRequestContext(extra);
3238
3236
  const mcpOptions = {
3239
3237
  messages: [],
3240
3238
  toolCallId: "",
@@ -4513,22 +4511,50 @@ Provided arguments: ${JSON.stringify(request.params.arguments, null, 2)}`
4513
4511
  _meta: withMastraToolStrictMeta(tool.mcp?._meta, tool.strict)
4514
4512
  };
4515
4513
  }
4516
- createProxiedRequestContext(extra) {
4514
+ async createProxiedRequestContext(extra) {
4517
4515
  const proxiedContext = new RequestContext();
4516
+ let extraRecord;
4518
4517
  if (extra && typeof extra === "object") {
4519
- Object.entries(extra).forEach(([key, value]) => {
4518
+ extraRecord = extra;
4519
+ Object.entries(extraRecord).forEach(([key, value]) => {
4520
4520
  proxiedContext.set(key, value);
4521
4521
  });
4522
4522
  }
4523
+ await this.resolveMappedFGAUser(proxiedContext, extraRecord);
4523
4524
  return proxiedContext;
4524
4525
  }
4526
+ async resolveMappedFGAUser(requestContext, extra) {
4527
+ if (!requestContext) {
4528
+ return void 0;
4529
+ }
4530
+ const existingUser = requestContext.get("user");
4531
+ if (existingUser) {
4532
+ return existingUser;
4533
+ }
4534
+ if (!this.mapAuthInfoToUser) {
4535
+ return void 0;
4536
+ }
4537
+ const authInfo = extra && "authInfo" in extra ? extra.authInfo : requestContext.get("authInfo");
4538
+ if (!authInfo) {
4539
+ return void 0;
4540
+ }
4541
+ const user = await this.mapAuthInfoToUser({
4542
+ authInfo,
4543
+ extra: extra ?? { authInfo },
4544
+ requestContext
4545
+ });
4546
+ if (user) {
4547
+ requestContext.set("user", user);
4548
+ }
4549
+ return user;
4550
+ }
4525
4551
  async getAuthorizedConvertedToolEntries(requestContext) {
4526
4552
  const entries = Object.entries(this.convertedTools);
4527
4553
  const fgaProvider = this.mastra?.getServer?.()?.fga;
4528
4554
  if (!fgaProvider) {
4529
4555
  return entries;
4530
4556
  }
4531
- const user = requestContext.get("user");
4557
+ const user = await this.resolveMappedFGAUser(requestContext);
4532
4558
  if (!user) {
4533
4559
  return [];
4534
4560
  }
@@ -4552,17 +4578,26 @@ Provided arguments: ${JSON.stringify(request.params.arguments, null, 2)}`
4552
4578
  if (!fgaProvider) {
4553
4579
  return;
4554
4580
  }
4555
- const { checkFGA, FGADeniedError, MastraFGAPermissions } = await import('@mastra/core/auth/ee');
4556
- const resourceId = JSON.stringify([this.id, toolId]);
4557
- const user = requestContext?.get("user");
4581
+ const { getMCPToolFGAResourceId, requireFGA, FGADeniedError, MastraFGAPermissions } = await import('@mastra/core/auth/ee');
4582
+ const resourceId = getMCPToolFGAResourceId(this.id, toolId);
4583
+ const user = await this.resolveMappedFGAUser(requestContext);
4558
4584
  if (!user) {
4559
4585
  throw new FGADeniedError({ id: "unknown" }, { type: "tool", id: resourceId }, MastraFGAPermissions.TOOLS_EXECUTE);
4560
4586
  }
4561
- await checkFGA({
4587
+ await requireFGA({
4562
4588
  fgaProvider,
4563
4589
  user,
4564
4590
  resource: { type: "tool", id: resourceId },
4565
- permission: MastraFGAPermissions.TOOLS_EXECUTE
4591
+ permission: MastraFGAPermissions.TOOLS_EXECUTE,
4592
+ requestContext,
4593
+ context: {
4594
+ resourceId
4595
+ },
4596
+ metadata: {
4597
+ mcpServerId: this.id,
4598
+ mcpServerName: this.name,
4599
+ toolId
4600
+ }
4566
4601
  });
4567
4602
  }
4568
4603
  /**