Repository navigation
fix(prompts): return INVALID_PARAMS (-32602) on wrong-type or extra arguments (#3974) - #3977
Open
Abdulrehman1978 wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
1 issue found across 3 files
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="src/mcp/server/mcpserver/prompts/base.py">
<violation number="1" location="src/mcp/server/mcpserver/prompts/base.py:190">
P2: `validate_arguments()` transforms each argument before `self.fn` runs, but `self.fn` is still wrapped in `validate_call`, so custom Pydantic validators run twice and can change valid values again. Call the original function after metadata validation instead of validating the already-validated arguments a second time.</violation>
</file>
Reply to a comment to ask cubic a question or push back. It learns from your replies.
Re-trigger cubic
|
|
||
| if self.fn_metadata is not None: | ||
| try: | ||
| validated_args = self.fn_metadata.validate_arguments(provided_args) |
There was a problem hiding this comment.
P2: validate_arguments() transforms each argument before self.fn runs, but self.fn is still wrapped in validate_call, so custom Pydantic validators run twice and can change valid values again. Call the original function after metadata validation instead of validating the already-validated arguments a second time.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At src/mcp/server/mcpserver/prompts/base.py, line 190:
<comment>`validate_arguments()` transforms each argument before `self.fn` runs, but `self.fn` is still wrapped in `validate_call`, so custom Pydantic validators run twice and can change valid values again. Call the original function after metadata validation instead of validating the already-validated arguments a second time.</comment>
<file context>
@@ -170,17 +172,30 @@ async def render(
+
+ if self.fn_metadata is not None:
+ try:
+ validated_args = self.fn_metadata.validate_arguments(provided_args)
+ except ValidationError as exc:
+ raise MCPError(code=INVALID_PARAMS, message=f"Invalid arguments for prompt {self.name}") from exc
</file context>
This branch has not been deployed
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
When a prompt is rendered with a wrong-type or unknown/extra argument,
Prompt.render()previously letvalidate_callfail inside thetry:block and converted it to a genericValueError, causing the server to return an internal error (-32603/0) instead ofINVALID_PARAMS(-32602).Fixes #3974
Changes
fn_metadata(FuncMetadata) onPromptinPrompt.from_function().Prompt.render(), reject unknown/extra arguments againstself.argumentswithMCPError(code=INVALID_PARAMS, ...)and validate argument types withself.fn_metadata.validate_arguments(...)before invokingself.fn, translatingValidationErrorintoMCPError(code=INVALID_PARAMS, message=f"Invalid arguments for prompt {self.name}")(preserving the withholding of Pydantic internals, while keeping internal prompt-body errors as internal errors).tests/interaction/mcpserver/test_prompts.pyand added unit + interaction tests for wrong-type and extra prompt arguments.Testing
uv --directory python-sdk run --no-sync pytest tests/server/mcpserver/prompts/ tests/interaction/mcpserver/test_prompts.py -q(74 passed)uv --directory python-sdk run --no-sync ruff check src/mcp/server/mcpserver/prompts/base.py tests/interaction/mcpserver/test_prompts.py tests/server/mcpserver/prompts/test_base.py(All checks passed!)