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: 24 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 (9 months ago)
- Default Branch: main
- Last Pushed: 2025-06-03T19:14:57.000Z (6 months ago)
- Last Synced: 2025-06-04T03:21:34.427Z (6 months 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
- awesome-mcp-servers - **fastify-mcp** - A Fastify plugin to run Model Context Protocol (MCP) servers `typescript` `ai` `fastify` `javascript` `model-context-protocol` `npm install haroldadmin/fastify-mcp` (🤖 AI/ML)
README
# fastify-mcp
Integrate [Model Context Protocol](https://modelcontextprotocol.io/) servers with your [Fastify](https://www.fastify.dev) app.
Supports the [Streamable HTTP transport](https://modelcontextprotocol.io/specification/2025-03-26/basic/transports#streamable-http) as well as the legacy [HTTP+SSE transport](https://modelcontextprotocol.io/specification/2024-11-05/basic/transports#http-with-sse).
## Usage
First, define your MCP server.
```ts
function createServer() {
const mcpServer = new McpServer({
name: "...",
version: "...",
});
mcpServer.tool("...");
mcpServer.resource("...");
return mcpServer.server;
}
```
Create a Fastify app and register the plugin.
```ts
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()
createServer,
});
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 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, as recommended by the MCP SDK examples.
### Session Events
The `Sessions` class emits the following events:
- `connected`: Emitted when a new session is added.
- `terminated`: Emitted when a session is removed.
- `error`: Emitted when an asynchronous event handler throws an error.
```ts
const sessions = new Sessions();
sessions.on("connected", (sessionId) => {
console.log(`Session ${sessionId} connected`);
});
sessions.on("terminated", (sessionId) => {
console.log(`Session ${sessionId} terminated`);
});
```
## 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.