-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Closed as not planned
Labels
bugSomething isn't workingSomething isn't workingneeds confirmationNeeds confirmation that the PR is actually required or needed.Needs confirmation that the PR is actually required or needed.
Description
Describe the bug
I write a demo for get document, but the sse server not work well.
To Reproduce
this is my code
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
import {
CallToolRequestSchema,
JSONRPCMessage,
ListResourcesRequestSchema,
ListToolsRequestSchema,
ReadResourceRequestSchema,
} from "@modelcontextprotocol/sdk/types.js";
import { SSEServerTransport } from "@modelcontextprotocol/sdk/server/sse.js";
import express from "express";
// import { getDocument } from './fs-docs.js'
const app = express();
const server = new Server(
{
name: "boiling-insights",
version: "0.1.0",
},
{
capabilities: {
resources: {},
tools: {},
// prompts: {},
templates: {},
},
}
);
const SCHEMA_PATH = "schema";
const PORT = 8808;
server.setRequestHandler(ReadResourceRequestSchema, async (request) => {
const resourceUrl = new URL(request.params.uri);
const pathComponents = resourceUrl.pathname.split("/");
const comps = [...pathComponents];
const schema = pathComponents.pop();
const document_id = pathComponents.pop();
if (schema !== SCHEMA_PATH || !document_id) {
throw new Error("Invalid resource URI");
}
try {
// get document content
const result = await getDocument(document_id);
return {
contents: [
{
uri: request.params.uri,
mimeType: "text/markdown",
text: result,
},
],
};
} catch (err) {
console.error(err);
throw err;
}
});
;
server.setRequestHandler(CallToolRequestSchema, async (request) => {
try {
const tool = request.params.name;
if (tool === "getDocument") {
// console.log(request.params);
const result = await getDocument(request.params.arguments?.sql as string);
return {
content: [{ type: "text", text: result }],
isError: false,
};
} else {
throw new Error(`Unknown tool: ${request.params.name}`);
}
} catch (err) {
console.error(err);
throw err;
}
});
export async function runMCPServer() {
let transport: SSEServerTransport;
app.get("/sse", async (req, res) => {
console.log("--> Received connection:", req.url);
transport = new SSEServerTransport("/message", res);
console.log("New SSE connection.");
await server.connect(transport);
const _onMsg = transport.onmessage; // original hook
const _onClose = transport.onclose;
const _onErr = transport.onerror;
transport.onmessage = (msg: JSONRPCMessage) => {
console.log(msg);
if (_onMsg) _onMsg(msg);
};
transport.onclose = () => {
console.log("Transport closed.");
if (_onClose) _onClose();
};
transport.onerror = (err) => {
console.error(err);
if (_onErr) _onErr(err);
};
server.onclose = async () => {
//clearInterval(updateInterval);
await server.close();
console.log("SSE connection closed.");
};
});
app.post("/message", async (req, res) => {
console.log("--> Received message (post)");
if (transport?.handlePostMessage) {
await transport.handlePostMessage(req, res);
} else {
console.error("transport.handlePostMessage UNDEFINED!");
}
console.log("<--", res.statusCode, res.statusMessage);
});
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
}
runMCPServer().catch(console.error);this is the cursor's error

junerver and Cupnfish
Metadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't workingneeds confirmationNeeds confirmation that the PR is actually required or needed.Needs confirmation that the PR is actually required or needed.