Spaces:
Sleeping
Sleeping
Backup Agent commited on
Commit ·
ba718eb
0
Parent(s):
Initialize SSE MCP Host
Browse files- Dockerfile +13 -0
- index.js +85 -0
- package.json +14 -0
Dockerfile
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM node:20-slim
|
| 2 |
+
|
| 3 |
+
WORKDIR /app
|
| 4 |
+
|
| 5 |
+
COPY package.json ./
|
| 6 |
+
RUN npm install --production
|
| 7 |
+
|
| 8 |
+
COPY index.js ./
|
| 9 |
+
|
| 10 |
+
EXPOSE 7860
|
| 11 |
+
ENV PORT=7860
|
| 12 |
+
|
| 13 |
+
CMD ["node", "index.js"]
|
index.js
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import express from "express";
|
| 2 |
+
import { Server } from "@modelcontextprotocol/sdk/server/mcp.js";
|
| 3 |
+
import { SSEServerTransport } from "@modelcontextprotocol/sdk/server/sse.js";
|
| 4 |
+
import { ListToolsRequestSchema, CallToolRequestSchema } from "@modelcontextprotocol/sdk/types.js";
|
| 5 |
+
|
| 6 |
+
const app = express();
|
| 7 |
+
const PORT = process.env.PORT || 7860;
|
| 8 |
+
|
| 9 |
+
const server = new Server(
|
| 10 |
+
{
|
| 11 |
+
name: "mcp-cloud-host",
|
| 12 |
+
version: "1.0.0",
|
| 13 |
+
},
|
| 14 |
+
{
|
| 15 |
+
capabilities: {
|
| 16 |
+
tools: {},
|
| 17 |
+
},
|
| 18 |
+
}
|
| 19 |
+
);
|
| 20 |
+
|
| 21 |
+
// Define tools
|
| 22 |
+
const calculateTool = {
|
| 23 |
+
name: "calculate",
|
| 24 |
+
description: "Evaluate a mathematical expression in JS safely",
|
| 25 |
+
inputSchema: {
|
| 26 |
+
type: "object",
|
| 27 |
+
properties: {
|
| 28 |
+
expression: {
|
| 29 |
+
type: "string",
|
| 30 |
+
description: "Math expression, e.g. 'Math.pow(2, 8) + 15'"
|
| 31 |
+
}
|
| 32 |
+
},
|
| 33 |
+
required: ["expression"]
|
| 34 |
+
}
|
| 35 |
+
};
|
| 36 |
+
|
| 37 |
+
server.setRequestHandler(ListToolsRequestSchema, async () => {
|
| 38 |
+
return {
|
| 39 |
+
tools: [calculateTool]
|
| 40 |
+
};
|
| 41 |
+
});
|
| 42 |
+
|
| 43 |
+
server.setRequestHandler(CallToolRequestSchema, async (request) => {
|
| 44 |
+
if (request.params.name === "calculate") {
|
| 45 |
+
const expr = request.params.arguments?.expression;
|
| 46 |
+
try {
|
| 47 |
+
if (/[^0-9\+\-\*\/\(\)\s\.\,Mathsqrtpow]/i.test(expr)) {
|
| 48 |
+
return {
|
| 49 |
+
content: [{ type: "text", text: "Error: Unauthorized characters in math expression" }],
|
| 50 |
+
isError: true
|
| 51 |
+
};
|
| 52 |
+
}
|
| 53 |
+
const result = Function(`"use strict"; return (${expr})`)();
|
| 54 |
+
return {
|
| 55 |
+
content: [{ type: "text", text: String(result) }]
|
| 56 |
+
};
|
| 57 |
+
} catch (e) {
|
| 58 |
+
return {
|
| 59 |
+
content: [{ type: "text", text: `Error: ${e.message}` }],
|
| 60 |
+
isError: true
|
| 61 |
+
};
|
| 62 |
+
}
|
| 63 |
+
}
|
| 64 |
+
throw new Error("Tool not found");
|
| 65 |
+
});
|
| 66 |
+
|
| 67 |
+
let transport = null;
|
| 68 |
+
|
| 69 |
+
app.get("/sse", (req, res) => {
|
| 70 |
+
console.log("New SSE connection established");
|
| 71 |
+
transport = new SSEServerTransport("/messages", res);
|
| 72 |
+
server.connect(transport).catch(console.error);
|
| 73 |
+
});
|
| 74 |
+
|
| 75 |
+
app.post("/messages", express.json(), async (req, res) => {
|
| 76 |
+
if (transport) {
|
| 77 |
+
await transport.handlePostMessage(req, res);
|
| 78 |
+
} else {
|
| 79 |
+
res.status(400).send("No active SSE transport connection");
|
| 80 |
+
}
|
| 81 |
+
});
|
| 82 |
+
|
| 83 |
+
app.listen(PORT, () => {
|
| 84 |
+
console.log(`SSE MCP Host listening on port ${PORT}`);
|
| 85 |
+
});
|
package.json
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "mcp-cloud-host",
|
| 3 |
+
"version": "1.0.0",
|
| 4 |
+
"description": "SSE MCP Host for 4-Space Architecture",
|
| 5 |
+
"main": "index.js",
|
| 6 |
+
"type": "module",
|
| 7 |
+
"scripts": {
|
| 8 |
+
"start": "node index.js"
|
| 9 |
+
},
|
| 10 |
+
"dependencies": {
|
| 11 |
+
"@modelcontextprotocol/sdk": "^1.0.1",
|
| 12 |
+
"express": "^4.19.2"
|
| 13 |
+
}
|
| 14 |
+
}
|