SpringAi调用Mcp

看了很多官网示例,没有找到对应依赖,无法调用? 我这篇帮你解决:

用示例 高德地图map吧,可以自行注册一个高德应用,有免费额度,确实很好用

值得推荐 我的应用 | 高德控制台

我自己写了很多demo示例,可以关注我的Gitee码云,里面有 langchain4j的示例:

复制代码
1.可以通过mysql兼容历史对话存储,进行上下文通话
2.通过自定义模版类型,让ai返回对应类型
3.调用mcp的测试demo:McpTest
4.通过分词器切割文档,通过向量模型分解向量存入向量milvus数据库

觉得有帮助记得给个Start和关注哦

https://gitee.com/djsijdbxbsjs/java-ai-demo.git

接入 当天天气示例:

对应的依赖在最下面

properties:

复制代码
spring.ai.mcp.client.connection-timeout=60s
spring.ai.mcp.client.type=ASYNC

mcp:
  server:
    gd: ${MCPMAPGD}

import com.fasterxml.jackson.databind.ObjectMapper;
import io.modelcontextprotocol.client.transport.HttpClientSseClientTransport;
import io.modelcontextprotocol.spec.McpClientTransport;
import org.springframework.ai.mcp.client.autoconfigure.NamedClientMcpTransport;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Collections;
import java.util.List;

@Configuration
public class McpConfig {
    @Value("${mcp.server.gd}")
    private String serverGd;

    @Bean
    public List<NamedClientMcpTransport> mcpClientTransport() {
        McpClientTransport transport = HttpClientSseClientTransport
                .builder("https://mcp.amap.com")
                .sseEndpoint("/sse?key="+serverGd)
                .objectMapper(new ObjectMapper())
                .build();

        return Collections.singletonList(new NamedClientMcpTransport("amap", transport));
    }

}

import io.modelcontextprotocol.client.McpAsyncClient;
import io.modelcontextprotocol.spec.McpSchema;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("ai")
@RestController
public class AiMcpDemoController {

    @Autowired
    private List<McpAsyncClient> mcpAsyncClients;

    @GetMapping("/chat_ollama")
    public Mono<McpSchema.CallToolResult> test(String city) {
        McpAsyncClient mcpClient = mcpAsyncClients.get(0);
        return mcpClient.listTools()
                .flatMap(tools -> {
//                    logger.info("tools: {}", tools);
                    Map map = new HashMap<>();
                    map.put("city", city);
                    return mcpClient.callTool(
                            new McpSchema.CallToolRequest(
                                    "maps_weather",
                                    map
                            )
                    );
                });
    }
}

Spring ai 官网示例依赖是统一管理,当然你也可以不用

复制代码
        <dependency>
            <groupId>org.springframework.ai</groupId>
            <artifactId>spring-ai-starter-mcp-client</artifactId>
        </dependency>



    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.ai</groupId>
                <artifactId>spring-ai-bom</artifactId>
                <version>1.0.0-SNAPSHOT</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
相关推荐
汤姆yu2 小时前
Hermes智能体使用指南
ai·大模型·智能体·hermes
camellias_2 小时前
【无标题】
java·tomcat
咸鱼2.03 小时前
【java入门到放弃】需要背诵
java·开发语言
椰猫子3 小时前
Java:异常(exception)
java·开发语言
企业架构师老王3 小时前
2026企业架构演进:科普Agent(龙虾)如何从“极客玩具”走向实在Agent规模化落地?
人工智能·ai·架构
鬼先生_sir3 小时前
Spring AI Alibaba 1.1.2.2 完整知识点库
人工智能·ai·agent·源码解析·springai
win x4 小时前
Redis 使用~如何在Java中连接使用redis
java·数据库·redis
星晨雪海4 小时前
基于 @Resource 的支付 Service 多实现类完整示例
java·开发语言
阿维的博客日记4 小时前
什么是逃逸分析
java·juc