MCP:图片搜索服务
This commit is contained in:
parent
497bb33098
commit
03c70db3ab
2
long-image-search-mcp-server/.gitattributes
vendored
Normal file
2
long-image-search-mcp-server/.gitattributes
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
/mvnw text eol=lf
|
||||
*.cmd text eol=crlf
|
33
long-image-search-mcp-server/.gitignore
vendored
Normal file
33
long-image-search-mcp-server/.gitignore
vendored
Normal file
@ -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/
|
84
long-image-search-mcp-server/pom.xml
Normal file
84
long-image-search-mcp-server/pom.xml
Normal file
@ -0,0 +1,84 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>3.5.0</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
<groupId>com.huangge1199</groupId>
|
||||
<artifactId>long-image-search-mcp-server</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>long-image-search-mcp</name>
|
||||
<description>long-image-search-mcp-server</description>
|
||||
<url/>
|
||||
<licenses>
|
||||
<license/>
|
||||
</licenses>
|
||||
<developers>
|
||||
<developer/>
|
||||
</developers>
|
||||
<scm>
|
||||
<connection/>
|
||||
<developerConnection/>
|
||||
<tag/>
|
||||
<url/>
|
||||
</scm>
|
||||
<properties>
|
||||
<java.version>21</java.version>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>cn.hutool</groupId>
|
||||
<artifactId>hutool-all</artifactId>
|
||||
<version>5.8.37</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.ai</groupId>
|
||||
<artifactId>spring-ai-mcp-server-webmvc-spring-boot-starter</artifactId>
|
||||
<version>1.0.0-M6</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<annotationProcessorPaths>
|
||||
<path>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
</path>
|
||||
</annotationProcessorPaths>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<configuration>
|
||||
<excludes>
|
||||
<exclude>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
</exclude>
|
||||
</excludes>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
@ -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<String> searchMediumImages(String query) {
|
||||
// 设置请求头(包含API密钥)
|
||||
Map<String, String> headers = new HashMap<>();
|
||||
headers.put("Authorization", API_KEY);
|
||||
|
||||
// 设置请求参数(仅包含query,可根据文档补充page、per_page等参数)
|
||||
Map<String, Object> 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());
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,9 @@
|
||||
spring:
|
||||
ai:
|
||||
mcp:
|
||||
server:
|
||||
name: long-image-search-mcp-server
|
||||
version: 0.0.1
|
||||
type: SYNC
|
||||
# sse
|
||||
stdio: false
|
@ -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
|
@ -0,0 +1,7 @@
|
||||
spring:
|
||||
application:
|
||||
name: long-image-search-mcp-server
|
||||
profiles:
|
||||
active: stdio
|
||||
server:
|
||||
port: 8127
|
@ -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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user