Handle task completion notifications to skip polling delay#1192
Open
Christian-Sidak wants to merge 1 commit intomodelcontextprotocol:mainfrom
Open
Handle task completion notifications to skip polling delay#1192Christian-Sidak wants to merge 1 commit intomodelcontextprotocol:mainfrom
Christian-Sidak wants to merge 1 commit intomodelcontextprotocol:mainfrom
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #1037
When a server sends a
notifications/tasks/statusnotification with a terminal status (completed,failed, orcancelled) for the task currently being polled, the inspector now immediately proceeds to fetch the result viatasks/getinstead of waiting for the full poll interval to elapse.Problem
The task polling loop in
callToolused a fixedsetTimeoutwith the server'spollIntervalbefore eachtasks/getcall. Even when the server sent anotifications/tasks/statusnotification 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
taskNotificationResolverRefthat holds a{ taskId, resolve }pair while the polling loop is activenotifications/tasks/statushandler to callresolve()when a terminal status arrives for the task being polledawait new Promise(resolve => setTimeout(resolve, pollInterval))with aPromise.racebetween the poll interval timer and the notification signalThis way, whichever comes first -- the poll interval elapsing or the notification arriving -- the loop proceeds to call
tasks/getimmediately.Test plan
notifications/tasks/statuson completion; the result should appear immediately instead of waiting for the next poll cycle