mcp
This commit is contained in:
parent
47becaa432
commit
f34c61535f
6
pom.xml
6
pom.xml
@ -64,7 +64,13 @@
|
|||||||
<groupId>org.jsoup</groupId>
|
<groupId>org.jsoup</groupId>
|
||||||
<artifactId>jsoup</artifactId>
|
<artifactId>jsoup</artifactId>
|
||||||
<version>1.20.1</version>
|
<version>1.20.1</version>
|
||||||
|
</dependency><!-- https://mvnrepository.com/artifact/dev.langchain4j/langchain4j-mcp -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>dev.langchain4j</groupId>
|
||||||
|
<artifactId>langchain4j-mcp</artifactId>
|
||||||
|
<version>1.1.0-beta7</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
@ -2,6 +2,7 @@ package com.huangge1199.ai.config;
|
|||||||
|
|
||||||
import com.huangge1199.ai.service.LangChainService;
|
import com.huangge1199.ai.service.LangChainService;
|
||||||
import com.huangge1199.ai.tool.InterviewQuestionTool;
|
import com.huangge1199.ai.tool.InterviewQuestionTool;
|
||||||
|
import dev.langchain4j.mcp.McpToolProvider;
|
||||||
import dev.langchain4j.memory.ChatMemory;
|
import dev.langchain4j.memory.ChatMemory;
|
||||||
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
|
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
|
||||||
import dev.langchain4j.model.chat.ChatModel;
|
import dev.langchain4j.model.chat.ChatModel;
|
||||||
@ -26,6 +27,9 @@ public class LangChainConfig {
|
|||||||
@Resource
|
@Resource
|
||||||
private ContentRetriever contentRetriever;
|
private ContentRetriever contentRetriever;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private McpToolProvider mcpToolProvider;
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public LangChainService langChainService() {
|
public LangChainService langChainService() {
|
||||||
ChatMemory chatMemory = MessageWindowChatMemory.withMaxMessages(10);
|
ChatMemory chatMemory = MessageWindowChatMemory.withMaxMessages(10);
|
||||||
@ -35,6 +39,7 @@ public class LangChainConfig {
|
|||||||
.chatMemoryProvider(memoryId->MessageWindowChatMemory.withMaxMessages(10))
|
.chatMemoryProvider(memoryId->MessageWindowChatMemory.withMaxMessages(10))
|
||||||
.contentRetriever(contentRetriever)
|
.contentRetriever(contentRetriever)
|
||||||
.tools(new InterviewQuestionTool())
|
.tools(new InterviewQuestionTool())
|
||||||
|
.toolProvider(mcpToolProvider)
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
44
src/main/java/com/huangge1199/ai/config/McpConfig.java
Normal file
44
src/main/java/com/huangge1199/ai/config/McpConfig.java
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
package com.huangge1199.ai.config;
|
||||||
|
|
||||||
|
import dev.langchain4j.mcp.McpToolProvider;
|
||||||
|
import dev.langchain4j.mcp.client.DefaultMcpClient;
|
||||||
|
import dev.langchain4j.mcp.client.McpClient;
|
||||||
|
import dev.langchain4j.mcp.client.transport.McpTransport;
|
||||||
|
import dev.langchain4j.mcp.client.transport.http.HttpMcpTransport;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* McpConfig
|
||||||
|
*
|
||||||
|
* @author huangge1199
|
||||||
|
* @since 2025/7/14 10:13:43
|
||||||
|
*/
|
||||||
|
@Configuration
|
||||||
|
public class McpConfig {
|
||||||
|
|
||||||
|
@Value("${bigmodel.api-key}")
|
||||||
|
private String apiKey;
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public McpToolProvider mcpToolProvider() {
|
||||||
|
// 和 MCP 服务通讯
|
||||||
|
McpTransport transport = new HttpMcpTransport.Builder()
|
||||||
|
.sseUrl("https://open.bigmodel.cn/api/mcp/web_search/sse?Authorization=" + apiKey)
|
||||||
|
.logRequests(true)
|
||||||
|
.logResponses(true)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
// 创建 MCP 客户端
|
||||||
|
McpClient mcpClient = new DefaultMcpClient.Builder()
|
||||||
|
.key("test")
|
||||||
|
.transport(transport)
|
||||||
|
.build();
|
||||||
|
// 从 MCP 客户端获取工具
|
||||||
|
return McpToolProvider.builder()
|
||||||
|
.mcpClients(mcpClient)
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -4,6 +4,8 @@ spring:
|
|||||||
name: ai-langChain4j
|
name: ai-langChain4j
|
||||||
profiles:
|
profiles:
|
||||||
active: local
|
active: local
|
||||||
|
server:
|
||||||
|
port: 8888
|
||||||
langchain4j:
|
langchain4j:
|
||||||
community:
|
community:
|
||||||
dashscope:
|
dashscope:
|
||||||
@ -13,5 +15,5 @@ langchain4j:
|
|||||||
embedding-model:
|
embedding-model:
|
||||||
model-name: text-embedding-v4
|
model-name: text-embedding-v4
|
||||||
api-key: <You API Key here>
|
api-key: <You API Key here>
|
||||||
server:
|
bigmodel:
|
||||||
port: 8888
|
api-key: <You API Key here>
|
||||||
|
Loading…
Reference in New Issue
Block a user