什么是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服务配置文件
- 如果是使用NVM版本管理工具需要指定npx的管理工具的绝对路径(Windows环境下,调用的是npx.cmd)
- 在执行这段命令的时候,会解析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();
}
}