package
Fastify adapters for the Model Context Protocol TypeScript server SDK - Fastify middleware
npm install @modelcontextprotocol/fastifyGitHub-flavored package documentation.
Across all versions.
Download trend unavailable.
Runtime metadata.
Tagged topics.
Estimated unpacked size across 1 packages.
61.9 kB
Includes dependencies.
Latest published Oct 2, 2026
latest
@modelcontextprotocol/fastifyFastify adapters for the MCP TypeScript server SDK.
This package is a thin Fastify integration layer for @modelcontextprotocol/server.
It does not implement MCP itself. Instead, it helps you:
npm install @modelcontextprotocol/server @modelcontextprotocol/fastify fastify
# For MCP Streamable HTTP over Node.js (IncomingMessage/ServerResponse):
npm install @modelcontextprotocol/nodecreateMcpFastifyApp(options?)hostHeaderValidation(allowedHostnames)localhostHostValidation()import { createMcpFastifyApp } from '@modelcontextprotocol/fastify';
const app = createMcpFastifyApp(); // default host is 127.0.0.1; protection enabledimport { createMcpFastifyApp } from '@modelcontextprotocol/fastify';
import { NodeStreamableHTTPServerTransport } from '@modelcontextprotocol/node';
import { McpServer } from '@modelcontextprotocol/server';
const app = createMcpFastifyApp();
app.post('/mcp', async (request, reply) => {
// Stateless example: create a server and a transport per request (with sessions: one pair per session).
const mcpServer = new McpServer({ name: 'my-server', version: '1.0.0' });
const transport = new NodeStreamableHTTPServerTransport({ sessionIdGenerator: undefined });
await mcpServer.connect(transport);
// Clean up when the client closes the connection (e.g. during SSE streaming).
To reject non-POST requests with 405 Method Not Allowed, add routes for GET and DELETE that send a JSON-RPC error response.
import { hostHeaderValidation } from '@modelcontextprotocol/fastify';
app.addHook('onRequest', hostHeaderValidation(['localhost', '127.0.0.1', '[::1]']));pcarleton
paul@modelcontextprotocol.io