add GET to examples servers

This commit is contained in:
ihrpr 2025-04-10 15:28:39 +01:00
commit 5c5a33021c
No known key found for this signature in database
4 changed files with 22 additions and 3 deletions

View file

@ -131,7 +131,6 @@ export class StreamableHTTPClientTransport implements Transport {
`Failed to open SSE stream: ${response.statusText}`,
);
}
// Successful connection, handle the SSE stream as a standalone listener
this._handleSseStream(response.body);
} catch (error) {

View file

@ -25,7 +25,7 @@ async function main(): Promise<void> {
const transport = new StreamableHTTPClientTransport(
new URL('http://localhost:3000/mcp')
);
const supportsStandaloneSse = false;
let supportsStandaloneSse = false;
// Connect the client using the transport and initialize the server
await client.connect(transport);
@ -34,7 +34,7 @@ async function main(): Promise<void> {
console.log('Opening SSE stream to receive server notifications...');
try {
await transport.openSseStream();
const supportsStandaloneSse = false;
supportsStandaloneSse = true;
console.log('SSE stream established successfully. Waiting for notifications...');
}
catch (error) {

View file

@ -138,6 +138,13 @@ app.post('/mcp', async (req: Request, res: Response) => {
}
});
// Handle GET requests for SSE streams according to spec
app.get('/mcp', async (req: Request, res: Response) => {
// Since this is a very simple example, we don't support GET requests for this server
// The spec requires returning 405 Method Not Allowed in this case
res.status(405).set('Allow', 'POST').send('Method Not Allowed');
});
// Helper function to detect initialize requests
function isInitializeRequest(body: unknown): boolean {
if (Array.isArray(body)) {

View file

@ -174,6 +174,19 @@ app.post('/mcp', async (req: Request, res: Response) => {
}
});
// Handle GET requests for SSE streams (now using built-in support from StreamableHTTP)
app.get('/mcp', async (req: Request, res: Response) => {
const sessionId = req.headers['mcp-session-id'] as string | undefined;
if (!sessionId || !transports[sessionId]) {
res.status(400).send('Invalid or missing session ID');
return;
}
console.log(`Establishing SSE stream for session ${sessionId}`);
const transport = transports[sessionId];
await transport.handleRequest(req, res);
});
// Helper function to detect initialize requests
function isInitializeRequest(body: unknown): boolean {
if (Array.isArray(body)) {