日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

jFinal 使用 SolonMCP 开发 MCP(拥抱新潮流)

發(fā)布時間:2025/5/22 编程问答 38 如意码农
生活随笔 收集整理的這篇文章主要介紹了 jFinal 使用 SolonMCP 开发 MCP(拥抱新潮流) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

MCP 官方的 java-sdk 目前只支持 java17+。直接基于 mcp-java-sdk 也比較復(fù)雜。使用 SolonMCP,可以基于 java8 開發(fā)(像 MVC 的開發(fā)風(fēng)格),且比較簡單。

1、SolonMCP 簡介

SolonMCP(全稱:solon-ai-mcp)是 solon 的一個擴展。支持內(nèi)嵌到 jfinal,vert.x,springboot2,springboot3 等框架使用。

Maven 主要依賴包:

<dependency>
<groupId>org.noear</groupId>
<artifactId>solon-ai-mcp</artifactId>
</dependency>

具體的示例參考:

  • https://gitee.com/opensolon/solon-ai-mcp-embedded-examples/tree/main/solon-ai-embedded-jfinal
  • https://gitee.com/opensolon/solon-ai-mcp-embedded-examples/tree/main/solon-ai-embedded-jfinal-newstyle

2、MCP 服務(wù)端開發(fā)

2.1、添加入口類 webapp.HelloApp(比較空,注意下 mcpServerConfig)

MCP 內(nèi)部是基于響應(yīng)式的,需要開啟異步支持。

public class HelloApp extends JFinalConfig {
public static void main(String[] args) {
UndertowServer.create(HelloApp.class)
.setDevMode(false)
.setPort(8080)
.onDeploy((cl, di) -> {
di.getFilters().get("jfinal").setAsyncSupported(true); //注意這個,要開啟異步支持
}).start();
} public void configConstant(Constants me) {
me.setDevMode(false);
} public void configRoute(Routes me) {
} public void configEngine(Engine me) {
} public void configPlugin(Plugins me) {
me.add(mcpServerConfig);
} public void configInterceptor(Interceptors me) {
} public void configHandler(Handlers me) {
me.add(mcpServerConfig);
} private McpServerConfig mcpServerConfig = new McpServerConfig();
}

2.2、添加 webapp.mcpserver.McpServerConfig(實現(xiàn) Handler、IPlugin 接口)

實現(xiàn) IPlugin 對接 Solon 的生命周期。實現(xiàn) Handler 對接 mcp 的請求處理。

public class McpServerConfig extends Handler implements IPlugin {
public boolean start() {
Solon.start(McpServerConfig.class, new String[]{"--cfg=mcpserver.yml"});
return true;
} public boolean stop() {
if (Solon.app() != null) {
Solon.stopBlock(false, Solon.cfg().stopDelay());
}
return true;
} @Override
public void handle(String target, HttpServletRequest request, HttpServletResponse response, boolean[] isHandled) {
if (target.startsWith("/mcp/")) {
Context ctx = new SolonServletContext(request, response); try {
//Solon處理(可能是空處理)
Solon.app().tryHandle(ctx); if (isHandled != null && isHandled.length > 0) {
isHandled[0] = true;
}
} catch (Throwable e) {
ctx.errors = e; throw e;
} finally {
ContextUtil.currentRemove();
}
} else {
if (next != null) {
next.handle(target, request, response, isHandled);
}
}
}
}

2.3、添加 webapp.mcpserver.tool.McpServer(實現(xiàn) Handler、IPlugin 接口)

這里是重點了,添加 mcp server 端點(支持多個端點)

@McpServerEndpoint(sseEndpoint = "/mcp/sse")
public class McpServer {
//
// 建議開啟編譯參數(shù):-parameters (否則,最好再配置參數(shù)的 name)
//
@ToolMapping(description = "查詢天氣預(yù)報")
public String getWeather(@Param(description = "城市位置") String location) {
return "晴,14度";
} @ResourceMapping(uri = "config://app-version", description = "獲取應(yīng)用版本號")
public String getAppVersion() {
return "v3.2.0";
} @ResourceMapping(uri = "db://users/{user_id}/email", description = "根據(jù)用戶ID查詢郵箱")
public String getEmail(@Param(description = "用戶Id") String user_id) {
return user_id + "@example.com";
} @PromptMapping(description = "生成關(guān)于某個主題的提問")
public Collection<ChatMessage> askQuestion(@Param(description = "主題") String topic) {
return Arrays.asList(
ChatMessage.ofUser("請解釋一下'" + topic + "'的概念?")
);
}
}

2.4、編譯后運行

或者開發(fā)時,直接運行 HelloApp:main 方法

3、MCP 客戶端開發(fā)

客戶端簡單些

public class McpClientTest {
public static void main(String[] args) throws Exception {
McpClientProvider toolProvider = McpClientProvider.builder()
.apiUrl("http://localhost:8080/mcp/sse")
.build(); //工具調(diào)用
Map<String, Object> map = Collections.singletonMap("location", "杭州");
String rst = toolProvider.callToolAsText("getWeather", map).getContent();
System.out.println(rst);
assert "晴,14度".equals(rst); //資源讀取
resourceContent = toolProvider.readResourceAsText("config://app-version").getContent();
System.out.println(resourceContent);
}
}

4、MCP 客戶端作為 LLM(ChatModel) 的工具集使用

也比較簡單。使用 ollama 做為 llm 提供者,方便本地測試。

public class McpClientTest {
private static final String apiUrl = "http://127.0.0.1:11434/api/chat";
private static final String provider = "ollama";
private static final String model = "qwen2.5:1.5b"; //"llama3.2";//deepseek-r1:1.5b; public static void main(String[] args) throws Exception {
//構(gòu)建 mcp client
McpClientProvider toolProvider = McpClientProvider.builder()
.apiUrl("http://localhost:8080/mcp/sse")
.build(); //構(gòu)建 llm 接口
ChatModel chatModel = ChatModel.of(apiUrl)
.provider(provider)
.model(model)
.defaultToolsAdd(toolProvider) //添加默認工具(這是 mcp client)
.build(); //請求
ChatResponse resp = chatModel.prompt("杭州今天的天氣怎么樣?")
.call(); System.out.println(resp.getMessage());
}
}

總結(jié)

以上是生活随笔為你收集整理的jFinal 使用 SolonMCP 开发 MCP(拥抱新潮流)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。