https://github.com/haroldadmin/fastify-mcp
A Fastify plugin to run Model Context Protocol (MCP) servers
https://github.com/haroldadmin/fastify-mcp
ai fastify javascript model-context-protocol
Last synced: 10 days ago
JSON representation
A Fastify plugin to run Model Context Protocol (MCP) servers
- Host: GitHub
- URL: https://github.com/haroldadmin/fastify-mcp
- Owner: haroldadmin
- License: apache-2.0
- Created: 2025-03-02T23:53:49.000Z (4 months ago)
- Default Branch: main
- Last Pushed: 2025-06-03T19:14:57.000Z (28 days ago)
- Last Synced: 2025-06-04T03:21:34.427Z (28 days ago)
- Topics: ai, fastify, javascript, model-context-protocol
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/fastify-mcp
- Size: 182 KB
- Stars: 11
- Watchers: 1
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# fastify-mcp
Integrate [Model Context Protocol](https://modelcontextprotocol.io/) servers with your [Fastify](https://www.fastify.dev) app over [SSE](https://en.wikipedia.org/wiki/Server-sent_events) connections.
## Usage
First, define your MCP server.
```ts
const mcpServer = new McpServer({
name: "...",
version: "...",
});mcpServer.tool("...");
mcpServer.resource("...");
```Create a Fastify app and register the plugin.
```ts
import { fastify } from "fastify";
import { fastifyMCPSSE } from "fastify-mcp";const app = fastify();
app.register(fastifyMCPSSE, {
// Pass the lower-level Server class to the plugin
server: mcpServer.server,
});app.listen({ port: 8080 });
```See the [examples](./examples) directory for more detailed examples.
## Installation
```bash
# npm
npm install fastify-mcp# yarn
yarn add fastify-mcp
```## Session Management
The official [MCP TypeScript SDK](https://github.com/modelcontextprotocol/typescript-sdk) does not support managing multiple SSE 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.
- The ID of a session is generated by `SSEServerTransport` from the official SDK.
- The session is removed from memory whenever the response stream is closed, either by the server or due to a client disconnection.## 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.