@@ -650,6 +650,46 @@ void testThrownMcpErrorAndJsonRpcError() throws Exception {
650650 mcpServer .close ();
651651 }
652652
653+ @ Test
654+ void testMissingHandlerReturnsMethodNotFoundError () throws Exception {
655+ var mcpServer = McpServer .sync (mcpStatelessServerTransport )
656+ .serverInfo ("test-server" , "1.0.0" )
657+ .capabilities (ServerCapabilities .builder ().tools (true ).build ())
658+ .build ();
659+
660+ // "server/discover" has no registered handler, and neither do the capabilities
661+ // this server does not advertise
662+ McpSchema .JSONRPCRequest jsonrpcRequest = new McpSchema .JSONRPCRequest (McpSchema .JSONRPC_VERSION ,
663+ "server/discover" , "test" , null );
664+
665+ MockHttpServletRequest request = new MockHttpServletRequest ("POST" , CUSTOM_MESSAGE_ENDPOINT );
666+ MockHttpServletResponse response = new MockHttpServletResponse ();
667+
668+ byte [] content = JSON_MAPPER .writeValueAsBytes (jsonrpcRequest );
669+ request .setContent (content );
670+ request .addHeader ("Content-Length" , Integer .toString (content .length ));
671+ request .addHeader ("Accept" , APPLICATION_JSON + ", " + TEXT_EVENT_STREAM );
672+ request .addHeader ("Content-Type" , APPLICATION_JSON );
673+ request .addHeader ("Cache-Control" , "no-cache" );
674+ request .addHeader (HttpHeaders .PROTOCOL_VERSION , ProtocolVersions .MCP_2025_03_26 );
675+
676+ mcpStatelessServerTransport .service (request , response );
677+
678+ assertThat (response .getStatus ()).isEqualTo (HttpServletResponse .SC_OK );
679+
680+ McpSchema .JSONRPCResponse jsonrpcResponse = JSON_MAPPER .readValue (response .getContentAsByteArray (),
681+ McpSchema .JSONRPCResponse .class );
682+
683+ assertThat (jsonrpcResponse ).isNotNull ();
684+ assertThat (jsonrpcResponse .id ()).isEqualTo ("test" );
685+ assertThat (jsonrpcResponse .result ()).isNull ();
686+ assertThat (jsonrpcResponse .error ()).isNotNull ();
687+ assertThat (jsonrpcResponse .error ().code ()).isEqualTo (ErrorCodes .METHOD_NOT_FOUND );
688+ assertThat (jsonrpcResponse .error ().message ()).isEqualTo ("Method not found: server/discover" );
689+
690+ mcpServer .close ();
691+ }
692+
653693 // ---------------------------------------
654694 // Bounded read
655695 // ---------------------------------------
0 commit comments