Get the FREE Ultimate OpenClaw Setup Guide →

fastify

Fastify plugin to easily spin up Model Context Protocol (MCP) HTTP servers

Installation
Run this command in your terminal to add the MCP server to Claude Code.
Run in terminal:
Command
claude mcp add --transport stdio flaviodelgrosso-fastify-mcp-server node src/index.js \
  --env PORT="3000" \
  --env NODE_ENV="production"

How to use

This MCP server plugin for Fastify enables building scalable MCP servers that communicate with clients using the Model Context Protocol over a streamable HTTP transport. It leverages the @modelcontextprotocol/sdk to provide a type-safe, session-managed MCP endpoint that can route requests, manage sessions, and optionally enforce Bearer token authentication. With the plugin, you can define MCP tools (actions) that your AI assistants or MCP clients can invoke, and you can monitor session lifecycles and errors through the integrated event system. The result is a robust, production-ready MCP server that fits neatly into a Fastify-based application stack.

To use it, install the package and register the plugin in your Fastify app, providing a factory that creates an MCP server instance and (optionally) a custom endpoint path. Tools are registered on the MCP server, which then exposes an endpoint (default /mcp) for creating and managing MCP sessions, handling requests, and streaming responses. If needed, you can enable Bearer token authentication to secure access and configure a custom session store (in-memory by default, with Redis available for larger deployments). This setup is suitable for building AI-assisted workflows that require structured MCP interactions with your services.

How to install

Prerequisites:

  • Node.js (>= 14.x) and npm/yarn installed
  • Basic TypeScript/JavaScript project setup for Fastify

Install the MCP server plugin and its required SDK:

npm install fastify-mcp-server @modelcontextprotocol/sdk

Create a Fastify application and register the MCP server plugin. Example:

import Fastify from 'fastify';
import FastifyMcpServer from 'fastify-mcp-server';

const app = Fastify({ logger: true });

function createMcpServer () {
  const mcp = new (require('@modelcontextprotocol/sdk').McpServer)({
    name: 'my-mcp-server',
    version: '1.0.0'
  });

  // Define MCP tools available to clients
  mcp.tool('say-hello', () => ({
    content: [{ type: 'text', text: 'Hello from MCP!' }]
  }));

  return mcp;
}

await app.register(FastifyMcpServer, {
  createMcpServer,
  endpoint: '/mcp' // optional, defaults to '/mcp'
});

await app.listen({ host: '0.0.0.0', port: 3000 });

Run the application:

npm run build && node dist/server.js

If your project uses ts-node or a TS build step, adjust the registration and startup commands accordingly.

Additional notes

Notes and tips:

  • Default endpoint is /mcp; you can customize it via the endpoint option when registering the plugin.
  • Bearer token authentication is supported; configure authorization in the plugin options if you need to restrict access.
  • The MCP server uses a session store; in-memory is suitable for development, while Redis is recommended for production with larger workloads.
  • Ensure your Node.js process has enough memory for high concurrency scenarios and enable graceful shutdown logic for clean session termination.
  • If you upgrade @modelcontextprotocol/sdk, review any breaking changes in MCP tool definitions and transport behavior.

Related MCP Servers

Sponsor this space

Reach thousands of developers