@mastra/mongodb 1.4.0 → 1.5.0-alpha.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.cjs CHANGED
@@ -14,7 +14,7 @@ var evals = require('@mastra/core/evals');
14
14
 
15
15
  // package.json
16
16
  var package_default = {
17
- version: "1.4.0"};
17
+ version: "1.5.0-alpha.0"};
18
18
  var MongoDBFilterTranslator = class extends filter.BaseFilterTranslator {
19
19
  getSupportedOperators() {
20
20
  return {
@@ -2230,6 +2230,559 @@ var MongoDBMCPClientsStorage = class _MongoDBMCPClientsStorage extends storage.M
2230
2230
  return result;
2231
2231
  }
2232
2232
  };
2233
+ var SNAPSHOT_FIELDS3 = [
2234
+ "name",
2235
+ "version",
2236
+ "description",
2237
+ "instructions",
2238
+ "repository",
2239
+ "releaseDate",
2240
+ "isLatest",
2241
+ "packageCanonical",
2242
+ "tools",
2243
+ "agents",
2244
+ "workflows"
2245
+ ];
2246
+ var MongoDBMCPServersStorage = class _MongoDBMCPServersStorage extends storage.MCPServersStorage {
2247
+ #connector;
2248
+ #skipDefaultIndexes;
2249
+ #indexes;
2250
+ static MANAGED_COLLECTIONS = [storage.TABLE_MCP_SERVERS, storage.TABLE_MCP_SERVER_VERSIONS];
2251
+ constructor(config) {
2252
+ super();
2253
+ this.#connector = resolveMongoDBConfig(config);
2254
+ this.#skipDefaultIndexes = config.skipDefaultIndexes;
2255
+ this.#indexes = config.indexes?.filter(
2256
+ (idx) => _MongoDBMCPServersStorage.MANAGED_COLLECTIONS.includes(idx.collection)
2257
+ );
2258
+ }
2259
+ async getCollection(name) {
2260
+ return this.#connector.getCollection(name);
2261
+ }
2262
+ getDefaultIndexDefinitions() {
2263
+ return [
2264
+ { collection: storage.TABLE_MCP_SERVERS, keys: { id: 1 }, options: { unique: true } },
2265
+ { collection: storage.TABLE_MCP_SERVERS, keys: { createdAt: -1 } },
2266
+ { collection: storage.TABLE_MCP_SERVERS, keys: { updatedAt: -1 } },
2267
+ { collection: storage.TABLE_MCP_SERVERS, keys: { authorId: 1 } },
2268
+ { collection: storage.TABLE_MCP_SERVER_VERSIONS, keys: { id: 1 }, options: { unique: true } },
2269
+ {
2270
+ collection: storage.TABLE_MCP_SERVER_VERSIONS,
2271
+ keys: { mcpServerId: 1, versionNumber: -1 },
2272
+ options: { unique: true }
2273
+ },
2274
+ { collection: storage.TABLE_MCP_SERVER_VERSIONS, keys: { mcpServerId: 1, createdAt: -1 } }
2275
+ ];
2276
+ }
2277
+ async createDefaultIndexes() {
2278
+ if (this.#skipDefaultIndexes) {
2279
+ return;
2280
+ }
2281
+ for (const indexDef of this.getDefaultIndexDefinitions()) {
2282
+ try {
2283
+ const collection = await this.getCollection(indexDef.collection);
2284
+ await collection.createIndex(indexDef.keys, indexDef.options);
2285
+ } catch (error) {
2286
+ this.logger?.warn?.(`Failed to create index on ${indexDef.collection}:`, error);
2287
+ }
2288
+ }
2289
+ }
2290
+ async createCustomIndexes() {
2291
+ if (!this.#indexes || this.#indexes.length === 0) {
2292
+ return;
2293
+ }
2294
+ for (const indexDef of this.#indexes) {
2295
+ try {
2296
+ const collection = await this.getCollection(indexDef.collection);
2297
+ await collection.createIndex(indexDef.keys, indexDef.options);
2298
+ } catch (error) {
2299
+ this.logger?.warn?.(`Failed to create custom index on ${indexDef.collection}:`, error);
2300
+ }
2301
+ }
2302
+ }
2303
+ async init() {
2304
+ await this.createDefaultIndexes();
2305
+ await this.createCustomIndexes();
2306
+ }
2307
+ async dangerouslyClearAll() {
2308
+ const versionsCollection = await this.getCollection(storage.TABLE_MCP_SERVER_VERSIONS);
2309
+ await versionsCollection.deleteMany({});
2310
+ const mcpServersCollection = await this.getCollection(storage.TABLE_MCP_SERVERS);
2311
+ await mcpServersCollection.deleteMany({});
2312
+ }
2313
+ // ==========================================================================
2314
+ // MCP Server CRUD
2315
+ // ==========================================================================
2316
+ async getById(id) {
2317
+ try {
2318
+ const collection = await this.getCollection(storage.TABLE_MCP_SERVERS);
2319
+ const result = await collection.findOne({ id });
2320
+ if (!result) {
2321
+ return null;
2322
+ }
2323
+ return this.transformMCPServer(result);
2324
+ } catch (error$1) {
2325
+ if (error$1 instanceof error.MastraError) throw error$1;
2326
+ throw new error.MastraError(
2327
+ {
2328
+ id: storage.createStorageErrorId("MONGODB", "GET_MCP_SERVER_BY_ID", "FAILED"),
2329
+ domain: error.ErrorDomain.STORAGE,
2330
+ category: error.ErrorCategory.THIRD_PARTY,
2331
+ details: { id }
2332
+ },
2333
+ error$1
2334
+ );
2335
+ }
2336
+ }
2337
+ async create(input) {
2338
+ const { mcpServer } = input;
2339
+ try {
2340
+ const collection = await this.getCollection(storage.TABLE_MCP_SERVERS);
2341
+ const existing = await collection.findOne({ id: mcpServer.id });
2342
+ if (existing) {
2343
+ throw new error.MastraError({
2344
+ id: storage.createStorageErrorId("MONGODB", "CREATE_MCP_SERVER", "ALREADY_EXISTS"),
2345
+ domain: error.ErrorDomain.STORAGE,
2346
+ category: error.ErrorCategory.USER,
2347
+ details: { id: mcpServer.id },
2348
+ text: `MCP server with id ${mcpServer.id} already exists`
2349
+ });
2350
+ }
2351
+ const now = /* @__PURE__ */ new Date();
2352
+ const newMCPServer = {
2353
+ id: mcpServer.id,
2354
+ status: "draft",
2355
+ activeVersionId: void 0,
2356
+ authorId: mcpServer.authorId,
2357
+ metadata: mcpServer.metadata,
2358
+ createdAt: now,
2359
+ updatedAt: now
2360
+ };
2361
+ await collection.insertOne(this.serializeMCPServer(newMCPServer));
2362
+ const snapshotConfig = {};
2363
+ for (const field of SNAPSHOT_FIELDS3) {
2364
+ if (mcpServer[field] !== void 0) {
2365
+ snapshotConfig[field] = mcpServer[field];
2366
+ }
2367
+ }
2368
+ const versionId = crypto.randomUUID();
2369
+ try {
2370
+ await this.createVersion({
2371
+ id: versionId,
2372
+ mcpServerId: mcpServer.id,
2373
+ versionNumber: 1,
2374
+ ...snapshotConfig,
2375
+ changedFields: Object.keys(snapshotConfig),
2376
+ changeMessage: "Initial version"
2377
+ });
2378
+ } catch (versionError) {
2379
+ await collection.deleteOne({ id: mcpServer.id });
2380
+ throw versionError;
2381
+ }
2382
+ return newMCPServer;
2383
+ } catch (error$1) {
2384
+ if (error$1 instanceof error.MastraError) {
2385
+ throw error$1;
2386
+ }
2387
+ throw new error.MastraError(
2388
+ {
2389
+ id: storage.createStorageErrorId("MONGODB", "CREATE_MCP_SERVER", "FAILED"),
2390
+ domain: error.ErrorDomain.STORAGE,
2391
+ category: error.ErrorCategory.THIRD_PARTY,
2392
+ details: { id: mcpServer.id }
2393
+ },
2394
+ error$1
2395
+ );
2396
+ }
2397
+ }
2398
+ async update(input) {
2399
+ const { id, ...updates } = input;
2400
+ try {
2401
+ const collection = await this.getCollection(storage.TABLE_MCP_SERVERS);
2402
+ const existingMCPServer = await collection.findOne({ id });
2403
+ if (!existingMCPServer) {
2404
+ throw new error.MastraError({
2405
+ id: storage.createStorageErrorId("MONGODB", "UPDATE_MCP_SERVER", "NOT_FOUND"),
2406
+ domain: error.ErrorDomain.STORAGE,
2407
+ category: error.ErrorCategory.USER,
2408
+ details: { id },
2409
+ text: `MCP server with id ${id} not found`
2410
+ });
2411
+ }
2412
+ const updateDoc = {
2413
+ updatedAt: /* @__PURE__ */ new Date()
2414
+ };
2415
+ const metadataFields = {
2416
+ authorId: updates.authorId,
2417
+ activeVersionId: updates.activeVersionId,
2418
+ metadata: updates.metadata,
2419
+ status: updates.status
2420
+ };
2421
+ if (metadataFields.authorId !== void 0) updateDoc.authorId = metadataFields.authorId;
2422
+ if (metadataFields.activeVersionId !== void 0) {
2423
+ updateDoc.activeVersionId = metadataFields.activeVersionId;
2424
+ }
2425
+ if (metadataFields.status !== void 0) {
2426
+ updateDoc.status = metadataFields.status;
2427
+ }
2428
+ if (metadataFields.metadata !== void 0) {
2429
+ const existingMetadata = existingMCPServer.metadata || {};
2430
+ updateDoc.metadata = { ...existingMetadata, ...metadataFields.metadata };
2431
+ }
2432
+ await collection.updateOne({ id }, { $set: updateDoc });
2433
+ const updatedMCPServer = await collection.findOne({ id });
2434
+ if (!updatedMCPServer) {
2435
+ throw new error.MastraError({
2436
+ id: storage.createStorageErrorId("MONGODB", "UPDATE_MCP_SERVER", "NOT_FOUND_AFTER_UPDATE"),
2437
+ domain: error.ErrorDomain.STORAGE,
2438
+ category: error.ErrorCategory.SYSTEM,
2439
+ text: `MCP server with id ${id} was deleted during update`,
2440
+ details: { id }
2441
+ });
2442
+ }
2443
+ return this.transformMCPServer(updatedMCPServer);
2444
+ } catch (error$1) {
2445
+ if (error$1 instanceof error.MastraError) {
2446
+ throw error$1;
2447
+ }
2448
+ throw new error.MastraError(
2449
+ {
2450
+ id: storage.createStorageErrorId("MONGODB", "UPDATE_MCP_SERVER", "FAILED"),
2451
+ domain: error.ErrorDomain.STORAGE,
2452
+ category: error.ErrorCategory.THIRD_PARTY,
2453
+ details: { id }
2454
+ },
2455
+ error$1
2456
+ );
2457
+ }
2458
+ }
2459
+ async delete(id) {
2460
+ try {
2461
+ await this.deleteVersionsByParentId(id);
2462
+ const collection = await this.getCollection(storage.TABLE_MCP_SERVERS);
2463
+ await collection.deleteOne({ id });
2464
+ } catch (error$1) {
2465
+ if (error$1 instanceof error.MastraError) throw error$1;
2466
+ throw new error.MastraError(
2467
+ {
2468
+ id: storage.createStorageErrorId("MONGODB", "DELETE_MCP_SERVER", "FAILED"),
2469
+ domain: error.ErrorDomain.STORAGE,
2470
+ category: error.ErrorCategory.THIRD_PARTY,
2471
+ details: { id }
2472
+ },
2473
+ error$1
2474
+ );
2475
+ }
2476
+ }
2477
+ async list(args) {
2478
+ try {
2479
+ const { page = 0, perPage: perPageInput, orderBy, authorId, metadata, status = "published" } = args || {};
2480
+ const { field, direction } = this.parseOrderBy(orderBy);
2481
+ if (page < 0) {
2482
+ throw new error.MastraError(
2483
+ {
2484
+ id: storage.createStorageErrorId("MONGODB", "LIST_MCP_SERVERS", "INVALID_PAGE"),
2485
+ domain: error.ErrorDomain.STORAGE,
2486
+ category: error.ErrorCategory.USER,
2487
+ details: { page }
2488
+ },
2489
+ new Error("page must be >= 0")
2490
+ );
2491
+ }
2492
+ const perPage = storage.normalizePerPage(perPageInput, 100);
2493
+ const { offset, perPage: perPageForResponse } = storage.calculatePagination(page, perPageInput, perPage);
2494
+ const collection = await this.getCollection(storage.TABLE_MCP_SERVERS);
2495
+ const filter = {};
2496
+ filter.status = status;
2497
+ if (authorId) {
2498
+ filter.authorId = authorId;
2499
+ }
2500
+ if (metadata) {
2501
+ for (const [key, value] of Object.entries(metadata)) {
2502
+ filter[`metadata.${key}`] = value;
2503
+ }
2504
+ }
2505
+ const total = await collection.countDocuments(filter);
2506
+ if (total === 0 || perPage === 0) {
2507
+ return {
2508
+ mcpServers: [],
2509
+ total,
2510
+ page,
2511
+ perPage: perPageForResponse,
2512
+ hasMore: false
2513
+ };
2514
+ }
2515
+ const sortOrder = direction === "ASC" ? 1 : -1;
2516
+ let cursor = collection.find(filter).sort({ [field]: sortOrder }).skip(offset);
2517
+ if (perPageInput !== false) {
2518
+ cursor = cursor.limit(perPage);
2519
+ }
2520
+ const results = await cursor.toArray();
2521
+ const mcpServers = results.map((doc) => this.transformMCPServer(doc));
2522
+ return {
2523
+ mcpServers,
2524
+ total,
2525
+ page,
2526
+ perPage: perPageForResponse,
2527
+ hasMore: perPageInput !== false && offset + perPage < total
2528
+ };
2529
+ } catch (error$1) {
2530
+ if (error$1 instanceof error.MastraError) {
2531
+ throw error$1;
2532
+ }
2533
+ throw new error.MastraError(
2534
+ {
2535
+ id: storage.createStorageErrorId("MONGODB", "LIST_MCP_SERVERS", "FAILED"),
2536
+ domain: error.ErrorDomain.STORAGE,
2537
+ category: error.ErrorCategory.THIRD_PARTY
2538
+ },
2539
+ error$1
2540
+ );
2541
+ }
2542
+ }
2543
+ // ==========================================================================
2544
+ // MCP Server Version Methods
2545
+ // ==========================================================================
2546
+ async createVersion(input) {
2547
+ try {
2548
+ const collection = await this.getCollection(storage.TABLE_MCP_SERVER_VERSIONS);
2549
+ const now = /* @__PURE__ */ new Date();
2550
+ const versionDoc = {
2551
+ id: input.id,
2552
+ mcpServerId: input.mcpServerId,
2553
+ versionNumber: input.versionNumber,
2554
+ changedFields: input.changedFields ?? void 0,
2555
+ changeMessage: input.changeMessage ?? void 0,
2556
+ createdAt: now
2557
+ };
2558
+ for (const field of SNAPSHOT_FIELDS3) {
2559
+ if (input[field] !== void 0) {
2560
+ versionDoc[field] = input[field];
2561
+ }
2562
+ }
2563
+ await collection.insertOne(versionDoc);
2564
+ return {
2565
+ ...input,
2566
+ createdAt: now
2567
+ };
2568
+ } catch (error$1) {
2569
+ throw new error.MastraError(
2570
+ {
2571
+ id: storage.createStorageErrorId("MONGODB", "CREATE_MCP_SERVER_VERSION", "FAILED"),
2572
+ domain: error.ErrorDomain.STORAGE,
2573
+ category: error.ErrorCategory.THIRD_PARTY,
2574
+ details: { versionId: input.id, mcpServerId: input.mcpServerId }
2575
+ },
2576
+ error$1
2577
+ );
2578
+ }
2579
+ }
2580
+ async getVersion(id) {
2581
+ try {
2582
+ const collection = await this.getCollection(storage.TABLE_MCP_SERVER_VERSIONS);
2583
+ const result = await collection.findOne({ id });
2584
+ if (!result) {
2585
+ return null;
2586
+ }
2587
+ return this.transformVersion(result);
2588
+ } catch (error$1) {
2589
+ throw new error.MastraError(
2590
+ {
2591
+ id: storage.createStorageErrorId("MONGODB", "GET_MCP_SERVER_VERSION", "FAILED"),
2592
+ domain: error.ErrorDomain.STORAGE,
2593
+ category: error.ErrorCategory.THIRD_PARTY,
2594
+ details: { versionId: id }
2595
+ },
2596
+ error$1
2597
+ );
2598
+ }
2599
+ }
2600
+ async getVersionByNumber(mcpServerId, versionNumber) {
2601
+ try {
2602
+ const collection = await this.getCollection(storage.TABLE_MCP_SERVER_VERSIONS);
2603
+ const result = await collection.findOne({ mcpServerId, versionNumber });
2604
+ if (!result) {
2605
+ return null;
2606
+ }
2607
+ return this.transformVersion(result);
2608
+ } catch (error$1) {
2609
+ throw new error.MastraError(
2610
+ {
2611
+ id: storage.createStorageErrorId("MONGODB", "GET_MCP_SERVER_VERSION_BY_NUMBER", "FAILED"),
2612
+ domain: error.ErrorDomain.STORAGE,
2613
+ category: error.ErrorCategory.THIRD_PARTY,
2614
+ details: { mcpServerId, versionNumber }
2615
+ },
2616
+ error$1
2617
+ );
2618
+ }
2619
+ }
2620
+ async getLatestVersion(mcpServerId) {
2621
+ try {
2622
+ const collection = await this.getCollection(storage.TABLE_MCP_SERVER_VERSIONS);
2623
+ const result = await collection.find({ mcpServerId }).sort({ versionNumber: -1 }).limit(1).toArray();
2624
+ if (!result || result.length === 0) {
2625
+ return null;
2626
+ }
2627
+ return this.transformVersion(result[0]);
2628
+ } catch (error$1) {
2629
+ throw new error.MastraError(
2630
+ {
2631
+ id: storage.createStorageErrorId("MONGODB", "GET_LATEST_MCP_SERVER_VERSION", "FAILED"),
2632
+ domain: error.ErrorDomain.STORAGE,
2633
+ category: error.ErrorCategory.THIRD_PARTY,
2634
+ details: { mcpServerId }
2635
+ },
2636
+ error$1
2637
+ );
2638
+ }
2639
+ }
2640
+ async listVersions(input) {
2641
+ const { mcpServerId, page = 0, perPage: perPageInput, orderBy } = input;
2642
+ if (page < 0) {
2643
+ throw new error.MastraError(
2644
+ {
2645
+ id: storage.createStorageErrorId("MONGODB", "LIST_MCP_SERVER_VERSIONS", "INVALID_PAGE"),
2646
+ domain: error.ErrorDomain.STORAGE,
2647
+ category: error.ErrorCategory.USER,
2648
+ details: { page }
2649
+ },
2650
+ new Error("page must be >= 0")
2651
+ );
2652
+ }
2653
+ const perPage = storage.normalizePerPage(perPageInput, 20);
2654
+ const { offset, perPage: perPageForResponse } = storage.calculatePagination(page, perPageInput, perPage);
2655
+ try {
2656
+ const { field, direction } = this.parseVersionOrderBy(orderBy);
2657
+ const collection = await this.getCollection(storage.TABLE_MCP_SERVER_VERSIONS);
2658
+ const total = await collection.countDocuments({ mcpServerId });
2659
+ if (total === 0 || perPage === 0) {
2660
+ return {
2661
+ versions: [],
2662
+ total,
2663
+ page,
2664
+ perPage: perPageForResponse,
2665
+ hasMore: false
2666
+ };
2667
+ }
2668
+ const sortOrder = direction === "ASC" ? 1 : -1;
2669
+ let cursor = collection.find({ mcpServerId }).sort({ [field]: sortOrder }).skip(offset);
2670
+ if (perPageInput !== false) {
2671
+ cursor = cursor.limit(perPage);
2672
+ }
2673
+ const results = await cursor.toArray();
2674
+ const versions = results.map((doc) => this.transformVersion(doc));
2675
+ return {
2676
+ versions,
2677
+ total,
2678
+ page,
2679
+ perPage: perPageForResponse,
2680
+ hasMore: perPageInput !== false && offset + perPage < total
2681
+ };
2682
+ } catch (error$1) {
2683
+ throw new error.MastraError(
2684
+ {
2685
+ id: storage.createStorageErrorId("MONGODB", "LIST_MCP_SERVER_VERSIONS", "FAILED"),
2686
+ domain: error.ErrorDomain.STORAGE,
2687
+ category: error.ErrorCategory.THIRD_PARTY,
2688
+ details: { mcpServerId }
2689
+ },
2690
+ error$1
2691
+ );
2692
+ }
2693
+ }
2694
+ async deleteVersion(id) {
2695
+ try {
2696
+ const collection = await this.getCollection(storage.TABLE_MCP_SERVER_VERSIONS);
2697
+ await collection.deleteOne({ id });
2698
+ } catch (error$1) {
2699
+ throw new error.MastraError(
2700
+ {
2701
+ id: storage.createStorageErrorId("MONGODB", "DELETE_MCP_SERVER_VERSION", "FAILED"),
2702
+ domain: error.ErrorDomain.STORAGE,
2703
+ category: error.ErrorCategory.THIRD_PARTY,
2704
+ details: { versionId: id }
2705
+ },
2706
+ error$1
2707
+ );
2708
+ }
2709
+ }
2710
+ async deleteVersionsByParentId(mcpServerId) {
2711
+ try {
2712
+ const collection = await this.getCollection(storage.TABLE_MCP_SERVER_VERSIONS);
2713
+ await collection.deleteMany({ mcpServerId });
2714
+ } catch (error$1) {
2715
+ throw new error.MastraError(
2716
+ {
2717
+ id: storage.createStorageErrorId("MONGODB", "DELETE_VERSIONS_BY_MCP_SERVER_ID", "FAILED"),
2718
+ domain: error.ErrorDomain.STORAGE,
2719
+ category: error.ErrorCategory.THIRD_PARTY,
2720
+ details: { mcpServerId }
2721
+ },
2722
+ error$1
2723
+ );
2724
+ }
2725
+ }
2726
+ async countVersions(mcpServerId) {
2727
+ try {
2728
+ const collection = await this.getCollection(storage.TABLE_MCP_SERVER_VERSIONS);
2729
+ return await collection.countDocuments({ mcpServerId });
2730
+ } catch (error$1) {
2731
+ throw new error.MastraError(
2732
+ {
2733
+ id: storage.createStorageErrorId("MONGODB", "COUNT_MCP_SERVER_VERSIONS", "FAILED"),
2734
+ domain: error.ErrorDomain.STORAGE,
2735
+ category: error.ErrorCategory.THIRD_PARTY,
2736
+ details: { mcpServerId }
2737
+ },
2738
+ error$1
2739
+ );
2740
+ }
2741
+ }
2742
+ // ==========================================================================
2743
+ // Private Helper Methods
2744
+ // ==========================================================================
2745
+ transformMCPServer(doc) {
2746
+ const { _id, ...rest } = doc;
2747
+ return {
2748
+ id: rest.id,
2749
+ status: rest.status,
2750
+ activeVersionId: rest.activeVersionId,
2751
+ authorId: rest.authorId,
2752
+ metadata: rest.metadata,
2753
+ createdAt: rest.createdAt instanceof Date ? rest.createdAt : new Date(rest.createdAt),
2754
+ updatedAt: rest.updatedAt instanceof Date ? rest.updatedAt : new Date(rest.updatedAt)
2755
+ };
2756
+ }
2757
+ serializeMCPServer(mcpServer) {
2758
+ return {
2759
+ id: mcpServer.id,
2760
+ status: mcpServer.status,
2761
+ activeVersionId: mcpServer.activeVersionId,
2762
+ authorId: mcpServer.authorId,
2763
+ metadata: mcpServer.metadata,
2764
+ createdAt: mcpServer.createdAt,
2765
+ updatedAt: mcpServer.updatedAt
2766
+ };
2767
+ }
2768
+ transformVersion(doc) {
2769
+ const { _id, ...version } = doc;
2770
+ const result = {
2771
+ id: version.id,
2772
+ mcpServerId: version.mcpServerId,
2773
+ versionNumber: version.versionNumber,
2774
+ changedFields: version.changedFields,
2775
+ changeMessage: version.changeMessage,
2776
+ createdAt: version.createdAt instanceof Date ? version.createdAt : new Date(version.createdAt)
2777
+ };
2778
+ for (const field of SNAPSHOT_FIELDS3) {
2779
+ if (version[field] !== void 0) {
2780
+ result[field] = version[field];
2781
+ }
2782
+ }
2783
+ return result;
2784
+ }
2785
+ };
2233
2786
  function formatDateForMongoDB(date) {
2234
2787
  return typeof date === "string" ? new Date(date) : date;
2235
2788
  }
