fastify-mcp

by haroldadmin

27 stars
399 downloads
Not rated
GitHub Website

About

A Fastify plugin to run Model Context Protocol (MCP) servers

Details

Author
haroldadmin
GitHub stars
27
Downloads
399
Categories
Other, AI

- Integrates MCP servers into any Fastify application.
- Supports Streamable HTTP and legacy HTTP+SSE transports.
- Built‑in session management with event emitters (connected, terminated, error).
- Works with the official MCP TypeScript SDK.
- Configurable endpoint path and stateful/stateless modes.

Install with npm install fastify-mcp or yarn add fastify-mcp. Define an MCP server using McpServer from the MCP TypeScript SDK, then register the streamableHttp plugin on a Fastify app, passing options such as stateful, mcpEndpoint, sessions, and createServer. The plugin handles session management automatically using an in-memory mapping.

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<StreamableHTTPServerTransport>()
createServer,
});

app.listen({ port: 8080 });

See the examples directory for more detailed examples.

Installation

```bash

No reviews yet — be the first

Sign in to leave a review

Use Google, GitHub, or an email account so ratings stay tied to real people.

Email sign in

No reviews posted yet.