import { MastraError } from '../chunk-FJEVLHJT.js';
import { MastraBase } from '../chunk-DNW46JHP.js';
import { RegisteredLogger } from '../chunk-M3LRSDLL.js';
import { randomUUID } from 'crypto';
import slugify from '@sindresorhus/slugify';

var MCPServerBase = class extends 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. */
  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 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 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 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: RegisteredLogger.MCP_SERVER, name: config.name });
    this.name = config.name;
    this.version = config.version;
    if (config.id) {
      this._id = slugify(config.id);
      this.idWasSet = true;
    } else {
      this._id = this.mastra?.generateId() || 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);
  }
};

export { MCPServerBase };
//# sourceMappingURL=index.js.map
//# sourceMappingURL=index.js.map