Back to MCP directory
publicPublicdnsLocal runtime

mcp-server-do

该项目实现了一个基于SSE传输层和Durable Objects的MCP服务器,支持与ModelContextProtocol TypeScript SDK集成,并提供了端到端的运行步骤。

article

README

🚀 SSE + MCP 服务器 + Durable Objects

本项目包含三个核心部分,与 @modelcontextprotocol/typescript-sdk 配合使用,为你提供了一个高效的运行环境:

  1. 一个 SSE 传输层,位于 /src/sse.ts,可与 @modelcontextprotocol/typescript-sdk 协同工作。
  2. 一个作为 Durable Object 的 MCP 服务器,位于 /src/mcp-server-do.ts
  3. 提供了端到端的运行步骤,方便你快速上手。

🚀 快速开始

运行步骤

  1. 克隆仓库:将此仓库克隆到本地。
  2. 安装依赖:在项目根目录下,运行 npm install 命令,安装所需的依赖。
  3. 启动 DO:执行 npm start 命令,启动 Durable Object。启动后,可通过 http://localhost:8787 访问。
  4. 运行 MCP Inspector:使用 npx @modelcontextprotocol/inspector 命令运行 MCP Inspector。
  5. 打开 Inspector:打开 Inspector 后,输入 http://localhost:8787/sse

💻 使用示例

基础用法

我从 这个示例 中获取灵感,该示例来自 @modelcontextprotocol/typescript-sdk

import express from "express";
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { SSEServerTransport } from "@modelcontextprotocol/sdk/server/sse.js";

const server = new McpServer({
  name: "example-server",
  version: "1.0.0"
});

// ... 设置服务器资源、工具和提示 ...

const app = express();

app.get("/sse", async (req, res) => {
  const transport = new SSEServerTransport("/messages", res);
  await server.connect(transport);
});

app.post("/messages", async (req, res) => {
  // 注意:要支持多个同时连接,这些消息需要路由到特定的传输。由于简单起见,此处未实现此逻辑。
  await transport.handlePostMessage(req, res);
});

app.listen(3001);

高级用法

参考上述示例,我将其实现为 Durable Objects 中的内容。但首先需要一个在 Workers 上运行的传输层。参考 sse.ts 来自 @modelcontextprotocol/typescript-sdk,我实现了一个类似的版本,尽量模仿现有代码的结构和功能。需要注意的是,SDK 中有一些与 Node.js 的 http 模块耦合较深的设计假设,这些假设会泄漏到 SSE 传输层及其输入类型中——但其实唯一有意义的接口变化是 handlePostMessage 接受请求并返回响应。看起来有一种巧妙的方法可以将其上游化?

📚 详细文档

本项目的灵感来源于 @modelcontextprotocol/typescript-sdk 中的 这个示例。在实现过程中,参考了 sse.ts 来自 @modelcontextprotocol/typescript-sdk,并将其实现为 Durable Objects 中的内容。同时,需要注意 SDK 中与 Node.js 的 http 模块耦合较深的设计假设对 SSE 传输层及其输入类型的影响。

help

Runtime guide

cloud

Hosted runtime

Hosted servers run from a provider-managed environment. You usually connect the MCP client to the hosted endpoint or follow the provider's authorization flow, without keeping a local process alive

  1. Open provider connection page
  2. Authorize or copy endpoint
  3. Connect from your MCP client
terminal

Local runtime / other methods

Local servers run on your own machine or infrastructure. You normally copy the server_config into your MCP client, install the required package, and provide env variables from env_schema when needed

  1. Copy server_config
  2. Install required package
  3. Fill env variables and restart client