-
Notifications
You must be signed in to change notification settings - Fork 31
Fix dynamic MCP tool argument registration #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jstar0
wants to merge
1
commit into
lasso-security:main
Choose a base branch
from
jstar0:fix/dynamic-tool-arguments
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,166 @@ | ||
| import inspect | ||
| import asyncio | ||
|
|
||
| from mcp.server.fastmcp import FastMCP | ||
| from mcp import types | ||
|
|
||
| from mcp_gateway.gateway import register_dynamic_tool | ||
|
|
||
|
|
||
| class RecordingFastMCP: | ||
| def __init__(self): | ||
| self.registered = {} | ||
|
|
||
| def tool(self, name=None, description=None): | ||
| def decorator(fn): | ||
| self.registered[name] = fn | ||
| return fn | ||
|
|
||
| return decorator | ||
|
|
||
|
|
||
| class RecordingProxiedServer: | ||
| def __init__(self): | ||
| self.calls = [] | ||
|
|
||
| async def call_tool(self, **kwargs): | ||
| self.calls.append(kwargs) | ||
| return types.CallToolResult( | ||
| content=[types.TextContent(type="text", text="ok")] | ||
| ) | ||
|
|
||
|
|
||
| def test_dynamic_tool_accepts_json_schema_names_that_are_not_python_identifiers(): | ||
| asyncio.run(_check_dynamic_tool_accepts_json_schema_names_that_are_not_python_identifiers()) | ||
|
|
||
|
|
||
| async def _check_dynamic_tool_accepts_json_schema_names_that_are_not_python_identifiers(): | ||
| gateway_mcp = RecordingFastMCP() | ||
| proxied_server = RecordingProxiedServer() | ||
| tool = types.Tool( | ||
| name="api-get-block-children", | ||
| description="Fetch children", | ||
| inputSchema={ | ||
| "type": "object", | ||
| "properties": { | ||
| "Notion-Version": {"type": "string"}, | ||
| "1st-page": {"type": "integer"}, | ||
| "class": {"type": "string"}, | ||
| }, | ||
| "required": ["Notion-Version"], | ||
| }, | ||
| ) | ||
|
|
||
| await register_dynamic_tool( | ||
| gateway_mcp, | ||
| "notion", | ||
| tool, | ||
| proxied_server, | ||
| plugin_manager=None, | ||
| ) | ||
|
|
||
| handler = gateway_mcp.registered["notion_api-get-block-children"] | ||
| signature = inspect.signature(handler) | ||
|
|
||
| assert "Notion_Version" in signature.parameters | ||
| assert "param_1st_page" in signature.parameters | ||
| assert "param_class" in signature.parameters | ||
| assert signature.parameters["Notion_Version"].default is inspect.Parameter.empty | ||
| assert signature.parameters["param_1st_page"].default is None | ||
| assert signature.parameters["param_class"].default is None | ||
|
|
||
| await handler( | ||
| Notion_Version="2025-06-20", | ||
| param_1st_page=3, | ||
| param_class="page", | ||
| ) | ||
|
|
||
| assert proxied_server.calls[0]["name"] == "api-get-block-children" | ||
| assert proxied_server.calls[0]["arguments"] == { | ||
| "Notion-Version": "2025-06-20", | ||
| "1st-page": 3, | ||
| "class": "page", | ||
| } | ||
|
|
||
|
|
||
| def test_dynamic_tool_omits_unset_optional_arguments(): | ||
| asyncio.run(_check_dynamic_tool_omits_unset_optional_arguments()) | ||
|
|
||
|
|
||
| async def _check_dynamic_tool_omits_unset_optional_arguments(): | ||
| gateway_mcp = RecordingFastMCP() | ||
| proxied_server = RecordingProxiedServer() | ||
| tool = types.Tool( | ||
| name="search", | ||
| description="Search", | ||
| inputSchema={ | ||
| "type": "object", | ||
| "properties": { | ||
| "query": {"type": "string"}, | ||
| "start-cursor": {"type": "string"}, | ||
| }, | ||
| "required": ["query"], | ||
| }, | ||
| ) | ||
|
|
||
| await register_dynamic_tool( | ||
| gateway_mcp, | ||
| "notion", | ||
| tool, | ||
| proxied_server, | ||
| plugin_manager=None, | ||
| ) | ||
|
|
||
| handler = gateway_mcp.registered["notion_search"] | ||
| await handler(query="blocks", start_cursor=None) | ||
|
|
||
| assert proxied_server.calls[0]["arguments"] == {"query": "blocks"} | ||
|
|
||
|
|
||
| def test_dynamic_tool_schema_uses_original_json_schema_names(): | ||
| asyncio.run(_check_dynamic_tool_schema_uses_original_json_schema_names()) | ||
|
|
||
|
|
||
| async def _check_dynamic_tool_schema_uses_original_json_schema_names(): | ||
| gateway_mcp = FastMCP("test") | ||
| proxied_server = RecordingProxiedServer() | ||
| tool = types.Tool( | ||
| name="api-get-block-children", | ||
| description="Fetch children", | ||
| inputSchema={ | ||
| "type": "object", | ||
| "properties": { | ||
| "Notion-Version": {"type": "string"}, | ||
| "start-cursor": {"type": "string"}, | ||
| }, | ||
| "required": ["Notion-Version"], | ||
| }, | ||
| ) | ||
|
|
||
| await register_dynamic_tool( | ||
| gateway_mcp, | ||
| "notion", | ||
| tool, | ||
| proxied_server, | ||
| plugin_manager=None, | ||
| ) | ||
|
|
||
| tools = await gateway_mcp.list_tools() | ||
| registered_tool = next( | ||
| tool for tool in tools if tool.name == "notion_api-get-block-children" | ||
| ) | ||
|
|
||
| assert registered_tool.inputSchema["properties"].keys() == { | ||
| "Notion-Version", | ||
| "start-cursor", | ||
| } | ||
| assert registered_tool.inputSchema["required"] == ["Notion-Version"] | ||
|
|
||
| await gateway_mcp.call_tool( | ||
| "notion_api-get-block-children", | ||
| {"Notion-Version": "2025-06-20"}, | ||
| ) | ||
|
|
||
| assert proxied_server.calls[0]["arguments"] == { | ||
| "Notion-Version": "2025-06-20" | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reserve internal handler names during sanitization.
At Line 194,
used_python_namesstarts empty, so a schema key like"ctx"sanitizes to"ctx". Inmcp_gateway/gateway.pyLine 57,ctxis already reserved for context; adding anotherctxparameter at Line 76 causesinspect.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