Skip to content
This repository was archived by the owner on Apr 11, 2026. It is now read-only.

Fix: Preserve McpError semantics when prompt methods are invoked via reflection#95

Open
vinsguru wants to merge 1 commit intospring-ai-community:mainfrom
vinsguru:mpc-error
Open

Fix: Preserve McpError semantics when prompt methods are invoked via reflection#95
vinsguru wants to merge 1 commit intospring-ai-community:mainfrom
vinsguru:mpc-error

Conversation

@vinsguru
Copy link
Copy Markdown

Problem

Prompt methods are invoked using Method.invoke, which wraps user-thrown exceptions in InvocationTargetException.
Because of this, McpError thrown by prompt implementations is never detected via instanceof checks and gets overridden by a generic error, losing its JSON-RPC code, message, and data.

Root Cause

Reflection wraps the original exception. The existing catch block checks Exception instanceof McpError, which is guaranteed to fail when the error is thrown from the invoked method.

Solution

  • Unwrap InvocationTargetException to obtain the real cause.
  • Re-throw McpError unchanged when encountered.
  • Convert all other exceptions into a structured McpError with a meaningful JSON-RPC error response.

Behavioral Changes

  • McpError thrown by prompt methods now propagates correctly.
  • Runtime exceptions from prompt methods are mapped to McpError instead of being swallowed or replaced.
  • Error details are preserved and testable.

Tests

  • Added/Updated coverage to verify:
    • Runtime exceptions are converted into McpError
    • Existing McpError instances are not overridden

This fixes #94

Unwrap InvocationTargetException to correctly detect and propagate
McpError thrown by prompt implementations. This preserves JSON-RPC
error code, message, and data instead of overriding them with a
generic error.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MCP server rewrites all custom JSON-RPC error codes to -32062 (cannot override even with McpError)

1 participant