diff --git a/mcp/video_task/README.md b/mcp/video_task/README.md index 083ba66..4936baf 100644 --- a/mcp/video_task/README.md +++ b/mcp/video_task/README.md @@ -149,6 +149,7 @@ Token 仅在每次 tool call 时从 header 读取,不写入任何存储、不 | initialize 时缺 token | `401` | JSON-RPC `error.message` 提示补充 header | | 智谱 API 4xx/5xx | `200`(MCP 工具返回) | `isError: true`,正文含智谱返回的状态码和 body | | 网络失败 | `200`(MCP 工具返回) | `isError: true`,正文含 `fetch` 错误信息 | +| 上游挂住不回(超时 >30s) | `200`(MCP 工具返回) | `isError: true`,正文提示 timed out;`fetch` 带 `AbortSignal.timeout`,不会无限卡住工具调用 | --- diff --git a/mcp/video_task/src/mcp-server.js b/mcp/video_task/src/mcp-server.js index 88a82a2..92a9467 100644 --- a/mcp/video_task/src/mcp-server.js +++ b/mcp/video_task/src/mcp-server.js @@ -19,6 +19,9 @@ const DEFAULT_QUALITY = "speed"; // speed | quality const DEFAULT_FPS = 30; // 30 | 60 const DEFAULT_WATERMARK_ENABLED = true; // 平台政策默认 true +// submit/query 都是快接口,卡住基本等于上游挂了;限时避免工具调用被拖死 +const UPSTREAM_TIMEOUT_MS = 30_000; + // ── 优雅退出 ────────────────────────────────────────────────────────────── // 容器里 node 以 PID 1 运行且未注册 SIGTERM 处理器时,内核不会对其执行默认 // 终止动作,docker stop/restart 会白等满 10s 宽限期才被 SIGKILL。 @@ -119,9 +122,19 @@ async function callZhipu({ url, token, method = "POST", body }) { Authorization: `Bearer ${token}`, "Content-Type": "application/json", }, + signal: AbortSignal.timeout(UPSTREAM_TIMEOUT_MS), }; if (body !== undefined) init.body = JSON.stringify(body); - const resp = await fetch(url, init); + let resp; + try { + resp = await fetch(url, init); + } catch (e) { + // AbortSignal.timeout 中止时抛 TimeoutError/AbortError,转成明确的超时错误 + if (e?.name === "TimeoutError" || e?.name === "AbortError") { + throw new Error(`Upstream request timed out after ${UPSTREAM_TIMEOUT_MS} ms`); + } + throw e; + } const text = await resp.text(); let data; try {