当前位置: 首页 > news >正文

AI2.0 【Mcp-client】 20260611

 

1、pom

<properties>
<java.version>17</java.version>
<spring-ai.version>2.0.0-M5</spring-ai.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webmvc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-openai</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-ollama</artifactId>
</dependency>
<!-- Redis 向量库-->
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-vector-store-redis</artifactId>
</dependency>
<!--Redis 客户端-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<exclusions>
<exclusion>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
<!-- Tika 通用文档读取器 -->
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-tika-document-reader</artifactId>
</dependency>
<!-- Markdown 文档读取器 -->
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-markdown-document-reader</artifactId>
</dependency>
<!-- 问答顾问器 advisor-->
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-advisors-vector-store</artifactId>
</dependency>
<!-- MCP 客户端:通过 SSE 连接本机 mcp-server(默认端口 18089) -->
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-mcp-client</artifactId>
</dependency>
</dependencies>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-bom</artifactId>
<version>${spring-ai.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

 

2、application.yml

2.1

${OPENAI_API_KEY}

image

 

 


spring:
autoconfigure:
exclude: #禁用 Ollama Embedding(如果你用 OpenAI),不然会冲突
- org.springframework.ai.model.ollama.autoconfigure.OllamaEmbeddingAutoConfiguration
application:
name: dashscope18088
data:
redis:
url: redis://192.168.91.165:6379
client-type: jedis
ai:
openai:
api-key: ${OPENAI_API_KEY}
base-url: https://dashscope.aliyuncs.com/compatible-mode/v1
chat:
options:
model: qwen3.6-plus
embedding:
options:
model: text-embedding-v4
dimensions: 2048 # 输出向量维度2048
# chat:
# model: qwen3.6-plus
mcp:
client:
enabled: true
type: SYNC
# 与 mcp-server 一致;须先启动 mcp-server(默认 http://localhost:18089)
sse:
connections:
mcp-weather-server:
url: http://localhost:18089
vectorstore:
redis:
initialize-schema: true #是否在启动时创建/初始化向量索引等结构
index-name: sf2026_ai_vector_index # RediSearch 里的索引名
prefix: sf2026 #存向量文档用的Redis键前级
ollama:
base-url: http://192.168.91.164:11434
chat:
model: qwen2.5:0.5b

# options:
# temperature: 0.7
# max-tokens: 2000
server:
port: 18088
logging:
level:
org.springframework.ai: debug
org.springframework.ai.mcp: debug







3、controller

 
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.mcp.SyncMcpToolCallbackProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyMcpChatController {

@Autowired
private ChatClient chatClient;

@Autowired
private SyncMcpToolCallbackProvider mcpToolCallbackProvider;

// /**
// *
// * @return
// */
// @RequestMapping("/aimcp")
// public String aiWithMcp() {
// String q = "查一下上海天气";
// String result = chatClient.prompt()
// .system("用户询问天气时,你必须调用工具查询后再用简短中文回答。")
// .user(q)
// .toolCallbacks(mcpToolCallbackProvider)
// .call()
// .content();
// System.out.println(result);
// return "OK";
// }

@RequestMapping("/aimcp")
public String aiWithMcp() {
String q = "查一下上海天气";
String result = chatClient.prompt()
.system("用户询问天气时,你必须调用工具查询后再用简短中文回答。")
.user(q)
.toolCallbacks(mcpToolCallbackProvider)
.advisors(a->a.param(ChatMemory.CONVERSATION_ID,"0001"))
.call()
.content();
System.out.println(result);
return result; // 直接返回回答,而不是固定的"OK"
}
}
 
 
4、http://localhost:18088/aimcp

image

 

image