'use strict';

var chunkBNR5CIPX_cjs = require('./chunk-BNR5CIPX.cjs');
var chunkAZORAK4H_cjs = require('./chunk-AZORAK4H.cjs');
var chunk64ITUOXI_cjs = require('./chunk-64ITUOXI.cjs');
var chunkO7I5CWRX_cjs = require('./chunk-O7I5CWRX.cjs');

// src/server/handlers/mcp.ts
var mcp_exports = {};
chunkO7I5CWRX_cjs.__export(mcp_exports, {
  EXECUTE_MCP_SERVER_TOOL_ROUTE: () => EXECUTE_MCP_SERVER_TOOL_ROUTE,
  GET_MCP_SERVER_DETAIL_ROUTE: () => GET_MCP_SERVER_DETAIL_ROUTE,
  GET_MCP_SERVER_TOOL_DETAIL_ROUTE: () => GET_MCP_SERVER_TOOL_DETAIL_ROUTE,
  LIST_MCP_SERVERS_ROUTE: () => LIST_MCP_SERVERS_ROUTE,
  LIST_MCP_SERVER_RESOURCES_ROUTE: () => LIST_MCP_SERVER_RESOURCES_ROUTE,
  LIST_MCP_SERVER_TOOLS_ROUTE: () => LIST_MCP_SERVER_TOOLS_ROUTE,
  MCP_HTTP_TRANSPORT_ROUTE: () => MCP_HTTP_TRANSPORT_ROUTE,
  MCP_SSE_MESSAGES_ROUTE: () => MCP_SSE_MESSAGES_ROUTE,
  MCP_SSE_TRANSPORT_ROUTE: () => MCP_SSE_TRANSPORT_ROUTE,
  READ_MCP_SERVER_RESOURCE_ROUTE: () => READ_MCP_SERVER_RESOURCE_ROUTE
});
var LIST_MCP_SERVERS_ROUTE = chunkAZORAK4H_cjs.createRoute({
  method: "GET",
  path: "/mcp/v0/servers",
  responseType: "json",
  queryParamSchema: chunkBNR5CIPX_cjs.listMcpServersQuerySchema,
  responseSchema: chunkBNR5CIPX_cjs.listMcpServersResponseSchema,
  summary: "List MCP servers",
  description: "Returns a list of registered MCP servers with pagination support",
  tags: ["MCP"],
  requiresAuth: true,
  handler: async ({
    mastra,
    routePrefix,
    page,
    perPage,
    limit,
    offset
  }) => {
    if (!mastra || typeof mastra.listMCPServers !== "function") {
      throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Mastra instance or listMCPServers method not available" });
    }
    const servers = mastra.listMCPServers();
    if (!servers) {
      return { servers: [], total_count: 0, next: null };
    }
    const serverList = Object.values(servers);
    const totalCount = serverList.length;
    const useLegacyFormat = (limit !== void 0 || offset !== void 0) && page === void 0 && perPage === void 0;
    const finalPerPage = perPage ?? limit;
    let finalPage = page;
    if (finalPage === void 0 && offset !== void 0 && finalPerPage !== void 0 && finalPerPage > 0) {
      finalPage = Math.floor(offset / finalPerPage);
    }
    const actualOffset = finalPage !== void 0 && finalPerPage !== void 0 ? finalPage * finalPerPage : 0;
    let paginatedServers = serverList;
    let nextUrl = null;
    if (finalPerPage !== void 0) {
      paginatedServers = serverList.slice(actualOffset, actualOffset + finalPerPage);
      if (actualOffset + finalPerPage < totalCount) {
        const nextPage = (finalPage ?? 0) + 1;
        const prefix = routePrefix ?? "";
        if (useLegacyFormat) {
          const nextOffset = actualOffset + finalPerPage;
          nextUrl = `${prefix}/mcp/v0/servers?limit=${finalPerPage}&offset=${nextOffset}`;
        } else {
          nextUrl = `${prefix}/mcp/v0/servers?perPage=${finalPerPage}&page=${nextPage}`;
        }
      }
    }
    const serverInfoList = paginatedServers.map((server) => server.getServerInfo());
    return {
      servers: serverInfoList,
      total_count: totalCount,
      next: nextUrl
    };
  }
});
var GET_MCP_SERVER_DETAIL_ROUTE = chunkAZORAK4H_cjs.createRoute({
  method: "GET",
  path: "/mcp/v0/servers/:id",
  responseType: "json",
  pathParamSchema: chunkBNR5CIPX_cjs.mcpServerDetailPathParams,
  queryParamSchema: chunkBNR5CIPX_cjs.getMcpServerDetailQuerySchema,
  responseSchema: chunkBNR5CIPX_cjs.serverDetailSchema,
  summary: "Get MCP server details",
  description: "Returns detailed information about a specific MCP server",
  tags: ["MCP"],
  requiresAuth: true,
  handler: async ({ mastra, id, version }) => {
    if (!mastra || typeof mastra.getMCPServerById !== "function") {
      throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Mastra instance or getMCPServerById method not available" });
    }
    const server = mastra.getMCPServerById(id);
    if (!server) {
      throw new chunk64ITUOXI_cjs.HTTPException(404, { message: `MCP server with ID '${id}' not found` });
    }
    const serverDetail = server.getServerDetail();
    if (version && serverDetail.version_detail.version !== version) {
      throw new chunk64ITUOXI_cjs.HTTPException(404, {
        message: `MCP server with ID '${id}' found, but not version '${version}'. Available version: ${serverDetail.version_detail.version}`
      });
    }
    return serverDetail;
  }
});
var LIST_MCP_SERVER_TOOLS_ROUTE = chunkAZORAK4H_cjs.createRoute({
  method: "GET",
  path: "/mcp/:serverId/tools",
  responseType: "json",
  pathParamSchema: chunkBNR5CIPX_cjs.mcpServerIdPathParams,
  responseSchema: chunkBNR5CIPX_cjs.listMcpServerToolsResponseSchema,
  summary: "List MCP server tools",
  description: "Returns a list of tools available on the specified MCP server",
  tags: ["MCP"],
  requiresAuth: true,
  handler: async ({ mastra, serverId, requestContext }) => {
    if (!mastra || typeof mastra.getMCPServerById !== "function") {
      throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Mastra instance or getMCPServerById method not available" });
    }
    const server = mastra.getMCPServerById(serverId);
    if (!server) {
      throw new chunk64ITUOXI_cjs.HTTPException(404, { message: `MCP server with ID '${serverId}' not found` });
    }
    if (typeof server.getToolListInfo !== "function") {
      throw new chunk64ITUOXI_cjs.HTTPException(501, { message: `Server '${serverId}' cannot list tools in this way.` });
    }
    return await server.getToolListInfo(requestContext);
  }
});
var GET_MCP_SERVER_TOOL_DETAIL_ROUTE = chunkAZORAK4H_cjs.createRoute({
  method: "GET",
  path: "/mcp/:serverId/tools/:toolId",
  responseType: "json",
  pathParamSchema: chunkBNR5CIPX_cjs.mcpServerToolPathParams,
  responseSchema: chunkBNR5CIPX_cjs.mcpToolInfoSchema,
  summary: "Get MCP server tool details",
  description: "Returns detailed information about a specific tool on the MCP server",
  tags: ["MCP"],
  requiresAuth: true,
  handler: async ({ mastra, serverId, toolId }) => {
    if (!mastra || typeof mastra.getMCPServerById !== "function") {
      throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Mastra instance or getMCPServerById method not available" });
    }
    const server = mastra.getMCPServerById(serverId);
    if (!server) {
      throw new chunk64ITUOXI_cjs.HTTPException(404, { message: `MCP server with ID '${serverId}' not found` });
    }
    if (typeof server.getToolInfo !== "function") {
      throw new chunk64ITUOXI_cjs.HTTPException(501, { message: `Server '${serverId}' cannot provide tool details in this way.` });
    }
    const toolInfo = await server.getToolInfo(toolId);
    if (!toolInfo) {
      throw new chunk64ITUOXI_cjs.HTTPException(404, { message: `Tool with ID '${toolId}' not found on MCP server '${serverId}'` });
    }
    return toolInfo;
  }
});
var EXECUTE_MCP_SERVER_TOOL_ROUTE = chunkAZORAK4H_cjs.createRoute({
  method: "POST",
  path: "/mcp/:serverId/tools/:toolId/execute",
  responseType: "json",
  pathParamSchema: chunkBNR5CIPX_cjs.mcpServerToolPathParams,
  bodySchema: chunkBNR5CIPX_cjs.executeToolBodySchema,
  responseSchema: chunkBNR5CIPX_cjs.executeToolResponseSchema,
  summary: "Execute MCP server tool",
  description: "Executes a tool on the specified MCP server with the provided arguments",
  tags: ["MCP"],
  requiresAuth: true,
  handler: async ({
    mastra,
    serverId,
    toolId,
    data,
    requestContext
  }) => {
    if (!mastra || typeof mastra.getMCPServerById !== "function") {
      throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Mastra instance or getMCPServerById method not available" });
    }
    const server = mastra.getMCPServerById(serverId);
    if (!server) {
      throw new chunk64ITUOXI_cjs.HTTPException(404, { message: `MCP server with ID '${serverId}' not found` });
    }
    if (typeof server.executeTool !== "function") {
      throw new chunk64ITUOXI_cjs.HTTPException(501, { message: `Server '${serverId}' cannot execute tools in this way.` });
    }
    const result = await server.executeTool(toolId, data, { requestContext });
    return { result };
  }
});
var LIST_MCP_SERVER_RESOURCES_ROUTE = chunkAZORAK4H_cjs.createRoute({
  method: "GET",
  path: "/mcp/:serverId/resources",
  responseType: "json",
  pathParamSchema: chunkBNR5CIPX_cjs.mcpServerResourcePathParams,
  responseSchema: chunkBNR5CIPX_cjs.listResourcesResponseSchema,
  summary: "List MCP server resources",
  description: "Returns a list of resources available on the MCP server, including ui:// app resources",
  tags: ["MCP"],
  requiresAuth: true,
  handler: async ({ mastra, serverId }) => {
    if (!mastra || typeof mastra.getMCPServerById !== "function") {
      throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Mastra instance or getMCPServerById method not available" });
    }
    const server = mastra.getMCPServerById(serverId);
    if (!server) {
      throw new chunk64ITUOXI_cjs.HTTPException(404, { message: `MCP server with ID '${serverId}' not found` });
    }
    if (typeof server.listResources !== "function") {
      return { resources: [] };
    }
    return server.listResources();
  }
});
var READ_MCP_SERVER_RESOURCE_ROUTE = chunkAZORAK4H_cjs.createRoute({
  method: "POST",
  path: "/mcp/:serverId/resources/read",
  responseType: "json",
  pathParamSchema: chunkBNR5CIPX_cjs.mcpServerResourcePathParams,
  bodySchema: chunkBNR5CIPX_cjs.readResourceBodySchema,
  responseSchema: chunkBNR5CIPX_cjs.readResourceResponseSchema,
  summary: "Read MCP server resource content",
  description: "Reads the content of a resource by URI, used for rendering MCP App ui:// resources",
  tags: ["MCP"],
  requiresAuth: true,
  handler: async ({ mastra, serverId, uri }) => {
    if (!mastra || typeof mastra.getMCPServerById !== "function") {
      throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Mastra instance or getMCPServerById method not available" });
    }
    const server = mastra.getMCPServerById(serverId);
    if (!server) {
      throw new chunk64ITUOXI_cjs.HTTPException(404, { message: `MCP server with ID '${serverId}' not found` });
    }
    if (typeof server.readResource !== "function") {
      throw new chunk64ITUOXI_cjs.HTTPException(501, { message: `Server '${serverId}' does not support reading resources` });
    }
    try {
      return await server.readResource(uri);
    } catch (error) {
      const message = error instanceof Error ? error.message : String(error);
      if (message.includes("not found") || message.includes("not configured")) {
        throw new chunk64ITUOXI_cjs.HTTPException(404, { message: `Resource '${uri}' not found on server '${serverId}'` });
      }
      throw error;
    }
  }
});
var MCP_HTTP_TRANSPORT_ROUTE = chunkAZORAK4H_cjs.createRoute({
  method: "ALL",
  path: "/mcp/:serverId/mcp",
  responseType: "mcp-http",
  pathParamSchema: chunkBNR5CIPX_cjs.mcpServerIdPathParams,
  summary: "MCP HTTP Transport",
  description: "Streamable HTTP transport endpoint for MCP protocol communication",
  tags: ["MCP"],
  requiresAuth: true,
  handler: async ({ mastra, serverId }) => {
    if (!mastra || typeof mastra.getMCPServerById !== "function") {
      throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Mastra instance or getMCPServerById method not available" });
    }
    const server = mastra.getMCPServerById(serverId);
    if (!server) {
      throw new chunk64ITUOXI_cjs.HTTPException(404, { message: `MCP server '${serverId}' not found` });
    }
    return {
      server,
      httpPath: `/mcp/${serverId}/mcp`
    };
  }
});
var MCP_SSE_TRANSPORT_ROUTE = chunkAZORAK4H_cjs.createRoute({
  method: "ALL",
  path: "/mcp/:serverId/sse",
  responseType: "mcp-sse",
  pathParamSchema: chunkBNR5CIPX_cjs.mcpServerIdPathParams,
  summary: "MCP SSE Transport",
  description: "SSE transport endpoint for MCP protocol communication",
  tags: ["MCP"],
  requiresAuth: true,
  handler: async ({ mastra, serverId }) => {
    if (!mastra || typeof mastra.getMCPServerById !== "function") {
      throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Mastra instance or getMCPServerById method not available" });
    }
    const server = mastra.getMCPServerById(serverId);
    if (!server) {
      throw new chunk64ITUOXI_cjs.HTTPException(404, { message: `MCP server '${serverId}' not found` });
    }
    return {
      server,
      ssePath: `/mcp/${serverId}/sse`,
      messagePath: `/mcp/${serverId}/messages`
    };
  }
});
var MCP_SSE_MESSAGES_ROUTE = chunkAZORAK4H_cjs.createRoute({
  method: "POST",
  path: "/mcp/:serverId/messages",
  responseType: "mcp-sse",
  pathParamSchema: chunkBNR5CIPX_cjs.mcpServerIdPathParams,
  summary: "MCP SSE Messages",
  description: "Message endpoint for SSE transport (posts messages to active SSE streams)",
  tags: ["MCP"],
  requiresAuth: true,
  handler: MCP_SSE_TRANSPORT_ROUTE.handler
});

