Spring Boot使用MCP服务器

1、JDK版本17

2、pom文件

复制代码
<?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 http://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.4.5</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.example</groupId>

    <artifactId>mcp-weather-starter-webmvc-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <name>Spring AI MCP Weather Sample</name>
    <description>Sample Spring Boot application demonstrating MCP client and server usage</description>

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

    <dependencies>
        <dependency>
            <groupId>org.springframework.ai</groupId>
            <artifactId>spring-ai-starter-mcp-server-webmvc</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

	<repositories>
		<repository>
			<id>spring-milestones</id>
			<name>Spring Milestones</name>
			<url>https://repo.spring.io/milestone</url>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</repository>
		<repository>
			<id>spring-snapshots</id>
			<name>Spring Snapshots</name>
			<url>https://repo.spring.io/snapshot</url>
			<releases>
				<enabled>false</enabled>
			</releases>
		</repository>
		<repository>
			<id>central-portal-snapshots</id>
			<name>Central Portal Snapshots</name>
			<url>https://central.sonatype.com/repository/maven-snapshots/</url>
			<releases>
				<enabled>false</enabled>
			</releases>
			<snapshots>
				<enabled>true</enabled>
			</snapshots>
		</repository>
	</repositories>
</project>

3、配置文件application.properties

复制代码
# spring.main.web-application-type=none

# NOTE: You must disable the banner and the console logging 
# to allow the STDIO transport to work !!!
spring.main.banner-mode=off
# logging.pattern.console=

# spring.ai.mcp.server.stdio=false

spring.ai.mcp.server.name=my-weather-server
spring.ai.mcp.server.version=0.0.1

logging.file.name=./model-context-protocol/weather/starter-webmvc-server/target/starter-webmvc-server.log

4、MCP服务

复制代码
BIService
复制代码
package org.springframework.ai.mcp.sample.server;

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

@Component
public class BIService {

    @Tool(description = "根据客户名称查询项目信息,输入参数是客户名称")
    public String getProjectInfoByName(@ToolParam(description = "客户名称") String name) {
        return "系统查询用户,"+name+"项目信息";
    }

    @Tool(description = "根据合同编号查询项目信息,输入参数是合同编号")
    public String getProjectByContactNumber(@ToolParam(description = "合同编号") String contactNumber) {
        return "系统查询合同编号,"+contactNumber+"项目信息";
    }

}
复制代码
MIService
复制代码
package org.springframework.ai.mcp.sample.server;

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

@Component
public class MIService {

    @Tool(description = "根据城市名称获取该城市的天气信息")
    public String getWeather(@ToolParam(description = "城市") String city){
        return city+":温度25度,大太阳,适合旅行";
    }

}

启动类

复制代码
package org.springframework.ai.mcp.sample.server;

import org.springframework.ai.tool.ToolCallback;
import org.springframework.ai.tool.ToolCallbackProvider;
import org.springframework.ai.tool.function.FunctionToolCallback;
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 McpServerApplication {

	public static void main(String[] args) {
		SpringApplication.run(McpServerApplication.class, args);
	}

	@Bean
	public ToolCallbackProvider mcpTool(BIService biService,MIService miService) {
		return MethodToolCallbackProvider.builder()
				.toolObjects(miService,biService)
				.build();
	}
}

5、cursor访问MCP服务

复制代码
{
  "mcpServers": {
    "bi-mcp-server": {
      "url": "http://localhost:8080/sse"
    }
  }

}

6、maxkb访问mcp服务

复制代码
{
	"bi-mcp-server": {
		"url": "http://host.docker.internal:8080/sse",
		"transport": "sse"
	}
}
相关推荐
whltaoin37 分钟前
SpringCloud 项目阶段九:Kafka 接入实战指南 —— 从基础概念、安装配置到 Spring Boot 实战及高可用设计
spring boot·spring cloud·kafka
callJJ1 小时前
从 0 开始理解 Spring 的核心思想 —— IoC 和 DI(2)
java·开发语言·后端·spring·ioc·di
不务专业的程序员--阿飞3 小时前
JVM无法分配内存
java·jvm·spring boot
你的人类朋友3 小时前
JWT的组成
后端
tritone3 小时前
我在阿贝云免费服务器上搭建RustDesk自建服务器(Self-Hosting)的真实体验【推荐】
运维·服务器
洲覆3 小时前
Redis 核心数据类型:从命令、结构到实战应用
服务器·数据库·redis·缓存
Q_Q5110082854 小时前
python+django/flask+uniapp基于微信小程序的瑜伽体验课预约系统
spring boot·python·django·flask·uni-app·node.js·php
小牛马爱写博客4 小时前
DNS 服务器与 DHCP 服务器详解及配置指南
linux·运维·服务器·dns·dhcp
什么半岛铁盒4 小时前
C++项目:仿muduo库高并发服务器-------Channel模块实现
linux·服务器·数据库·c++·mysql·ubuntu
2503_924806854 小时前
动态IP使用中 报错407 怎么办???
服务器·tcp/ip·php