|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) 2025 Huawei Technologies Co., Ltd. All rights reserved. |
| 3 | + * This file is a part of the ModelEngine Project. |
| 4 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 5 | + *--------------------------------------------------------------------------------------------*/ |
| 6 | + |
| 7 | +package modelengine.fel.tool.mcp.server; |
| 8 | + |
| 9 | +import static modelengine.fitframework.inspection.Validation.notBlank; |
| 10 | +import static modelengine.fitframework.inspection.Validation.notNull; |
| 11 | + |
| 12 | +import modelengine.fel.tool.mcp.server.entity.JsonRpcEntity; |
| 13 | +import modelengine.fel.tool.mcp.server.handler.InitializeHandler; |
| 14 | +import modelengine.fel.tool.mcp.server.handler.ToolCallHandler; |
| 15 | +import modelengine.fel.tool.mcp.server.handler.ToolListHandler; |
| 16 | +import modelengine.fel.tool.mcp.server.handler.UnsupportedMethodHandler; |
| 17 | +import modelengine.fit.http.annotation.GetMapping; |
| 18 | +import modelengine.fit.http.annotation.PostMapping; |
| 19 | +import modelengine.fit.http.annotation.RequestBody; |
| 20 | +import modelengine.fit.http.annotation.RequestQuery; |
| 21 | +import modelengine.fit.http.entity.TextEvent; |
| 22 | +import modelengine.fit.http.server.HttpClassicServerResponse; |
| 23 | +import modelengine.fitframework.annotation.Component; |
| 24 | +import modelengine.fitframework.annotation.Fit; |
| 25 | +import modelengine.fitframework.annotation.Value; |
| 26 | +import modelengine.fitframework.flowable.Choir; |
| 27 | +import modelengine.fitframework.flowable.Emitter; |
| 28 | +import modelengine.fitframework.log.Logger; |
| 29 | +import modelengine.fitframework.schedule.ExecutePolicy; |
| 30 | +import modelengine.fitframework.schedule.Task; |
| 31 | +import modelengine.fitframework.schedule.ThreadPoolScheduler; |
| 32 | +import modelengine.fitframework.serialization.ObjectSerializer; |
| 33 | +import modelengine.fitframework.util.CollectionUtils; |
| 34 | +import modelengine.fitframework.util.MapUtils; |
| 35 | +import modelengine.fitframework.util.StringUtils; |
| 36 | +import modelengine.fitframework.util.UuidUtils; |
| 37 | + |
| 38 | +import java.util.ArrayList; |
| 39 | +import java.util.HashMap; |
| 40 | +import java.util.List; |
| 41 | +import java.util.Map; |
| 42 | +import java.util.concurrent.ConcurrentHashMap; |
| 43 | + |
| 44 | +/** |
| 45 | + * FIT MCP Server controller. |
| 46 | + * |
| 47 | + * @author 季聿阶 |
| 48 | + * @since 2025-05-13 |
| 49 | + */ |
| 50 | +@Component |
| 51 | +public class McpController { |
| 52 | + private static final Logger log = Logger.get(McpController.class); |
| 53 | + private static final String MESSAGE_PATH = "/mcp/message"; |
| 54 | + private static final String EVENT_ENDPOINT = "endpoint"; |
| 55 | + private static final String EVENT_MESSAGE = "message"; |
| 56 | + private static final String METHOD_INITIALIZE = "initialize"; |
| 57 | + private static final String METHOD_TOOLS_LIST = "tools/list"; |
| 58 | + private static final String METHOD_TOOLS_CALL = "tools/call"; |
| 59 | + private static final String RESPONSE_OK = StringUtils.EMPTY; |
| 60 | + |
| 61 | + private final Map<String, Emitter<TextEvent>> emitters = new ConcurrentHashMap<>(); |
| 62 | + private final Map<String, HttpClassicServerResponse> responses = new ConcurrentHashMap<>(); |
| 63 | + private final Map<String, MessageHandler> methodHandlers = new HashMap<>(); |
| 64 | + private final MessageHandler unsupportedMethodHandler = new UnsupportedMethodHandler(); |
| 65 | + private final String baseUrl; |
| 66 | + private final ObjectSerializer serializer; |
| 67 | + |
| 68 | + /** |
| 69 | + * Constructs a new instance of the McpController class. |
| 70 | + * |
| 71 | + * @param baseUrl The base URL for the MCP server as a {@link String}, used to construct message endpoints. |
| 72 | + * @param serializer The JSON serializer used to serialize and deserialize RPC messages, as an |
| 73 | + * {@link ObjectSerializer}. |
| 74 | + * @param mcpServer The MCP server instance used to handle tool operations such as initialization, |
| 75 | + * listing tools, and calling tools, as a {@link McpServer}. |
| 76 | + */ |
| 77 | + public McpController(@Value("${base-url}") String baseUrl, @Fit(alias = "json") ObjectSerializer serializer, |
| 78 | + McpServer mcpServer) { |
| 79 | + this.baseUrl = notBlank(baseUrl, "The base URL for MCP server cannot be blank."); |
| 80 | + this.serializer = notNull(serializer, "The json serializer cannot be null."); |
| 81 | + notNull(mcpServer, "The MCP server cannot be null."); |
| 82 | + |
| 83 | + this.methodHandlers.put(METHOD_INITIALIZE, new InitializeHandler(mcpServer)); |
| 84 | + this.methodHandlers.put(METHOD_TOOLS_LIST, new ToolListHandler(mcpServer)); |
| 85 | + this.methodHandlers.put(METHOD_TOOLS_CALL, new ToolCallHandler(mcpServer, this.serializer)); |
| 86 | + |
| 87 | + ThreadPoolScheduler channelDetectorScheduler = ThreadPoolScheduler.custom() |
| 88 | + .corePoolSize(1) |
| 89 | + .isDaemonThread(true) |
| 90 | + .threadPoolName("mcp-server-channel-detector") |
| 91 | + .build(); |
| 92 | + channelDetectorScheduler.schedule(Task.builder().policy(ExecutePolicy.fixedDelay(10000)).runnable(() -> { |
| 93 | + if (MapUtils.isEmpty(this.responses)) { |
| 94 | + return; |
| 95 | + } |
| 96 | + List<String> toRemoved = new ArrayList<>(); |
| 97 | + for (Map.Entry<String, HttpClassicServerResponse> entry : this.responses.entrySet()) { |
| 98 | + if (entry.getValue().isActive()) { |
| 99 | + continue; |
| 100 | + } |
| 101 | + toRemoved.add(entry.getKey()); |
| 102 | + } |
| 103 | + if (CollectionUtils.isEmpty(toRemoved)) { |
| 104 | + return; |
| 105 | + } |
| 106 | + toRemoved.forEach(this.responses::remove); |
| 107 | + toRemoved.forEach(this.emitters::remove); |
| 108 | + log.info("Channels are inactive, remove emitters and responses. [sessionIds={}]", toRemoved); |
| 109 | + }).build()); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Creates a Server-Sent Events (SSE) channel for real-time communication with the client. |
| 114 | + * |
| 115 | + * <p>This method generates a unique session ID and registers an emitter to send events.</p> |
| 116 | + * |
| 117 | + * @param response The HTTP server response object used to manage the SSE connection as a |
| 118 | + * {@link HttpClassicServerResponse}. |
| 119 | + * @return A {@link Choir}{@code <}{@link TextEvent}{@code >} object that emits text events to the connected client. |
| 120 | + */ |
| 121 | + @GetMapping(path = "/sse") |
| 122 | + public Choir<TextEvent> createSse(HttpClassicServerResponse response) { |
| 123 | + String sessionId = UuidUtils.randomUuidString(); |
| 124 | + this.responses.put(sessionId, response); |
| 125 | + log.info("New SSE channel for MCP server created. [sessionId={}]", sessionId); |
| 126 | + return Choir.create(emitter -> { |
| 127 | + emitters.put(sessionId, emitter); |
| 128 | + TextEvent textEvent = TextEvent.custom() |
| 129 | + .id(sessionId) |
| 130 | + .event(EVENT_ENDPOINT) |
| 131 | + .data(this.baseUrl + MESSAGE_PATH + "?sessionId=" + sessionId) |
| 132 | + .build(); |
| 133 | + emitter.emit(textEvent); |
| 134 | + }); |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Receives and processes an MCP message via HTTP POST request. |
| 139 | + * |
| 140 | + * <p>This method handles incoming JSON-RPC requests, routes them to the appropriate handler, |
| 141 | + * and returns a response via the associated event emitter.</p> |
| 142 | + * |
| 143 | + * @param sessionId The session ID used to identify the current client session. |
| 144 | + * @param request The JSON-RPC request entity containing the method name and parameters. |
| 145 | + * @return Always returns an empty string ({@value #RESPONSE_OK}) to indicate success. |
| 146 | + */ |
| 147 | + @PostMapping(path = MESSAGE_PATH) |
| 148 | + public Object receiveMcpMessage(@RequestQuery(name = "sessionId") String sessionId, |
| 149 | + @RequestBody JsonRpcEntity request) { |
| 150 | + log.info("Receive MCP message. [sessionId={}, request={}]", sessionId, request); |
| 151 | + Object id = request.getId(); |
| 152 | + if (id == null) { |
| 153 | + // Request without an ID indicates a notification message, ignore. |
| 154 | + return RESPONSE_OK; |
| 155 | + } |
| 156 | + MessageHandler handler = this.methodHandlers.getOrDefault(request.getMethod(), this.unsupportedMethodHandler); |
| 157 | + JsonRpcEntity response = new JsonRpcEntity(); |
| 158 | + response.setId(id); |
| 159 | + try { |
| 160 | + Object result = handler.handle(request.getParams()); |
| 161 | + response.setResult(result); |
| 162 | + } catch (Exception e) { |
| 163 | + log.error("Failed to handle MCP message.", e); |
| 164 | + response.setError(e.getMessage()); |
| 165 | + } |
| 166 | + String serialized = this.serializer.serialize(response); |
| 167 | + TextEvent textEvent = TextEvent.custom().id(sessionId).event(EVENT_MESSAGE).data(serialized).build(); |
| 168 | + Emitter<TextEvent> emitter = this.emitters.get(sessionId); |
| 169 | + emitter.emit(textEvent); |
| 170 | + log.info("Send MCP message. [response={}]", serialized); |
| 171 | + return RESPONSE_OK; |
| 172 | + } |
| 173 | +} |
0 commit comments