'use strict';

var chunkULL6VJ5E_cjs = require('./chunk-ULL6VJ5E.cjs');
var chunk2XZ2466F_cjs = require('./chunk-2XZ2466F.cjs');
var chunkEFX5WYRT_cjs = require('./chunk-EFX5WYRT.cjs');
var chunkZYXDUS6Q_cjs = require('./chunk-ZYXDUS6Q.cjs');
var chunkY2BKW5ND_cjs = require('./chunk-Y2BKW5ND.cjs');
var chunk6P7P7Y5U_cjs = require('./chunk-6P7P7Y5U.cjs');
var chunk64ITUOXI_cjs = require('./chunk-64ITUOXI.cjs');

// src/server/handlers/stored-mcp-clients.ts
var LIST_STORED_MCP_CLIENTS_ROUTE = chunk6P7P7Y5U_cjs.createRoute({
  method: "GET",
  path: "/stored/mcp-clients",
  responseType: "json",
  queryParamSchema: chunkULL6VJ5E_cjs.listStoredMCPClientsQuerySchema,
  responseSchema: chunkULL6VJ5E_cjs.listStoredMCPClientsResponseSchema,
  summary: "List stored MCP clients",
  description: "Returns a paginated list of all MCP client configurations stored in the database",
  tags: ["Stored MCP Clients"],
  requiresAuth: true,
  handler: async ({ mastra, page, perPage, orderBy, status, authorId, metadata }) => {
    try {
      const storage = mastra.getStorage();
      if (!storage) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Storage is not configured" });
      }
      const mcpClientStore = await storage.getStore("mcpClients");
      if (!mcpClientStore) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "MCP clients storage domain is not available" });
      }
      const result = await mcpClientStore.listResolved({
        page,
        perPage,
        orderBy,
        status,
        authorId,
        metadata
      });
      return result;
    } catch (error) {
      return chunkY2BKW5ND_cjs.handleError(error, "Error listing stored MCP clients");
    }
  }
});
var GET_STORED_MCP_CLIENT_ROUTE = chunk6P7P7Y5U_cjs.createRoute({
  method: "GET",
  path: "/stored/mcp-clients/:storedMCPClientId",
  responseType: "json",
  pathParamSchema: chunkULL6VJ5E_cjs.storedMCPClientIdPathParams,
  queryParamSchema: chunkZYXDUS6Q_cjs.statusQuerySchema,
  responseSchema: chunkULL6VJ5E_cjs.getStoredMCPClientResponseSchema,
  summary: "Get stored MCP client by ID",
  description: "Returns a specific MCP client from storage by its unique identifier. Use ?status=draft to resolve with the latest (draft) version, or ?status=published (default) for the active published version.",
  tags: ["Stored MCP Clients"],
  requiresAuth: true,
  handler: async ({ mastra, storedMCPClientId, status }) => {
    try {
      const storage = mastra.getStorage();
      if (!storage) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Storage is not configured" });
      }
      const mcpClientStore = await storage.getStore("mcpClients");
      if (!mcpClientStore) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "MCP clients storage domain is not available" });
      }
      const mcpClient = await mcpClientStore.getByIdResolved(storedMCPClientId, { status });
      if (!mcpClient) {
        throw new chunk64ITUOXI_cjs.HTTPException(404, { message: `Stored MCP client with id ${storedMCPClientId} not found` });
      }
      return mcpClient;
    } catch (error) {
      return chunkY2BKW5ND_cjs.handleError(error, "Error getting stored MCP client");
    }
  }
});
var CREATE_STORED_MCP_CLIENT_ROUTE = chunk6P7P7Y5U_cjs.createRoute({
  method: "POST",
  path: "/stored/mcp-clients",
  responseType: "json",
  bodySchema: chunkULL6VJ5E_cjs.createStoredMCPClientBodySchema,
  responseSchema: chunkULL6VJ5E_cjs.createStoredMCPClientResponseSchema,
  summary: "Create stored MCP client",
  description: "Creates a new MCP client configuration in storage with the provided servers",
  tags: ["Stored MCP Clients"],
  requiresAuth: true,
  handler: async ({ mastra, id: providedId, authorId, metadata, name, description, servers }) => {
    try {
      const storage = mastra.getStorage();
      if (!storage) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Storage is not configured" });
      }
      const mcpClientStore = await storage.getStore("mcpClients");
      if (!mcpClientStore) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "MCP clients storage domain is not available" });
      }
      const id = providedId || chunkEFX5WYRT_cjs.toSlug(name);
      if (!id) {
        throw new chunk64ITUOXI_cjs.HTTPException(400, {
          message: "Could not derive MCP client ID from name. Please provide an explicit id."
        });
      }
      const existing = await mcpClientStore.getById(id);
      if (existing) {
        throw new chunk64ITUOXI_cjs.HTTPException(409, { message: `MCP client with id ${id} already exists` });
      }
      await mcpClientStore.create({
        mcpClient: {
          id,
          authorId,
          metadata,
          name,
          description,
          servers
        }
      });
      const resolved = await mcpClientStore.getByIdResolved(id, { status: "draft" });
      if (!resolved) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Failed to resolve created MCP client" });
      }
      return resolved;
    } catch (error) {
      return chunkY2BKW5ND_cjs.handleError(error, "Error creating stored MCP client");
    }
  }
});
var UPDATE_STORED_MCP_CLIENT_ROUTE = chunk6P7P7Y5U_cjs.createRoute({
  method: "PATCH",
  path: "/stored/mcp-clients/:storedMCPClientId",
  responseType: "json",
  pathParamSchema: chunkULL6VJ5E_cjs.storedMCPClientIdPathParams,
  bodySchema: chunkULL6VJ5E_cjs.updateStoredMCPClientBodySchema,
  responseSchema: chunkULL6VJ5E_cjs.updateStoredMCPClientResponseSchema,
  summary: "Update stored MCP client",
  description: "Updates an existing MCP client in storage with the provided fields",
  tags: ["Stored MCP Clients"],
  requiresAuth: true,
  handler: async ({
    mastra,
    storedMCPClientId,
    // Metadata-level fields
    authorId,
    metadata,
    // Config fields (snapshot-level)
    name,
    description,
    servers
  }) => {
    try {
      const storage = mastra.getStorage();
      if (!storage) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Storage is not configured" });
      }
      const mcpClientStore = await storage.getStore("mcpClients");
      if (!mcpClientStore) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "MCP clients storage domain is not available" });
      }
      const existing = await mcpClientStore.getById(storedMCPClientId);
      if (!existing) {
        throw new chunk64ITUOXI_cjs.HTTPException(404, { message: `Stored MCP client with id ${storedMCPClientId} not found` });
      }
      const updatedMCPClient = await mcpClientStore.update({
        id: storedMCPClientId,
        authorId,
        metadata,
        name,
        description,
        servers
      });
      const configFields = { name, description, servers };
      const providedConfigFields = Object.fromEntries(Object.entries(configFields).filter(([_, v]) => v !== void 0));
      await chunk2XZ2466F_cjs.handleAutoVersioning(
        mcpClientStore,
        storedMCPClientId,
        "mcpClientId",
        chunk2XZ2466F_cjs.MCP_CLIENT_SNAPSHOT_CONFIG_FIELDS,
        existing,
        updatedMCPClient,
        providedConfigFields
      );
      const resolved = await mcpClientStore.getByIdResolved(storedMCPClientId, { status: "draft" });
      if (!resolved) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Failed to resolve updated MCP client" });
      }
      return resolved;
    } catch (error) {
      return chunkY2BKW5ND_cjs.handleError(error, "Error updating stored MCP client");
    }
  }
});
var DELETE_STORED_MCP_CLIENT_ROUTE = chunk6P7P7Y5U_cjs.createRoute({
  method: "DELETE",
  path: "/stored/mcp-clients/:storedMCPClientId",
  responseType: "json",
  pathParamSchema: chunkULL6VJ5E_cjs.storedMCPClientIdPathParams,
  responseSchema: chunkULL6VJ5E_cjs.deleteStoredMCPClientResponseSchema,
  summary: "Delete stored MCP client",
  description: "Deletes an MCP client from storage by its unique identifier",
  tags: ["Stored MCP Clients"],
  requiresAuth: true,
  handler: async ({ mastra, storedMCPClientId }) => {
    try {
      const storage = mastra.getStorage();
      if (!storage) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Storage is not configured" });
      }
      const mcpClientStore = await storage.getStore("mcpClients");
      if (!mcpClientStore) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "MCP clients storage domain is not available" });
      }
      const existing = await mcpClientStore.getById(storedMCPClientId);
      if (!existing) {
        throw new chunk64ITUOXI_cjs.HTTPException(404, { message: `Stored MCP client with id ${storedMCPClientId} not found` });
      }
      await mcpClientStore.delete(storedMCPClientId);
      return {
        success: true,
        message: `MCP client ${storedMCPClientId} deleted successfully`
      };
    } catch (error) {
      return chunkY2BKW5ND_cjs.handleError(error, "Error deleting stored MCP client");
    }
  }
});

exports.CREATE_STORED_MCP_CLIENT_ROUTE = CREATE_STORED_MCP_CLIENT_ROUTE;
exports.DELETE_STORED_MCP_CLIENT_ROUTE = DELETE_STORED_MCP_CLIENT_ROUTE;
exports.GET_STORED_MCP_CLIENT_ROUTE = GET_STORED_MCP_CLIENT_ROUTE;
exports.LIST_STORED_MCP_CLIENTS_ROUTE = LIST_STORED_MCP_CLIENTS_ROUTE;
exports.UPDATE_STORED_MCP_CLIENT_ROUTE = UPDATE_STORED_MCP_CLIENT_ROUTE;
//# sourceMappingURL=chunk-TKBRQY5C.cjs.map
//# sourceMappingURL=chunk-TKBRQY5C.cjs.map