Skip to content

Fix error behavior on unregistered handlers in stateless server handler#800

Open
maff wants to merge 1 commit into
modelcontextprotocol:mainfrom
maff:fix-stateless-server-unregistered-handler-behavior
Open

Fix error behavior on unregistered handlers in stateless server handler#800
maff wants to merge 1 commit into
modelcontextprotocol:mainfrom
maff:fix-stateless-server-unregistered-handler-behavior

Conversation

@maff

@maff maff commented Feb 16, 2026

Copy link
Copy Markdown

When a client requests a resource without a registered handler despite a stateless server not advertising it, the server now returns with a JSON-RPC method not found error (-32601), aligned with the stateful implementation.

Motivation and Context

Closes issue #784

How Has This Been Tested?

Integration test covering unregistered handler response with stateless transport.

Breaking Changes

Behavioral change to return a proper JSON-RPC error instead of a HTTP 500 response. Breaking only in terms of changed behavior in case of an error.

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

@maff maff force-pushed the fix-stateless-server-unregistered-handler-behavior branch from caf8474 to 3ac4484 Compare February 16, 2026 19:40
HttpResponse<String> response;

try {
HttpRequest request = HttpRequest.newBuilder()

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

ℹ️ Not using the MCP client here as it would throw a client error due to missing capabilities advertised in initialization response.

@maff

maff commented Feb 18, 2026

Copy link
Copy Markdown
Author

@tzolov do you see a chance of this change making it into 0.18.0 and Spring AI 2.0.0? This basically leads to error logs & metrics due to internal server errors caused by misbehaving clients.

Instead of throwing an McpError, it now returns with a JSON-RPC method not found error (-32601), aligned with the stateful implementation.
@maff maff force-pushed the fix-stateless-server-unregistered-handler-behavior branch from 2d76d16 to ef0dbeb Compare March 16, 2026 10:30
@Kehrlann Kehrlann self-assigned this Jun 25, 2026

@Kehrlann Kehrlann left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hey @maff ! Sorry for the long review delay.

Thanks for your contribution. There's a nicer way to write the integ test using a client transport.

@@ -0,0 +1,40 @@
/*
* Copyright 2024-2025 the original author or authors.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
* Copyright 2024-2025 the original author or authors.
* Copyright 2026-2026 the original author or authors.

.build();

response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please use a client transport instead of raw HTTP requests.
See #1041 for inspiration


@ParameterizedTest
@ValueSource(strings = { "tools/list", "resources/list", "prompts/list" })
void testMissingHandlerReturnsMethodNotFoundError(String method) throws Exception {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Don't do a parameterized test, instead make a request with method foo/bar or any non-existing method.

@Kehrlann Kehrlann added bug Something isn't working area/server waiting for user Waiting for user feedback or more details area/transport labels Jun 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/server area/transport bug Something isn't working waiting for user Waiting for user feedback or more details

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants