K0K0V0K commented on code in PR #8665: URL: https://github.com/apache/hadoop/pull/8665#discussion_r3801334731
########## 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: Thanks for raising this—agreed that JSON-RPC notifications shouldn't receive a JSON-RPC response payload. However, at the transport layer, an HTTP server still needs to return an HTTP status code for the request. https://modelcontextprotocol.io/specification/2025-06-18/basic/transports#sending-messages-to-the-server > 2.1 Sending Messages to the Server > ... > 4. If the input is a JSON-RPC response or notification: > If the server accepts the input, the server MUST return HTTP status code 202 Accepted with no body. ``` public static McpHttpResponse notification() { return new McpHttpResponse(STATUS_ACCEPTED, Collections.emptyMap(), null); } ``` Because of this rule, returning 202 Accepted with an empty body appears to be fully compliant with the specification. Let me know if I've missed anything! -- 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]