@@ -3604,32 +4157,40 @@ var MemoryStorageMongoDB = class _MemoryStorageMongoDB extends storage.MemorySto
3604
4157
  const retentionFloor = input.messageTokensThreshold * (1 - input.activationRatio);
3605
4158
  const targetMessageTokens = Math.max(0, input.currentPendingTokens - retentionFloor);
3606
4159
  let cumulativeMessageTokens = 0;
3607
- let bestBoundary = 0;
3608
- let bestBoundaryMessageTokens = 0;
4160
+ let bestOverBoundary = 0;
4161
+ let bestOverTokens = 0;
4162
+ let bestUnderBoundary = 0;
4163
+ let bestUnderTokens = 0;
3609
4164
  for (let i = 0; i < chunks.length; i++) {
3610
4165
  cumulativeMessageTokens += chunks[i].messageTokens ?? 0;
3611
4166
  const boundary = i + 1;
3612
- const isUnder = cumulativeMessageTokens <= targetMessageTokens;
3613
- const bestIsUnder = bestBoundaryMessageTokens <= targetMessageTokens;
3614
- if (bestBoundary === 0) {
3615
- bestBoundary = boundary;
3616
- bestBoundaryMessageTokens = cumulativeMessageTokens;
3617
- } else if (isUnder && !bestIsUnder) {
3618
- bestBoundary = boundary;
3619
- bestBoundaryMessageTokens = cumulativeMessageTokens;
3620
- } else if (isUnder && bestIsUnder) {
3621
- if (cumulativeMessageTokens > bestBoundaryMessageTokens) {
3622
- bestBoundary = boundary;
3623
- bestBoundaryMessageTokens = cumulativeMessageTokens;
4167
+ if (cumulativeMessageTokens >= targetMessageTokens) {
4168
+ if (bestOverBoundary === 0 || cumulativeMessageTokens < bestOverTokens) {
4169
+ bestOverBoundary = boundary;
4170
+ bestOverTokens = cumulativeMessageTokens;
3624
4171
  }
3625
- } else if (!isUnder && !bestIsUnder) {
3626
- if (cumulativeMessageTokens < bestBoundaryMessageTokens) {
3627
- bestBoundary = boundary;
3628
- bestBoundaryMessageTokens = cumulativeMessageTokens;
4172
+ } else {
4173
+ if (cumulativeMessageTokens > bestUnderTokens) {
4174
+ bestUnderBoundary = boundary;
4175
+ bestUnderTokens = cumulativeMessageTokens;
3629
4176
  }
3630
4177
  }
3631
4178
  }
3632
- const chunksToActivate = bestBoundary === 0 ? 1 : bestBoundary;
4179
+ const maxOvershoot = retentionFloor * 0.95;
4180
+ const overshoot = bestOverTokens - targetMessageTokens;
4181
+ const remainingAfterOver = input.currentPendingTokens - bestOverTokens;
4182
+ let chunksToActivate;
4183
+ if (input.forceMaxActivation && bestOverBoundary > 0) {
4184
+ chunksToActivate = bestOverBoundary;
4185
+ } else if (bestOverBoundary > 0 && overshoot <= maxOvershoot && (remainingAfterOver >= 1e3 || retentionFloor === 0)) {
4186
+ chunksToActivate = bestOverBoundary;
4187
+ } else if (bestUnderBoundary > 0) {
4188
+ chunksToActivate = bestUnderBoundary;
4189
+ } else if (bestOverBoundary > 0) {
4190
+ chunksToActivate = bestOverBoundary;
4191
+ } else {
4192
+ chunksToActivate = 1;
4193
+ }
3633
4194
  const activatedChunks = chunks.slice(0, chunksToActivate);
3634
4195
  const remainingChunks = chunks.slice(chunksToActivate);
3635
4196
  const activatedContent = activatedChunks.map((c) => c.observations).join("\n\n");
@@ -3661,6 +4222,7 @@ ${activatedContent}` : activatedContent;
3661
4222
  }
3662
4223
  }
3663
4224
  );
4225
+ const latestChunkHints = activatedChunks[activatedChunks.length - 1];
3664
4226
  return {
3665
4227
  chunksActivated: activatedChunks.length,
3666
4228
  messageTokensActivated: activatedMessageTokens,
@@ -3675,7 +4237,9 @@ ${activatedContent}` : activatedContent;
3675
4237
  observationTokens: c.tokenCount,
3676
4238
  messageCount: c.messageIds.length,
3677
4239
  observations: c.observations
3678
- }))
4240
+ })),
4241
+ suggestedContinuation: latestChunkHints?.suggestedContinuation ?? void 0,
4242
+ currentTask: latestChunkHints?.currentTask ?? void 0
3679
4243
  };
