Skip to content

fix(prompts): return INVALID_PARAMS (-32602) on wrong-type or extra arguments (#3974) - #3977

Open
Abdulrehman1978 wants to merge 1 commit into
modelcontextprotocol:mainfrom
Abdulrehman1978:fix/prompt-invalid-args-error-code
Open

Abdulrehman1978 wants to merge 1 commit into
modelcontextprotocol:mainfrom
Abdulrehman1978:fix/prompt-invalid-args-error-code

Conversation

@Abdulrehman1978

Copy link
Copy Markdown

Summary

When a prompt is rendered with a wrong-type or unknown/extra argument, Prompt.render() previously let validate_call fail inside the try: block and converted it to a generic ValueError, causing the server to return an internal error (-32603 / 0) instead of INVALID_PARAMS (-32602).

Fixes #3974

Changes

  • Store fn_metadata (FuncMetadata) on Prompt in Prompt.from_function().
  • In Prompt.render(), reject unknown/extra arguments against self.arguments with MCPError(code=INVALID_PARAMS, ...) and validate argument types with self.fn_metadata.validate_arguments(...) before invoking self.fn, translating ValidationError into MCPError(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).
  • Updated tests/interaction/mcpserver/test_prompts.py and 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!)

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Wrong or extra prompt arguments return an internal error instead of -32602

1 participant