UNPKG

@mastra/core

Version:
189 lines (188 loc) • 7.42 kB
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); const require_base = require("../base-BIzR1D3_.cjs"); const require_error = require("../error-D90lJ6M7.cjs"); const require_logger = require("../logger-CvzZYuJd.cjs"); const require_slugify = require("../slugify-D98M2vDP.cjs"); // src/mcp/types.ts /** * Error ids an {@link MCPServerEventsInspector} throws as `MastraError`s. * @experimental */ const MCP_SERVER_EVENT_ERROR_IDS = { unknownEvent: "MCP_SERVER_UNKNOWN_EVENT", unsupportedDelivery: "MCP_SERVER_EVENT_UNSUPPORTED_DELIVERY", invalidArguments: "MCP_SERVER_EVENT_INVALID_ARGUMENTS", forbidden: "MCP_SERVER_EVENT_FORBIDDEN", notEmittable: "MCP_SERVER_EVENT_NOT_EMITTABLE", invalidPayload: "MCP_SERVER_EVENT_INVALID_PAYLOAD", tooManyStreams: "MCP_SERVER_EVENT_TOO_MANY_STREAMS" }; // 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 { /** * Set to `2` by servers that speak MCP 2026-07-28 (`@mastra/mcp` 2.x). Such a server * has no standalone SSE transport and its `executeTool` resolves to a * `MCPToolExecutionResultV2`. 1.x servers leave it undefined. */ mcpVersion; /** 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; /** A human-readable display title for the MCP server. */ title; /** The URL of the server's website. */ websiteUrl; /** Icons clients can display for the server. */ icons; /** 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 = require_slugify.slugify(config.id); this.idWasSet = true; } else this._id = this.mastra?.generateId() || globalThis.crypto.randomUUID(); this.description = config.description; this.title = config.title; this.websiteUrl = config.websiteUrl; this.icons = config.icons; 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); } /** * Start the MCP server using the standalone HTTP+SSE transport. * Only `@mastra/mcp` 1.x overrides this; MCP 2026-07-28 removed the transport, so the * base implementation throws and 2.x servers inherit that. * @deprecated Removed in the next core major together with the 1.x server contract. * Use `startHTTP` (Streamable HTTP) instead. * @param options Options for the SSE transport */ async startSSE(_options) { throw new Error(`MCP server '${this.id}' does not implement the standalone SSE transport (removed in MCP 2026-07-28); use startHTTP instead`); } /** * Start the MCP server using the standalone Hono SSE transport. * Only `@mastra/mcp` 1.x overrides this; MCP 2026-07-28 removed the transport, so the * base implementation throws and 2.x servers inherit that. * @deprecated Removed in the next core major together with the 1.x server contract. * Use `startHTTP` (Streamable HTTP) instead. * @param options Options for the SSE transport */ async startHonoSSE(_options) { throw new Error(`MCP server '${this.id}' does not implement the standalone Hono SSE transport (removed in MCP 2026-07-28); use startHTTP instead`); } }; exports.MCPServerBase = MCPServerBase; exports.MCP_SERVER_EVENT_ERROR_IDS = MCP_SERVER_EVENT_ERROR_IDS; //# sourceMappingURL=index.cjs.map