exports.EXECUTE_MCP_SERVER_TOOL_ROUTE = EXECUTE_MCP_SERVER_TOOL_ROUTE;
exports.GET_MCP_SERVER_DETAIL_ROUTE = GET_MCP_SERVER_DETAIL_ROUTE;
exports.GET_MCP_SERVER_TOOL_DETAIL_ROUTE = GET_MCP_SERVER_TOOL_DETAIL_ROUTE;
exports.LIST_MCP_SERVERS_ROUTE = LIST_MCP_SERVERS_ROUTE;
exports.LIST_MCP_SERVER_RESOURCES_ROUTE = LIST_MCP_SERVER_RESOURCES_ROUTE;
exports.LIST_MCP_SERVER_TOOLS_ROUTE = LIST_MCP_SERVER_TOOLS_ROUTE;
exports.MCP_HTTP_TRANSPORT_ROUTE = MCP_HTTP_TRANSPORT_ROUTE;
exports.MCP_SSE_MESSAGES_ROUTE = MCP_SSE_MESSAGES_ROUTE;
exports.MCP_SSE_TRANSPORT_ROUTE = MCP_SSE_TRANSPORT_ROUTE;
exports.READ_MCP_SERVER_RESOURCE_ROUTE = READ_MCP_SERVER_RESOURCE_ROUTE;
exports.mcp_exports = mcp_exports;
//# sourceMappingURL=chunk-XHA6OBT3.cjs.map
//# sourceMappingURL=chunk-XHA6OBT3.cjs.map