Fix bridge server crash with newer FastMCP versions#38
Conversation
FastMCP no longer exposes `_tool_manager._tools` as a public attribute. Use the public `remove_tool()` API with a fallback to the old internal API for backward compatibility. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughThe Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
src/napari_mcp/bridge_server.py (1)
122-128: Narrow exception handling and consider adding debug logging.The fallback logic is reasonable for backward compatibility, but can be improved:
(AttributeError, Exception)is redundant—Exceptionalready coversAttributeError. The second catch on line 124 should catch onlyAttributeErrorsince the_tool_manager._tools.pop()call won't raise exceptions with the defaultNone.- Catching broad
Exceptionin the first try-except (line 123) could mask unexpected errors. Sinceremove_tool()is now a stable API in FastMCP v2.3.4+, consider catching onlyAttributeError(for older versions) or letting other exceptions propagate to aid troubleshooting.- Silent failure provides no feedback if removal fails unexpectedly. Adding a debug log when both approaches fail would help with future troubleshooting.
♻️ Optional: Improved exception handling with logging
+ import logging + logger = logging.getLogger(__name__) + # Remove lifecycle tools that should not be available in bridge mode for name in ("close_viewer", "init_viewer"): try: self.server.remove_tool(name) - except (AttributeError, Exception): + except AttributeError: + # remove_tool() not available in FastMCP < v2.3.4 try: self.server._tool_manager._tools.pop(name, None) - except (AttributeError, Exception): - pass + except AttributeError: + logger.debug("Could not remove tool %r using any method", name) + except Exception as e: + logger.debug("remove_tool(%r) failed unexpectedly: %s", name, e)🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/napari_mcp/bridge_server.py` around lines 122 - 128, Replace the broad exception handling around tool removal so only AttributeError is caught for backward-compatibility: attempt self.server.remove_tool(name) and except AttributeError try the fallback self.server._tool_manager._tools.pop(name, None); if that also raises AttributeError or returns None/logically indicates failure, emit a debug log (via self.logger or module logger) describing the tool name and failure; do not catch Exception globally so other errors from remove_tool propagate for visibility.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@src/napari_mcp/bridge_server.py`:
- Around line 122-128: Replace the broad exception handling around tool removal
so only AttributeError is caught for backward-compatibility: attempt
self.server.remove_tool(name) and except AttributeError try the fallback
self.server._tool_manager._tools.pop(name, None); if that also raises
AttributeError or returns None/logically indicates failure, emit a debug log
(via self.logger or module logger) describing the tool name and failure; do not
catch Exception globally so other errors from remove_tool propagate for
visibility.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 8560aa5b-f1a6-46d3-9d7e-e6a3eb97111d
📒 Files selected for processing (1)
src/napari_mcp/bridge_server.py
Address review feedback: catch only AttributeError for backward compatibility instead of broad Exception, and log debug messages when tool removal fails. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
|
CodeRabbit feedback is integrated, ready for review. |
Summary
NapariBridgeServer.__init__crashes withAttributeError: 'FastMCP' object has no attribute '_tool_manager'on newer versions of FastMCPremove_tool()API instead, with a fallback to the old_tool_manager._tools.pop()for backward compatibility🤖 Generated with Claude Code
Summary by CodeRabbit