Skip to content

Commit 43eac7d

Browse files
committed
接口解耦
1 parent e6de1a5 commit 43eac7d

7 files changed

Lines changed: 64 additions & 88 deletions

File tree

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

Lines changed: 21 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
package modelengine.fel.tool.mcp.server;
88

99
import io.modelcontextprotocol.server.McpServerFeatures;
10-
import io.modelcontextprotocol.server.McpSyncServerExchange;
1110
import io.modelcontextprotocol.spec.McpSchema;
1211
import modelengine.fel.tool.mcp.entity.ServerSchema;
1312
import modelengine.fel.tool.mcp.entity.Tool;
@@ -23,7 +22,6 @@
2322
import java.util.List;
2423
import java.util.Map;
2524
import java.util.concurrent.ConcurrentHashMap;
26-
import java.util.function.BiFunction;
2725

2826
import static modelengine.fel.tool.info.schema.PluginSchema.TYPE;
2927
import static modelengine.fel.tool.info.schema.ToolsSchema.PROPERTIES;
@@ -59,8 +57,10 @@ public DefaultMcpServer(ToolExecuteService toolExecuteService, McpSyncServer mcp
5957

6058
@Override
6159
public ServerSchema getSchema() {
62-
McpSchema.Implementation info = this.mcpSyncServer.getServerInfo();
63-
McpSchema.ServerCapabilities capabilities= this.mcpSyncServer.getServerCapabilities();
60+
ServerSchema.Info info = new ServerSchema.Info("FIT Store MCP Server", "3.6.0-SNAPSHOT");
61+
ServerSchema.Capabilities.Logging logging = new ServerSchema.Capabilities.Logging();
62+
ServerSchema.Capabilities.Tools tools = new ServerSchema.Capabilities.Tools(true);
63+
ServerSchema.Capabilities capabilities = new ServerSchema.Capabilities(logging, tools);
6464
return new ServerSchema("2025-06-18", capabilities, info);
6565
}
6666

@@ -77,8 +77,7 @@ public void registerToolsChangedObserver(ToolsChangedObserver observer) {
7777
}
7878

7979
@Override
80-
public void addTool(String name, String description, McpSchema.JsonSchema inputSchema,
81-
BiFunction<McpSyncServerExchange, McpSchema.CallToolRequest, McpSchema.CallToolResult> callHandler) {
80+
public void onToolAdded(String name, String description, Map<String, Object> parameters) {
8281
if (StringUtils.isBlank(name)) {
8382
log.warn("Tool addition is ignored: tool name is blank.");
8483
return;
@@ -87,36 +86,45 @@ public void addTool(String name, String description, McpSchema.JsonSchema inputS
8786
log.warn("Tool addition is ignored: tool description is blank. [toolName={}]", name);
8887
return;
8988
}
90-
if (inputSchema == null) {
89+
if (MapUtils.isEmpty(parameters)) {
9190
log.warn("Tool addition is ignored: tool schema is null or empty. [toolName={}]", name);
9291
return;
9392
}
94-
if (callHandler == null) {
95-
log.warn("Tool addition is ignored: tool call handler is null or empty. [toolName={}]", name);
93+
if (!(parameters.get(TYPE) instanceof String)
94+
|| parameters.get(PROPERTIES) != null && !(parameters.get(PROPERTIES) instanceof Map)
95+
|| parameters.get(REQUIRED) != null && !(parameters.get(REQUIRED) instanceof List)) {
96+
log.warn("Invalid parameter schema. [toolName={}]", name);
9697
return;
9798
}
98-
99+
@SuppressWarnings("unchecked")
100+
McpSchema.JsonSchema inputSchema = new McpSchema.JsonSchema((String) parameters.get(TYPE),
101+
(Map<String, Object>) parameters.get(PROPERTIES), (List<String>) parameters.get(REQUIRED),
102+
null, null,null);
99103
McpServerFeatures.SyncToolSpecification toolSpecification = McpServerFeatures.SyncToolSpecification.builder()
100104
.tool(McpSchema.Tool.builder()
101105
.name(name)
102106
.description(description)
103107
.inputSchema(inputSchema)
104108
.build())
105-
.callHandler(callHandler)
109+
.callHandler((exchange, request) -> {
110+
Map<String, Object> args = request.arguments();
111+
String result = this.toolExecuteService.execute(name, args);
112+
return new McpSchema.CallToolResult(result, false);
113+
})
106114
.build();
107115
this.mcpSyncServer.addTool(toolSpecification);
108116

109117
Tool tool = new Tool();
110118
tool.setName(name);
111119
tool.setDescription(description);
112-
tool.setInputSchema(inputSchema);
120+
tool.setInputSchema(parameters);
113121
this.tools.put(name, tool);
114122
log.info("Tool added to MCP server. [toolName={}, description={}, schema={}]", name, description, inputSchema);
115123
this.toolsChangedObservers.forEach(ToolsChangedObserver::onToolsChanged);
116124
}
117125

118126
@Override
119-
public void removeTool(String name) {
127+
public void onToolRemoved(String name) {
120128
if (StringUtils.isBlank(name)) {
121129
log.warn("Tool removal is ignored: tool name is blank.");
122130
return;
@@ -126,32 +134,4 @@ public void removeTool(String name) {
126134
log.info("Tool removed from MCP server. [toolName={}]", name);
127135
this.toolsChangedObservers.forEach(ToolsChangedObserver::onToolsChanged);
128136
}
129-
130-
@Override
131-
public void onToolAdded(String name, String description, Map<String, Object> parameters) {
132-
if (MapUtils.isEmpty(parameters)) {
133-
log.warn("Tool addition is ignored: tool schema is null or empty. [toolName={}]", name);
134-
return;
135-
}
136-
if (!(parameters.get(TYPE) instanceof String)
137-
|| parameters.get(PROPERTIES) != null && !(parameters.get(PROPERTIES) instanceof Map)
138-
|| parameters.get(REQUIRED) != null && !(parameters.get(REQUIRED) instanceof List)) {
139-
log.warn("Invalid parameter schema. [toolName={}]", name);
140-
return;
141-
}
142-
@SuppressWarnings("unchecked")
143-
McpSchema.JsonSchema hkxSchema = new McpSchema.JsonSchema((String) parameters.get(TYPE),
144-
(Map<String, Object>) parameters.get(PROPERTIES), (List<String>) parameters.get(REQUIRED),
145-
null, null,null);
146-
this.addTool(name, description, hkxSchema, (exchange, request) -> {
147-
Map<String, Object> args = request.arguments();
148-
String result = this.toolExecuteService.execute(name, args);
149-
return new McpSchema.CallToolResult(result, false);
150-
});
151-
}
152-
153-
@Override
154-
public void onToolRemoved(String name) {
155-
this.removeTool(name);
156-
}
157137
}

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,8 @@ public McpSyncServer mcpSyncServer(DefaultMcpStreamableServerTransportProvider t
3434
return io.modelcontextprotocol.server.McpServer.sync(transportProvider)
3535
.serverInfo("FIT Store MCP Server", "3.6.0-SNAPSHOT")
3636
.capabilities(McpSchema.ServerCapabilities.builder()
37-
.resources(false, true) // Enable resource support
3837
.tools(true) // Enable tool support
39-
.prompts(true) // Enable prompt support
4038
.logging() // Enable logging support
41-
.completions() // Enable completions support
4239
.build())
4340
.requestTimeout(Duration.ofSeconds(10))
4441
.build();

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

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,10 @@
66

77
package modelengine.fel.tool.mcp.server;
88

9-
import io.modelcontextprotocol.server.McpSyncServerExchange;
10-
import io.modelcontextprotocol.spec.McpSchema;
119
import modelengine.fel.tool.mcp.entity.ServerSchema;
1210
import modelengine.fel.tool.mcp.entity.Tool;
1311

1412
import java.util.List;
15-
import java.util.function.BiFunction;
1613

1714
/**
1815
* Represents the MCP Server.
@@ -35,24 +32,6 @@ public interface McpServer {
3532
*/
3633
List<Tool> getTools();
3734

38-
/**
39-
* Add a tool.
40-
*
41-
* @param name The name of the added tool, as a {@link String}.
42-
* @param description A description of the added tool, as a {@link String}.
43-
* @param inputSchema The parameters associated with the added tool, as a {@link McpSchema.JsonSchema}.
44-
* @param callHandler The tool call handler as a {@link BiFunction}
45-
*/
46-
void addTool(String name, String description, McpSchema.JsonSchema inputSchema,
47-
BiFunction<McpSyncServerExchange, McpSchema.CallToolRequest, McpSchema.CallToolResult> callHandler);
48-
49-
/**
50-
* Remove a tool.
51-
*
52-
* @param name The name of the removed tool, as a {@link String}.
53-
*/
54-
void removeTool(String name);
55-
5635
/**
5736
* Registers MCP server tools changed observer.
5837
*

framework/fel/java/plugins/tool-mcp-server/src/test/java/modelengine/fel/tool/mcp/server/support/DefaultMcpServerTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,16 @@ void returnExpectedServerInfo() {
6868
ServerSchema info = server.getSchema();
6969

7070
assertThat(info).returns("2025-06-18", ServerSchema::protocolVersion);
71+
72+
ServerSchema.Capabilities capabilities = info.capabilities();
73+
assertThat(capabilities).isNotNull();
74+
75+
ServerSchema.Capabilities.Tools toolsCapability = capabilities.tools();
76+
assertThat(toolsCapability).returns(true, ServerSchema.Capabilities.Tools::listChanged);
77+
78+
ServerSchema.Info serverInfo = info.serverInfo();
79+
assertThat(serverInfo).returns("FIT Store MCP Server", ServerSchema.Info::name)
80+
.returns("3.6.0-SNAPSHOT", ServerSchema.Info::version);
7181
}
7282
}
7383

framework/fel/java/services/tool-mcp-common/pom.xml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,5 @@
3737
<groupId>org.assertj</groupId>
3838
<artifactId>assertj-core</artifactId>
3939
</dependency>
40-
41-
<!-- MCP -->
42-
<dependency>
43-
<groupId>io.modelcontextprotocol.sdk</groupId>
44-
<artifactId>mcp</artifactId>
45-
<version>0.14.0</version>
46-
</dependency>
4740
</dependencies>
4841
</project>

framework/fel/java/services/tool-mcp-common/src/main/java/modelengine/fel/tool/mcp/entity/ServerSchema.java

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,18 @@
66

77
package modelengine.fel.tool.mcp.entity;
88

9-
import io.modelcontextprotocol.spec.McpSchema;
9+
import static modelengine.fitframework.util.ObjectUtils.cast;
1010

1111
import java.util.Map;
1212

13-
import static modelengine.fitframework.util.ObjectUtils.cast;
14-
1513
/**
1614
* Represents a server entity in the MCP framework, encapsulating information about the server's protocol version,
1715
* capabilities, and additional server details.
1816
*
1917
* @author 季聿阶
2018
* @since 2025-05-22
2119
*/
22-
public record ServerSchema(String protocolVersion, McpSchema.ServerCapabilities capabilities, McpSchema.Implementation serverInfo) {
20+
public record ServerSchema(String protocolVersion, Capabilities capabilities, Info serverInfo) {
2321
/**
2422
* Creates a new {@link ServerSchema} instance based on the provided map of server information.
2523
*
@@ -29,17 +27,35 @@ public record ServerSchema(String protocolVersion, McpSchema.ServerCapabilities
2927
public static ServerSchema create(Map<String, Object> map) {
3028
String protocolVersion = cast(map.get("protocolVersion"));
3129
Map<String, Object> capabilitiesMap = cast(map.get("capabilities"));
30+
Capabilities.Logging logging = new Capabilities.Logging();
3231
Map<String, Object> toolsMap = cast(capabilitiesMap.get("tools"));
3332
boolean toolsListChanged = cast(toolsMap.getOrDefault("listChanged", false));
34-
McpSchema.ServerCapabilities capabilities = McpSchema.ServerCapabilities.builder()
35-
.tools(toolsListChanged) // Enable tool support
36-
.logging() // Enable logging support
37-
.completions() // Enable completions support
38-
.build();
33+
Capabilities.Tools tools = new Capabilities.Tools(toolsListChanged);
34+
Capabilities capabilities = new Capabilities(logging, tools);
3935
Map<String, Object> infoMap = cast(map.get("serverInfo"));
4036
String name = cast(infoMap.get("name"));
4137
String version = cast(infoMap.get("version"));
42-
McpSchema.Implementation serverInfo = new McpSchema.Implementation(name, version);
38+
Info serverInfo = new Info(name, version);
4339
return new ServerSchema(protocolVersion, capabilities, serverInfo);
4440
}
41+
42+
/**
43+
* Represents the capabilities supported by the server, including logging and tool-related functionalities.
44+
*/
45+
public record Capabilities(Logging logging, Tools tools) {
46+
/**
47+
* Represents the logging capabilities of the server.
48+
*/
49+
public record Logging() {}
50+
51+
/**
52+
* Represents the tool-related capabilities of the server, including whether the tool list has changed.
53+
*/
54+
public record Tools(boolean listChanged) {}
55+
}
56+
57+
/**
58+
* Represents additional information about the server, such as its name and version.
59+
*/
60+
public record Info(String name, String version) {}
4561
}

framework/fel/java/services/tool-mcp-common/src/main/java/modelengine/fel/tool/mcp/entity/Tool.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
package modelengine.fel.tool.mcp.entity;
88

9-
import io.modelcontextprotocol.spec.McpSchema;
9+
import java.util.Map;
1010

1111
/**
1212
* Represents a tool entity with name, description, and schema.
@@ -31,7 +31,7 @@ public class Tool {
3131
* The input schema that defines the expected parameters when invoking the tool.
3232
* Typically represented using a map structure, e.g., JSON schema format.
3333
*/
34-
private McpSchema.JsonSchema inputSchema;
34+
private Map<String, Object> inputSchema;
3535

3636
/**
3737
* Gets the name of the tool.
@@ -73,18 +73,19 @@ public void setDescription(String description) {
7373
* Gets the input schema of the tool.
7474
* This defines the required and optional parameters for invoking the tool.
7575
*
76-
* @return The tool's input schema as a {@link McpSchema.JsonSchema}.
76+
* @return The tool's input schema as a {@link Map}{@code <}{@link String}{@code , }{@link Object}{@code >}.
7777
*/
78-
public McpSchema.JsonSchema getInputSchema() {
78+
public Map<String, Object> getInputSchema() {
7979
return this.inputSchema;
8080
}
8181

8282
/**
8383
* Sets the input schema of the tool.
8484
*
85-
* @param inputSchema The new input schema for the tool, as a {@link McpSchema.JsonSchema}.
85+
* @param inputSchema The new input schema for the tool, as a
86+
* {@link Map}{@code <}{@link String}{@code , }{@link Object}{@code >}.
8687
*/
87-
public void setInputSchema(McpSchema.JsonSchema inputSchema) {
88+
public void setInputSchema(Map<String, Object> inputSchema) {
8889
this.inputSchema = inputSchema;
8990
}
9091
}

0 commit comments

Comments
 (0)