在 Spring AI 中,MCP(Model Calling Protocol)是一种用于模型调用的协议,允许你通过标准接口与不同的模型服务进行交互。Spring AI 提供了对 MCP 的支持,包括 spring-ai-starter-mcp-server-webflux 用于搭建 MCP Server,以及 spring-ai-starter-mcp-client 用于构建 MCP Client。


🧩 项目结构建议

mcp-demo/
├── mcp-server/              # MCP Server 模块
└── mcp-client/              # MCP Client 模块

✅ 一、MCP Server 端配置(基于 WebFlux)

1. 添加依赖 (pom.xml)

<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-starter-mcp-server-webflux</artifactId>
    <version>1.0.0-M8</version> <!-- 使用最新版本 -->
</dependency>

2. 创建一个服务类(使用 @Tool 注解)

import org.springframework.ai.tool.annotation.Tool;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Service
public class McpServerService {
    @Tool(description = "测试mcp")
    public String mcpServer1() {
        return "这是测试mcp-server-1";
    }

    @Tool(description = "我爱mcp")
    public String mcpServer2() {
        return "这是测试mcp-server-2";
    }

    @Tool(description = "获取当前时间")
    public String getDate() {
        return LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }
}

3. 创建一个配置类

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;

@Configuration
public class McpServerConfig {
    @Bean
    public ToolCallbackProvider toolCallbackProvider(McpServerService mcpServerService) {
        return MethodToolCallbackProvider
                .builder()
                .toolObjects(mcpServerService)
                .build();
    }
}

4. 启动类

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

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

4. 配置文件(application.properties)

spring.application.name=mcp-server
server.port=8090

✅ 二、MCP Client 端配置

1. 添加依赖 (pom.xml)

	<dependencies>
        <dependency>
            <groupId>org.springframework.ai</groupId>
            <artifactId>spring-ai-starter-model-ollama</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.ai</groupId>
            <artifactId>spring-ai-starter-mcp-client</artifactId>
            <version>1.0.0-M8</version> <!-- 使用最新版本 -->
        </dependency>

        <!--        必须加上-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

    </dependencies>

2. 配置文件(application.properties)

spring.application.name=mcp-client
server.port=8080
spring.ai.ollama.base-url=http://localhost:11434
spring.ai.ollama.chat.model=qwen3:8b
# mcp
spring.ai.mcp.client.name=mcp-client
# mcp-server
spring.ai.mcp.client.sse.connections.server1.url=http://localhost:8090
spring.ai.mcp.client.toolcallback.enabled=true

3. 服务类

@Service
public class ChatService {
    @Autowired
    private OllamaChatModel ollamaChatModel;
    @Autowired
    private ToolCallbackProvider toolCallbackProvider;

    public Flux<String> stream(ChatDTO chatDTO) {
        if (chatDTO.model() == null) {
            throw new IllegalArgumentException("model不能为空");
        }

        ChatOptions options = OllamaOptions.builder()
                .model(chatDTO.model())
                .build();

        Prompt prompt = Prompt.builder()
                .chatOptions(options)
                .content(chatDTO.message())
                .build();

        ChatClient chatClient = ChatClient.builder(ollamaChatModel)
                .defaultToolCallbacks(toolCallbackProvider)
                .build();

        return chatClient.prompt(prompt).stream().content();

    }
}

4. 控制器

@RestController
@RequestMapping("/chat")
public class ChatController {
    @Autowired
    private ChatService chatService;

    @RequestMapping(value = "/stream")
    public Flux<String> stream(@Valid @RequestBody ChatDTO chatDTO) {
        return chatService.stream(chatDTO);
    }
}

5. 启动类

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

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

🧪 测试流程

  1. 启动 mcp-server 应用(监听端口 8090)
  2. 启动 mcp-client 应用(默认端口 8080)
  3. 访问:
    http://localhost:8080/chat/stream
    
    {
     "model": "qwen3:0.6b",
     "message": "当前时间"
    }
    

🔍 总结

组件 功能
spring-ai-starter-mcp-server-webflux 提供 MCP Server 实现,支持注册工具并对外暴露 HTTP 接口
@Tool 注解 标记某个方法为可远程调用的工具
spring-ai-starter-mcp-client 客户端用来调用远程 MCP 工具

💡 补充说明

  • 当前 Spring AI 对 MCP 的支持仍在演进中,请参考官方文档获取最新 API。
  • 可以扩展支持更多类型参数(如 JSON 对象),只需确保序列化兼容。

Logo

欢迎加入DeepSeek 技术社区。在这里,你可以找到志同道合的朋友,共同探索AI技术的奥秘。

更多推荐