在微服务架构的海洋中,MCP(Module Communication Protocol)就像一艘智能帆船,它让不同 AI 模块的通信变得优雅而高效。本文将带你构建一艘属于自己的 AI 智能帆船——自定义 Spring AI MCP Server。通过模块化设计模式、动态路由策略和AI 模型热插拔三大核心思想,我们将创建一个可扩展、可演进的 AI 服务基础设施。
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.aigroupId>
<artifactId>spring-ai-mcp-server-webmvc-spring-boot-starterartifactId>
<version>1.0.0-M6version>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starterartifactId>
dependency>
dependencies>
Reactive RouterFunction
实现的智能路由@McpService(name = "weather-service", version = "1.0")
public interface WeatherService {
@McpOperation(description = "获取城市天气", inputType = String.class)
String getWeather(String city);
}
@Service
public class WeatherServiceImpl implements WeatherService {
private final AiModelClient aiModelClient;
public WeatherServiceImpl(AiModelClient aiModelClient) {
this.aiModelClient = aiModelClient;
}
@Override
public String getWeather(String city) {
// 动态选择模型:Qwen vs Llama
String model = ModelSelector.select(city);
return aiModelClient.invoke(model,
Map.of("city", city, "temperature_unit", "Celsius"));
}
}
@Component
public class ModelSelector {
@Value("${ai.models.weather}")
private List<String> weatherModels;
public String select(String city) {
// 基于地理位置选择最佳模型
if (city.startsWith("Shanghai")) {
return "qwen-max";
} else {
return "llama-3";
}
}
}
@Configuration
public class McpRouterConfig {
@Bean
public RouterFunction<ServerResponse> mcpRoutes(WeatherService weatherService) {
return RouterFunctions.route(
RequestPredicates.POST("/mcp/weather")
.and(RequestPredicates.contentType(MediaType.APPLICATION_JSON)),
request -> ServerResponse.ok().body(
ValueOps.of(weatherService.getWeather(request.body())))
);
}
}
@Component
public class RequestContextFilter implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) {
try {
// 提取上下文信息
String tenantId = request.getParameter("tenant");
// 设置线程局部变量
ContextManager.setCurrentContext(Context.builder()
.tenantId(tenantId)
.modelVersion("v1.2")
.build());
chain.doFilter(request, response);
} finally {
ContextManager.clearCurrentContext();
}
}
}
management:
metrics:
tags:
application: mcp-server
export:
prometheus:
enabled: true
@Component
public class AiCallMetrics {
private final Counter aiCalls = Metrics.counter("ai.calls.total");
public void recordCall(String model, Duration duration) {
aiCalls
.tag("model", model)
.increment();
Metrics.timer("ai.call.duration")
.tag("model", model)
.record(duration);
}
}
FROM openjdk:17-jdk-slim
VOLUME /tmp
ARG JAR_FILE=target/mcp-server-0.0.1-SNAPSHOT.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java", "-jar", "-Dspring.profiles.active=prod", "app.jar"]
apiVersion: apps/v1
kind: Deployment
metadata:
name: mcp-server
spec:
replicas: 3
selector:
matchLabels:
app: mcp-server
template:
metadata:
labels:
app: mcp-server
spec:
containers:
- name: mcp-server
image: your-registry/mcp-server:latest
ports:
- containerPort: 8080
envFrom:
- configMapRef:
name: mcp-config
通过本文的实践,你已经掌握了构建自定义 Spring AI MCP Server 的核心能力。这就像获得了一套 AI 乐高积木——你可以:
现在,是时候让你的创造力在 AI 世界中自由翱翔了!
GitHub 示例项目:spring-ai-mcp-demo(包含完整代码和 Docker Compose 部署文件)
官方文档参考:Spring AI MCP 官方文档(包含最新 API 和配置说明)