feat(mcps): unify SSE/Stdio transport and fix handshake timeouts

This commit is contained in:
2026-03-05 12:04:19 +01:00
parent 29423123b3
commit daa2750f89
5 changed files with 141 additions and 75 deletions

View File

@@ -7,6 +7,7 @@ import {
Tool,
} from "@modelcontextprotocol/sdk/types.js";
import axios from "axios";
import https from "https";
const SERPBEAR_BASE_URL = process.env.SERPBEAR_BASE_URL || "https://serpbear.infra.mintel.me";
const SERPBEAR_API_KEY = process.env.SERPBEAR_API_KEY;
@@ -18,8 +19,12 @@ if (!SERPBEAR_API_KEY) {
const serpbearClient = axios.create({
baseURL: `${SERPBEAR_BASE_URL}/api`,
headers: { apiKey: SERPBEAR_API_KEY },
httpsAgent: new https.Agent({
rejectUnauthorized: false,
}),
});
// --- Tool Definitions ---
const LIST_DOMAINS_TOOL: Tool = {
name: "serpbear_list_domains",
@@ -199,26 +204,39 @@ server.setRequestHandler(CallToolRequestSchema, async (request) => {
// --- Express / SSE Server ---
async function run() {
const app = express();
let transport: SSEServerTransport | null = null;
const isStdio = process.argv.includes('--stdio');
app.get('/sse', async (req, res) => {
console.error('New SSE connection established');
transport = new SSEServerTransport('/message', res);
if (isStdio) {
const { StdioServerTransport } = await import('@modelcontextprotocol/sdk/server/stdio.js');
const transport = new StdioServerTransport();
await server.connect(transport);
});
console.error('SerpBear MCP server is running on stdio');
} else {
const app = express();
let transport: SSEServerTransport | null = null;
app.post('/message', async (req, res) => {
if (!transport) { res.status(400).send('No active SSE connection'); return; }
await transport.handlePostMessage(req, res);
});
app.get('/sse', async (req, res) => {
console.error('New SSE connection established');
transport = new SSEServerTransport('/message', res);
await server.connect(transport);
});
const PORT = process.env.SERPBEAR_MCP_PORT || 3004;
app.listen(PORT, () => {
console.error(`SerpBear MCP server running on http://localhost:${PORT}/sse`);
});
app.post('/message', async (req, res) => {
if (!transport) {
res.status(400).send('No active SSE connection');
return;
}
await transport.handlePostMessage(req, res);
});
const PORT = process.env.SERPBEAR_MCP_PORT || 3004;
app.listen(PORT, () => {
console.error(`SerpBear MCP server running on http://localhost:${PORT}/sse`);
});
}
}
run().catch((err) => {
console.error("Fatal error:", err);
process.exit(1);