Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update instructions for running the server with HTTP SSE #169

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,15 +224,37 @@ const server = new McpServer({

const app = express();

let transportMap = new Map();

app.get("/sse", async (req, res) => {
const transport = new SSEServerTransport("/messages", res);
const sessionId = transport.sessionId;

// Set the onclose handler to remove the transport from the transportMap
transport.onclose = () => {
transportMap.delete(sessionId);
console.log(`Transport ${sessionId} has been closed.`);
};

transportMap.set(sessionId, transport);
await server.connect(transport);
});

app.post("/messages", async (req, res) => {
// Note: to support multiple simultaneous connections, these messages will
// need to be routed to a specific matching transport. (This logic isn't
// implemented here, for simplicity.)

const sessionId = req.query.sessionId;
if (!sessionId) {
return res.status(400).json({ error: "SessionId is not found" });
}

const transport = transportMap.get(sessionId);
if (!transport) {
return res.redirect("/sse");
}

await transport.handlePostMessage(req, res);
});

Expand Down