K0K0V0K commented on code in PR #8665: URL: https://github.com/apache/hadoop/pull/8665#discussion_r3794516231
########## hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/mcp/McpJsonRpcValidator.java: ########## @@ -0,0 +1,88 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.mcp; + +import org.apache.hadoop.classification.InterfaceAudience; +import org.apache.hadoop.classification.InterfaceStability; + +import com.fasterxml.jackson.databind.JsonNode; + +/** + * Validates JSON-RPC envelope fields required by MCP 2025-06-18. + */ [email protected] [email protected] +final class McpJsonRpcValidator { + + private McpJsonRpcValidator() { + } + + /** + * @return an error response when the envelope is invalid, otherwise {@code null} + */ + static McpHttpResponse validate(JsonNode requestNode, McpJsonRpcResponses responses) { + if (requestNode == null || requestNode.isNull() || !requestNode.isObject()) { + return responses.error(null, McpJsonRpc.INVALID_REQUEST, McpJsonRpc.INVALID_REQUEST_MESSAGE); + } + + JsonNode jsonrpcNode = requestNode.get("jsonrpc"); + if (jsonrpcNode == null || jsonrpcNode.isNull() + || !jsonrpcNode.isTextual() + || !McpJsonRpc.VERSION.equals(jsonrpcNode.asText())) { + return responses.error(responseId(requestNode), McpJsonRpc.INVALID_REQUEST, + McpJsonRpc.INVALID_REQUEST_MESSAGE); + } + + JsonNode methodNode = requestNode.get("method"); + if (methodNode == null || methodNode.isNull() || !methodNode.isTextual() + || methodNode.asText().isEmpty()) { + return responses.error(responseId(requestNode), McpJsonRpc.INVALID_REQUEST, + McpJsonRpc.INVALID_REQUEST_MESSAGE); + } + + String method = methodNode.asText(); + if (isNotification(method)) { + if (requestNode.has("id")) { + return responses.error(responseId(requestNode), McpJsonRpc.INVALID_REQUEST, + McpJsonRpc.INVALID_REQUEST_MESSAGE); + } + return null; + } + + if (!isValidRequestId(requestNode.get("id"))) { + return responses.error(null, McpJsonRpc.INVALID_REQUEST, McpJsonRpc.INVALID_REQUEST_MESSAGE); + } + return null; + } + + static boolean isNotification(String method) { + return method.startsWith("notifications/"); + } Review Comment: Thanks for pointing this out; however, I am not 100% sure how this should be fixed... Let's say a client sends a message like: `{"jsonrpc":"2.0","method":"tools/list"}` According to the JSON-RPC spec (https://www.jsonrpc.org/specification): > A Notification is a Request object without an "id" member. ... no Response object needs to be returned to the client. But the MCP spec says (https://modelcontextprotocol.io/specification/2025-06-18/basic/index): > 1.1 Requests > Requests MUST include a string or integer ID. > Unlike base JSON-RPC, the ID MUST NOT be null. > > 1.3 Notifications > Notifications MUST NOT include an ID. And for tool listing (https://modelcontextprotocol.io/specification/2025-06-18/server/tools): > 3.1 Listing Tools > To discover available tools, clients send a tools/list request. Is this example message is a notification, or is it an invalid MCP request? The MCP docs also state: > 1 Messages > All messages between MCP clients and servers MUST follow the JSON-RPC 2.0 specification. What do you think, what would be the right behaviour in this case? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
