java本地实现mcp的服务器调用

前置准备工具

  1. 去申请个deepseek apiKey,https://www.deepseek.com/
  2. 新建一个springBoot项目
  3. idea插件安装Cline

代码配置

依赖

java 复制代码
<?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>4.0.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.lin</groupId>
    <artifactId>mcpServer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>mcpServer</name>
    <description>mcpServer</description>
    <url/>
    <licenses>
        <license/>
    </licenses>
    <developers>
        <developer/>
    </developers>
    <scm>
        <connection/>
        <developerConnection/>
        <tag/>
        <url/>
    </scm>
    <properties>
        <java.version>17</java.version>
        <spring-ai.version>2.0.0-M2</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-mcp-server-webmvc</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webmvc-test</artifactId>
            <scope>test</scope>
        </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>

    <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>

service

java 复制代码
package com.lin.mcpserver.service;

import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @author 
 */
@Service
public class WeatherService {

    @Tool(description = "根据城市名称获取天气预报")
    public String getWeatherByCity(
            @ToolParam(description = "城市名称") String cityName) {
        System.out.println("我来查询了");
        // 这里可以是复杂的业务逻辑,例如调用第三方天气API
        Map<String, String> mockData = Map.of(
                "北京", "多云转晴,15~25°C,微风",
                "上海", "小雨,18~22°C,东南风3级",
                "深圳", "雷阵雨,25~32°C,南风2级"
        );
        return mockData.getOrDefault(cityName, "抱歉,暂未找到该城市的天气信息。");
    }


}

config

java 复制代码
package com.lin.mcpserver.config;

import com.lin.mcpserver.service.WeatherService;
import org.springframework.ai.tool.ToolCallbackProvider;
import org.springframework.ai.tool.method.MethodToolCallbackProvider;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author
 */
@Configuration
public class McpConfig {
    @Bean
    public ToolCallbackProvider weatherTools(WeatherService weatherService) {
        // 将 WeatherService 中所有带有 @Tool 注解的方法注册为 MCP 工具
        return MethodToolCallbackProvider.builder()
                .toolObjects(weatherService) // 可以传入多个 Bean
                .build();
    }
}

配置

java 复制代码
spring.application.name=mcpServer

spring.ai.mcp.server.enabled=true
spring.ai.mcp.server.name=mcp-server
spring.ai.mcp.server.version=1.0.0
spring.ai.mcp.server.sse-endpoint=/sse
spring.ai.mcp.server.sse-message-endpoint=/mcp
spring.ai.mcp.server.capabilities.tool=true
spring.ai.mcp.server.capabilities.resource=false
spring.ai.mcp.server.capabilities.prompt=false

logging.level.org.springframework.ai.mcp=DEBUG



server.port=8081

cline配置

启动项目后,在cline里配置好deepseek的apiKey

然后在MCP Servers里配置本地的mcp Server服务器

按本地配置的填入后是长这样

点击add Server,可以看见加入成功,且成功连接

这时候再去问ai,某个城市的天气怎么样,ai就会识别去调用mcp的服务器去查询。

相关推荐
季明洵1 分钟前
Java实现循环队列、栈实现队列、队列实现栈
java·数据结构·算法··队列
天上飞的粉红小猪3 分钟前
数据链路层
linux·服务器·网络
+VX:Fegn08954 分钟前
计算机毕业设计|基于springboot + vue社区智慧消防管理系统(源码+数据库+文档)
数据库·vue.js·spring boot·后端·课程设计
开发者导航6 分钟前
精选高质量网址资源的高效聚合综合性的网址导航:跳跳兔导航网
服务器·人工智能·程序人生·搜索引擎·开源软件
一 乐8 分钟前
英语学习平台系统|基于springboot + vue英语学习平台系统(源码+数据库+文档)
java·vue.js·spring boot·学习·论文·毕设·英语学习平台系统
+VX:Fegn08951 小时前
计算机毕业设计|基于springboot + vue物业管理系统(源码+数据库+文档)
数据库·vue.js·spring boot·后端·课程设计
无心水8 小时前
【任务调度:数据库锁 + 线程池实战】3、 从 SELECT 到 UPDATE:深入理解 SKIP LOCKED 的锁机制与隔离级别
java·分布式·科技·spring·架构
funnycoffee1238 小时前
linux系统DNS修改命令
linux·运维·服务器·linux dns
编程小白gogogo9 小时前
苍穹外卖图片不显示解决教程
java·spring boot
舟舟亢亢9 小时前
算法总结——二叉树【hot100】(上)
java·开发语言·算法