Model Context Protocol 是Anthropic 于2024年11月重磅开源的「模型上下文协议」MCP。
- 其是一种开放的通信协议,是人工智能领域的 USB 接口。
在大模型和其他数据源(数据、工具、开发环境等)之间建立了双向、并且更加安全的连接。
MCP 将LLM的数据孤岛被彻底打破,LLM应用和外部数据源、工具都将无缝集成。
目标是实现LLM应用程序与外部数据源和工具之间的无缝集成。
官方文档:https://modelcontextprotocol.io/introduction
目前有很多现成的 MCP Server:
Spring AI 如何集成 MCP 协议
MCP Client Boot Starters:
对于 MCP Client,Spring AI 提供了如下两个 Starter 集成 MCP Client。
spring-ai-mcp-client-spring-boot-starter:实现基于 STDIO 和 HTTP 的 SSE 传输协议的 MCP Client。
spring-ai-mcp-client-webflux-spring-boot-starter:实现基于 WebFlux 的 SSE 传输协议的 MCP Client。
MCP Server Boot Starters:
对于 MCP Server, Spring AI 提供了如下三个 Starter 集成 MCP Server。
spring-ai-mcp-server-spring-boot-starter: 实现支持 STDIO 传输协议的 MCP Server
spring-ai-mcp-server-webmvc-spring-boot-starter: 实现基于 WebMCV 的 SSE 传输协议的 MCP Server
spring-ai-mcp-server-webflux-spring-boot-starter: 实现基于 WebFlux 的 SSE 传输协议的 MCP Server
根据业务场景,可以选择其中的 MCP Server Starter 来实现一个 MCP Server。
Spring MCP Client 实现示例
<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-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>
配置文件:
spring.ai.ollama.base-url=http://localhost:11434
spring.ai.ollama.chat.model=qwen2.5:latest
# MCP Client Configuration
spring.ai.mcp.client.enabled=true
spring.ai.mcp.client.name=mcp-client
spring.ai.mcp.client.version=1.0.0
spring.ai.mcp.client.type=SYNC
spring.ai.mcp.client.request-timeout=30s
spring.ai.mcp.client.stdio.servers-configuration=classpath:/mcp-servers-config.json
mcp-servers-config.json:
对于
args
的第三个参数,可以指定你想要访问的文件路径,可以是多个。比如:
"args": [ "-y", "@modelcontextprotocol/server-filesystem", ".", "/Users" ]
{
"mcpServers": {
"filesystem": {
"command": "npx",
"args": [
"-y",
"@modelcontextprotocol/server-filesystem",
"."
]
}
}
}
代码实现:
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.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@RestController
public static class ChatController {
@Resource
private OllamaChatModel ollamaChatModel;
@Resource
private SyncMcpToolCallbackProvider toolCallbackProvider;
@GetMapping("/chat")
public String call(@RequestParam String input) {
ChatClient chatClient = ChatClient.builder(ollamaChatModel)
.defaultTools(toolCallbackProvider.getToolCallbacks())
.build();
return chatClient.prompt(input).call().content();
}
}
}