Skip to content

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

Description

@ultramancode

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

Bug description

Some servlet transport error paths serialize McpError directly instead of returning a JSON-RPC error response envelope.

Because McpError extends RuntimeException, the response body can expose exception-shaped fields such as:

  • stackTrace
  • cause
  • localizedMessage
  • jsonRpcError

For example, on current main, a servlet transport validation error returns a body like this:

Image

This is not the JSON-RPC response shape clients expect.

Environment

  • MCP Java SDK version: current main
  • Java version: 21
  • Transport: servlet server transports

Steps to reproduce

  1. Run an MCP server with a servlet transport.
  2. Send a request that triggers a servlet transport error path.
  3. Inspect the response body.

Expected behavior

The response body should look like:

{
  "jsonrpc": "2.0",
  "error": {
    "code": -32601,
    "message": "Both application/json and text/event-stream required in Accept header"
  }
}

Activity

  1. ultramancode commented on May 12, 2026

    @ultramancode
    Author

    I opened a follow-up PR for this issue: #956

  2. added
    bugSomething isn't working
    ready for workThe goal is clear and work towards it can be commenced
    P2Moderate issues affecting some users, edge cases, potentially valuable feature
    on Aug 17, 2026
  3. added theissue type on Aug 17, 2026
  4. yuchen-liu-everlaw commented on Aug 18, 2026

    @yuchen-liu-everlaw

    I ran into a similar issue. A malformed payload such as below, emits a ServerResponse of McpError in WebMvcStatelessServerTransport. But McpError does not get translate into a message by DefaultEntityResponse from spring-webmvc (7.0.8). It emits a HttpMediaTypeNotAcceptableException instead of a JSON-PRC error. Can be reproduced by:

    curl -i -sS -X POST http://localhost:8080/mcp \
      -H 'Content-Type: application/json' \
      -H 'Accept: application/json, text/event-stream' \
      -d '{"jsonrpc":"2.0","id":1,"method":"tools/list"'
  5. ultramancode commented on Aug 25, 2026

    @ultramancode
    Author

    @yuchen-liu-everlaw
    Thanks for sharing this. The WebMVC transport used to be part of the MCP Java SDK, but it moved to the Spring AI project for its 2.0
    release. I opened an issue and a PR for this:

    Issue: spring-projects/spring-ai#6855
    PR: spring-projects/spring-ai#6856

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Moderate issues affecting some users, edge cases, potentially valuable featurebugSomething isn't workingready for workThe goal is clear and work towards it can be commenced

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions