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


##########
hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/mcp/McpRequestHandler.java:
##########
@@ -0,0 +1,187 @@
+/**
+ * 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.ServerCapabilities;
+import org.apache.hadoop.mcp.McpSchema.Tool;
+import org.apache.hadoop.mcp.McpServer.RegisteredTool;
+
+import com.fasterxml.jackson.core.type.TypeReference;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+
+/**
+ * Transport-neutral MCP JSON-RPC request handler.
+ */
[email protected]
[email protected]
+public final class McpRequestHandler {
+
+  public static final String SESSION_HEADER = "Mcp-Session-Id";
+
+  public static final String METHOD_INITIALIZE = "initialize";
+  public static final String METHOD_TOOLS_LIST = "tools/list";
+  public static final String METHOD_TOOLS_CALL = "tools/call";
+
+  private static final String TOOL_EXECUTION_FAILED_MESSAGE = "Tool execution 
failed";
+
+  private static final TypeReference<Map<String, Object>> PARAMS_TYPE = new 
TypeReference<>() {};
+
+  private final ObjectMapper objectMapper;
+  private final String serverName;
+  private final String serverVersion;
+  private final ServerCapabilities capabilities;
+  private final Map<String, RegisteredTool> tools;
+  private final McpJsonRpcResponses jsonRpcResponses;
+
+  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));
+    this.jsonRpcResponses = new McpJsonRpcResponses(objectMapper);
+  }
+
+  public McpHttpResponse handle(JsonNode requestNode) {
+    return handle(requestNode, new McpCallContext(null));
+  }
+
+  public McpHttpResponse parseErrorResponse() {
+    return jsonRpcResponses.error(null, McpJsonRpc.PARSE_ERROR, 
McpJsonRpc.PARSE_ERROR_MESSAGE);
+  }
+
+  public McpHttpResponse requestBodyTooLargeResponse() {
+    return jsonRpcResponses.error(null, McpJsonRpc.INVALID_REQUEST,
+        McpJsonRpc.REQUEST_BODY_TOO_LARGE_MESSAGE);
+  }
+
+  public McpHttpResponse handle(JsonNode requestNode, McpCallContext context) {
+    McpHttpResponse error = McpJsonRpcValidator.validate(requestNode, 
jsonRpcResponses);
+    if (error != null) {
+      return error;
+    }
+
+    String method = requestNode.get("method").asText();
+    if (McpJsonRpcValidator.isNotification(method)) {
+      return McpHttpResponse.notification();
+    }
+
+    JsonNode idNode = requestNode.get("id");
+    JsonNode paramsNode = requestNode.get("params");
+    if (paramsNode != null && !paramsNode.isNull() && !paramsNode.isObject()) {
+      return jsonRpcResponses.error(idNode, McpJsonRpc.INVALID_PARAMS,
+          "params must be a JSON object");
+    }
+    Map<String, Object> params = paramsNode == null || paramsNode.isNull()
+        ? Collections.emptyMap()
+        : objectMapper.convertValue(paramsNode, PARAMS_TYPE);

Review Comment:
   We currently accept `"params": null`, but the JSON-RPC 2.0 specification and 
the MCP schema definitions do not allow it, so we should reject it.
   
   > https://www.jsonrpc.org/specification
   If present, parameters for the rpc call MUST be provided as a Structured 
value. Either by-position through an Array or by-name through an Object.
   
   > 
https://modelcontextprotocol.io/specification/2025-06-18/basic/index#requests
     params?: {
       [key: string]: unknown;
     };



##########
hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/mcp/McpRequestHandler.java:
##########
@@ -0,0 +1,187 @@
+/**
+ * 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.ServerCapabilities;
+import org.apache.hadoop.mcp.McpSchema.Tool;
+import org.apache.hadoop.mcp.McpServer.RegisteredTool;
+
+import com.fasterxml.jackson.core.type.TypeReference;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+
+/**
+ * Transport-neutral MCP JSON-RPC request handler.
+ */
[email protected]
[email protected]
+public final class McpRequestHandler {
+
+  public static final String SESSION_HEADER = "Mcp-Session-Id";
+
+  public static final String METHOD_INITIALIZE = "initialize";
+  public static final String METHOD_TOOLS_LIST = "tools/list";
+  public static final String METHOD_TOOLS_CALL = "tools/call";
+
+  private static final String TOOL_EXECUTION_FAILED_MESSAGE = "Tool execution 
failed";
+
+  private static final TypeReference<Map<String, Object>> PARAMS_TYPE = new 
TypeReference<>() {};
+
+  private final ObjectMapper objectMapper;
+  private final String serverName;
+  private final String serverVersion;
+  private final ServerCapabilities capabilities;
+  private final Map<String, RegisteredTool> tools;
+  private final McpJsonRpcResponses jsonRpcResponses;
+
+  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));
+    this.jsonRpcResponses = new McpJsonRpcResponses(objectMapper);
+  }
+
+  public McpHttpResponse handle(JsonNode requestNode) {
+    return handle(requestNode, new McpCallContext(null));
+  }
+
+  public McpHttpResponse parseErrorResponse() {
+    return jsonRpcResponses.error(null, McpJsonRpc.PARSE_ERROR, 
McpJsonRpc.PARSE_ERROR_MESSAGE);
+  }
+
+  public McpHttpResponse requestBodyTooLargeResponse() {
+    return jsonRpcResponses.error(null, McpJsonRpc.INVALID_REQUEST,
+        McpJsonRpc.REQUEST_BODY_TOO_LARGE_MESSAGE);
+  }
+
+  public McpHttpResponse handle(JsonNode requestNode, McpCallContext context) {
+    McpHttpResponse error = McpJsonRpcValidator.validate(requestNode, 
jsonRpcResponses);
+    if (error != null) {
+      return error;
+    }
+
+    String method = requestNode.get("method").asText();
+    if (McpJsonRpcValidator.isNotification(method)) {
+      return McpHttpResponse.notification();
+    }

Review Comment:
   > 
https://modelcontextprotocol.io/specification/2025-06-18/basic/index#notifications
   Notifications are sent from the client to the server or vice versa, as a 
one-way message. The receiver MUST NOT send a response.
   
   If it’s a notification, the receiver must not send a response.



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