Skip to content

Conversation

salignatmoandal
Copy link

Hi everyone,
I came across a TODO in the codebase and went ahead and implemented it — hope that helps!###

Summary

This PR implements proper request cancellation in the MCP server. When a notifications/cancelled message is received, the server now effectively cancels the corresponding in-flight request using Go's context cancellation mechanism.

Details

  • Added a cancels map and mutex to MCPServer to track cancellable contexts by request ID.
  • When handling a ToolsCallRequest, a cancellable context is created and stored.
  • When a notifications/cancelled message is received, the server looks up and calls the corresponding cancel function, stopping the ongoing task.
  • The cancel function is cleaned up after the request completes or is cancelled.

Why

Previously, cancellation requests were only logged and had no effect. This change allows clients to actually interrupt long-running or unwanted operations, improving resource usage and responsiveness.


N/A

Let me know if you want a test scenario or further improvements!

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.

1 participant