如何使用java構(gòu)建mcp-server

1 添加maven依賴

<?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.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>org.example</groupId>
    <artifactId>jdbc-mcp-server</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <spring-ai.version>1.0.0-M6</spring-ai.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.ai</groupId>
            <artifactId>spring-ai-mcp-server-webmvc-spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.2.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>6.1.0</version>
        </dependency>
        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <version>8.0.33</version>
        </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.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

2 配置文件application.properties內(nèi)容如下:

spring.ai.mcp.server.name=jdbc-mcp-server
# 不是本地進程通信方式,使用sse方式
spring.ai.mcp.server.stdio=false

spring.data.jdbc.dialect=mysql
spring.datasource.url=jdbc:mysql://localhost:3306/gm?useSSL=false&serverTimezone=UTC
spring.datasource.username=gongmin
spring.datasource.password=gongmin
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

3 申明mcp server tools:

package org.example.mcp.service;

import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

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

/**
 * TODO
 *
 * @author gongmin
 * @date 2025/4/16 17:39
 */
@Service
public class JdbcQueryService {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Tool(description = "查詢數(shù)據(jù)庫中所有的表")
    public List<Map<String, Object>> queryAllTables() {
        String sql = "SELECT TABLE_NAME , TABLE_COMMENT FROM information_schema.tables WHERE TABLE_SCHEMA = DATABASE()";
        return jdbcTemplate.queryForList(sql);
    }

    @Tool(description = "查詢數(shù)據(jù)庫中的表的數(shù)據(jù)")
    public List<Map<String, Object>> queryTable(@ToolParam(description = "表名") String tableName) {
        String sql = "SELECT * FROM " + tableName;
        return jdbcTemplate.queryForList(sql);
    }

    @Tool(description = "實現(xiàn)兩個數(shù)的加法")
    public int add(@ToolParam(description = "數(shù)值1") int num1, @ToolParam(description = "數(shù)值2") int num2) {
        return num1 + num2;
    }
}

4 注冊mcp server tools

package org.example.mcp.config;

import org.example.mcp.service.JdbcQueryService;
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;

/**
 * TODO
 *
 * @author gongmin
 * @date 2025/4/16 17:40
 */
@Configuration
public class McpConfig {
    @Bean
    ToolCallbackProvider jdbcQueryTools(JdbcQueryService jdbcQueryService) {
        return MethodToolCallbackProvider
                .builder()
                .toolObjects(jdbcQueryService)
                .build();
    }
}

5 申明配置類注冊Bean

package org.example.mcp.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;

/**
 * TODO
 *
 * @author gongmin
 * @date 2025/4/16 20:50
 */
@Configuration
public class DatabaseConfig {

    @Bean
    public JdbcTemplate jdbcTemplate(DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }
}

6 啟動類

package org.example.mcp;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * TODO
 *
 * @author gongmin
 * @date 2025/4/16 19:56
 */
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容