Integrate Model Context Protocol servers with your Fastify app over SSE connections.
First, define your MCP server.
const mcpServer = new McpServer({
name: "...",
version: "...",
});
mcpServer.tool("...");
mcpServer.resource("...");
Create a Fastify app and register the plugin.
import { fastify } from "fastify";
import { fastifyMCPSSE } from "fastify-mcp";
const app = fastify();
app.register(fastifyMCPSSE, {
// Pass the lower-level Server class to the plugin
server: mcpServer.server,
});
app.listen({ port: 8080 });
See the examples directory for more detailed examples.
# npm
npm install fastify-mcp
# yarn
yarn add fastify-mcp
The official MCP TypeScript SDK does not support managing multiple SSE sessions out of the box, and therefore it's the host server's responsibility to do so.
This package uses an in-memory mapping of each active session against its session ID to manage multiple sessions.
- The ID of a session is generated by
SSEServerTransport
from the official SDK. - The session is removed from memory whenever the response stream is closed, either by the server or due to a client disconnection.
The Sessions
class emits the following events:
connected
: Emitted when a new session is added.terminated
: Emitted when a session is removed.error
: Emitted when an asynchronous event handler throws an error.
const sessions = new Sessions();
sessions.on("connected", (sessionId) => {
console.log(`Session ${sessionId} connected`);
});
sessions.on("terminated", (sessionId) => {
console.log(`Session ${sessionId} terminated`);
});
Please file an issue if you encounter a problem when using this package. Pull requests for new features or bug fixes are also welcome.