You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The Model Context Protocol allows applications to provide context for LLMs in a standardized way, separating the concerns of providing context from the actual LLM interaction. This TypeScript SDK implements the full MCP specification, making it easy to:
29
30
30
31
- Build MCP clients that can connect to any MCP server
31
32
- Create MCP servers that expose resources, prompts and tools
32
-
- Use standard transports like stdio and SSE
33
+
- Use standard transports like stdio and Streamable HTTP
33
34
- Handle all MCP protocol messages and lifecycle events
34
35
35
36
## Installation
@@ -207,14 +208,18 @@ const transport = new StdioServerTransport();
207
208
awaitserver.connect(transport);
208
209
```
209
210
210
-
### HTTP with SSE
211
+
### Streamable HTTP
211
212
212
-
For remote servers, start a web server with a Server-Sent Events (SSE) endpoint, and a separate endpoint for the client to send its messages to:
213
+
For remote servers, set up a Streamable HTTP transport that handles both client requests and server-to-client notifications.
// ... set up server resources, tools, and prompts ...
327
+
328
+
const app =express();
329
+
app.use(express.json());
330
+
331
+
// Handle all MCP requests (GET, POST, DELETE) at a single endpoint
332
+
app.all('/mcp', async (req, res) => {
333
+
// Create a transport with sessionIdGenerator set to return undefined
334
+
// This disables session tracking completely
335
+
const transport =newStreamableHTTPServerTransport({
336
+
sessionIdGenerator: () =>undefined,
337
+
req,
338
+
res
339
+
});
340
+
341
+
// Connect to server and handle the request
342
+
awaitserver.connect(transport);
343
+
awaittransport.handleRequest(req, res);
344
+
});
345
+
346
+
app.listen(3000);
347
+
```
348
+
349
+
This stateless approach is useful for:
350
+
- Simple API wrappers
351
+
- RESTful scenarios where each request is independent
352
+
- Horizontally scaled deployments without shared session state
353
+
254
354
### Testing and Debugging
255
355
256
356
To test your server, you can use the [MCP Inspector](https://github.com/modelcontextprotocol/inspector). See its README for more information.
@@ -596,6 +696,101 @@ This setup allows you to:
596
696
- Provide custom documentation URLs
597
697
- Maintain control over the OAuth flow while delegating to an external provider
598
698
699
+
### Backwards Compatibility
700
+
701
+
The SDK provides support for backwards compatibility between different protocol versions:
702
+
703
+
#### Client-Side Compatibility
704
+
705
+
For clients that need to work with both Streamable HTTP and older SSE servers:
// Handle Streamable HTTP transport for modern clients
761
+
// Implementation as shown in the "With Session Management" example
762
+
// ...
763
+
});
764
+
765
+
// Legacy SSE endpoint for older clients
766
+
app.get('/sse', async (req, res) => {
767
+
// Create SSE transport for legacy clients
768
+
const transport =newSSEServerTransport('/messages', res);
769
+
transports.sse[transport.sessionId] =transport;
770
+
771
+
res.on("close", () => {
772
+
deletetransports.sse[transport.sessionId];
773
+
});
774
+
775
+
awaitserver.connect(transport);
776
+
});
777
+
778
+
// Legacy message endpoint for older clients
779
+
app.post('/messages', async (req, res) => {
780
+
const sessionId =req.query.sessionIdasstring;
781
+
const transport =transports.sse[sessionId];
782
+
if (transport) {
783
+
awaittransport.handlePostMessage(req, res);
784
+
} else {
785
+
res.status(400).send('No transport found for sessionId');
786
+
}
787
+
});
788
+
789
+
app.listen(3000);
790
+
```
791
+
792
+
**Note**: The SSE transport is now deprecated in favor of Streamable HTTP. New implementations should use Streamable HTTP, and existing SSE implementations should plan to migrate.
0 commit comments