From Tool Calling to MCP: Building a Natural Language Search with Spring AI and MCP Server
Summary
This article details refactoring a natural language order search feature from a tightly coupled Spring AI tool-calling implementation to one utilizing the Model Context Protocol (MCP). MCP, an open standard by Anthropic, enables AI models to communicate with external tools and data sources via a universal interface. The refactored architecture separates the AI application (MCP client) from the data access layer (MCP server), allowing the `search_customer_orders` tool to be exposed as a standalone, independently deployable service. The MCP server, built with Spring Boot and `spring-ai-starter-mcp-server-webmvc`, registers the `CustomerOrderTool` as a `ToolCallbackProvider`. The AI application, using `spring-ai-starter-mcp-client` and `spring-ai-starter-model-openai`, connects to the MCP server via SSE, discovers its capabilities, and invokes tools through a `ChatClient` configured with `ToolCallbackProvider`.
Key takeaway
For AI Architects designing scalable, multi-application AI systems, adopting the Model Context Protocol (MCP) is crucial. This approach allows you to decouple AI applications from specific tool implementations, fostering reusability and independent versioning of business logic. Consider refactoring single-application tool-calling patterns to MCP servers when your capabilities need to be shared or managed with distinct release cycles, enhancing maintainability and platform growth.
Key insights
MCP standardizes AI model communication with external tools, enabling reusability and independent deployment.
Principles
- Separate AI orchestration from business logic.
- Standardize tool interfaces for broad compatibility.
Method
Expose data access capabilities as an MCP server using `spring-ai-starter-mcp-server-webmvc`, then configure an AI application as an MCP client with `spring-ai-starter-mcp-client` to discover and invoke these tools.
In practice
- Use MCP for shared AI capabilities.
- Deploy tools independently from AI applications.
- Centralize observability for tool invocations.
Topics
- Model Context Protocol
- Spring AI
- Tool Calling
- Natural Language Search
- MCP Server
Code references
Best for: AI Engineer, AI Architect, Software Engineer
Related on AIssential
Editorial summary, takeaway, and curation by AIssential. Original article published by NLP on Medium.