Spaces:
Paused
Paused
Update server.js
Browse files
server.js
CHANGED
|
@@ -7,8 +7,7 @@ import { CallToolRequestSchema, ListToolsRequestSchema } from "@modelcontextprot
|
|
| 7 |
|
| 8 |
const app = express();
|
| 9 |
app.use(cors());
|
| 10 |
-
//
|
| 11 |
-
app.use('/messages', express.json());
|
| 12 |
|
| 13 |
const PORT = process.env.PORT || 7860;
|
| 14 |
|
|
@@ -30,7 +29,8 @@ async function initBrowser() {
|
|
| 30 |
'--no-sandbox',
|
| 31 |
'--disable-setuid-sandbox',
|
| 32 |
'--disable-dev-shm-usage',
|
| 33 |
-
'--disable-gpu'
|
|
|
|
| 34 |
],
|
| 35 |
executablePath: process.env.PUPPETEER_EXECUTABLE_PATH || '/usr/bin/google-chrome-stable'
|
| 36 |
});
|
|
@@ -97,7 +97,7 @@ mcpServer.setRequestHandler(CallToolRequestSchema, async (request) => {
|
|
| 97 |
}
|
| 98 |
case "get_page_content": {
|
| 99 |
const text = await page.evaluate(() => document.body.innerText);
|
| 100 |
-
return { content: [{ type: "text", text: text.substring(0, 5000) }] };
|
| 101 |
}
|
| 102 |
case "click_element": {
|
| 103 |
const { selector } = request.params.arguments;
|
|
@@ -110,27 +110,59 @@ mcpServer.setRequestHandler(CallToolRequestSchema, async (request) => {
|
|
| 110 |
return { content: [{ type: "text", text: `Typed "${text}" into ${selector}` }] };
|
| 111 |
}
|
| 112 |
default:
|
| 113 |
-
throw new Error(
|
| 114 |
}
|
| 115 |
} catch (error) {
|
| 116 |
return { content: [{ type: "text", text: `Error executing tool: ${error.message}` }], isError: true };
|
| 117 |
}
|
| 118 |
});
|
| 119 |
|
|
|
|
|
|
|
|
|
|
| 120 |
// SSE Endpoint for MCP Connection
|
| 121 |
-
let transport;
|
| 122 |
app.get('/sse', async (req, res) => {
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
});
|
| 127 |
|
| 128 |
-
// Endpoint to receive messages from the client
|
| 129 |
app.post('/messages', async (req, res) => {
|
|
|
|
|
|
|
|
|
|
| 130 |
if (transport) {
|
| 131 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
} else {
|
| 133 |
-
|
|
|
|
| 134 |
}
|
| 135 |
});
|
| 136 |
|
|
|
|
| 7 |
|
| 8 |
const app = express();
|
| 9 |
app.use(cors());
|
| 10 |
+
// ❌ express.json() ব্যবহার করা যাবে না, কারণ MCP SDK নিজেই র ডেটা স্ট্রিম হ্যান্ডেল করে।
|
|
|
|
| 11 |
|
| 12 |
const PORT = process.env.PORT || 7860;
|
| 13 |
|
|
|
|
| 29 |
'--no-sandbox',
|
| 30 |
'--disable-setuid-sandbox',
|
| 31 |
'--disable-dev-shm-usage',
|
| 32 |
+
'--disable-gpu',
|
| 33 |
+
'--window-size=1280,800'
|
| 34 |
],
|
| 35 |
executablePath: process.env.PUPPETEER_EXECUTABLE_PATH || '/usr/bin/google-chrome-stable'
|
| 36 |
});
|
|
|
|
| 97 |
}
|
| 98 |
case "get_page_content": {
|
| 99 |
const text = await page.evaluate(() => document.body.innerText);
|
| 100 |
+
return { content: [{ type: "text", text: text.substring(0, 5000) }] };
|
| 101 |
}
|
| 102 |
case "click_element": {
|
| 103 |
const { selector } = request.params.arguments;
|
|
|
|
| 110 |
return { content: [{ type: "text", text: `Typed "${text}" into ${selector}` }] };
|
| 111 |
}
|
| 112 |
default:
|
| 113 |
+
throw new Error(`Tool ${request.params.name} not found`);
|
| 114 |
}
|
| 115 |
} catch (error) {
|
| 116 |
return { content: [{ type: "text", text: `Error executing tool: ${error.message}` }], isError: true };
|
| 117 |
}
|
| 118 |
});
|
| 119 |
|
| 120 |
+
// Store active SSE connections (Professional Session Management)
|
| 121 |
+
const transports = new Map();
|
| 122 |
+
|
| 123 |
// SSE Endpoint for MCP Connection
|
|
|
|
| 124 |
app.get('/sse', async (req, res) => {
|
| 125 |
+
try {
|
| 126 |
+
// Generate a unique session ID for each connection
|
| 127 |
+
const sessionId = Math.random().toString(36).substring(2, 15);
|
| 128 |
+
|
| 129 |
+
// Build dynamic absolute URL for Hugging Face Proxy routing
|
| 130 |
+
const protocol = req.headers['x-forwarded-proto'] || req.protocol;
|
| 131 |
+
const host = req.headers['x-forwarded-host'] || req.get('host');
|
| 132 |
+
const messagesEndpoint = `${protocol}://${host}/messages?sessionId=${sessionId}`;
|
| 133 |
+
|
| 134 |
+
const transport = new SSEServerTransport(messagesEndpoint, res);
|
| 135 |
+
transports.set(sessionId, transport);
|
| 136 |
+
|
| 137 |
+
await mcpServer.connect(transport);
|
| 138 |
+
console.log(`New MCP Client connected via SSE [Session: ${sessionId}]`);
|
| 139 |
+
|
| 140 |
+
// Cleanup on disconnect
|
| 141 |
+
req.on('close', () => {
|
| 142 |
+
console.log(`Client disconnected [Session: ${sessionId}]`);
|
| 143 |
+
transports.delete(sessionId);
|
| 144 |
+
});
|
| 145 |
+
|
| 146 |
+
} catch (error) {
|
| 147 |
+
console.error("Error establishing SSE connection:", error);
|
| 148 |
+
res.status(500).send("Internal Server Error");
|
| 149 |
+
}
|
| 150 |
});
|
| 151 |
|
| 152 |
+
// Endpoint to receive POST messages from the client
|
| 153 |
app.post('/messages', async (req, res) => {
|
| 154 |
+
const sessionId = req.query.sessionId;
|
| 155 |
+
const transport = transports.get(sessionId);
|
| 156 |
+
|
| 157 |
if (transport) {
|
| 158 |
+
try {
|
| 159 |
+
await transport.handlePostMessage(req, res);
|
| 160 |
+
} catch (error) {
|
| 161 |
+
console.error(`Error handling POST message for session ${sessionId}:`, error);
|
| 162 |
+
}
|
| 163 |
} else {
|
| 164 |
+
console.warn(`Received message for unknown session: ${sessionId}`);
|
| 165 |
+
res.status(404).send("Session not found or expired");
|
| 166 |
}
|
| 167 |
});
|
| 168 |
|