From 03c70db3ab7ebf5ada6a081b2e9159cdcb8a15fd Mon Sep 17 00:00:00 2001 From: huangge1199 Date: Wed, 4 Jun 2025 16:08:19 +0800 Subject: [PATCH] =?UTF-8?q?MCP=EF=BC=9A=E5=9B=BE=E7=89=87=E6=90=9C?= =?UTF-8?q?=E7=B4=A2=E6=9C=8D=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- long-image-search-mcp-server/.gitattributes | 2 + long-image-search-mcp-server/.gitignore | 33 ++++++++ long-image-search-mcp-server/pom.xml | 84 +++++++++++++++++++ .../LongImageSearchMcpApplication.java | 24 ++++++ .../tools/ImageSearchTool.java | 74 ++++++++++++++++ .../src/main/resources/application-sse.yml | 9 ++ .../src/main/resources/application-stdio.yml | 13 +++ .../src/main/resources/application.yml | 7 ++ .../tools/ImageSearchToolTest.java | 28 +++++++ pom.xml | 3 + 10 files changed, 277 insertions(+) create mode 100644 long-image-search-mcp-server/.gitattributes create mode 100644 long-image-search-mcp-server/.gitignore create mode 100644 long-image-search-mcp-server/pom.xml create mode 100644 long-image-search-mcp-server/src/main/java/com/huangge1199/longimagesearchmcp/LongImageSearchMcpApplication.java create mode 100644 long-image-search-mcp-server/src/main/java/com/huangge1199/longimagesearchmcp/tools/ImageSearchTool.java create mode 100644 long-image-search-mcp-server/src/main/resources/application-sse.yml create mode 100644 long-image-search-mcp-server/src/main/resources/application-stdio.yml create mode 100644 long-image-search-mcp-server/src/main/resources/application.yml create mode 100644 long-image-search-mcp-server/src/test/java/com/huangge1199/longimagesearchmcp/tools/ImageSearchToolTest.java diff --git a/long-image-search-mcp-server/.gitattributes b/long-image-search-mcp-server/.gitattributes new file mode 100644 index 0000000..3b41682 --- /dev/null +++ b/long-image-search-mcp-server/.gitattributes @@ -0,0 +1,2 @@ +/mvnw text eol=lf +*.cmd text eol=crlf diff --git a/long-image-search-mcp-server/.gitignore b/long-image-search-mcp-server/.gitignore new file mode 100644 index 0000000..667aaef --- /dev/null +++ b/long-image-search-mcp-server/.gitignore @@ -0,0 +1,33 @@ +HELP.md +target/ +.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ diff --git a/long-image-search-mcp-server/pom.xml b/long-image-search-mcp-server/pom.xml new file mode 100644 index 0000000..2bd5ba2 --- /dev/null +++ b/long-image-search-mcp-server/pom.xml @@ -0,0 +1,84 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 3.5.0 + + + com.huangge1199 + long-image-search-mcp-server + 0.0.1-SNAPSHOT + long-image-search-mcp + long-image-search-mcp-server + + + + + + + + + + + + + + + 21 + + + + org.springframework.boot + spring-boot-starter-test + test + + + org.projectlombok + lombok + true + + + cn.hutool + hutool-all + 5.8.37 + + + org.springframework.ai + spring-ai-mcp-server-webmvc-spring-boot-starter + 1.0.0-M6 + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + + + org.projectlombok + lombok + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + org.projectlombok + lombok + + + + + + + + diff --git a/long-image-search-mcp-server/src/main/java/com/huangge1199/longimagesearchmcp/LongImageSearchMcpApplication.java b/long-image-search-mcp-server/src/main/java/com/huangge1199/longimagesearchmcp/LongImageSearchMcpApplication.java new file mode 100644 index 0000000..efd4623 --- /dev/null +++ b/long-image-search-mcp-server/src/main/java/com/huangge1199/longimagesearchmcp/LongImageSearchMcpApplication.java @@ -0,0 +1,24 @@ +package com.huangge1199.longimagesearchmcp; + +import com.huangge1199.longimagesearchmcp.tools.ImageSearchTool; +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 LongImageSearchMcpApplication { + + public static void main(String[] args) { + SpringApplication.run(LongImageSearchMcpApplication.class, args); + } + + @Bean + public ToolCallbackProvider imageSearchTools(ImageSearchTool imageSearchTool) { + return MethodToolCallbackProvider.builder() + .toolObjects(imageSearchTool) + .build(); + } + +} diff --git a/long-image-search-mcp-server/src/main/java/com/huangge1199/longimagesearchmcp/tools/ImageSearchTool.java b/long-image-search-mcp-server/src/main/java/com/huangge1199/longimagesearchmcp/tools/ImageSearchTool.java new file mode 100644 index 0000000..677a1c3 --- /dev/null +++ b/long-image-search-mcp-server/src/main/java/com/huangge1199/longimagesearchmcp/tools/ImageSearchTool.java @@ -0,0 +1,74 @@ +package com.huangge1199.longimagesearchmcp.tools; + +import cn.hutool.core.util.StrUtil; +import cn.hutool.http.HttpUtil; +import cn.hutool.json.JSONObject; +import cn.hutool.json.JSONUtil; +import org.springframework.ai.tool.annotation.Tool; +import org.springframework.ai.tool.annotation.ToolParam; +import org.springframework.stereotype.Service; + +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +/** + * ImageSearchTool + * + * @author huangge1199 + * @since 2025/6/4 15:48:11 + */ +@Service +public class ImageSearchTool { + + // 替换为你的 Pexels API 密钥(需从官网申请) + private static final String API_KEY = "你的api-key"; + + // Pexels 常规搜索接口(请以文档为准) + private static final String API_URL = "https://api.pexels.com/v1/search"; + + @Tool(description = "search image from web") + public String searchImage(@ToolParam(description = "Search query keyword") String query) { + try { + return String.join(",", searchMediumImages(query)); + } catch (Exception e) { + return "Error search image: " + e.getMessage(); + } + } + + /** + * 搜索中等尺寸的图片列表 + * + * @param query + * @return + */ + public List searchMediumImages(String query) { + // 设置请求头(包含API密钥) + Map headers = new HashMap<>(); + headers.put("Authorization", API_KEY); + + // 设置请求参数(仅包含query,可根据文档补充page、per_page等参数) + Map params = new HashMap<>(); + params.put("query", query); + + // 发送 GET 请求 + String response = HttpUtil.createGet(API_URL) + .addHeaders(headers) + .form(params) + .execute() + .body(); + + // 解析响应JSON(假设响应结构包含"photos"数组,每个元素包含"medium"字段) + return JSONUtil.parseObj(response) + .getJSONArray("photos") + .stream() + .map(photoObj -> (JSONObject) photoObj) + .map(photoObj -> photoObj.getJSONObject("src")) + .map(photo -> photo.getStr("medium")) + .filter(StrUtil::isNotBlank) + .collect(Collectors.toList()); + } +} + diff --git a/long-image-search-mcp-server/src/main/resources/application-sse.yml b/long-image-search-mcp-server/src/main/resources/application-sse.yml new file mode 100644 index 0000000..494816b --- /dev/null +++ b/long-image-search-mcp-server/src/main/resources/application-sse.yml @@ -0,0 +1,9 @@ +spring: + ai: + mcp: + server: + name: long-image-search-mcp-server + version: 0.0.1 + type: SYNC + # sse + stdio: false diff --git a/long-image-search-mcp-server/src/main/resources/application-stdio.yml b/long-image-search-mcp-server/src/main/resources/application-stdio.yml new file mode 100644 index 0000000..bef27ef --- /dev/null +++ b/long-image-search-mcp-server/src/main/resources/application-stdio.yml @@ -0,0 +1,13 @@ +spring: + ai: + mcp: + server: + name: long-image-search-mcp-server + version: 0.0.1 + type: SYNC + # stdio + stdio: true + # stdio + main: + web-application-type: none + banner-mode: off diff --git a/long-image-search-mcp-server/src/main/resources/application.yml b/long-image-search-mcp-server/src/main/resources/application.yml new file mode 100644 index 0000000..fd06e9a --- /dev/null +++ b/long-image-search-mcp-server/src/main/resources/application.yml @@ -0,0 +1,7 @@ +spring: + application: + name: long-image-search-mcp-server + profiles: + active: stdio +server: + port: 8127 diff --git a/long-image-search-mcp-server/src/test/java/com/huangge1199/longimagesearchmcp/tools/ImageSearchToolTest.java b/long-image-search-mcp-server/src/test/java/com/huangge1199/longimagesearchmcp/tools/ImageSearchToolTest.java new file mode 100644 index 0000000..2eaec91 --- /dev/null +++ b/long-image-search-mcp-server/src/test/java/com/huangge1199/longimagesearchmcp/tools/ImageSearchToolTest.java @@ -0,0 +1,28 @@ +package com.huangge1199.longimagesearchmcp.tools; + +import jakarta.annotation.Resource; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +import static org.junit.jupiter.api.Assertions.*; + +/** + * ImageSearchToolTest + * + * @author huangge1199 + * @since 2025/6/4 15:51:37 + */ +@SpringBootTest +class ImageSearchToolTest { + + @Resource + private ImageSearchTool imageSearchTool; + + @Test + void searchImage() { + String result = imageSearchTool.searchImage("computer"); + System.out.println(result); + Assertions.assertNotNull(result); + } +} diff --git a/pom.xml b/pom.xml index ac9ce42..05cd2b8 100644 --- a/pom.xml +++ b/pom.xml @@ -26,6 +26,9 @@ + + long-image-search-mcp-server + 21