diff --git a/.changeset/fix-automatic-task-polling-abort.md b/.changeset/fix-automatic-task-polling-abort.md new file mode 100644 index 000000000..f83440fa8 --- /dev/null +++ b/.changeset/fix-automatic-task-polling-abort.md @@ -0,0 +1,5 @@ +--- +'@modelcontextprotocol/server': patch +--- + +Check AbortSignal in handleAutomaticTaskPolling to stop cancelled requests from polling indefinitely. Previously, if a client cancelled a tools/call request during automatic task polling, the poll loop continued consuming server resources until the task completed or the process died. diff --git a/packages/server/src/server/mcp.ts b/packages/server/src/server/mcp.ts index fb45fd5db..7cf62ed77 100644 --- a/packages/server/src/server/mcp.ts +++ b/packages/server/src/server/mcp.ts @@ -326,6 +326,9 @@ export class McpServer { const pollInterval = task.pollInterval ?? 5000; while (task.status !== 'completed' && task.status !== 'failed' && task.status !== 'cancelled') { + if (ctx.mcpReq.signal.aborted) { + throw new ProtocolError(ProtocolErrorCode.InternalError, 'Request cancelled during task polling'); + } await new Promise(resolve => setTimeout(resolve, pollInterval)); const updatedTask = await ctx.task.store.getTask(taskId); if (!updatedTask) {