Get the FREE Ultimate OpenClaw Setup Guide →

fastify

A Fastify plugin to run Model Context Protocol (MCP) 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 haroldadmin-fastify-mcp npx -y fastify-mcp \
  --env PORT="server port if applicable" \
  --env DEBUG="optional debug flag"

How to use

This MCP server package provides an integration layer that lets you expose and manage Model Context Protocol (MCP) servers inside a Fastify application. It exposes a plugin that you can register with your Fastify app to handle MCP sessions and transport through the Streamable HTTP transport by default, while also supporting the legacy HTTP+SSE transport if needed. The example shows creating an MCP server instance, configuring tools and resources, and then registering the plugin on your Fastify app. When you start the Fastify server, the MCP endpoints become available under the configured mcpEndpoint, and sessions are managed by an in-memory map within the host server.

What you can do with this package:

  • Create MCP servers that declare their tools and resources programmatically, then attach them to a Fastify instance.
  • Use Streamable HTTP transport for scalable, streaming MCP communications, with a fallback option for the legacy HTTP+SSE transport if you require compatibility.
  • Manage multiple MCP sessions in the host application, with events for session connection, termination, and error handling. The Sessions utility helps you track active sessions and react to lifecycle changes in your application logic.

How to install

Prerequisites:

  • Node.js (14+ or as required by your environment)
  • npm (or yarn)

Install the package:

# npm
npm install fastify-mcp

# yarn
yarn add fastify-mcp

Usage overview:

  1. Create an MCP server instance by defining tools and resources via the MCP server API.
  2. Create and configure a Fastify app, then register the fastify-mcp plugin with appropriate options.
  3. Start your Fastify app to serve MCP endpoints.

Example workflow:

# If you want to run via npx for quick experimentation
npx -y fastify-mcp

Note: The MCP server is typically integrated as part of a larger Fastify application, so you usually run your application (not the MCP server package in isolation). The installation step ensures the plugin and its dependencies are available to your app.

Additional notes

Tips and caveats:

  • The plugin supports a stateful or stateless MCP endpoint via the stateful option. Choose based on whether you need to maintain per-session state.
  • The Sessions class helps you manage multiple MCP sessions; listen to connected and terminated events to manage lifecycle in your host application.
  • If you need to switch transports, you can configure the transport options but the default focus is on Streamable HTTP transport for MCP interactions.
  • Environment variables are optional but can be used to toggle debug logs or adjust port configurations in hosting environments. Ensure your host app’s port does not conflict with MCP endpoints.
  • Since MCP is hosted inside your Fastify app, you should ensure proper error handling around MCP session handlers to avoid unhandled exceptions in the server process.

Related MCP Servers

Sponsor this space

Reach thousands of developers