removes session-id generation/deps (#16)
This commit is contained in:
1 parent
96b50d2925
commit
05f66d611f
1 file changed
+12
-40
+12
-40
@@ -1,39 +1,14 @@
|
|||||||
import { randomUUID } from 'node:crypto';
|
|
||||||
import express, { type Request, type Response } from 'express';
|
import express, { type Request, type Response } from 'express';
|
||||||
import config from '../config.js';
|
import config from '../config.js';
|
||||||
import { server } from '../server.js';
|
import { server } from '../server.js';
|
||||||
import { registerSigIntHandler } from '../helpers.js';
|
|
||||||
import { isInitializeRequest } from '@modelcontextprotocol/sdk/types.js';
|
|
||||||
import { StreamableHTTPServerTransport } from '@modelcontextprotocol/sdk/server/streamableHttp.js';
|
import { StreamableHTTPServerTransport } from '@modelcontextprotocol/sdk/server/streamableHttp.js';
|
||||||
import { InMemoryEventStore } from '@modelcontextprotocol/sdk/examples/shared/inMemoryEventStore.js';
|
|
||||||
|
|
||||||
const getTransport = async (
|
const yieldGenericServerError = (res: Response) => {
|
||||||
req: Request,
|
res.status(500).json({
|
||||||
transports: Map<string, StreamableHTTPServerTransport>,
|
id: null,
|
||||||
sessionId?: string | string[]
|
jsonrpc: '2.0',
|
||||||
): Promise<StreamableHTTPServerTransport> => {
|
error: { code: -32603, message: 'Internal server error' },
|
||||||
// Belongs to an existing session; re-use the transport
|
|
||||||
if (sessionId && transports.has(sessionId as string)) {
|
|
||||||
return transports.get(sessionId as string) as StreamableHTTPServerTransport;
|
|
||||||
}
|
|
||||||
|
|
||||||
// No session ID and this is an initialization request; create a new transport
|
|
||||||
if (!sessionId && isInitializeRequest(req.body)) {
|
|
||||||
const transport = new StreamableHTTPServerTransport({
|
|
||||||
eventStore: new InMemoryEventStore(),
|
|
||||||
sessionIdGenerator: () => randomUUID(),
|
|
||||||
onsessioninitialized: (sessionId) => transports.set(sessionId, transport),
|
|
||||||
});
|
});
|
||||||
|
|
||||||
transport.onclose = () => transports.delete(transport.sessionId as string);
|
|
||||||
|
|
||||||
await server.connect(transport);
|
|
||||||
|
|
||||||
return transport;
|
|
||||||
}
|
|
||||||
|
|
||||||
// No session ID and this is not an initialization request; throw an error
|
|
||||||
throw new Error('Invalid request - no session ID or not initialization request');
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export const start = () => {
|
export const start = () => {
|
||||||
@@ -43,21 +18,21 @@ export const start = () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
const transports = new Map<string, StreamableHTTPServerTransport>();
|
|
||||||
|
|
||||||
app.use(express.json());
|
app.use(express.json());
|
||||||
|
|
||||||
app.all('/mcp', async (req: Request, res: Response) => {
|
app.all('/mcp', async (req: Request, res: Response) => {
|
||||||
try {
|
try {
|
||||||
const transport = await getTransport(req, transports, req.headers['mcp-session-id']);
|
const transport = new StreamableHTTPServerTransport({
|
||||||
|
// Setting to undefined will opt-out of session-id generation
|
||||||
|
sessionIdGenerator: undefined,
|
||||||
|
});
|
||||||
|
|
||||||
|
await server.connect(transport);
|
||||||
await transport.handleRequest(req, res, req.body);
|
await transport.handleRequest(req, res, req.body);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (!res.headersSent) {
|
if (!res.headersSent) {
|
||||||
res.status(500).json({
|
yieldGenericServerError(res);
|
||||||
id: null,
|
|
||||||
jsonrpc: '2.0',
|
|
||||||
error: { code: -32603, message: 'Internal server error' },
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -69,9 +44,6 @@ export const start = () => {
|
|||||||
app.listen(config.port, config.host, () => {
|
app.listen(config.port, config.host, () => {
|
||||||
console.error(`Server is running on http://${config.host}:${config.port}/mcp`);
|
console.error(`Server is running on http://${config.host}:${config.port}/mcp`);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Register a SIGINT handler to close all transports when the server is shut down
|
|
||||||
registerSigIntHandler(transports);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export default { start };
|
export default { start };
|
||||||
Reference in new issue
Block a user