fastify-mcp
A Fastify plugin to run Model Context Protocol (MCP) servers
Documentation
fastify-mcp
Integrate Model Context Protocol servers with your Fastify app.
Supports the Streamable HTTP transport as well as the legacy HTTP+SSE transport.
Usage
First, define your MCP server.
function createServer() {
const mcpServer = new McpServer({
name: "...",
version: "...",
});
mcpServer.tool("...");
mcpServer.resource("...");
return mcpServer.server;
}Create a Fastify app and register the plugin.
import { fastify } from "fastify";
import { streamableHttp } from "fastify-mcp";
import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
const app = fastify();
app.register(streamableHttp, {
// Set to `true` if you want a stateful server
stateful: false,
mcpEndpoint: "/mcp",
sessions: new Sessions()
createServer,
});
app.listen({ port: 8080 });See the examples directory for more detailed examples.
Installation
# npm
npm install fastify-mcp
# yarn
yarn add fastify-mcpSession Management
The official MCP TypeScript SDK does not support managing multiple 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, as recommended by the MCP SDK examples.
Session Events
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`);
});Contributing
Please file an issue if you encounter a problem when using this package. Pull requests for new features or bug fixes are also welcome.
Frequently asked questions
What is fastify-mcp?
fastify-mcp is A Fastify plugin to run Model Context Protocol (MCP) servers
How do I install fastify-mcp?
Open the GitHub repository and follow its README. Most MCP servers are added to your client's MCP config, then called by your agent.
Is fastify-mcp open source?
Yes — it is hosted on GitHub at https://github.com/haroldadmin/fastify-mcp and has 21 stars.
Related MCP tools
Composio equips your AI agents & LLMs with 100+ high-quality integrations via function calling for the Model Context Protocol. Enhance AI assistants with powerf
📦 Repomix is a powerful tool that packs your entire repository into a single, AI-friendly file. Perfect for when you need to feed your codebase to Large Lan...
The TypeScript AI agent framework. ⚡ Assistants, RAG, observability. Supports any LLM: GPT-4, Claude, Gemini, Llama. Built for the Model Context Protocol to enh
Mcp-use is the easiest way to interact with mcp servers with custom agents TypeScript-based implementation. Trusted by 8100+ developers.
A Model Context Protocol server for converting almost anything to Markdown TypeScript-based implementation. Trusted by 2200+ developers.
Enhanced ChatGPT Clone: Features Agents, MCP, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini...
Run your own MCP server? See who uses it and what to fix.
Measure it with TrackMCP