偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

解鎖 AI 與后端服務(wù)對話的力量:基于 Spring Boot + Spring AI 構(gòu)建 Claude 本地 MCP 服務(wù)端

人工智能
本文將從零開始,教你如何使用 Spring Boot + Spring AI 構(gòu)建一套符合 MCP 協(xié)議的服務(wù)端,并無縫接入 Claude Desktop 客戶端。最終實現(xiàn) Claude 通過自然語言與本地服務(wù)交互,比如“新增一個課程”或“獲取所有課程信息”。

在現(xiàn)代 AI 應(yīng)用中,一個日益突出的挑戰(zhàn)是:如何讓大語言模型理解并調(diào)用我們的業(yè)務(wù)系統(tǒng)、數(shù)據(jù)庫或私有 API?Model Context Protocol(MCP)為我們提供了這座橋梁。通過實現(xiàn) MCP 協(xié)議,我們可以讓 Claude 等模型“開口”與本地服務(wù)交流,調(diào)用私有接口、訪問實時數(shù)據(jù),甚至直接執(zhí)行復(fù)雜業(yè)務(wù)邏輯。

本文將從零開始,教你如何使用 Spring Boot + Spring AI 構(gòu)建一套符合 MCP 協(xié)議的服務(wù)端,并無縫接入 Claude Desktop 客戶端。最終實現(xiàn) Claude 通過自然語言與本地服務(wù)交互,比如“新增一個課程”或“獲取所有課程信息”。

什么是 MCP?為什么它重要?

Model Context Protocol(MCP) 是一套開放標(biāo)準(zhǔn),它的核心作用是讓 AI 模型通過結(jié)構(gòu)化協(xié)議與外部世界交互。具體能力包括:

  • 訪問受限或私有數(shù)據(jù)源
  • 調(diào)用本地 API 執(zhí)行業(yè)務(wù)邏輯
  • 執(zhí)行模型訓(xùn)練時未具備的功能
  • 實現(xiàn) AI 與任意第三方系統(tǒng)的實時交互

你可以把 MCP 理解為 LLM 的「插件系統(tǒng)」,讓原本只能回答文字的模型,具備了「動手能力」。

技術(shù)選型與開發(fā)框架

本項目使用如下技術(shù)棧:

  • Spring Boot 3.3+
  • Spring AI + MCP Tool Framework
  • PostgreSQL 作為業(yè)務(wù)數(shù)據(jù)源
  • Claude Desktop 作為對接客戶端
  • Maven 構(gòu)建工具

項目概覽

圖片

安裝 Claude Desktop 在本項目中,我們使用的是 Claude Desktop。如果你尚未安裝,請前往 Claude 官方網(wǎng)站下載并安裝 Claude Desktop。

安裝并打開后,你將看到如下所示的聊天窗口:

圖片

第一步:構(gòu)建課程業(yè)務(wù)服務(wù)(Spring Boot)

這一層是我們的真實業(yè)務(wù)系統(tǒng),用于模擬 Claude 要調(diào)用的“第三方服務(wù)”。

/controller/CourseController.java

package com.icoderoad.claude.controller;


import com.icoderoad.claude.model.Course;
import com.icoderoad.claude.service.CourseService;
import org.springframework.web.bind.annotation.*;


import java.util.List;


@RestController
@RequestMapping("/api/courses")
public class CourseController {
    private final CourseService courseService;


    public CourseController(CourseService courseService) {
        this.courseService = courseService;
    }


    @GetMapping("/all")
    public List<Course> all() {
        return courseService.getAll();
    }


    @GetMapping("/{title}")
    public Course get(@PathVariable String title) {
        return courseService.getByTitle(title);
    }


    @PostMapping
    public Course add(@RequestBody Course course) {
        return courseService.create(course);
    }


    @PutMapping("/{title}")
    public Course update(@PathVariable String title, @RequestBody Course course) {
        return courseService.update(title, course);
    }


    @DeleteMapping("/{title}")
    public void delete(@PathVariable String title) {
        courseService.delete(title);
    }
}

/service/CourseService.java

package com.icoderoad.claude.service;


import com.icoderoad.claude.entity.CourseEntity;
import com.icoderoad.claude.model.Course;
import com.icoderoad.claude.repository.CourseRepository;
import org.springframework.stereotype.Service;


import java.util.List;


@Service
public class CourseService {
    private final CourseRepository repo;


    public CourseService(CourseRepository repo) {
        this.repo = repo;
    }


    public List<Course> getAll() {
        return repo.findAll().stream()
                .map(e -> new Course(e.getTitle(), e.getUrl()))
                .toList();
    }


    public Course getByTitle(String title) {
        return repo.findByTitle(title)
                .map(e -> new Course(e.getTitle(), e.getUrl()))
                .orElse(null);
    }


    public Course create(Course course) {
        var entity = new CourseEntity();
        entity.setTitle(course.title());
        entity.setUrl(course.url());
        var saved = repo.save(entity);
        return new Course(saved.getTitle(), saved.getUrl());
    }


    public Course update(String title, Course course) {
        return repo.findByTitle(title).map(e -> {
            e.setUrl(course.url());
            var updated = repo.save(e);
            return new Course(updated.getTitle(), updated.getUrl());
        }).orElse(null);
    }


    public void delete(String title) {
        repo.findByTitle(title).ifPresent(repo::delete);
    }
}

