'use strict';

var chunkWIOYCLJC_cjs = require('./chunk-WIOYCLJC.cjs');
var chunk2XZ2466F_cjs = require('./chunk-2XZ2466F.cjs');
var chunkDIG2K5CV_cjs = require('./chunk-DIG2K5CV.cjs');
var chunkDRDVJ35H_cjs = require('./chunk-DRDVJ35H.cjs');
var chunkZ7LCIYK7_cjs = require('./chunk-Z7LCIYK7.cjs');
var chunkN54Q3AQR_cjs = require('./chunk-N54Q3AQR.cjs');
var chunk64ITUOXI_cjs = require('./chunk-64ITUOXI.cjs');

// src/server/handlers/stored-mcp-clients.ts
var LIST_STORED_MCP_CLIENTS_ROUTE = chunkN54Q3AQR_cjs.createRoute({
  method: "GET",
  path: "/stored/mcp-clients",
  responseType: "json",
  queryParamSchema: chunkWIOYCLJC_cjs.listStoredMCPClientsQuerySchema,
  responseSchema: chunkWIOYCLJC_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, requestContext }) => {
    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 scope = await chunkDRDVJ35H_cjs.getStoredResourceScope(mastra, requestContext);
      const result = await mcpClientStore.listResolved({
        page,
        perPage,
        orderBy,
        status,
        authorId,
        metadata: chunkDRDVJ35H_cjs.scopeStoredResourceMetadata(metadata, scope)
      });
      return result;
    } catch (error) {
      return chunkZ7LCIYK7_cjs.handleError(error, "Error listing stored MCP clients");
    }
  }
});
var GET_STORED_MCP_CLIENT_ROUTE = chunkN54Q3AQR_cjs.createRoute({
  method: "GET",
  path: "/stored/mcp-clients/:storedMCPClientId",
  responseType: "json",
  pathParamSchema: chunkWIOYCLJC_cjs.storedMCPClientIdPathParams,
  queryParamSchema: chunkDIG2K5CV_cjs.statusQuerySchema,
  responseSchema: chunkWIOYCLJC_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, requestContext }) => {
    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` });
      }
      chunkDRDVJ35H_cjs.assertStoredResourceScope(mcpClient, await chunkDRDVJ35H_cjs.getStoredResourceScope(mastra, requestContext));
      return mcpClient;
    } catch (error) {
      return chunkZ7LCIYK7_cjs.handleError(error, "Error getting stored MCP client");
    }
  }
});
var CREATE_STORED_MCP_CLIENT_ROUTE = chunkN54Q3AQR_cjs.createRoute({
  method: "POST",
  path: "/stored/mcp-clients",
  responseType: "json",
  bodySchema: chunkWIOYCLJC_cjs.createStoredMCPClientBodySchema,
  responseSchema: chunkWIOYCLJC_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, requestContext }) => {
    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 || chunkDRDVJ35H_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: chunkDRDVJ35H_cjs.scopeStoredResourceMetadata(metadata, await chunkDRDVJ35H_cjs.getStoredResourceScope(mastra, requestContext)),
          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 chunkZ7LCIYK7_cjs.handleError(error, "Error creating stored MCP client");
    }
  }
});
var UPDATE_STORED_MCP_CLIENT_ROUTE = chunkN54Q3AQR_cjs.createRoute({
  method: "PATCH",
  path: "/stored/mcp-clients/:storedMCPClientId",
  responseType: "json",
  pathParamSchema: chunkWIOYCLJC_cjs.storedMCPClientIdPathParams,
  bodySchema: chunkWIOYCLJC_cjs.updateStoredMCPClientBodySchema,
  responseSchema: chunkWIOYCLJC_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,
    requestContext,
    // 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 scope = await chunkDRDVJ35H_cjs.getStoredResourceScope(mastra, requestContext);
      chunkDRDVJ35H_cjs.assertStoredResourceScope(existing, scope);
      const scopedMetadata = metadata !== void 0 ? chunkDRDVJ35H_cjs.scopeStoredResourceMetadata({ ...existing.metadata ?? {}, ...metadata }, scope) : void 0;
      const updatedMCPClient = await mcpClientStore.update({
        id: storedMCPClientId,
        authorId,
        ...scopedMetadata !== void 0 ? { metadata: scopedMetadata } : {},
        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 chunkZ7LCIYK7_cjs.handleError(error, "Error updating stored MCP client");
    }
  }
});
var DELETE_STORED_MCP_CLIENT_ROUTE = chunkN54Q3AQR_cjs.createRoute({
  method: "DELETE",
  path: "/stored/mcp-clients/:storedMCPClientId",
  responseType: "json",
  pathParamSchema: chunkWIOYCLJC_cjs.storedMCPClientIdPathParams,
  responseSchema: chunkWIOYCLJC_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, requestContext }) => {
    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` });
      }
      chunkDRDVJ35H_cjs.assertStoredResourceScope(existing, await chunkDRDVJ35H_cjs.getStoredResourceScope(mastra, requestContext));
      await mcpClientStore.delete(storedMCPClientId);
      return {
        success: true,
        message: `MCP client ${storedMCPClientId} deleted successfully`
      };
    } catch (error) {
      return chunkZ7LCIYK7_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-YKY2YBJI.cjs.map
//# sourceMappingURL=chunk-YKY2YBJI.cjs.map