import { Server } from "@modelcontextprotocol/sdk/server/index.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { ListToolsRequestSchema, CallToolRequestSchema } from "@modelcontextprotocol/sdk/types.js";
import { ListComponentsTool, ListComponentsInputSchema } from "./tools/list-components.js";
import { GetComponentTool, GetComponentInputSchema } from "./tools/get-component.js";
import { composeTool } from "./tools/compose.js";
class GlideUIMCPServer {
  server;
  listComponentsTool;
  getComponentTool;
  constructor() {
    this.server = new Server(
      {
        name: "glide-ui-mcp-server",
        version: "0.1.0"
      },
      {
        capabilities: {
          tools: {}
        }
      }
    );
    this.listComponentsTool = new ListComponentsTool();
    this.getComponentTool = new GetComponentTool();
    this.setupHandlers();
  }
  setupHandlers() {
    this.server.setRequestHandler(ListToolsRequestSchema, async () => {
      const tools = [
        {
          name: "list_components",
          description: "List all available UI components from the Glide UI library",
          inputSchema: {
            type: "object",
            properties: {
              category: {
                type: "string",
                description: "Filter components by category"
              },
              detailed: {
                type: "boolean",
                description: "Include detailed component information"
              }
            }
          }
        },
        {
          name: "get_component",
          description: "Get detailed information and source code for a specific component",
          inputSchema: {
            type: "object",
            properties: {
              name: {
                type: "string",
                description: 'Component name (e.g., "button" or "charts/area-chart")'
              }
            },
            required: ["name"]
          }
        },
        {
          name: "compose_ui",
          description: "Compose Glide UI components from natural language",
          inputSchema: {
            type: "object",
            properties: {
              prompt: {
                type: "string",
                description: "Natural language description of the component to create"
              },
              includeTypes: {
                type: "boolean",
                description: "Include TypeScript interfaces in output",
                default: true
              }
            },
            required: ["prompt"]
          }
        }
      ];
      return { tools };
    });
    this.server.setRequestHandler(CallToolRequestSchema, async (request) => {
      const { name, arguments: args } = request.params;
      try {
        switch (name) {
          case "list_components": {
            const input = ListComponentsInputSchema.parse(args);
            const result = await this.listComponentsTool.execute(input);
            return {
              content: [
                {
                  type: "text",
                  text: JSON.stringify(result, null, 2)
                }
              ]
            };
          }
          case "get_component": {
            const input = GetComponentInputSchema.parse(args);
            const result = await this.getComponentTool.execute(input);
            return {
              content: [
                {
                  type: "text",
                  text: JSON.stringify(result, null, 2)
                }
              ]
            };
          }
          case "compose_ui": {
            if (!args) {
              throw new Error("Arguments required for compose_ui");
            }
            const result = await composeTool.handler(
              args
            );
            return {
              content: [
                {
                  type: "text",
                  text: JSON.stringify(result, null, 2)
                }
              ]
            };
          }
          default:
            throw new Error(`Unknown tool: ${name}`);
        }
      } catch (error) {
        return {
          content: [
            {
              type: "text",
              text: error instanceof Error ? `Error: ${error.message}` : "Unknown error"
            }
          ]
        };
      }
    });
  }
  async start() {
    const transport = new StdioServerTransport();
    await this.server.connect(transport);
    console.error("Glide UI MCP Server started");
  }
}
export {
  GlideUIMCPServer
};
