vlogesh517w/ui-framework-mcp
0
1import express from "express";2import { McpServer } from "@modelcontextprotocol/server";3import { createMcpExpressApp } from "@modelcontextprotocol/express";4import z from "zod";5 6// ✅ CHANGE THIS to your RAG API base7const RAG_QUERY_URL = "https://vlogesh517w-ui-framework-doc.hf.space/query";8 9const app = express();10 11// This creates an express app with MCP-safe defaults (host header validation etc.)12const mcpApp = createMcpExpressApp(); // :contentReference[oaicite:1]{index=1}13 14// Create MCP server15const server = new McpServer({16 name: "ui-framework-rag-tools",17 version: "1.0.0"18});19 20// Register a tool that calls your RAG /query endpoint21server.registerTool(22 "rag_query",23 {24 title: "RAG Query",25 description: "Query the UI framework docs via RAG (hosted API).",26 inputSchema: z.object({27 question: z.string().describe("The question to ask the documentation"),28 top_k: z.number().optional().default(5).describe("How many chunks to retrieve")29 })30 },31 async ({ question, top_k }) => {32 const resp = await fetch(RAG_QUERY_URL, {33 method: "POST",34 headers: { "Content-Type": "application/json" },35 body: JSON.stringify({ question, top_k })36 });37 38 if (!resp.ok) {39 const text = await resp.text();40 return {41 content: [{ type: "text", text: `RAG API error (${resp.status}): ${text}` }],42 isError: true43 };44 }45 46 const data = await resp.json();47 48 // Format for Copilot49 const lines = [];50 lines.push(`Question: ${data.question}`);51 lines.push("");52 lines.push("Top matches:");53 for (const m of data.matches || []) {54 lines.push(`- file=${m.file}, chunk=${m.chunk}, distance=${m.distance}`);55 lines.push(` ${String(m.text).slice(0, 400)}...`);56 }57 58 return {59 content: [{ type: "text", text: lines.join("\n") }]60 };61 }62);63 64// Mount MCP endpoint65// Most clients will connect to this URL66mcpApp.use("/", server.server); // attach MCP server to express middleware67 68app.get("/", (_req, res) => res.status(200).send("OK: MCP server is running"));69app.use("/", mcpApp);70 71// HF Spaces uses PORT env var72const port = process.env.PORT || 7860;73app.listen(port, "0.0.0.0", () => {74 console.log(`MCP server listening on port ${port}`);75});76 