HF

haroldadmin/fastify-mcp

API 开发

本页由 AI 翻译,技术细节请以原文为准。 查看原文

27 stars 8 forks 质量 58 趋势 15Apache-2.0

一个用于运行模型上下文协议(MCP)服务器的 Fastify 插件

概览

将模型上下文协议服务器集成到你的Fastify应用中。 支持可流式HTTP传输以及传统的HTTP+SSE传输。 创建一个Fastify应用并注册插件。 更详细的例子请参见示例目录。 官方的MCP TypeScript SDK不支持开箱即用地管理多个会话,因此这部分由主机服务器负责。 该软件包使用每个活跃会话与其会话ID的内存映射来管理多个会话,正如MCP SDK示例所推荐的。 Sessions级会发布以下事件: - 连接:添加新会话时发出。- 终止:会话被移除时发出。- 错误:异步事件处理程序抛出错误时发出。

概述(AI 翻译)

将模型上下文协议服务器集成到你的Fastify应用中。

支持可流式HTTP传输以及传统的HTTP+SSE传输。

用途

首先,定义你的MCP服务器。

创建一个Fastify应用并注册插件。

更详细的例子请参见示例目录。

安装

会话管理

官方的MCP TypeScript SDK不支持开箱即用地管理多个会话,因此这部分由主机服务器负责。

该软件包使用每个活跃会话与其会话ID的内存映射来管理多个会话,正如MCP SDK示例所推荐的。

场次活动

Sessions 级发射以下事件:

  • connected:添加新会话时发出。
  • terminated:会话被移除时发出。
  • error:异步事件处理程序抛出错误时发出。

贡献

如果您在使用该软件包时遇到问题,请提交问题。也欢迎提交新功能或修复错误的拉取请求。

本页由 AI 翻译,技术细节请以原文为准。

README

fastify-mcp

Integrate Model Context Protocol servers with your Fastify app.

Supports the Streamable HTTP transport as well as the legacy HTTP+SSE transport.

Usage

First, define your MCP server.

function createServer() {
  const mcpServer = new McpServer({
    name: "...",
    version: "...",
  });

  mcpServer.tool("...");
  mcpServer.resource("...");

  return mcpServer.server;
}

Create a Fastify app and register the plugin.

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 directory for more detailed examples.

Installation

# npm
npm install fastify-mcp

# yarn
yarn add fastify-mcp

Session Management

The official MCP 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.
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.

在 GitHub 查看完整 README

安装

This server does not publish a one-line install command.

Open the repository installation guide 新手?先看 mcpskill.org 教程 →