Fix: Skip currently executing file during tool discovery #668
+110
−57
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.
The Bug:
When an entrypoint file imports its parent package and calls add_tools_from_module() on that package, and the same entrypoint file also defines tools using @app.tool or @tool decorators, then the server fails to start with an
AttributeError. This is because the tools would be discovered via AST parsing, but those tools weren't added to the module's namespace yet because the file is still executing.For example, this would fail on startup:
The fix:
Skip the entrypoint file. This means that any tool defined inside of the entrypoint file must be added via MCPApp.add_tool(...) or instead use the recommended @app.tool.