LamiumAmplexicaule commented on code in PR #8665: URL: https://github.com/apache/hadoop/pull/8665#discussion_r3791601785
########## 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: > https://www.jsonrpc.org/specification A Notification is a Request object without an "id" member. Whether it’s a request or a notification should be determined by the presence or absence of the id field. -- 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]
