SSE+MCP服务器+持久对象
- 与以下对象一起工作的SSE传输层
@modelcontextprotocol/typescript-sdk(/src/sse.ts) - 作为持久对象的MCP服务器(
/src/mcp-server-do.ts) - 端到端运行它的步骤
运行它
- 克隆此仓库
npm installnpm start启动DO(在http://localhost:8787)npx @modelcontextprotocol/inspector运行MCP检查器- 打开检查器,输入
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"
});
// ... set up server resources, tools, and prompts ...
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) => {
// Note: to support multiple simultaneous connections, these messages will
// need to be routed to a specific matching transport. (This logic isn't
// implemented here, for simplicity.)
await transport.handlePostMessage(req, res);
});
app.listen(3001);…并在Durable Objects中实现了同样的功能。但首先需要一个适用于Workers的传输层。
跟随 sse.ts 从 @modelcontextprotocol/typescript-sdk,我做了一个,试图模仿现有的形状。SDK中围绕其使用的一些非常基本的假设 node:http 通过输入类型泄漏到SSE传输之外,但实际上唯一有意义的接口更改是 handlePostMessage 接收请求并返回响应。似乎有一种巧妙的方法可以逆流而上?
