LamiumAmplexicaule commented on code in PR #8665:
URL: https://github.com/apache/hadoop/pull/8665#discussion_r3796652236


##########
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:
   I think that message should be treated as a notification.
   The reason is that I think the following should be MCP guiding principle:
   
   > 
https://modelcontextprotocol.io/specification/2025-06-18/basic/index#messages
   1 Messages
   All messages between MCP clients and servers MUST follow the JSON-RPC 2.0 
specification.
   
   Unless the MCP specification explicitly states otherwise, we should follow 
JSON-RPC 2.0.



-- 
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]

Reply via email to