UNPKG

@mastra/core

Version:
141 lines (140 loc) 5.38 kB
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); const require_rolldown_runtime = require("../rolldown-runtime-uwYp4b74.cjs"); const require_logger = require("../logger-BPclhj7J.cjs"); const require_base = require("../base-B6soWsYg.cjs"); const require_error = require("../error-B-e62x-A.cjs"); let crypto = require("crypto"); let _sindresorhus_slugify = require("@sindresorhus/slugify"); _sindresorhus_slugify = require_rolldown_runtime.__toESM(_sindresorhus_slugify, 1); //#region src/mcp/index.ts /** * Abstract base class for MCP server implementations. * This provides a common interface and shared functionality for all MCP servers * that can be registered with Mastra, including handling of server metadata. */ var MCPServerBase = class extends require_base.MastraBase { /** Tracks if the server ID has been definitively set. */ idWasSet = false; /** The display name of the MCP server. */ name; /** The semantic version of the MCP server. */ version; /** Internal storage for the server's unique ID. */ _id; /** A description of what the MCP server does. */ description; /** Optional instructions describing how to use the server and its features. */ instructions; /** Repository information for the server's source code. */ repository; /** The release date of this server version (ISO 8601 string). */ releaseDate; /** Indicates if this version is the latest available. */ isLatest; /** The canonical packaging format (e.g., "npm", "docker"), if applicable. */ packageCanonical; /** Information about installable packages for this server. */ packages; /** Information about remote access points for this server. */ remotes; /** The tools registered with and converted by this MCP server. */ convertedTools; /** Reference to the Mastra instance if this server is registered with one. */ mastra; /** Agents to be exposed as tools. */ agents; /** Workflows to be exposed as tools. */ workflows; /** Original tools configuration for re-conversion when Mastra instance is registered. Mutable to support dynamic tool management. */ originalTools; /** * Public getter for the server's unique ID. * The ID is set at construction or by Mastra and is read-only afterwards. */ get id() { return this._id; } /** * Gets a read-only view of the registered tools. * @returns A readonly record of converted tools. */ tools() { return this.convertedTools; } /** * Sets the server's unique ID. This method is typically called by Mastra when * registering the server, using the key provided in the Mastra configuration. * It ensures the ID is set only once. * If an ID was already provided in the MCPServerConfig, this method will be a no-op. * @param id The unique ID to assign to the server. */ setId(id) { if (this.idWasSet) return; this._id = id; this.idWasSet = true; } /** * Internal method used by Mastra to register itself with the server. * @param mastra The Mastra instance. * @internal */ __registerMastra(mastra) { this.mastra = mastra; this.convertedTools = this.convertTools(this.originalTools, this.agents, this.workflows); if (this.originalTools && typeof this.originalTools === "object") Object.entries(this.originalTools).forEach(([key, tool]) => { try { if (tool && typeof tool === "object" && "id" in tool) { const toolKey = typeof tool.id === "string" ? tool.id : key; mastra.addTool(tool, toolKey); } } catch (error) { if (!(error instanceof require_error.MastraError) || error.id !== "MASTRA_ADD_TOOL_DUPLICATE_KEY") throw error; } }); if (this.agents && typeof this.agents === "object") Object.entries(this.agents).forEach(([key, agent]) => { try { mastra.addAgent(agent, key); } catch (error) { if (!(error instanceof require_error.MastraError) || error.id !== "MASTRA_ADD_AGENT_DUPLICATE_KEY") throw error; } }); if (this.workflows && typeof this.workflows === "object") Object.entries(this.workflows).forEach(([key, workflow]) => { try { mastra.addWorkflow(workflow, key); } catch (error) { if (!(error instanceof require_error.MastraError) || error.id !== "MASTRA_ADD_WORKFLOW_DUPLICATE_KEY") throw error; } }); } /** * Constructor for the MCPServerBase. * @param config Configuration options for the MCP server, including metadata. */ constructor(config) { super({ component: require_logger.RegisteredLogger.MCP_SERVER, name: config.name }); this.name = config.name; this.version = config.version; if (config.id) { this._id = (0, _sindresorhus_slugify.default)(config.id); this.idWasSet = true; } else this._id = this.mastra?.generateId() || (0, crypto.randomUUID)(); this.description = config.description; this.instructions = config.instructions; this.repository = config.repository; this.releaseDate = config.releaseDate || (/* @__PURE__ */ new Date()).toISOString(); this.isLatest = config.isLatest === void 0 ? true : config.isLatest; this.packageCanonical = config.packageCanonical; this.packages = config.packages; this.remotes = config.remotes; this.agents = config.agents; this.workflows = config.workflows; this.originalTools = config.tools; this.convertedTools = this.convertTools(config.tools, config.agents, config.workflows); } }; //#endregion exports.MCPServerBase = MCPServerBase; //# sourceMappingURL=index.cjs.map