3680
4244
  } catch (error$1) {
3681
4245
  if (error$1 instanceof error.MastraError) {
@@ -4573,7 +5137,7 @@ Note: This migration may take some time for large collections.
4573
5137
  return span;
4574
5138
  }
4575
5139
  };
4576
- var SNAPSHOT_FIELDS3 = ["name", "description", "content", "rules"];
5140
+ var SNAPSHOT_FIELDS4 = ["name", "description", "content", "rules"];
4577
5141
  var MongoDBPromptBlocksStorage = class _MongoDBPromptBlocksStorage extends storage.PromptBlocksStorage {
4578
5142
  #connector;
4579
5143
  #skipDefaultIndexes;
@@ -4690,7 +5254,7 @@ var MongoDBPromptBlocksStorage = class _MongoDBPromptBlocksStorage extends stora
4690
5254
  };
4691
5255
  await collection.insertOne(this.serializeBlock(newBlock));
4692
5256
  const snapshotConfig = {};
4693
- for (const field of SNAPSHOT_FIELDS3) {
5257
+ for (const field of SNAPSHOT_FIELDS4) {
4694
5258
  if (promptBlock[field] !== void 0) {
4695
5259
  snapshotConfig[field] = promptBlock[field];
4696
5260
  }
@@ -4879,7 +5443,7 @@ var MongoDBPromptBlocksStorage = class _MongoDBPromptBlocksStorage extends stora
4879
5443
  changeMessage: input.changeMessage ?? void 0,
4880
5444
  createdAt: now
4881
5445
  };
4882
- for (const field of SNAPSHOT_FIELDS3) {
5446
+ for (const field of SNAPSHOT_FIELDS4) {
4883
5447
  if (input[field] !== void 0) {
4884
5448
  versionDoc[field] = input[field];
4885
5449
  }
@@ -5099,7 +5663,7 @@ var MongoDBPromptBlocksStorage = class _MongoDBPromptBlocksStorage extends stora
5099
5663
  changeMessage: version.changeMessage,
5100
5664
  createdAt: version.createdAt instanceof Date ? version.createdAt : new Date(version.createdAt)
5101
5665
  };
5102
- for (const field of SNAPSHOT_FIELDS3) {
5666
+ for (const field of SNAPSHOT_FIELDS4) {
5103
5667
  if (version[field] !== void 0) {
5104
5668
  result[field] = version[field];
5105
5669
  }
@@ -5107,7 +5671,7 @@ var MongoDBPromptBlocksStorage = class _MongoDBPromptBlocksStorage extends stora
5107
5671
  return result;
5108
5672
  }
5109
5673
  };
5110
- var SNAPSHOT_FIELDS4 = [
5674
+ var SNAPSHOT_FIELDS5 = [
5111
5675
  "name",
5112
5676
  "description",
5113
5677
  "type",
@@ -5233,7 +5797,7 @@ var MongoDBScorerDefinitionsStorage = class _MongoDBScorerDefinitionsStorage ext
5233
5797
  };
5234
5798
  await collection.insertOne(this.serializeScorerDefinition(newScorerDefinition));
5235
5799
  const snapshotConfig = {};
5236
- for (const field of SNAPSHOT_FIELDS4) {
5800
+ for (const field of SNAPSHOT_FIELDS5) {
5237
5801
  if (scorerDefinition[field] !== void 0) {
5238
5802
  snapshotConfig[field] = scorerDefinition[field];
5239
5803
  }
@@ -5424,7 +5988,7 @@ var MongoDBScorerDefinitionsStorage = class _MongoDBScorerDefinitionsStorage ext
5424
5988
  changeMessage: input.changeMessage ?? void 0,
5425
5989
  createdAt: now
5426
5990
  };
5427
- for (const field of SNAPSHOT_FIELDS4) {
5991
+ for (const field of SNAPSHOT_FIELDS5) {
5428
5992
  if (input[field] !== void 0) {
5429
5993
  versionDoc[field] = input[field];
5430
5994
  }
@@ -5644,7 +6208,7 @@ var MongoDBScorerDefinitionsStorage = class _MongoDBScorerDefinitionsStorage ext
5644
6208
  changeMessage: version.changeMessage,
5645
6209
  createdAt: version.createdAt instanceof Date ? version.createdAt : new Date(version.createdAt)
5646
6210
  };
5647
- for (const field of SNAPSHOT_FIELDS4) {
6211
+ for (const field of SNAPSHOT_FIELDS5) {
5648
6212
  if (version[field] !== void 0) {
5649
6213
  result[field] = version[field];
5650
6214
  }
@@ -6020,7 +6584,7 @@ var ScoresStorageMongoDB = class _ScoresStorageMongoDB extends storage.ScoresSto
6020
6584
  }
6021
6585
  }
6022
6586
  };
6023
- var SNAPSHOT_FIELDS5 = [
6587
+ var SNAPSHOT_FIELDS6 = [
6024
6588
  "name",
6025
6589
  "description",
6026
6590
  "instructions",
@@ -6149,7 +6713,7 @@ var MongoDBSkillsStorage = class _MongoDBSkillsStorage extends storage.SkillsSto
6149
6713
  };
6150
6714
  await collection.insertOne(this.serializeSkill(newSkill));
6151
6715
  const snapshotConfig = {};
6152
- for (const field of SNAPSHOT_FIELDS5) {
6716
+ for (const field of SNAPSHOT_FIELDS6) {
6153
6717
  if (skill[field] !== void 0) {
6154
6718
  snapshotConfig[field] = skill[field];
6155
6719
  }
@@ -6207,7 +6771,7 @@ var MongoDBSkillsStorage = class _MongoDBSkillsStorage extends storage.SkillsSto
6207
6771
  status: updates.status
6208
6772
  };
6209
6773
  const configFields = {};
6210
- for (const field of SNAPSHOT_FIELDS5) {
6774
+ for (const field of SNAPSHOT_FIELDS6) {
6211
6775
  if (updates[field] !== void 0) {
6212
6776
  configFields[field] = updates[field];
6213
6777
  }
@@ -6368,7 +6932,7 @@ var MongoDBSkillsStorage = class _MongoDBSkillsStorage extends storage.SkillsSto
6368
6932
  changeMessage: input.changeMessage ?? void 0,
6369
6933
  createdAt: now
6370
6934
  };
6371
- for (const field of SNAPSHOT_FIELDS5) {
6935
+ for (const field of SNAPSHOT_FIELDS6) {
6372
6936
  if (input[field] !== void 0) {
6373
6937
  versionDoc[field] = input[field];
6374
6938
  }
@@ -6586,7 +7150,7 @@ var MongoDBSkillsStorage = class _MongoDBSkillsStorage extends storage.SkillsSto
6586
7150
  changeMessage: version.changeMessage,
6587
7151
  createdAt: version.createdAt instanceof Date ? version.createdAt : new Date(version.createdAt)
6588
7152
  };
6589
- for (const field of SNAPSHOT_FIELDS5) {
7153
+ for (const field of SNAPSHOT_FIELDS6) {
6590
7154
  if (version[field] !== void 0) {
6591
7155
  result[field] = version[field];
6592
7156
  }
@@ -6595,7 +7159,7 @@ var MongoDBSkillsStorage = class _MongoDBSkillsStorage extends storage.SkillsSto
6595
7159
  }
6596
7160
  extractSnapshotFields(version) {
6597
7161
  const result = {};
6598
- for (const field of SNAPSHOT_FIELDS5) {
7162
+ for (const field of SNAPSHOT_FIELDS6) {
6599
7163
  if (version[field] !== void 0) {
6600
7164
  result[field] = version[field];
6601
7165
  }
@@ -6881,7 +7445,7 @@ var WorkflowsStorageMongoDB = class _WorkflowsStorageMongoDB extends storage.Wor
6881
7445
  };
6882
7446
  }
6883
7447
  };
6884
- var SNAPSHOT_FIELDS6 = [
7448
+ var SNAPSHOT_FIELDS7 = [
6885
7449
  "name",
6886
7450
  "description",
6887
7451
  "filesystem",
@@ -7020,7 +7584,7 @@ var MongoDBWorkspacesStorage = class _MongoDBWorkspacesStorage extends storage.W
7020
7584
  };
7021
7585
  await collection.insertOne(this.serializeWorkspace(newWorkspace));
7022
7586
  const snapshotConfig = {};
7023
- for (const field of SNAPSHOT_FIELDS6) {
7587
+ for (const field of SNAPSHOT_FIELDS7) {
7024
7588
  if (workspace[field] !== void 0) {
7025
7589
  snapshotConfig[field] = workspace[field];
7026
7590
  }
@@ -7079,7 +7643,7 @@ var MongoDBWorkspacesStorage = class _MongoDBWorkspacesStorage extends storage.W
7079
7643
  status: updates.status
7080
7644
  };
7081
7645
  const configFields = {};
7082
- for (const field of SNAPSHOT_FIELDS6) {
7646
+ for (const field of SNAPSHOT_FIELDS7) {
7083
7647
  if (updates[field] !== void 0) {
7084
7648
  configFields[field] = updates[field];
7085
7649
  }
@@ -7244,7 +7808,7 @@ var MongoDBWorkspacesStorage = class _MongoDBWorkspacesStorage extends storage.W
7244
7808
  changeMessage: input.changeMessage ?? void 0,
7245
7809
  createdAt: now
7246
7810
  };
7247
- for (const field of SNAPSHOT_FIELDS6) {
7811
+ for (const field of SNAPSHOT_FIELDS7) {
7248
7812
  if (input[field] !== void 0) {
7249
7813
  versionDoc[field] = input[field];
7250
7814
  }
@@ -7464,7 +8028,7 @@ var MongoDBWorkspacesStorage = class _MongoDBWorkspacesStorage extends storage.W
7464
8028
  changeMessage: version.changeMessage,
7465
8029
  createdAt: version.createdAt instanceof Date ? version.createdAt : new Date(version.createdAt)
7466
8030
  };
7467
- for (const field of SNAPSHOT_FIELDS6) {
8031
+ for (const field of SNAPSHOT_FIELDS7) {
7468
8032
  if (version[field] !== void 0) {
7469
8033
  result[field] = version[field];
7470
8034
  }
@@ -7473,7 +8037,7 @@ var MongoDBWorkspacesStorage = class _MongoDBWorkspacesStorage extends storage.W
7473
8037
  }
7474
8038
  extractSnapshotFields(version) {
7475
8039
  const result = {};
7476
- for (const field of SNAPSHOT_FIELDS6) {
8040
+ for (const field of SNAPSHOT_FIELDS7) {
7477
8041
  if (version[field] !== void 0) {
7478
8042
  result[field] = version[field];
7479
8043
  }
@@ -7502,6 +8066,7 @@ var MongoDBStore = class extends storage.MastraCompositeStore {
7502
8066
  const promptBlocks = new MongoDBPromptBlocksStorage(domainConfig);
7503
8067
  const scorerDefinitions = new MongoDBScorerDefinitionsStorage(domainConfig);
7504
8068
  const mcpClients = new MongoDBMCPClientsStorage(domainConfig);
8069
+ const mcpServers = new MongoDBMCPServersStorage(domainConfig);
7505
8070
  const workspaces = new MongoDBWorkspacesStorage(domainConfig);
7506
8071
  const skills = new MongoDBSkillsStorage(domainConfig);
7507
8072
  const blobs = new MongoDBBlobStore(domainConfig);
@@ -7514,6 +8079,7 @@ var MongoDBStore = class extends storage.MastraCompositeStore {
7514
8079
  promptBlocks,
7515
8080
  scorerDefinitions,
7516
8081
  mcpClients,
8082
+ mcpServers,
7517
8083
  workspaces,
7518
8084
  skills,
7519
8085
  blobs
@@ -7640,6 +8206,7 @@ exports.MemoryStorageMongoDB = MemoryStorageMongoDB;
7640
8206
  exports.MongoDBAgentsStorage = MongoDBAgentsStorage;
7641
8207
  exports.MongoDBBlobStore = MongoDBBlobStore;
7642
8208
  exports.MongoDBMCPClientsStorage = MongoDBMCPClientsStorage;
8209
+ exports.MongoDBMCPServersStorage = MongoDBMCPServersStorage;
7643
8210
  exports.MongoDBPromptBlocksStorage = MongoDBPromptBlocksStorage;
7644
8211
  exports.MongoDBScorerDefinitionsStorage = MongoDBScorerDefinitionsStorage;
7645
8212
  exports.MongoDBSkillsStorage = MongoDBSkillsStorage;