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

This article demonstrates how to convert a traditional Spring Boot book‑management service into an AI‑enabled MCP server, covering dependency setup, entity definition, tool annotations, MCP configuration, chat client integration, data initialization, and testing, with complete code examples.

Top Architect
Top Architect
Top Architect
Integrating Spring Boot Services with Model Context Protocol (MCP) for AI‑Driven Book Management

This article introduces Model Context Protocol (MCP) as a universal adapter that lets large language models (LLMs) interact with various services using a single language, simplifying AI integration for backend systems.

Understanding MCP – MCP acts like a translation layer similar to gRPC, allowing AI models to call services without learning each service’s specific API.

Transforming a Spring Boot Service – The tutorial starts with a simple Book entity and a BookService interface for querying books by title, author, or category.

1. Import Dependencies

<!-- Spring AI core dependency -->
<dependency>
  <groupId>org.springframework.ai</groupId>
  <artifactId>spring-ai-core</artifactId>
</dependency>

<!-- Anthropic model support -->
<dependency>
  <groupId>org.springframework.ai</groupId>
  <artifactId>spring-ai-anthropic-spring-boot-starter</artifactId>
</dependency>

<!-- MCP server support (WebMVC) -->
<dependency>
  <groupId>org.springframework.ai</groupId>
  <artifactId>spring-ai-mcp-server-webmvc-spring-boot-starter</artifactId>
</dependency>

Because these are preview versions, additional Maven repositories must be added:

<repositories>
  <repository>
    <id>spring-milestones</id>
    <name>Spring Milestones</name>
    <url>https://repo.spring.io/milestone</url>
    <snapshots><enabled>false</enabled></snapshots>
  </repository>
  <repository>
    <id>spring-snapshots</id>
    <name>Spring Snapshots</name>
    <url>https://repo.spring.io/snapshot</url>
    <snapshots><enabled>true</enabled></snapshots>
    <releases><enabled>false</enabled></releases>
  </repository>
</repositories>

2. Configuration – Add AI key and enable MCP in application.yml (or application.properties).

spring.ai.anthropic.api-key=YOUR_API_KEY
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

3. Annotate Service Methods – Use @Tool and @ToolParam to expose methods to MCP.

@Service
@RequiredArgsConstructor
public class BookServiceImpl implements BookService {
    @Resource
    private BookRepository bookRepository;

    @Override
    @Tool(name = "findBooksByTitle", description = "Search books by title keyword")
    public List<Book> findBooksByTitle(@ToolParam(description = "Title keyword") String title) {
        return bookRepository.findByTitleContaining(title);
    }

    @Override
    @Tool(name = "findBooksByAuthor", description = "Find books by exact author name")
    public List<Book> findBooksByAuthor(@ToolParam(description = "Author name") String author) {
        return bookRepository.findByAuthor(author);
    }

    @Override
    @Tool(name = "findBooksByCategory", description = "Find books by category")
    public List<Book> findBooksByCategory(@ToolParam(description = "Category") String category) {
        return bookRepository.findByCategory(category);
    }
}

4. Register Tools with MCP Server

@Configuration
public class McpServerConfig {
    @Bean
    public ToolCallbackProvider bookToolCallbackProvider(BookService bookService) {
        return MethodToolCallbackProvider.builder()
                .toolObjects(bookService)
                .build();
    }
}

5. Chat Client Configuration – Set a system prompt and register the tool callbacks.

@Configuration
public class ChatClientConfig {
    @Autowired
    private ToolCallbackProvider toolCallbackProvider;

    @Bean
    public ChatClient chatClient(ChatClient.Builder builder) {
        return builder
                .defaultSystem("You are a book‑management assistant. Use the provided tools to query books.")
                .defaultTools(toolCallbackProvider)
                .build();
    }
}

Alternatively, expose the query methods as Function<String, List<Book>> beans and register them by name.

@Service
public class BookQueryService {
    @Resource
    private BookService bookService;

    @Bean
    public Function<String, List<Book>> findBooksByTitle() {
        return title -> bookService.findBooksByTitle(title);
    }
    // similar beans for author and category
}

6. Controller and Data Initialization – A REST controller forwards chat requests to the AI client, and a CommandLineRunner loads sample book data at startup.

@RestController
@RequestMapping("/api/chat")
public class ChatController {
    @Resource
    private ChatClient chatClient;

    @PostMapping
    public ResponseEntity<ChatResponse> chat(@RequestBody ChatRequest request) {
        String content = chatClient.prompt()
                .user(request.getMessage())
                .call()
                .content();
        return ResponseEntity.ok(new ChatResponse(content));
    }
}

Running the application and sending a natural‑language query such as “Find all books published in 2023” triggers the LLM to invoke the appropriate MCP‑exposed tool, returning the matching records from the database.

Conclusion – By integrating Spring Boot with MCP, developers can turn ordinary CRUD services into conversational AI assistants with minimal code changes, paving the way for “dialog‑as‑a‑service” architectures.

JavaMCPbackend developmentSpring BootAI integrationChatClient
Top Architect
Written by

Top Architect

Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.

0 followers
Reader feedback

How this landed with the community

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.