Enterprise AI Agents with Spring AI, Alibaba & MCP: Why Java Is Core Infrastructure
The article walks through building a production‑grade AI Agent system using Spring AI, Alibaba's extensions and the Model Context Protocol (MCP), showing how Java's mature ecosystem—security, microservices, data governance, and workflow—makes it the backbone for next‑generation AI‑native applications.
Model Context Protocol (MCP)
MCP (Model Context Protocol) is a standard protocol that enables large‑model agents to communicate with external systems such as databases, files, HTTP APIs, and micro‑services. It is described as the "HTTP + USB" for AI agents because it provides a uniform, extensible interface for tool invocation.
Limitations of traditional function calling
Existing function‑calling approaches suffer from fragmented tool standards, loss of context, difficult tool management, poor scalability, and the absence of a unified runtime. These issues prevent agents from being reliably extended in enterprise environments.
Enterprise AI architecture transformation
Traditional stack: Frontend → Spring Boot → MySQL AI‑enabled stack:
AI Agent
↓
MCP Gateway
↓
Spring AI Runtime
↓
Enterprise MicroservicesThe microservice layer becomes an "AI operating system" that agents can orchestrate directly.
Why Java is a natural fit
Enterprise AI workloads require robust permission systems, workflow orchestration, audit logging, data governance, and distributed job scheduling—capabilities that have matured in the Java ecosystem (e.g., Flowable, Camunda, Temporal, XXL‑Job). Java therefore offers a low‑cost path for integrating AI agents into existing back‑ends.
Step‑by‑step construction of an AI Agent demo
Project creation : Initialise a Spring Boot project named spring-ai-agent-demo with JDK 17 and Spring Boot 3.3.x.
pom.xml : Include Spring Boot starter, spring-ai-bom, the MCP starter ( spring-ai-starter-mcp-server-webmvc), Redis, PostgreSQL, spring-ai-pgvector-store, and Lombok. Example snippet:
<properties>
<java.version>17</java.version>
<spring-boot.version>3.3.6</spring-boot.version>
<spring-ai.version>1.0.0</spring-ai.version>
</properties>
...
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-mcp-server-webmvc</artifactId>
</dependency>
...application.yml : Configure server port, PostgreSQL datasource, Redis, OpenAI (or DeepSeek) API key, and MCP server metadata.
server:
port: 8080
spring:
datasource:
url: jdbc:postgresql://localhost:5432/ai_demo
username: postgres
password: 123456
data:
redis:
host: localhost
port: 6379
ai:
openai:
api-key: ${OPENAI_API_KEY}
mcp:
server:
enabled: true
name: spring-ai-agent-demo
version: 1.0.0Entity definition :
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Order {
private Long id;
private String productName;
private BigDecimal amount;
private String status;
}Service layer exposing business logic:
@Service
public class OrderService {
public Order queryOrder(Long orderId) {
return new Order(orderId, "MacBook Pro", new BigDecimal("19999.00"), "PAID");
}
public String querySalesTrend() { return "最近7天销量下降30%"; }
public String queryRefundRate() { return "退款率上升15%"; }
public String queryInventory() { return "MacBook Pro 库存仅剩 12 台"; }
}Tool exposure : Annotate service methods with @Tool in a Spring bean so MCP can generate a schema.
@Service
@RequiredArgsConstructor
public class OrderTool {
private final OrderService orderService;
@Tool(description = "根据订单ID查询订单详情")
public Order queryOrder(Long orderId) { return orderService.queryOrder(orderId); }
@Tool(description = "查询最近7天销量趋势")
public String querySalesTrend() { return orderService.querySalesTrend(); }
@Tool(description = "查询最近7天退款率")
public String queryRefundRate() { return orderService.queryRefundRate(); }
@Tool(description = "查询当前库存情况")
public String queryInventory() { return orderService.queryInventory(); }
}Register tools with MCP : Provide a ToolCallbackProvider bean that scans the tool object.
@Configuration
public class McpToolConfig {
@Bean
public ToolCallbackProvider orderTools(OrderTool orderTool) {
return MethodToolCallbackProvider.builder()
.toolObjects(orderTool)
.build();
}
}Run the application :
mvn clean package -DskipTests
java -jar target/spring-ai-agent-demo-0.0.1-SNAPSHOT.jarThe service starts an MCP server automatically.
Claude Desktop integration : Add an entry to Claude’s claude_desktop_config.json pointing to the JAR command. After restart Claude discovers the four tools ( queryOrder, querySalesTrend, queryRefundRate, queryInventory) and can invoke them without the user explicitly naming them.
Multi‑tool calling scenario
When a user asks “Analyze recent sales decline”, Claude automatically calls querySalesTrend, queryRefundRate, and queryInventory, then synthesises a natural‑language summary such as:
最近销量下降主要原因:
1. 退款率上涨15%
2. 库存不足
3. 数码产品需求下降Stateful agents with Redis memory
A MemoryService pushes interaction strings into a Redis list, enabling the agent to retain context across sessions.
@Service
public class MemoryService {
@Autowired
private RedisTemplate<String, String> redisTemplate;
public void saveMemory(String userId, String content) {
redisTemplate.opsForList().leftPush(userId, content);
}
}Retrieval‑augmented generation (RAG) using pgvector
Install the vector extension in PostgreSQL and create a table for document embeddings:
CREATE EXTENSION vector;
CREATE TABLE document_embedding (
id BIGSERIAL PRIMARY KEY,
content TEXT,
embedding vector(1536)
);Perform similarity search with Spring AI’s vector store:
List<Document> documents =
vectorStore.similaritySearch(
SearchRequest.query("如何退款"));Security considerations for tool invocation
Apply Spring Security method‑level checks, e.g. @PreAuthorize("hasRole('ADMIN')"), on sensitive tools.
Log every tool call with an interceptor:
@Slf4j
@Component
public class ToolAuditInterceptor {
public void audit(String toolName) {
log.info("AI调用Tool: {}", toolName);
}
}Validate parameters and maintain a whitelist of allowed tools to mitigate prompt‑injection attacks.
AI workflow orchestration
Beyond single calls, agents can coordinate multiple tools using a workflow engine such as Temporal. The typical flow is:
Agent → Temporal Workflow → Multiple Tools → Result aggregation → LLM responseTemporal provides state persistence, retries, and long‑running task management, which aligns with Java’s mature workflow libraries (Flowable, Camunda, Activiti, Temporal, XXL‑Job).
Multi‑agent teams
Future architectures will compose specialized agents (e.g., CEO Agent, Operations Agent, Customer‑Service Agent, Finance Agent, Development Agent) that collaborate automatically, forming an autonomous agent ecosystem rather than a single monolithic bot.
Roadmap for Java developers
Master Spring AI, MCP, function calling, and prompt engineering.
Learn RAG techniques, vector databases, and embedding generation.
Explore multi‑agent coordination, AI security, and AI‑native system design.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
LuTiao Programming
LuTiao Programming is a friendly community offering free programming lessons. We inspire learners to explore new ideas and technologies and quickly acquire job-ready skills.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
