gcharanteja commited on
Commit ·
8e07ffa
1
Parent(s): 0c3ae10
pal
Browse files
server.js
CHANGED
|
@@ -1,187 +1,15 @@
|
|
| 1 |
-
|
| 2 |
-
import fs from "node:fs";
|
| 3 |
-
import path from "node:path";
|
| 4 |
-
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
|
| 5 |
-
import { SSEServerTransport } from "@modelcontextprotocol/sdk/server/sse.js";
|
| 6 |
-
import {
|
| 7 |
-
ListToolsRequestSchema,
|
| 8 |
-
CallToolRequestSchema,
|
| 9 |
-
} from "@modelcontextprotocol/sdk/types.js";
|
| 10 |
-
import { vectorTools, handleVectorTool } from "./tools/vectorTool.js";
|
| 11 |
-
import { curateTools, handleCurateTool } from "./tools/curateTool.js";
|
| 12 |
-
import { routerTools, handleRouterTool } from "./tools/routerTool.js";
|
| 13 |
|
| 14 |
-
const
|
| 15 |
-
const
|
| 16 |
-
const DATA_DIR = process.env.DATA_DIR || "/data";
|
| 17 |
-
const CONFIG_PATH = path.join(DATA_DIR, "config.json");
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
console.error(`Config file not found at ${CONFIG_PATH}`);
|
| 23 |
-
process.exit(1);
|
| 24 |
-
}
|
| 25 |
-
const config = JSON.parse(fs.readFileSync(CONFIG_PATH, "utf8"));
|
| 26 |
-
return config;
|
| 27 |
-
} catch (error) {
|
| 28 |
-
console.error(`Failed to load config from ${CONFIG_PATH}:`, error.message);
|
| 29 |
-
process.exit(1);
|
| 30 |
-
}
|
| 31 |
-
}
|
| 32 |
-
|
| 33 |
-
const config = loadConfig();
|
| 34 |
-
|
| 35 |
-
function createMcpServer() {
|
| 36 |
-
const server = new Server(
|
| 37 |
-
{
|
| 38 |
-
name: "mcponly-server",
|
| 39 |
-
version: "1.0.0",
|
| 40 |
-
},
|
| 41 |
-
{
|
| 42 |
-
capabilities: {
|
| 43 |
-
tools: {},
|
| 44 |
-
},
|
| 45 |
-
},
|
| 46 |
-
);
|
| 47 |
-
|
| 48 |
-
server.setRequestHandler(ListToolsRequestSchema, async () => ({
|
| 49 |
-
tools: [...routerTools, ...curateTools, ...vectorTools],
|
| 50 |
-
}));
|
| 51 |
-
|
| 52 |
-
server.setRequestHandler(CallToolRequestSchema, async (request) => {
|
| 53 |
-
const { name, arguments: args = {} } = request.params;
|
| 54 |
-
|
| 55 |
-
const routerResult = await handleRouterTool(name, args, config);
|
| 56 |
-
if (routerResult) return routerResult;
|
| 57 |
-
|
| 58 |
-
const curateResult = await handleCurateTool(name, args, config);
|
| 59 |
-
if (curateResult) return curateResult;
|
| 60 |
-
|
| 61 |
-
const vectorResult = await handleVectorTool(name, args);
|
| 62 |
-
if (vectorResult) return vectorResult;
|
| 63 |
-
|
| 64 |
-
return {
|
| 65 |
-
content: [{ type: "text", text: `Unknown tool: ${name}` }],
|
| 66 |
-
isError: true,
|
| 67 |
-
};
|
| 68 |
-
});
|
| 69 |
-
|
| 70 |
-
return server;
|
| 71 |
-
}
|
| 72 |
-
|
| 73 |
-
function sendJson(res, statusCode, payload) {
|
| 74 |
-
res.writeHead(statusCode, { "Content-Type": "application/json" });
|
| 75 |
-
res.end(JSON.stringify(payload));
|
| 76 |
-
}
|
| 77 |
-
|
| 78 |
-
function readBody(req) {
|
| 79 |
-
return new Promise((resolve, reject) => {
|
| 80 |
-
const chunks = [];
|
| 81 |
-
|
| 82 |
-
req.on("data", (chunk) => chunks.push(chunk));
|
| 83 |
-
req.on("end", () => {
|
| 84 |
-
if (chunks.length === 0) {
|
| 85 |
-
resolve(undefined);
|
| 86 |
-
return;
|
| 87 |
-
}
|
| 88 |
-
|
| 89 |
-
const text = Buffer.concat(chunks).toString("utf8");
|
| 90 |
-
|
| 91 |
-
try {
|
| 92 |
-
resolve(JSON.parse(text));
|
| 93 |
-
} catch (error) {
|
| 94 |
-
reject(new Error("Invalid JSON body"));
|
| 95 |
-
}
|
| 96 |
-
});
|
| 97 |
-
req.on("error", reject);
|
| 98 |
-
});
|
| 99 |
-
}
|
| 100 |
-
|
| 101 |
-
async function main() {
|
| 102 |
-
const transports = new Map();
|
| 103 |
-
|
| 104 |
-
const httpServer = http.createServer(async (req, res) => {
|
| 105 |
-
if (!req.url) {
|
| 106 |
-
sendJson(res, 400, {
|
| 107 |
-
jsonrpc: "2.0",
|
| 108 |
-
error: { code: -32600, message: "Missing request URL" },
|
| 109 |
-
id: null,
|
| 110 |
-
});
|
| 111 |
-
return;
|
| 112 |
-
}
|
| 113 |
-
|
| 114 |
-
const hostHeader = req.headers.host || `${HOST}:${PORT}`;
|
| 115 |
-
const url = new URL(req.url, `http://${hostHeader}`);
|
| 116 |
-
|
| 117 |
-
if (req.method === "GET" && url.pathname === "/health") {
|
| 118 |
-
sendJson(res, 200, { status: "ok" });
|
| 119 |
-
return;
|
| 120 |
-
}
|
| 121 |
-
|
| 122 |
-
if (req.method === "GET" && url.pathname === "/sse") {
|
| 123 |
-
const transport = new SSEServerTransport("/messages", res);
|
| 124 |
-
transports.set(transport.sessionId, transport);
|
| 125 |
-
|
| 126 |
-
transport.onclose = () => {
|
| 127 |
-
transports.delete(transport.sessionId);
|
| 128 |
-
};
|
| 129 |
-
|
| 130 |
-
res.on("close", () => {
|
| 131 |
-
transports.delete(transport.sessionId);
|
| 132 |
-
});
|
| 133 |
-
|
| 134 |
-
await createMcpServer().connect(transport);
|
| 135 |
-
return;
|
| 136 |
-
}
|
| 137 |
-
|
| 138 |
-
if (req.method === "POST" && url.pathname === "/messages") {
|
| 139 |
-
const sessionId = url.searchParams.get("sessionId");
|
| 140 |
-
|
| 141 |
-
if (!sessionId) {
|
| 142 |
-
sendJson(res, 400, {
|
| 143 |
-
jsonrpc: "2.0",
|
| 144 |
-
error: { code: -32600, message: "Missing sessionId query parameter" },
|
| 145 |
-
id: null,
|
| 146 |
-
});
|
| 147 |
-
return;
|
| 148 |
-
}
|
| 149 |
-
|
| 150 |
-
const transport = transports.get(sessionId);
|
| 151 |
-
|
| 152 |
-
if (!transport) {
|
| 153 |
-
sendJson(res, 404, {
|
| 154 |
-
jsonrpc: "2.0",
|
| 155 |
-
error: { code: -32000, message: "Session not found" },
|
| 156 |
-
id: null,
|
| 157 |
-
});
|
| 158 |
-
return;
|
| 159 |
-
}
|
| 160 |
-
|
| 161 |
-
const body = await readBody(req);
|
| 162 |
-
await transport.handlePostMessage(req, res, body);
|
| 163 |
-
return;
|
| 164 |
-
}
|
| 165 |
-
|
| 166 |
-
sendJson(res, 404, {
|
| 167 |
-
jsonrpc: "2.0",
|
| 168 |
-
error: { code: -32601, message: "Not found" },
|
| 169 |
-
id: null,
|
| 170 |
-
});
|
| 171 |
-
});
|
| 172 |
-
|
| 173 |
-
httpServer.listen(PORT, HOST, () => {
|
| 174 |
-
console.log(`MCP SSE server listening on http://${HOST}:${PORT}`);
|
| 175 |
-
console.log(`SSE endpoint: http://${HOST}:${PORT}/sse`);
|
| 176 |
-
console.log(`POST endpoint: http://${HOST}:${PORT}/messages`);
|
| 177 |
-
});
|
| 178 |
-
|
| 179 |
-
process.on("SIGINT", () => {
|
| 180 |
-
httpServer.close(() => process.exit(0));
|
| 181 |
-
});
|
| 182 |
-
}
|
| 183 |
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
});
|
|
|
|
|
|
|
|
|
| 1 |
+
const express = require('express');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
+
const app = express();
|
| 4 |
+
const PORT = process.env.PORT || 3000;
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
app.get('/hello', (req, res) => {
|
| 7 |
+
res.send('hello world pal');
|
| 8 |
+
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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