@mastra/core
Version:
324 lines (323 loc) • 10.8 kB
JavaScript
const require_deep_equal = require("./deep-equal-BvQBG8wE.cjs");
const require_filesystem_versioned = require("./filesystem-versioned-8Ag_Np8l.cjs");
//#region src/storage/domains/mcp-clients/base.ts
var MCPClientsStorage = class extends require_filesystem_versioned.VersionedStorageDomain {
listKey = "mcpClients";
versionMetadataFields = [
"id",
"mcpClientId",
"versionNumber",
"changedFields",
"changeMessage",
"createdAt"
];
constructor() {
super({
component: "STORAGE",
name: "MCP_CLIENTS"
});
}
};
//#endregion
//#region src/storage/domains/mcp-clients/inmemory.ts
var InMemoryMCPClientsStorage = class extends MCPClientsStorage {
db;
constructor({ db }) {
super();
this.db = db;
}
async dangerouslyClearAll() {
this.db.mcpClients.clear();
this.db.mcpClientVersions.clear();
}
async getById(id) {
const config = this.db.mcpClients.get(id);
return config ? this.deepCopyConfig(config) : null;
}
async create(input) {
const { mcpClient } = input;
if (this.db.mcpClients.has(mcpClient.id)) throw new Error(`MCP client with id ${mcpClient.id} already exists`);
const now = /* @__PURE__ */ new Date();
const newConfig = {
id: mcpClient.id,
status: "draft",
activeVersionId: void 0,
authorId: mcpClient.authorId,
metadata: mcpClient.metadata,
createdAt: now,
updatedAt: now
};
this.db.mcpClients.set(mcpClient.id, newConfig);
const { id: _id, authorId: _authorId, metadata: _metadata, ...snapshotConfig } = mcpClient;
const versionId = crypto.randomUUID();
await this.createVersion({
id: versionId,
mcpClientId: mcpClient.id,
versionNumber: 1,
...snapshotConfig,
changedFields: Object.keys(snapshotConfig),
changeMessage: "Initial version"
});
return this.deepCopyConfig(newConfig);
}
async update(input) {
const { id, ...updates } = input;
const existingConfig = this.db.mcpClients.get(id);
if (!existingConfig) throw new Error(`MCP client with id ${id} not found`);
const { authorId, activeVersionId, metadata, status } = updates;
const updatedConfig = {
...existingConfig,
...authorId !== void 0 && { authorId },
...activeVersionId !== void 0 && { activeVersionId },
...status !== void 0 && { status },
...metadata !== void 0 && { metadata: {
...existingConfig.metadata,
...metadata
} },
updatedAt: /* @__PURE__ */ new Date()
};
this.db.mcpClients.set(id, updatedConfig);
return this.deepCopyConfig(updatedConfig);
}
async delete(id) {
this.db.mcpClients.delete(id);
await this.deleteVersionsByParentId(id);
}
async list(args) {
const { page = 0, perPage: perPageInput, orderBy, authorId, metadata, status } = args || {};
const { field, direction } = this.parseOrderBy(orderBy);
const perPage = require_filesystem_versioned.normalizePerPage(perPageInput, 100);
if (page < 0) throw new Error("page must be >= 0");
const maxOffset = Number.MAX_SAFE_INTEGER / 2;
if (page * perPage > maxOffset) throw new Error("page value too large");
let configs = Array.from(this.db.mcpClients.values());
if (status) configs = configs.filter((config) => config.status === status);
if (authorId !== void 0) configs = configs.filter((config) => config.authorId === authorId);
if (metadata && Object.keys(metadata).length > 0) configs = configs.filter((config) => {
if (!config.metadata) return false;
return Object.entries(metadata).every(([key, value]) => require_deep_equal.deepEqual(config.metadata[key], value));
});
const clonedConfigs = this.sortConfigs(configs, field, direction).map((config) => this.deepCopyConfig(config));
const { offset, perPage: perPageForResponse } = require_filesystem_versioned.calculatePagination(page, perPageInput, perPage);
return {
mcpClients: clonedConfigs.slice(offset, offset + perPage),
total: clonedConfigs.length,
page,
perPage: perPageForResponse,
hasMore: offset + perPage < clonedConfigs.length
};
}
async createVersion(input) {
if (this.db.mcpClientVersions.has(input.id)) throw new Error(`Version with id ${input.id} already exists`);
for (const version of this.db.mcpClientVersions.values()) if (version.mcpClientId === input.mcpClientId && version.versionNumber === input.versionNumber) throw new Error(`Version number ${input.versionNumber} already exists for MCP client ${input.mcpClientId}`);
const version = {
...input,
createdAt: /* @__PURE__ */ new Date()
};
this.db.mcpClientVersions.set(input.id, this.deepCopyVersion(version));
return this.deepCopyVersion(version);
}
async getVersion(id) {
const version = this.db.mcpClientVersions.get(id);
return version ? this.deepCopyVersion(version) : null;
}
async getVersionByNumber(mcpClientId, versionNumber) {
for (const version of this.db.mcpClientVersions.values()) if (version.mcpClientId === mcpClientId && version.versionNumber === versionNumber) return this.deepCopyVersion(version);
return null;
}
async getLatestVersion(mcpClientId) {
let latest = null;
for (const version of this.db.mcpClientVersions.values()) if (version.mcpClientId === mcpClientId) {
if (!latest || version.versionNumber > latest.versionNumber) latest = version;
}
return latest ? this.deepCopyVersion(latest) : null;
}
async listVersions(input) {
const { mcpClientId, page = 0, perPage: perPageInput, orderBy } = input;
const { field, direction } = this.parseVersionOrderBy(orderBy);
const perPage = require_filesystem_versioned.normalizePerPage(perPageInput, 20);
if (page < 0) throw new Error("page must be >= 0");
const maxOffset = Number.MAX_SAFE_INTEGER / 2;
if (page * perPage > maxOffset) throw new Error("page value too large");
let versions = Array.from(this.db.mcpClientVersions.values()).filter((v) => v.mcpClientId === mcpClientId);
versions = this.sortVersions(versions, field, direction);
const clonedVersions = versions.map((v) => this.deepCopyVersion(v));
const total = clonedVersions.length;
const { offset, perPage: perPageForResponse } = require_filesystem_versioned.calculatePagination(page, perPageInput, perPage);
return {
versions: clonedVersions.slice(offset, offset + perPage),
total,
page,
perPage: perPageForResponse,
hasMore: offset + perPage < total
};
}
async deleteVersion(id) {
this.db.mcpClientVersions.delete(id);
}
async deleteVersionsByParentId(entityId) {
const idsToDelete = [];
for (const [id, version] of this.db.mcpClientVersions.entries()) if (version.mcpClientId === entityId) idsToDelete.push(id);
for (const id of idsToDelete) this.db.mcpClientVersions.delete(id);
}
async countVersions(mcpClientId) {
let count = 0;
for (const version of this.db.mcpClientVersions.values()) if (version.mcpClientId === mcpClientId) count++;
return count;
}
deepCopyConfig(config) {
return {
...config,
metadata: config.metadata ? { ...config.metadata } : config.metadata
};
}
deepCopyVersion(version) {
return {
...version,
servers: version.servers ? JSON.parse(JSON.stringify(version.servers)) : version.servers,
changedFields: version.changedFields ? [...version.changedFields] : version.changedFields
};
}
sortConfigs(configs, field, direction) {
return configs.sort((a, b) => {
const aValue = a[field].getTime();
const bValue = b[field].getTime();
return direction === "ASC" ? aValue - bValue : bValue - aValue;
});
}
sortVersions(versions, field, direction) {
return versions.sort((a, b) => {
let aVal;
let bVal;
if (field === "createdAt") {
aVal = a.createdAt.getTime();
bVal = b.createdAt.getTime();
} else {
aVal = a.versionNumber;
bVal = b.versionNumber;
}
return direction === "ASC" ? aVal - bVal : bVal - aVal;
});
}
};
//#endregion
//#region src/storage/domains/mcp-clients/filesystem.ts
var FilesystemMCPClientsStorage = class extends MCPClientsStorage {
helpers;
constructor({ db }) {
super();
this.helpers = new require_filesystem_versioned.FilesystemVersionedHelpers({
db,
entitiesFile: "mcp-clients.json",
parentIdField: "mcpClientId",
name: "FilesystemMCPClientsStorage",
versionMetadataFields: [
"id",
"mcpClientId",
"versionNumber",
"changedFields",
"changeMessage",
"createdAt"
]
});
}
async init() {
await this.helpers.db.init();
}
async dangerouslyClearAll() {
await this.helpers.dangerouslyClearAll();
}
async getById(id) {
return this.helpers.getById(id);
}
async create(input) {
const { mcpClient } = input;
const now = /* @__PURE__ */ new Date();
const entity = {
id: mcpClient.id,
status: "draft",
activeVersionId: void 0,
authorId: mcpClient.authorId,
metadata: mcpClient.metadata,
createdAt: now,
updatedAt: now
};
await this.helpers.createEntity(mcpClient.id, entity);
const { id: _id, authorId: _authorId, metadata: _metadata, ...snapshotConfig } = mcpClient;
const versionId = crypto.randomUUID();
await this.createVersion({
id: versionId,
mcpClientId: mcpClient.id,
versionNumber: 1,
...snapshotConfig,
changedFields: Object.keys(snapshotConfig),
changeMessage: "Initial version"
});
return structuredClone(entity);
}
async update(input) {
const { id, ...updates } = input;
return this.helpers.updateEntity(id, updates);
}
async delete(id) {
await this.helpers.deleteEntity(id);
}
async list(args) {
const { page, perPage, orderBy, authorId, metadata, status } = args || {};
return await this.helpers.listEntities({
page,
perPage,
orderBy,
listKey: "mcpClients",
filters: {
authorId,
metadata,
status
}
});
}
async createVersion(input) {
return this.helpers.createVersion(input);
}
async getVersion(id) {
return this.helpers.getVersion(id);
}
async getVersionByNumber(mcpClientId, versionNumber) {
return this.helpers.getVersionByNumber(mcpClientId, versionNumber);
}
async getLatestVersion(mcpClientId) {
return this.helpers.getLatestVersion(mcpClientId);
}
async listVersions(input) {
return await this.helpers.listVersions(input, "mcpClientId");
}
async deleteVersion(id) {
await this.helpers.deleteVersion(id);
}
async deleteVersionsByParentId(entityId) {
await this.helpers.deleteVersionsByParentId(entityId);
}
async countVersions(mcpClientId) {
return this.helpers.countVersions(mcpClientId);
}
};
//#endregion
Object.defineProperty(exports, "FilesystemMCPClientsStorage", {
enumerable: true,
get: function() {
return FilesystemMCPClientsStorage;
}
});
Object.defineProperty(exports, "InMemoryMCPClientsStorage", {
enumerable: true,
get: function() {
return InMemoryMCPClientsStorage;
}
});
Object.defineProperty(exports, "MCPClientsStorage", {
enumerable: true,
get: function() {
return MCPClientsStorage;
}
});
//# sourceMappingURL=filesystem-BdBNosM7.cjs.map