Skip to content

Commit e6f4051

Browse files
committed
修改注释
1 parent 79b6852 commit e6f4051

File tree

4 files changed

+60
-45
lines changed

4 files changed

+60
-45
lines changed

framework/fel/java/plugins/tool-mcp-server/src/main/java/modelengine/fel/tool/mcp/server/config/McpSseServerConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import java.time.Duration;
1919

2020
/**
21-
* Mcp Server Bean implemented with MCP SDK.
21+
* MCP SSE Server Bean implemented with MCP SDK.
2222
*
2323
* @author 黄可欣
2424
* @since 2025-11-10

framework/fel/java/plugins/tool-mcp-server/src/main/java/modelengine/fel/tool/mcp/server/config/McpStreamableServerConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import java.time.Duration;
1919

2020
/**
21-
* Mcp Server Bean implemented with MCP SDK.
21+
* MCP Streamable Server Bean implemented with MCP SDK.
2222
*
2323
* @author 黄可欣
2424
* @since 2025-10-22

framework/fel/java/plugins/tool-mcp-server/src/main/java/modelengine/fel/tool/mcp/server/support/DefaultMcpServer.java

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,9 @@ public class DefaultMcpServer implements McpServer, ToolChangedObserver {
4949
/**
5050
* Constructs a new instance of the DefaultMcpServer class.
5151
*
52-
* @param toolExecuteService The service used to execute tools when handling tool call requests.
53-
* @throws IllegalArgumentException If {@code toolExecuteService} is null.
52+
* @param toolExecuteService The service used to execute tools when handling tool call requests
53+
* @param mcpSyncSseServer The MCP sync server for SSE transport
54+
* @param mcpSyncStreamableServer The MCP sync server for Streamable transport
5455
*/
5556
public DefaultMcpServer(ToolExecuteService toolExecuteService,
5657
@Fit(alias = "McpSyncSseServer") McpSyncServer mcpSyncSseServer,
@@ -122,17 +123,11 @@ public void onToolRemoved(String name) {
122123

123124
/**
124125
* Creates a tool specification for the MCP server.
125-
* <p>
126-
* This method constructs a {@link McpServerFeatures.SyncToolSpecification} that includes:
127-
* <ul>
128-
* <li>Tool metadata (name, description, input schema)</li>
129-
* <li>Call handler that executes the tool and handles exceptions</li>
130-
* </ul>
131126
*
132-
* @param name The name of the tool.
133-
* @param description The description of the tool.
134-
* @param parameters The parameter schema containing type, properties, and required fields.
135-
* @return A fully configured {@link McpServerFeatures.SyncToolSpecification}.
127+
* @param name The name of the tool
128+
* @param description The description of the tool
129+
* @param parameters The parameter schema containing type, properties, and required fields
130+
* @return A configured {@link McpServerFeatures.SyncToolSpecification}
136131
*/
137132
private McpServerFeatures.SyncToolSpecification createToolSpecification(String name, String description,
138133
Map<String, Object> parameters) {
@@ -152,16 +147,10 @@ private McpServerFeatures.SyncToolSpecification createToolSpecification(String n
152147

153148
/**
154149
* Executes a tool and handles any exceptions that may occur.
155-
* <p>
156-
* This method handles two types of exceptions:
157-
* <ul>
158-
* <li>{@link IllegalArgumentException}: Invalid tool arguments (logged as warning)</li>
159-
* <li>{@link Exception}: Any other execution failure (logged as error)</li>
160-
* </ul>
161150
*
162-
* @param toolName The name of the tool to execute.
163-
* @param request The tool call request containing arguments.
164-
* @return A {@link McpSchema.CallToolResult} with the execution result or error message.
151+
* @param toolName The name of the tool to execute
152+
* @param request The tool call request containing arguments
153+
* @return A {@link McpSchema.CallToolResult} with the execution result or error message
165154
*/
166155
private McpSchema.CallToolResult executeToolWithErrorHandling(String toolName, McpSchema.CallToolRequest request) {
167156
try {

framework/fel/java/plugins/tool-mcp-server/src/main/java/modelengine/fel/tool/mcp/server/transport/FitMcpSseServerTransportProvider.java

Lines changed: 48 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,6 @@ public class FitMcpSseServerTransportProvider implements McpServerTransportProvi
5555
private static final Logger logger = Logger.get(FitMcpSseServerTransportProvider.class);
5656
private static final String MESSAGE_ENDPOINT = "/mcp/message";
5757
private static final String SSE_ENDPOINT = "/mcp/sse";
58-
/**
59-
* Event type for sending the message endpoint URI to clients.
60-
*/
6158
public static final String ENDPOINT_EVENT_TYPE = "endpoint";
6259

6360
private final McpJsonMapper jsonMapper;
@@ -94,11 +91,21 @@ private FitMcpSseServerTransportProvider(McpJsonMapper jsonMapper, Duration keep
9491
}
9592
}
9693

94+
/**
95+
* Returns the list of supported MCP protocol versions.
96+
*
97+
* @return A list of supported protocol version strings
98+
*/
9799
@Override
98100
public List<String> protocolVersions() {
99101
return List.of(ProtocolVersions.MCP_2024_11_05);
100102
}
101103

104+
/**
105+
* Sets the session factory used to create new MCP server sessions.
106+
*
107+
* @param sessionFactory The factory for creating server sessions
108+
*/
102109
@Override
103110
public void setSessionFactory(McpServerSession.Factory sessionFactory) {
104111
this.sessionFactory = sessionFactory;
@@ -162,15 +169,15 @@ public Mono<Void> closeGracefully() {
162169
* establishing an SSE connection. This method:
163170
* <ul>
164171
* <li>Generates a unique session ID</li>
165-
* <li>Creates a new session with a FitMcpSessionTransport</li>
166-
* <li>Sends an initial endpoint event to inform the client where to send
167-
* messages</li>
172+
* <li>Creates a new session with a {@link FitSseMcpSessionTransport}</li>
173+
* <li>Sends an initial endpoint event to inform the client where to send messages</li>
168174
* <li>Maintains the session in the sessions map</li>
169175
* </ul>
170176
*
171177
* @param request The incoming server request
172-
* @return A ServerResponse configured for SSE communication, or an error response if
173-
* the server is shutting down or the connection fails
178+
* @param response The HTTP response for SSE communication
179+
* @return A {@link Choir}{@code <}{@link TextEvent}{@code >} object for SSE streaming,
180+
* or an error response if the server is shutting down or the connection fails
174181
*/
175182
@GetMapping(path = SSE_ENDPOINT)
176183
public Object handleSseConnection(HttpClassicServerRequest request, HttpClassicServerResponse response) {
@@ -212,14 +219,16 @@ public Object handleSseConnection(HttpClassicServerRequest request, HttpClassicS
212219
/**
213220
* Handles incoming JSON-RPC messages from clients. This method:
214221
* <ul>
222+
* <li>Validates the session ID from the request parameter</li>
215223
* <li>Deserializes the request body into a JSON-RPC message</li>
216224
* <li>Processes the message through the session's handle method</li>
217225
* <li>Returns appropriate HTTP responses based on the processing result</li>
218226
* </ul>
219227
*
220228
* @param request The incoming server request containing the JSON-RPC message
221-
* @return A ServerResponse indicating success (200 OK) or appropriate error status
222-
* with error details in case of failures
229+
* @param response The HTTP response to set status code and return data
230+
* @param sessionId The session ID from the request parameter
231+
* @return An error {@link Entity} if validation fails, or {@code null} on success
223232
*/
224233
@PostMapping(path = MESSAGE_ENDPOINT)
225234
public Object handleMessage(HttpClassicServerRequest request, HttpClassicServerResponse response,
@@ -258,6 +267,14 @@ public Object handleMessage(HttpClassicServerRequest request, HttpClassicServerR
258267
}
259268
}
260269

270+
/**
271+
* Adds an observer to the SSE emitter to handle connection lifecycle events.
272+
* The observer removes the session from the sessions map when the connection
273+
* completes or fails.
274+
*
275+
* @param emitter The SSE emitter to observe
276+
* @param sessionId The session ID associated with this emitter
277+
*/
261278
private void addEmitterObserver(Emitter<TextEvent> emitter, String sessionId) {
262279
emitter.observe(new Emitter.Observer<TextEvent>() {
263280
@Override
@@ -307,25 +324,31 @@ private Object validateRequestSessionId(String sessionId, HttpClassicServerRespo
307324
}
308325

309326
/**
310-
* Implementation of McpServerTransport for WebMVC SSE sessions. This class handles
311-
* the transport-level communication for a specific client session.
327+
* Implementation of {@link McpServerTransport} for FIT SSE sessions.
328+
* This class handles the transport-level communication for a specific client session.
329+
*
330+
* <p>
331+
* This class is thread-safe and uses a {@link ReentrantLock} to synchronize access to the
332+
* underlying SSE emitter to prevent race conditions when multiple threads attempt to
333+
* send messages concurrently.
312334
*/
313335
private class FitSseMcpSessionTransport implements McpServerTransport {
314336
private final String sessionId;
315337
private final Emitter<TextEvent> emitter;
316338
private final HttpClassicServerResponse response;
317339

318340
/**
319-
* Lock to ensure thread-safe access to the SSE builder when sending messages.
341+
* Lock to ensure thread-safe access to the SSE emitter when sending messages.
320342
* This prevents concurrent modifications that could lead to corrupted SSE events.
321343
*/
322344
private final ReentrantLock sseBuilderLock = new ReentrantLock();
323345

324346
/**
325-
* Creates a new session transport with the specified ID and SSE builder.
347+
* Creates a new session transport with the specified ID and SSE emitter.
326348
*
327349
* @param sessionId The unique identifier for this session
328-
* @param emitter The emitter for sending events
350+
* @param emitter The emitter for sending SSE events to the client
351+
* @param response The HTTP response for checking connection status
329352
*/
330353
FitSseMcpSessionTransport(String sessionId, Emitter<TextEvent> emitter, HttpClassicServerResponse response) {
331354
this.sessionId = sessionId;
@@ -336,14 +359,16 @@ private class FitSseMcpSessionTransport implements McpServerTransport {
336359

337360
/**
338361
* Sends a JSON-RPC message to the client through the SSE connection.
362+
* The message is serialized to JSON and sent as an SSE event with type "message".
363+
* This method is thread-safe and checks if the connection is still active before sending.
339364
*
340365
* @param message The JSON-RPC message to send
341366
* @return A Mono that completes when the message has been sent
342367
*/
343368
@Override
344369
public Mono<Void> sendMessage(McpSchema.JSONRPCMessage message) {
345370
return Mono.fromRunnable(() -> {
346-
sseBuilderLock.lock();
371+
this.sseBuilderLock.lock();
347372
// Check if connection is still active before sending
348373
if (!this.response.isActive()) {
349374
logger.warn("[SSE] Connection inactive detected while sending message. [sessionId={}]",
@@ -367,7 +392,7 @@ public Mono<Void> sendMessage(McpSchema.JSONRPCMessage message) {
367392
e);
368393
this.emitter.fail(e);
369394
} finally {
370-
sseBuilderLock.unlock();
395+
this.sseBuilderLock.unlock();
371396
}
372397
});
373398
}
@@ -397,20 +422,21 @@ public Mono<Void> closeGracefully() {
397422

398423
/**
399424
* Closes the transport immediately.
425+
* Completes the SSE emitter and releases any associated resources.
400426
*/
401427
@Override
402428
public void close() {
403-
sseBuilderLock.lock();
404-
logger.debug("[SSE] Closing session transport. [sessionId={}]", sessionId);
429+
this.sseBuilderLock.lock();
430+
logger.debug("[SSE] Closing session transport. [sessionId={}]", this.sessionId);
405431
try {
406432
this.emitter.complete();
407-
logger.info("[SSE] Closed SSE builder successfully. [sessionId={}]", sessionId);
433+
logger.info("[SSE] Closed SSE builder successfully. [sessionId={}]", this.sessionId);
408434
} catch (Exception e) {
409435
logger.warn("[SSE] Failed to complete SSE builder. [sessionId={}, error={}]",
410-
sessionId,
436+
this.sessionId,
411437
e.getMessage());
412438
} finally {
413-
sseBuilderLock.unlock();
439+
this.sseBuilderLock.unlock();
414440
}
415441
}
416442

0 commit comments

Comments
 (0)