MCP Server & Client in Spring AI: Stop Coupling Tools to Your AI Host
This article discusses a solution to the problem of tightly coupling AI tools to the AI service in a Spring Boot application. It introduces the Model Context Protocol (MCP) to decouple the tools from the AI service, enabling dynamic tool discovery and independent deployments.
Why it matters
This approach helps address common issues with tightly coupled AI tools, such as deployment coupling, lack of sharing, and static tool inventory, making it easier to manage and evolve AI-powered applications.
Key Points
- 1Tight coupling of AI tools to the AI service leads to issues like deployment coupling, lack of sharing, and static tool inventory
- 2The MCP solution separates the AI service and the tool server into two independently deployable Spring Boot apps
- 3The AI service uses the MCP client to dynamically discover tools exposed by the MCP tool server, without requiring a restart
- 4Tools are defined using the @McpTool annotation, allowing easy addition and versioning of tools
Details
The article describes a two-service setup where the MCP Tool Server owns the tool implementations and exposes them via the @McpTool annotation over Streamable HTTP. The AI Chat Service, which is the user-facing REST API, uses the SyncMcpToolCallbackProvider to auto-discover the tools exposed by the MCP Tool Server on each request. This allows adding new tools without redeploying the AI service. The key insight is that the ToolCallbackProvider refetches the tool list from the server on each getToolCallbacks() call, enabling dynamic tool discovery at runtime.
No comments yet
Be the first to comment