fastify-mcp

by haroldadmin

27 68.5k downloads Not rated yet Apache-2.0

About

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

Details

License
Apache-2.0

Explore

- 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.

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.

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.