Fix dynamic MCP tool argument registration#21
Conversation
📝 WalkthroughWalkthroughThe PR fixes dynamic tool registration failures when MCP tool parameter names contain characters invalid in Python identifiers (e.g., hyphens). A new ChangesSafe Python identifier generation for dynamic tool registration
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 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.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@mcp_gateway/config.py`:
- Around line 194-196: The used_python_names set in the parameter sanitization
logic starts empty, allowing reserved parameter names like ctx (which is already
reserved in gateway.py) to be sanitized and added as duplicate parameters,
causing inspect.Signature to fail. Pre-populate the used_python_names set with
reserved/internal parameter names such as ctx before iterating through the
properties.items() loop to ensure the sanitization process respects these
reserved names and prevents duplicate parameters.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: b62888ca-435f-490b-849c-259b49f1eb10
📒 Files selected for processing (3)
mcp_gateway/config.pymcp_gateway/gateway.pytests/test_dynamic_tool_registration.py
📜 Review details
🔇 Additional comments (1)
tests/test_dynamic_tool_registration.py (1)
33-167: LGTM!
| used_python_names = set() | ||
| for param_name, param_schema in properties.items(): | ||
| param_type = Any # Default type |
There was a problem hiding this comment.
Reserve internal handler names during sanitization.
At Line 194, used_python_names starts empty, so a schema key like "ctx" sanitizes to "ctx". In mcp_gateway/gateway.py Line 57, ctx is already reserved for context; adding another ctx parameter at Line 76 causes inspect.Signature(...) to fail with a duplicate-parameter error, and tool registration breaks.
Suggested fix
def get_tool_params_description(tool: types.Tool) -> List[ToolParamDescription]:
param_signatures = []
@@
- used_python_names = set()
+ # Reserve handler-internal argument names used by gateway.create_typed_handler
+ used_python_names = {"ctx"}Also applies to: 224-237
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@mcp_gateway/config.py` around lines 194 - 196, The used_python_names set in
the parameter sanitization logic starts empty, allowing reserved parameter names
like ctx (which is already reserved in gateway.py) to be sanitized and added as
duplicate parameters, causing inspect.Signature to fail. Pre-populate the
used_python_names set with reserved/internal parameter names such as ctx before
iterating through the properties.items() loop to ensure the sanitization process
respects these reserved names and prevents duplicate parameters.
Summary
Fixes #19 and #20.
Dynamic tool registration can now handle downstream MCP tools whose JSON Schema argument names are not valid Python identifiers, such as
Notion-Version, while still exposing the original MCP argument names to clients.Changes
inputSchema.requiredso required arguments stay required and optional arguments receive defaults.Verification
Summary by CodeRabbit
Release Notes
Bug Fixes