From 12d0732b8e676c09c5e5885ef2b862bf2144a701 Mon Sep 17 00:00:00 2001 From: liangjinglin Date: Sat, 26 Jul 2025 17:54:35 +0800 Subject: [PATCH] =?UTF-8?q?20250726=20=E5=A2=9E=E5=8A=A0mcp=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 10 +++++++++ .../springaidemo/SpringAiDemoApplication.java | 9 ++++++++ .../springaidemo/tool/McpDemoTool.java | 21 +++++++++++++++++++ src/main/resources/application.yml | 6 ++++++ 4 files changed, 46 insertions(+) create mode 100644 src/main/java/com/example/springaidemo/tool/McpDemoTool.java diff --git a/pom.xml b/pom.xml index 279853e..9bce0c1 100644 --- a/pom.xml +++ b/pom.xml @@ -79,6 +79,16 @@ ${elasticsearch.version} + + + org.springframework.ai + spring-ai-starter-mcp-server-webmvc + + + org.springframework.ai + spring-ai-mcp + + com.fasterxml.jackson.core diff --git a/src/main/java/com/example/springaidemo/SpringAiDemoApplication.java b/src/main/java/com/example/springaidemo/SpringAiDemoApplication.java index 0f6267e..681fbaf 100644 --- a/src/main/java/com/example/springaidemo/SpringAiDemoApplication.java +++ b/src/main/java/com/example/springaidemo/SpringAiDemoApplication.java @@ -1,7 +1,11 @@ package com.example.springaidemo; +import com.example.springaidemo.tool.McpDemoTool; +import org.springframework.ai.tool.ToolCallbackProvider; +import org.springframework.ai.tool.method.MethodToolCallbackProvider; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; @SpringBootApplication public class SpringAiDemoApplication { @@ -15,4 +19,9 @@ public class SpringAiDemoApplication { System.setProperty("proxySet", "true"); SpringApplication.run(SpringAiDemoApplication.class, args); } + + @Bean + public ToolCallbackProvider heroInfoTool(McpDemoTool mcpDemoTool) { + return MethodToolCallbackProvider.builder().toolObjects(mcpDemoTool).build(); + } } \ No newline at end of file diff --git a/src/main/java/com/example/springaidemo/tool/McpDemoTool.java b/src/main/java/com/example/springaidemo/tool/McpDemoTool.java new file mode 100644 index 0000000..ffa3457 --- /dev/null +++ b/src/main/java/com/example/springaidemo/tool/McpDemoTool.java @@ -0,0 +1,21 @@ +package com.example.springaidemo.tool; + +import org.springframework.ai.tool.annotation.Tool; +import org.springframework.stereotype.Service; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +@Service +public class McpDemoTool { + + private Map heroInfoMap = Map.of("支援","耀佳音:一个靠唱歌来提升队伍战斗力的英魂", + "强攻", "艾莲乔:一个靠冰属性进行攻击的强力输出英魂", + "异常", "一个靠冰焰给敌人积蓄异常属性,再消耗继续进行瞬间爆发的英魂"); + + @Tool(description = "查找绝境英魂信息") + public String HeroInfos(String query) { + return heroInfoMap.get(query) == null ? "没有找到相关的英魂的信息" : heroInfoMap.get(query).toString(); + } +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index a4bbb52..fcc7d09 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -42,6 +42,12 @@ spring: # chat: # options: # model: ${OLLAMA_MODEL:llama2} + mcp: + server: + name: james-hero-mcp-server + version: 1.0.0 + type: sync + server: port: 8009