Skip to content

fix(server): serialize servlet errors as JSON-RPC envelopes#956

Open
ultramancode wants to merge 1 commit into
modelcontextprotocol:mainfrom
ultramancode:fix/servlet-jsonrpc-error-response
Open

fix(server): serialize servlet errors as JSON-RPC envelopes#956
ultramancode wants to merge 1 commit into
modelcontextprotocol:mainfrom
ultramancode:fix/servlet-jsonrpc-error-response

Conversation

@ultramancode
Copy link
Copy Markdown

Fixes servlet transport error serialization so servlet transports write JSON-RPC error responses for McpError instead of serializing the Java exception object.

Fixes #955

Motivation and Context

Some servlet transport error paths currently serialize McpError directly. Since McpError extends RuntimeException, the response body can include exception-shaped fields such as stackTrace, cause, localizedMessage, and jsonRpcError.

This makes servlet error responses inconsistent with the JSON-RPC error response shape expected by MCP clients.

Before:

servlet-error-before

After:

servlet-error-after

How Has This Been Tested?

Added servlet transport tests covering JSON-RPC error serialization for:

  • stateless servlet transport errors
  • streamable servlet transport errors
  • SSE servlet transport errors
  • parsed request id preservation
  • no-id transport-level rejection paths

I also verified the behavior locally with a small servlet server and Postman using the same request before and after the fix.

Breaking Changes

None intended.

The existing HTTP status behavior for transport-level rejections is preserved. The response body is changed to the expected JSON-RPC error envelope instead of serializing the McpError exception object.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Additional context

This change keeps the existing servlet transport status-code behavior for transport-level rejections, but writes a JSON-RPC error response body consistently.

When the request id has already been parsed, the response preserves it. For transport-level rejection paths where no request id is available, the response omits the id field.

References:

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.

Servlet transports serialize McpError exceptions instead of JSON-RPC error envelopes

1 participant