An open API service indexing awesome lists of open source software.

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

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.