mcp-longjobs
mcp-longjobs
适用于 MCP 的持久、可恢复操作——能够经受超时、断连和客户端重启的长时任务与大文件。今天,在任何客户端上都能用。
问题
有三件事会让每个做实际工作的 MCP 服务器出问题:
长时工具调用会超时。 客户端对每次调用设置超时(通常为 10–60 秒)。一次爬取、一次构建、一个批处理任务失败——而模型的“重试”会从头重启整个操作。
失败无法修复。 一次失败的调用返回的是自由格式的错误,于是模型只能猜测:盲目重试,或者放弃。它无法修正一个参数然后继续。
大文件没有传输方案。 二进制内容要么是 JSON 中的 base64(33% 开销,硬性消息大小上限),要么是没有任何约定的裸 URL——没有分块、没有断点续传、没有完整性校验。
2026-07-28 版 MCP 规范 增加了 Tasks——支持中途输入和持久句柄的异步执行。但目前还没有客户端支持它,而且规范要求服务器对未选择加入的客户端拒绝任务。因此,每个长时运行的服务器都需要一条能在当今客户端上工作的回退路径。这就是本包。
Related MCP server: Simple Streamable HTTP MCP Server
你能得到什么
import { z } from "zod";
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { JsonFileSessionStore, withTasks, withFileTransfer, asToolRegistrar } from "mcp-longjobs";
const mcp = new McpServer({ name: "my-server", version: "1.0.0" });
const registrar = asToolRegistrar(mcp);
const store = new JsonFileSessionStore("./state/sessions.json");
const tasks = withTasks(registrar, { store });
tasks.taskTool("crawl-site", {
description: "Crawl a site and produce a report (takes minutes)",
inputSchema: { url: z.string(), maxPages: z.number().default(50) },
}, async (args, ctx) => {
for (const page of pages) {
if (ctx.signal.aborted) throw new Error("cancelled");
await ctx.progress(`Crawled ${page.url}`, done / total);
if (needsConfirmation(page)) {
const answer = await ctx.needInput({ prompt: `Include ${page.url}?`, choices: ["yes", "no"] });
if (answer === "no") continue;
}
}
return { summary, reportPath }; // small result for the model; big artifacts go through file transfer
});
withFileTransfer(registrar, { store, storageDir: "./state/blobs" });在当今的客户端上,模型会体验到(无需 Tasks 支持):
crawl-site立即返回一个taskId和轮询durable_task_get的说明——不再有超时。轮询显示实时进度:
{ "status": "working", "progress": { "message": "Crawled /pricing", "fraction": 0.4 } }。中途提出的问题会将任务暂停为
input_required;模型通过durable_task_respond回答,任务会从停止处继续。客户端崩溃?新会话? 使用相同的
taskId调用durable_task_get仍然有效——状态保存在存储中,而不是连接中。durable_task_cancel会在下一个检查点协作式地中止工作。
失败是数据,而不是协议错误——一个结构化错误封装,模型可以在一次往返中修复:
{
"status": "failed",
"error": {
"code": "offset_mismatch",
"message": "Expected offset 131072, got 0.",
"retryable": true,
"recoveryHint": "Do NOT resend the whole file. Re-send this chunk starting at offset 131072.",
"partial": { "cursor": 131072 }
}
}包(子路径导出)
Import | 用途 |
|
|
|
|
| 会话模型、可插拔存储(内存、JSON 文件)、结构化错误封装 |
设计说明
字节永远不会流经模型。 模型只看到元数据:句柄、大小、sha256、进度。通过工具调用传输的分块适用于中小型负载;大文件应带外传输(计划提供 TUS 端点),由模型验证完整性。
模型是导演,而不是信使。 门面工具的结果自带说明(“使用此 id 调用
durable_task_get”、“从偏移量 N 处恢复”),因此任何有能力的模型都可以在零宿主端支持的情况下驱动该协议。失败是可修复的数据。 每次失败都携带
code、retryable、recoveryHint和partial.cursor——哪里出错了、重试是否可行、应该改做什么、以及哪些已经成功。生命周期词汇与规范一致。
working / input_required / completed / failed / cancelled,因此原生适配器以后可以无缝接入,而不会造成破坏性变更。
状态
组件 | 状态 |
Tasks 回退门面(进度 / 输入 / 取消) | ✅ 已实现 |
持久会话存储(内存、JSON 文件) | ✅ 已实现 |
带断点续传和校验和的分块文件传输 | ✅ 已实现 |
原生 ext-tasks 适配器( | 🔜 跟踪 SDK 的实验性 Tasks API |
用于大文件的 TUS 1.0 带外端点 | 🔜 计划中——参见 mcp#189 |
Redis / SQLite 存储、Python 移植 | 🔜 计划中 |
快速开始
git clone https://github.com/ljppanda/mcp-longjobs
cd mcp-longjobs
npm install && npm run build
node dist/examples/report-generator.js(一旦发布到 npm,同一个服务器只需一条命令即可运行:npx mcp-longjobs。)
将你的客户端指向它(stdio):
{
"mcpServers": {
"report-generator": {
"command": "node",
"args": ["/absolute/path/to/mcp-longjobs/dist/examples/report-generator.js"]
}
}
}然后问:“生成一份关于电动汽车电池的报告,包含 3 个部分。” 观察模型启动任务、轮询 durable_task_get 并获取结果。在运行中途杀掉客户端,重新启动,然后询问同一个 taskId——它会恢复。
开发
npm install
npm test # vitest
npm run build # tsc -> dist/
npm run example # build + run the demo server贡献
欢迎提交 PR——尤其是:存储后端(SQLite/Redis)、原生 ext-tasks 适配器和 TUS 端点。如果是较大的改动,请先开一个 issue。
许可证
Maintenance
Related MCP Servers
- AlicenseAqualityBmaintenanceAsync MCP server for running long-running AI tasks with real-time progress monitoring, enabling users to start, monitor, and manage complex AI workflows across multiple models.6345MIT
- FlicenseNot gradedqualityDmaintenanceA reference implementation demonstrating proper MCP server patterns with HTTP transport, featuring session management, progress notifications, and example tools for testing server functionality. Serves as a clean template for building MCP servers with streamable responses and comprehensive error handling.7
- AlicenseAqualityAmaintenanceA fire-and-poll MCP server that lets Claude Code run long background jobs without hitting tool-call timeouts.3MIT
- FlicenseNot gradedqualityBmaintenanceRemote MCP server that launches user-supplied scripts inside disposable Docker containers, returning task IDs for async tracking and bounded output tails.
Related MCP Connectors
MCP server for the FFmpeg Micro video transcoding API — create, monitor, download transcodes.
MCP protocol requiring task acceptance and provenance tags. Self-hosted only - see README.
Remote MCP server for RunComfy Serverless API (ComfyUI): deployments and async inference.
Latest Blog Posts
- Who's Calling? MCP Hosts Are an Identity Blind Spot (And the Spec Knows It)By Om-Shree-0709 on .mcpAgent IdentityOAuth 2.1
- Your AI Chatbot Just Exposed Your CEO's Salary to an InternBy Om-Shree-0709 on .Agent IdentityMCP SecurityOAuth Delegation
- Why MCP Servers Need Execution Sandboxing (And Why Your Current Stack Isn't Enough)By Om-Shree-0709 on .Agentic AiPrompt InjectionWebAssembly
MCP directory API
We provide all the information about MCP servers via our MCP API.
curl -X GET 'https://glama.ai/api/mcp/v1/servers/ljppanda/mcp-longjobs'
If you have feedback or need assistance with the MCP directory API, please join our Discord server