Skip to content

Handle task completion notifications to skip polling delay#1192

Open
Christian-Sidak wants to merge 1 commit intomodelcontextprotocol:mainfrom
Christian-Sidak:fix/handle-task-complete-notification
Open

Handle task completion notifications to skip polling delay#1192
Christian-Sidak wants to merge 1 commit intomodelcontextprotocol:mainfrom
Christian-Sidak:fix/handle-task-complete-notification

Conversation

@Christian-Sidak
Copy link
Copy Markdown

Summary

Fixes #1037

When a server sends a notifications/tasks/status notification with a terminal status (completed, failed, or cancelled) for the task currently being polled, the inspector now immediately proceeds to fetch the result via tasks/get instead of waiting for the full poll interval to elapse.

Problem

The task polling loop in callTool used a fixed setTimeout with the server's pollInterval before each tasks/get call. Even when the server sent a notifications/tasks/status notification indicating the task was done, the inspector ignored it and kept sleeping until the timer expired. With long poll intervals, this caused a noticeable delay before the result appeared.

Approach

  • Added a taskNotificationResolverRef that holds a { taskId, resolve } pair while the polling loop is active
  • Modified the notifications/tasks/status handler to call resolve() when a terminal status arrives for the task being polled
  • Replaced the polling loop's await new Promise(resolve => setTimeout(resolve, pollInterval)) with a Promise.race between the poll interval timer and the notification signal

This way, whichever comes first -- the poll interval elapsing or the notification arriving -- the loop proceeds to call tasks/get immediately.

Test plan

  • All 515 existing tests pass
  • Client builds successfully
  • Manual: call a tool as a task against a server that sends notifications/tasks/status on completion; the result should appear immediately instead of waiting for the next poll cycle

When a server sends a notifications/tasks/status with a terminal status
(completed, failed, cancelled) for the task currently being polled, the
inspector now immediately proceeds to fetch the result via tasks/get
instead of waiting for the full poll interval to elapse.

This is done by racing the poll interval timer against a promise that
resolves when the relevant task notification arrives.

Fixes modelcontextprotocol#1037
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Task complete notification is not handled

1 participant