Skip to content

Commit 1781d31

Browse files
committed
readme suggested changes
1 parent 864aa21 commit 1781d31

File tree

1 file changed

+28
-28
lines changed

1 file changed

+28
-28
lines changed

README.md

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -214,19 +214,15 @@ For remote servers, set up a Streamable HTTP transport that handles both client
214214

215215
#### With Session Management
216216

217+
In some cases, servers need to be stateful. This is achieved by [session management](https://modelcontextprotocol.io/specification/2025-03-26/basic/transports#session-management).
218+
217219
```typescript
218220
import express from "express";
219221
import { randomUUID } from "node:crypto";
220222
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
221223
import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
222224
import { InMemoryEventStore } from "@modelcontextprotocol/sdk/inMemory.js";
223225

224-
const server = new McpServer({
225-
name: "example-server",
226-
version: "1.0.0"
227-
});
228-
229-
// ... set up server resources, tools, and prompts ...
230226

231227
const app = express();
232228
app.use(express.json());
@@ -261,6 +257,12 @@ app.post('/mcp', async (req, res) => {
261257
delete transports[transport.sessionId];
262258
}
263259
};
260+
const server = new McpServer({
261+
name: "example-server",
262+
version: "1.0.0"
263+
});
264+
265+
// ... set up server resources, tools, and prompts ...
264266

265267
// Connect to the MCP server
266268
await server.connect(transport);
@@ -281,30 +283,23 @@ app.post('/mcp', async (req, res) => {
281283
await transport.handleRequest(req, res, req.body);
282284
});
283285

284-
// Handle GET requests for server-to-client notifications via SSE
285-
app.get('/mcp', async (req, res) => {
286+
// Reusable handler for GET and DELETE requests
287+
const handleSessionRequest = async (req: express.Request, res: express.Response) => {
286288
const sessionId = req.headers['mcp-session-id'] as string | undefined;
287289
if (!sessionId || !transports[sessionId]) {
288290
res.status(400).send('Invalid or missing session ID');
289291
return;
290292
}
291-
292-
// Support resumability with Last-Event-ID header
293+
293294
const transport = transports[sessionId];
294295
await transport.handleRequest(req, res);
295-
});
296+
};
297+
298+
// Handle GET requests for server-to-client notifications via SSE
299+
app.get('/mcp', handleSessionRequest);
296300

297301
// Handle DELETE requests for session termination
298-
app.delete('/mcp', async (req, res) => {
299-
const sessionId = req.headers['mcp-session-id'] as string | undefined;
300-
if (!sessionId || !transports[sessionId]) {
301-
res.status(400).send('Invalid or missing session ID');
302-
return;
303-
}
304-
305-
const transport = transports[sessionId];
306-
await transport.handleRequest(req, res);
307-
});
302+
app.delete('/mcp', handleSessionRequest);
308303

309304
app.listen(3000);
310305
```
@@ -698,7 +693,7 @@ This setup allows you to:
698693

699694
### Backwards Compatibility
700695

701-
The SDK provides support for backwards compatibility between different protocol versions:
696+
Clients and servers with StreamableHttp tranport can maintain [backwards compatibility](https://modelcontextprotocol.io/specification/2025-03-26/basic/transports#backwards-compatibility) with the deprecated HTTP+SSE transport (from protocol version 2024-11-05) as follows
702697

703698
#### Client-Side Compatibility
704699

@@ -708,21 +703,26 @@ For clients that need to work with both Streamable HTTP and older SSE servers:
708703
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
709704
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
710705
import { SSEClientTransport } from "@modelcontextprotocol/sdk/client/sse.js";
711-
712-
// First try connecting with Streamable HTTP transport
706+
let client: Client|undefined = undefined
707+
const baseUrl = new URL(url);
713708
try {
709+
client = new Client({
710+
name: 'streamable-http-client',
711+
version: '1.0.0'
712+
});
714713
const transport = new StreamableHTTPClientTransport(
715-
new URL("http://localhost:3000/mcp")
714+
new URL(baseUrl)
716715
);
717716
await client.connect(transport);
718717
console.log("Connected using Streamable HTTP transport");
719718
} catch (error) {
720719
// If that fails with a 4xx error, try the older SSE transport
721720
console.log("Streamable HTTP connection failed, falling back to SSE transport");
722-
const sseTransport = new SSEClientTransport({
723-
sseUrl: new URL("http://localhost:3000/sse"),
724-
postUrl: new URL("http://localhost:3000/messages")
721+
client = new Client({
722+
name: 'sse-client',
723+
version: '1.0.0'
725724
});
725+
const sseTransport = new SSEClientTransport(baseUrl);
726726
await client.connect(sseTransport);
727727
console.log("Connected using SSE transport");
728728
}

0 commit comments

Comments
 (0)