第二步:基于 MCP 定義 AI 可調(diào)用的工具服務(wù)

這一步中,我們將業(yè)務(wù)系統(tǒng)“暴露”給 Claude 模型,并注冊為 MCP 工具。

/mcp/CourseToolService.java

package com.icoderoad.claude.mcp;


import com.icoderoad.claude.model.Course;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;


import java.util.Arrays;
import java.util.List;


@Service
public class CourseToolService {


    private final RestTemplate rest;
    private static final String BASE = "http://localhost:9090/api/courses";


    public CourseToolService(RestTemplate rest) {
        this.rest = rest;
    }


    @Tool(name = "get_courses", description = "獲取所有課程")
    public List<Course> getCourses() {
        Course[] list = rest.getForObject(BASE + "/all", Course[].class);
        return Arrays.asList(list);
    }


    @Tool(name = "get_course", description = "按標(biāo)題獲取課程")
    public Course getCourse(String title) {
        return rest.getForObject(BASE + "/" + title, Course.class);
    }


    @Tool(name = "add_course", description = "新增課程")
    public Course addCourse(Course course) {
        return rest.postForObject(BASE, course, Course.class);
    }


    @Tool(name = "update_course", description = "修改課程")
    public void update(String title, Course course) {
        var entity = new HttpEntity<>(course);
        rest.exchange(BASE + "/" + title, HttpMethod.PUT, entity, Void.class);
    }


    @Tool(name = "delete_course", description = "刪除課程")
    public void delete(String title) {
        rest.delete(BASE + "/" + title);
    }
}

第三步:Spring Boot 啟動類配置 + 注冊 MCP 工具

package com.icoderoad.claude;


import com.icoderoad.claude.mcp.CourseToolService;
import org.springframework.ai.support.ToolCallbacks;
import org.springframework.ai.tool.ToolCallback;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;


import java.util.List;


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


    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }


    @Bean
    public List<ToolCallback> tools(CourseToolService service) {
        return List.of(ToolCallbacks.from(service));
    }
}

application.properties 配置 MCP 模式。

spring.application.name=claude
spring.main.web-application-type=none
spring.ai.mcp.server.name=claude-mcp
spring.ai.mcp.server.version=0.0.1
spring.main.banner-mode=off

啟動并接入 Claude Desktop。

使用以下命令構(gòu)建 MCP 服務(wù)端:

mvn clean package

找到構(gòu)建出的 JAR 包,比如:

target/claude-0.0.1-SNAPSHOT.jar

然后,在 Claude Desktop 的配置文件(如 ~/.claude/claude_desktop_config.json)中,添加如下 MCP Server 配置:

{
  "mcpServers": {
    "claude-mcp": {
      "command": "/usr/bin/java",
      "args": [
        "-jar",
        "/home/user/claude/target/claude-0.0.1-SNAPSHOT.jar"
      ]
    }
  }
}

重啟 Claude Desktop,在 設(shè)置 > Developer > MCP Servers 處即可看到 claude-mcp 工具服務(wù)被成功識別。

驗證 Claude AI 的調(diào)用能力

打開 Claude Desktop 對話框,嘗試:

請?zhí)砑右粋€新課程:標(biāo)題為 "Spring AI 入門",地址是 "https://icoderoad.com/spring-ai"

你會看到 Claude 調(diào)用了我們的 MCP 服務(wù),業(yè)務(wù)系統(tǒng)的數(shù)據(jù)庫被成功寫入一條新記錄!

總結(jié):AI × Java 工程師的未來已來

通過本文,我們成功構(gòu)建了一個 Spring Boot + Spring AI 驅(qū)動的 MCP 本地服務(wù)端,實現(xiàn)了 Claude 模型對本地 API 的實時調(diào)用。這種模式背后的巨大意義在于:

  • 為 AI 模型賦予了執(zhí)行力,接入真實世界的業(yè)務(wù)邏輯;
  • MCP 成為大模型“插件系統(tǒng)”,打通企業(yè)私域數(shù)據(jù);
  • Spring AI 提供優(yōu)雅的注解式開發(fā)體驗,讓 Java 工程師也能輕松參與大模型生態(tài)。

從今天開始,你的每一行業(yè)務(wù)邏輯都可以是 Claude 的可調(diào)用工具,讓 AI 真正成為你的超級編程搭子。

責(zé)任編輯:武曉燕 來源: 路條編程
相關(guān)推薦

2025-04-29 09:07:21

2025-03-28 09:33:11

2025-02-19 12:00:00

SpringBootDeepSeekAI

2025-07-29 09:06:29

SpringAIMCP

2025-07-08 07:33:48

2025-08-06 01:44:00

2025-09-30 09:20:48

SpringAIMCP

2024-10-10 08:34:34

事務(wù)外包模式

2024-04-28 08:18:28

SpringAIGen AI

2022-10-10 08:00:00

微服務(wù)Spring Boo容器

2025-04-29 00:04:00

2025-08-21 07:07:49

2025-03-31 09:30:52

2025-06-03 01:04:00

MCPAI架構(gòu)

2025-09-24 10:21:11

2025-06-03 09:31:05

2022-05-18 08:32:05

服務(wù)監(jiān)控Prometheus開源

2025-04-21 04:22:00

Spring AIMCPDeepSeek

2022-03-18 09:00:00

開發(fā)Web服務(wù)應(yīng)用程序
點贊
收藏

51CTO技術(shù)棧公眾號