Skip to content

fix(slackbot): create MCP server per agent run to avoid cancel scope error#1289

Merged
zzstoatzz merged 1 commit intomainfrom
fix/slackbot-mcp-cancel-scope
Feb 13, 2026
Merged

fix(slackbot): create MCP server per agent run to avoid cancel scope error#1289
zzstoatzz merged 1 commit intomainfrom
fix/slackbot-mcp-cancel-scope

Conversation

@zzstoatzz
Copy link
Copy Markdown
Collaborator

Summary

  • Moved MCPServerStreamableHTTP instantiation from module-level singleton into create_agent() so each concurrent message handler gets its own instance
  • Fixes "Attempted to exit cancel scope in a different task than it was entered in" error that occurred when concurrent Slack messages shared the singleton's anyio task group/cancel scope across asyncio.create_task boundaries

Root cause

streamablehttp_client internally uses anyio.create_task_group(), which binds a cancel scope to the async task that enters it. When Task A opened the MCP connection but Task B was the last to close it, the cancel scope teardown happened in the wrong task.

Test plan

  • Deploy slackbot and send concurrent messages to verify no cancel scope errors
  • Verify MCP slack-search tool still works correctly per-request

🤖 Generated with Claude Code

…error

The module-level MCPServerStreamableHTTP singleton was shared across
concurrent asyncio.create_task handlers. streamablehttp_client uses
anyio.create_task_group() internally, binding a cancel scope to the
entering task. When the last handler to exit was a different task than
the one that opened the connection, anyio raised "Attempted to exit
cancel scope in a different task than it was entered in".

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings February 13, 2026 18:37
@zzstoatzz zzstoatzz merged commit 8d9bc95 into main Feb 13, 2026
7 checks passed
@zzstoatzz zzstoatzz deleted the fix/slackbot-mcp-cancel-scope branch February 13, 2026 18:37
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a concurrency bug in the slackbot where a shared module-level MCP server instance caused anyio cancel scope errors when handling concurrent Slack messages. The fix moves the MCPServerStreamableHTTP instantiation from module scope into the create_agent() function, ensuring each agent run gets its own isolated instance.

Changes:

  • Removed module-level slack_search_mcp singleton that was shared across all agent runs
  • Created new MCPServerStreamableHTTP instance per-agent within create_agent() function
  • Eliminates cancel scope task mismatch errors caused by concurrent messages sharing the same MCP server context

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants