-
Notifications
You must be signed in to change notification settings - Fork 332
[FEL] MCP客户端添加SSE和Elicitation支持 #384
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
CodeCasterX
merged 4 commits into
ModelEngine-Group:3.6.x
from
relat-ivity:mcp-sse-client
Nov 27, 2025
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
...in/java/modelengine/fel/tool/mcp/client/support/handler/DefaultMcpElicitationHandler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| /*--------------------------------------------------------------------------------------------- | ||
| * Copyright (c) 2025 Huawei Technologies Co., Ltd. All rights reserved. | ||
| * This file is a part of the ModelEngine Project. | ||
| * Licensed under the MIT License. See License.txt in the project root for license information. | ||
| *--------------------------------------------------------------------------------------------*/ | ||
|
|
||
| package modelengine.fel.tool.mcp.client.support.handler; | ||
|
|
||
| import io.modelcontextprotocol.spec.McpSchema; | ||
| import modelengine.fel.tool.mcp.client.elicitation.ElicitRequest; | ||
| import modelengine.fel.tool.mcp.client.elicitation.ElicitResult; | ||
| import modelengine.fitframework.log.Logger; | ||
|
|
||
| import java.util.function.Function; | ||
|
|
||
| /** | ||
| * Default MCP elicitation handler that delegates to an external handler function. | ||
| * | ||
| * <p>Converts {@link McpSchema.ElicitRequest} to {@link ElicitRequest}, | ||
| * calls the user's handler, and converts {@link ElicitResult} back to {@link McpSchema.ElicitResult}. | ||
| * | ||
| * @author 黄可欣 | ||
| * @since 2025-11-25 | ||
| */ | ||
| public class DefaultMcpElicitationHandler { | ||
| private static final Logger log = Logger.get(DefaultMcpElicitationHandler.class); | ||
| private final String clientId; | ||
| private final Function<ElicitRequest, ElicitResult> elicitationHandler; | ||
|
|
||
| /** | ||
| * Constructs a new handler. | ||
| * | ||
| * @param clientId The client ID. | ||
| * @param elicitationHandler The user's handler function that processes {@link ElicitRequest} | ||
| * and returns {@link ElicitResult}. | ||
| */ | ||
| public DefaultMcpElicitationHandler(String clientId, Function<ElicitRequest, ElicitResult> elicitationHandler) { | ||
| this.clientId = clientId; | ||
| this.elicitationHandler = elicitationHandler; | ||
| } | ||
|
|
||
| /** | ||
| * Handles an elicitation request by converting {@link McpSchema.ElicitRequest} to {@link ElicitRequest}, | ||
| * delegating to the user's handler, and converting {@link ElicitResult} back to {@link McpSchema.ElicitResult}. | ||
| * | ||
| * @param request The {@link McpSchema.ElicitRequest} from MCP server. | ||
| * @return The {@link McpSchema.ElicitResult} to send back to MCP server. | ||
| */ | ||
| public McpSchema.ElicitResult handleElicitationRequest(McpSchema.ElicitRequest request) { | ||
| log.info("Received elicitation request from MCP server. [clientId={}, message={}, requestSchema={}]", | ||
| this.clientId, | ||
| request.message(), | ||
| request.requestedSchema()); | ||
|
|
||
| try { | ||
| ElicitRequest elicitRequest = new ElicitRequest(request.message(), request.requestedSchema()); | ||
| ElicitResult result = this.elicitationHandler.apply(elicitRequest); | ||
| log.info("Successfully handled elicitation request. [clientId={}, action={}, content={}]", | ||
| this.clientId, | ||
| result.action(), | ||
| result.content()); | ||
|
|
||
| McpSchema.ElicitResult.Action mcpAction = switch (result.action()) { | ||
| case ACCEPT -> McpSchema.ElicitResult.Action.ACCEPT; | ||
| case DECLINE -> McpSchema.ElicitResult.Action.DECLINE; | ||
| case CANCEL -> McpSchema.ElicitResult.Action.CANCEL; | ||
| }; | ||
| return new McpSchema.ElicitResult(mcpAction, result.content()); | ||
| } catch (Exception e) { | ||
| log.error("Failed to handle elicitation request. [clientId={}, error={}]", | ||
| this.clientId, | ||
| e.getMessage(), | ||
| e); | ||
| throw new IllegalStateException("Failed to handle elicitation request: " + e.getMessage(), e); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
...ient-service/src/main/java/modelengine/fel/tool/mcp/client/elicitation/ElicitRequest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| /*--------------------------------------------------------------------------------------------- | ||
| * Copyright (c) 2025 Huawei Technologies Co., Ltd. All rights reserved. | ||
| * This file is a part of the ModelEngine Project. | ||
| * Licensed under the MIT License. See License.txt in the project root for license information. | ||
| *--------------------------------------------------------------------------------------------*/ | ||
|
|
||
| package modelengine.fel.tool.mcp.client.elicitation; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * Represents an elicitation request from an MCP server. | ||
| * This is a simplified version that doesn't depend on MCP SDK types. | ||
| * | ||
| * @param message The message describing what information is needed from the user | ||
CodeCasterX marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * @param requestedSchema The JSON schema defining the expected data structure | ||
| * @author 黄可欣 | ||
| * @since 2025-11-25 | ||
| */ | ||
| public record ElicitRequest(String message, Map<String, Object> requestedSchema) {} | ||
29 changes: 29 additions & 0 deletions
29
...lient-service/src/main/java/modelengine/fel/tool/mcp/client/elicitation/ElicitResult.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| /*--------------------------------------------------------------------------------------------- | ||
| * Copyright (c) 2025 Huawei Technologies Co., Ltd. All rights reserved. | ||
| * This file is a part of the ModelEngine Project. | ||
| * Licensed under the MIT License. See License.txt in the project root for license information. | ||
| *--------------------------------------------------------------------------------------------*/ | ||
|
|
||
| package modelengine.fel.tool.mcp.client.elicitation; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * Represents the result of handling an elicitation request. | ||
| * This is a simplified version that doesn't depend on MCP SDK types. | ||
| * | ||
| * @param action The action to take | ||
| * @param content The user-provided data matching the requested schema | ||
| * @author 黄可欣 | ||
| * @since 2025-11-25 | ||
| */ | ||
| public record ElicitResult(Action action, Map<String, Object> content) { | ||
| /** | ||
| * Action types for elicitation results. | ||
| */ | ||
| public enum Action { | ||
| ACCEPT, | ||
CodeCasterX marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| DECLINE, | ||
| CANCEL | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.