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


##########
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:
   Thanks @LamiumAmplexicaule for pointing this out!
   You right, i have to validate ID is not null.
   Also i found many other things in the linked specification what is not 
implemented now, but they are must haves.
   For example session lifecycle management and rate limiting.
   I will update the PR with these codes.



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