Backend Development 13 min read

Integrating Spring Boot with Model Context Protocol (MCP) for AI‑Powered Book Management

This article demonstrates how to transform a traditional Spring Boot book‑management service into an AI‑driven MCP service by importing Spring AI dependencies, configuring proxy and MCP server settings, annotating service methods with @Tool, registering them, and exposing a chat endpoint that lets large language models query and manipulate book data through natural language.

Architecture Digest
Architecture Digest
Architecture Digest
Integrating Spring Boot with Model Context Protocol (MCP) for AI‑Powered Book Management

With the rapid rise of large language models, users can now interact with software using natural language commands such as 帮我查找所有2023年出版的图书 or 创建一个新用户叫张三,邮箱是[email protected] , dramatically lowering the learning curve for new users.

The Model Context Protocol (MCP) works like a universal adapter, allowing an AI model to speak a single language while communicating with many heterogeneous services and databases.

To convert a Spring Boot book‑management service into an MCP service, follow these steps:

Import the required Maven dependencies: <dependency> <groupId>org.springframework.ai</groupId> <artifactId>spring-ai-core</artifactId> </dependency> <dependency> <groupId>org.springframework.ai</groupId> <artifactId>spring-ai-anthropic-spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.ai</groupId> <artifactId>spring-ai-mcp-server-webmvc-spring-boot-starter</artifactId> </dependency>

Add Spring milestone repositories to the pom.xml so the preview versions can be resolved.

Configure proxy settings for Anthropic API access in a ProxyConfig class.

Enable the MCP server and set its properties (name, version, type, SSE endpoint) in application.yml . spring.ai.mcp.server.enabled=true spring.ai.mcp.server.name=book-management-server spring.ai.mcp.server.version=1.0.0 spring.ai.mcp.server.type=SYNC spring.ai.mcp.server.sse-message-endpoint=/mcp/message

Annotate service methods with @Tool and @ToolParam to expose them as MCP tools, e.g.: @Tool(name = "findBooksByTitle", description = "根据书名模糊查询图书,支持部分标题匹配") public List<Book> findBooksByTitle(@ToolParam(description = "书名关键词") String title) { return bookRepository.findByTitleContaining(title); }

Register the tools in a configuration class using MethodToolCallbackProvider . @Bean public ToolCallbackProvider bookToolCallbackProvider(BookService bookService) { return MethodToolCallbackProvider.builder() .toolObjects(bookService) .build(); }

Configure a ChatClient with a default system prompt and the tool callbacks. builder.defaultSystem("你是一个图书管理助手,可以帮助用户查询图书信息。你可以根据书名模糊查询、根据作者查询和根据分类查询图书。") .defaultTools(toolCallbackProvider) .build();

Optionally expose the same methods as function beans and reference them by name in the chat client configuration.

Create a REST controller that forwards user messages to the ChatClient and returns the AI response.

Provide a CommandLineRunner data initializer to preload sample books into the database.

After deployment, the AI model can understand user queries, automatically invoke the matching @Tool methods, and return structured book information, turning a conventional CRUD API into an intelligent conversational interface.

BackendJavaAIMCPSpring BootChatbot
Architecture Digest
Written by

Architecture Digest

Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.