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


##########
hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/mcp/McpRequestHandler.java:
##########
@@ -0,0 +1,217 @@
+/**
+ * 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 java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.UUID;
+
+import org.apache.hadoop.classification.InterfaceAudience;
+import org.apache.hadoop.classification.InterfaceStability;
+import org.apache.hadoop.mcp.McpSchema.CallToolResult;
+import org.apache.hadoop.mcp.McpSchema.ServerCapabilities;
+import org.apache.hadoop.mcp.McpSchema.TextContent;
+import org.apache.hadoop.mcp.McpSchema.Tool;
+import org.apache.hadoop.mcp.McpServer.RegisteredTool;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+
+/**
+ * Transport-neutral MCP JSON-RPC request handler.
+ */
[email protected]
[email protected]
+public final class McpRequestHandler {
+
+  public static final String SESSION_HEADER = "MCP-Session-Id";
+
+  private static final String DEFAULT_PROTOCOL_VERSION = "2025-06-18";
+  private static final List<String> SUPPORTED_PROTOCOL_VERSIONS = List.of(
+      "2024-11-05",
+      "2025-03-26",
+      DEFAULT_PROTOCOL_VERSION);
+
+  private final ObjectMapper objectMapper;
+  private final String serverName;
+  private final String serverVersion;
+  private final ServerCapabilities capabilities;
+  private final Map<String, RegisteredTool> tools;
+
+  McpRequestHandler(ObjectMapper objectMapper, String serverName, String 
serverVersion,
+      ServerCapabilities capabilities, Map<String, RegisteredTool> tools) {
+    this.objectMapper = objectMapper;
+    this.serverName = serverName;
+    this.serverVersion = serverVersion;
+    this.capabilities = capabilities;
+    this.tools = Collections.unmodifiableMap(new LinkedHashMap<>(tools));
+  }
+
+  public McpHttpResponse handle(JsonNode requestNode) {
+    return handle(requestNode, new McpCallContext(null));
+  }
+
+  public McpHttpResponse parseErrorResponse() {
+    return errorResponse(null, McpJsonRpc.PARSE_ERROR, 
McpJsonRpc.PARSE_ERROR_MESSAGE);
+  }
+
+  public McpHttpResponse requestBodyTooLargeResponse() {
+    return errorResponse(null, McpJsonRpc.INVALID_REQUEST,
+        McpJsonRpc.REQUEST_BODY_TOO_LARGE_MESSAGE);
+  }
+
+  public McpHttpResponse handle(JsonNode requestNode, McpCallContext context) {
+    if (requestNode == null || requestNode.isNull() || 
!requestNode.isObject()) {
+      return errorResponse(null, McpJsonRpc.INVALID_REQUEST, 
McpJsonRpc.INVALID_REQUEST_MESSAGE);
+    }
+
+    JsonNode methodNode = requestNode.get("method");
+    if (methodNode == null || methodNode.isNull()) {
+      return errorResponse(requestNode.get("id"), McpJsonRpc.INVALID_REQUEST,
+          McpJsonRpc.INVALID_REQUEST_MESSAGE);
+    }
+
+    String method = methodNode.asText();
+    if (method.startsWith("notifications/")) {
+      return McpHttpResponse.notification();
+    }
+
+    JsonNode idNode = requestNode.get("id");

Review Comment:
   I checked what's missing to achieve full compatibility with the 2025-06-18 
core requirements, and I estimate around 1,500 lines of new code are needed to 
fully cover them.
   Since this PR is already quite large, I propose creating a single follow-up 
Jira ticket to address these remaining gaps in a separate PR. However, I'm open 
to adding them to the current PR if you'd prefer—let me know your preference!
   Proposed Jira Ticket: Complete MCP 2025-06-18 Core Specification Compliance
   Scope:
   1.  JSON-RPC envelope validation
     ⚬ Validate jsonrpc is "2.0"
     ⚬ Validate MCP ID rules (string/integer, non-null; notifications omit ID)
     ⚬ https://modelcontextprotocol.io/specification/2025-06-18/basic#requests
   2.  MCP session lifecycle management
     ⚬ Session creation on initialize
     ⚬ Mcp-Session-Id header on responses
     ⚬ Gate tools/* for valid sessions
     ⚬ Session idle TTL + expired session handling
     ⚬ https://modelcontextprotocol.io/specification/2025-06-18/basic/lifecycle
   3.  Duplicate request ID rejection per session
     ⚬ Track used IDs per session
     ⚬ Return duplicate-ID error on reuse
     ⚬ https://modelcontextprotocol.io/specification/2025-06-18/basic#requests
   4.  Streamable HTTP transport validation
     ⚬ 
https://modelcontextprotocol.io/specification/2025-06-18/basic/transports#sending-messages-to-the-server
   5.  Tool output sanitization and rate limiting
     ⚬ 
https://modelcontextprotocol.io/specification/2025-06-18/server/tools#security-considerations
   



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