Spring AI通过MCP支持大语言模型调用工具

什么是MCP

模型上下文协议(英语:Model Context Protocol,缩写:MCP)是 Anthropic 所推动的一项开放标准,旨在为大型语言模型(LLMs)应用提供一个标准化的接口,使其能够连接和交互外部数据源和工具。

模型上下文协议的目标是克服 LLMs 应用仅依赖其训练数据的局限性,使其能够访问所需的上下文信息,并执行更广泛的任务。该协议通过定义一套通用的规则和格式,使得 LLMs 应用可以在需要时动态地获取相关信息和执行操作,从而增强其功能和应用范围。

安装与测试

部署本地大语言模型

通过官网直接下载并安装:ollama.com/

执行命令:ollama pull qwen2.5:latest

出现下图内容,则是完成安装:

安装npx

安装nodejs环境

方法一(推荐)

通过nvm版本管理软件安装,下载地址:github.com/coreybutler...

perl 复制代码
nvm -v 
nvm install 20.9.0
nvm use 20.9.0
nvm list
方法二

通过官网直接下载并安装:nodejs.org/zh-cn

安装npx

复制代码
npm install -g npx

注意:如果是通过方法一安装的,在配置mcp服务的时候,需要通过绝对路径调用而不能直接使用npx

创建Springboot项目

mcp-service在初始化的时候需要读取cmd执行的结果,如果设置了默认字符集或者其他自动执行的命令需要注意一下这个影响。

相关依赖

xml 复制代码
<properties>
    <spring-ai.version>1.0.0-M6</spring-ai.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.ai</groupId>
        <artifactId>spring-ai-mcp-client-spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.ai</groupId>
        <artifactId>spring-ai-mcp-server-webmvc-spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.ai</groupId>
        <artifactId>spring-ai-ollama-spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-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>

Application配置文件

yaml 复制代码
spring:
  application:
    name: mcp
  ai:
    ollama:
      base-url: http://localhost:11434
      chat:
        model: qwen2.5:latest
    mcp:
      client:
        enabled: true
        name: mcp-client
        version: 1.0.0
        type: SYNC
        request-timeout: 30s
        stdio:
          servers-configuration: classpath:/mcp-servers-config.json

mcp服务配置文件

  1. 如果是使用NVM版本管理工具需要指定npx的管理工具的绝对路径(Windows环境下,调用的是npx.cmd)
  2. 在执行这段命令的时候,会解析shell的返回结果。在Windows环境下,如果设置了默认执行的脚本(比如: chcp 65001),会影响到结果的解析,并且也不要修改默认的字符集为65001。
perl 复制代码
{
  "mcpServers": {
    "filesystem": {
      "command": "npx",
      "args": ["-y", "@modelcontextprotocol/server-filesystem", "."]
    }
  }
}

配置大模型调用工具

kotlin 复制代码
package org.example.mcp.config;

import jakarta.annotation.Resource;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.mcp.SyncMcpToolCallbackProvider;
import org.springframework.ai.ollama.OllamaChatModel;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

@Component
public class AiConfig {

    @Resource
    private OllamaChatModel ollamaChatModel;

    @Resource
    private SyncMcpToolCallbackProvider toolCallbackProvider;

    @Bean
    public ChatClient chatClient() {
        return ChatClient.builder(ollamaChatModel).defaultTools(toolCallbackProvider.getToolCallbacks()).build();
    }

}

提供外部接口

kotlin 复制代码
package org.example.mcp.contoller;

import jakarta.annotation.Resource;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ChatController {

    @Resource
    private ChatClient chatClient;

    @GetMapping("/chat")
    public String call(@RequestParam String input) {
        return chatClient.prompt(input).call().content();
    }
}

测试效果

完整代码

github.com/JordyWang/S...

参考资料

modelcontextprotocol.io/quickstart/...

juejin.cn/post/747422...

相关推荐
tiger从容淡定是人生2 天前
AI替代软件战略(一):从 CCleaner 到 MCP 架构重构 —— TigerCleaner 的工程实践
人工智能·重构·架构·c#·mcp
@SmartSi2 天前
Spring AI 实战:如何使用 MCP Server 搭建 MCP 天气查询服务
spring ai·mcp
qcx232 天前
拆解 Warp AI Agent(五):跨生态联邦——10 种 Skill + MCP + 多 Harness 互操作设计
人工智能·rust·ai agent·skill·warp·mcp·harness
无糖可乐没有灵魂2 天前
AI Agent结构图例和工作流程描述
ai·llm·prompt·agent·mcp·skills
Joseph Cooper2 天前
AI Agent 落地入门:从模型、工具到 Skills 与 MCP 的分工
人工智能·ai·agent·claude·skill·mcp
@SmartSi2 天前
Spring AI 实战:如何使用 MCP Client 接入 MCP 天气查询服务
spring ai·mcp
少许极端3 天前
AI修炼记2-MCP
人工智能·ai·mcp
Zhencode3 天前
Python创建MCP服务
python·mcp