-
Notifications
You must be signed in to change notification settings - Fork 1.7k
[v2] Make ToolTaskHandler.getTask/getTaskResult optional and actually invoke them #1764
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
felixweinberger
wants to merge
2
commits into
main
Choose a base branch
from
fweinberger/tooltask-handlers
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+320
−314
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| --- | ||
| "@modelcontextprotocol/core": minor | ||
| "@modelcontextprotocol/server": minor | ||
| --- | ||
|
|
||
| Make `ToolTaskHandler.getTask`/`getTaskResult` optional and actually invoke them | ||
|
|
||
| **Bug fix:** `getTask` and `getTaskResult` handlers registered via `registerToolTask` were never invoked — `tasks/get` and `tasks/result` requests always hit `TaskStore` directly. | ||
|
|
||
| **Breaking changes (experimental API):** | ||
|
|
||
| - `getTask` and `getTaskResult` are now **optional** on `ToolTaskHandler`. When omitted, `TaskStore` handles the requests (previous de-facto behavior). | ||
| - `TaskRequestHandler` signature changed: handlers receive only `(ctx: TaskServerContext)`, not the tool's input arguments. | ||
|
|
||
| **Migration:** If your handlers just delegated to `ctx.task.store`, delete them. If you're proxying an external job system (Step Functions, CI/CD pipelines), keep them and drop the `args` parameter. |
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
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
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
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡
_taskToToolMap grows without bound: entries are added via_recordTask()but never deleted when tasks reach terminal status or when the server closes. For long-running servers using customgetTask/getTaskResulthandlers, this is an unbounded memory leak. Consider deleting entries after returning a terminal result in_dispatch, and clearing the map on close (similar to howTaskManager._taskProgressTokenshas_cleanupTaskProgressHandler()andonClose()cleanup).Extended reasoning...
What the bug is
The
_taskToToolMap (line 60) inExperimentalMcpServerTasksrecordstaskId -> toolNamemappings whenever a task is created for a tool that has customgetTaskorgetTaskResulthandlers. Entries are added in_recordTask()but there are zero calls to_taskToTool.delete()or_taskToTool.clear()anywhere in the class or codebase.Code path
The flow is:
tools/callwith task augmentation.McpServercallsthis._experimental.tasks._recordTask(taskId, toolName)(mcp.ts lines 200 and 322)._recordTask()checks if the tool has customgetTask/getTaskResulthandlers and, if so, stores the mapping:this._taskToTool.set(taskId, toolName)(mcpServer.ts line 77).tasks/getortasks/resultarrives,_dispatch()reads from the map to route to the correct handler.Why existing code does not prevent it
The
TaskManagerclass in core has cleanup patterns for its own maps:_cleanupTaskProgressHandler()deletes entries from_taskProgressTokenswhen a task reaches terminal status, andonClose()clears the entire map. TheExperimentalMcpServerTasksclass follows none of these patterns -- it has noonClose()hook and no per-task cleanup.Step-by-step proof
_taskToToolis empty (size 0).proxy-task(which has customgetTask/getTaskResult)._recordTask("T1", "proxy-task")runs. Map size = 1._dispatchreturns the result. Map still has entry"T1" -> "proxy-task"-- size = 1.Impact
Each entry is just two short strings (~100 bytes), so the leak is slow. For the common case (no custom handlers),
_recordTaskskips the.set()call entirely, so there is no leak. The leak only manifests for the uncommon proxy use case (customgetTask/getTaskResulthandlers) on a long-running server that processes many tasks. The PR description already acknowledges the in-memory-only nature of this map, but the concern there was about cross-instance behavior -- within a single process the map grows monotonically.Suggested fix
Delete the entry in
_dispatchafter a terminal result is returned (when the method isgetTaskResult, since that is the final lookup). Also add anonClose()method that callsthis._taskToTool.clear()and wire it into the server shutdown path. This mirrors the_taskProgressTokenscleanup pattern inTaskManager.