Sponsored by Deepsite.site

Spring AI integration with MCP (Model Context Protocol)

Created By
kuldeepsingh997 months ago
Build your own mcp server with spring ai
Content

Spring AI integration with MCP (Model Context Protocol)

The Model Context Protocol (MCP) is a standardized protocol that enables AI models to interact with external tools and resources in a structured way. It supports multiple transport mechanisms to provide flexibility across different environments.

The MCP Java SDK provides a Java implementation of the Model Context Protocol, enabling standardized interaction with AI models and tools through both synchronous and asynchronous communication patterns.

Spring AI MCP extends the MCP Java SDK with Spring Boot integration, providing both client and server starters. Bootstrap your AI applications with MCP support using Spring Initializer.

MCP Java SDK Architecture

The Java MCP implementation follows a three-layer architecture

  • Client/Server Layer: The McpClient handles client-side operations while the McpServer manages server-side protocol operations. Both utilize McpSession for communication management.

  • Session Layer (McpSession): Manages communication patterns and state through the DefaultMcpSession implementation.

  • Transport Layer (McpTransport): Handles JSON-RPC message serialization and deserialization with support for multiple transport implementations.

MCP Client

The MCP Client is a key component in the Model Context Protocol (MCP) architecture, responsible for establishing and managing connections with MCP servers.

MCP Client

The MCP Server is a foundational component in the Model Context Protocol (MCP) architecture that provides tools, resources, and capabilities to clients.

What is MCP ?

MCP follows a client-server architecture that revolves around several key components:

MCP Host: is our main application that integrates with an LLM and requires it to connect with external data sources

MCP Clients: are components that establish and maintain 1:1 connections with MCP servers

MCP Servers: are components that integrate with external data sources and expose functionalities to interact with them

Tools: refer to the executable functions/methods that the MCP servers expose for clients to invoke Additionally, to handle communication between clients and servers, MCP provides two transport channels.

To enable communication through standard input and output streams with local processes and command-line tools, it provides the Standard Input/Output (stdio) transport type. Alternatively, for HTTP-based communication between clients and servers, it provides the Server-Sent Events (SSE) transport type.

What is done in the example ?

In this turorial MCP Server connects to Postgresql DB and its fetches data, we have also exposed two Tools in MCP Server. MCP Client connects to MCP Server and based on the Prompt LLM decides which Tool to connect.

Step 1 Create Postgresql DB

Run docker-compose.yml file and run the table.sql to create table.

Step 2 Run ollama locally

Download ollama and run any model as per you machine capacity

Step 3 Creating MCP Server

Download the mcp-server project and run it.

Important classes to note - SellerAccountTools

@Service
public class SellerAccountTools {
    private final SellerAccountRepository sellerAccountRepository;

    public SellerAccountTools(SellerAccountRepository sellerAccountRepository) {
        this.sellerAccountRepository = sellerAccountRepository;
    }

    @Tool(name = "Search Seller account by name", description = "Find all Seller Accounts by name")
    public String getAccountByName(
            @ToolParam(description = "Seller Account Name") String name) {
        List<SellerAccount> accountList = sellerAccountRepository.findByName(name);
        StringBuilder result = new StringBuilder();
        for (SellerAccount account : accountList) {
            result.append(account.toString()).append("\n");
        }
        return result.toString();
    }

    @Tool(description = "Find all Seller Accounts by owner")
    public String getAccountByOwner(
            @ToolParam(description = "Seller Account owner") String owner) {
        List<SellerAccount> accountList = sellerAccountRepository.findByOwner(owner);
        StringBuilder result = new StringBuilder();
        for (SellerAccount account : accountList) {
            result.append(account.toString()).append("\n");
        }
        return result.toString();
    }
}

Explanation

  • Tools allows server to expose tools that can be invoked by LLM, for this example i have created two tools which will search from account table by Name and by Owner.
  • Based on the prompt AI LLM will automatocally pick the correct method but we need to write name and descrition properly

Step 4 Creating MCP Client

Download the mcp-client project and run.

Step 5 Testing

DB Table Data

image

Testing with Promts

image

Here you can observe that based on the prompt LLM is automatically selecting the right tool and returning the results and it also format it.

Recommend Servers
TraeBuild with Free GPT-4.1 & Claude 3.7. Fully MCP-Ready.
TimeA Model Context Protocol server that provides time and timezone conversion capabilities. This server enables LLMs to get current time information and perform timezone conversions using IANA timezone names, with automatic system timezone detection.
AiimagemultistyleA Model Context Protocol (MCP) server for image generation and manipulation using fal.ai's Stable Diffusion model.
WindsurfThe new purpose-built IDE to harness magic
Context7Context7 MCP Server -- Up-to-date code documentation for LLMs and AI code editors
EdgeOne Pages MCPAn MCP service designed for deploying HTML content to EdgeOne Pages and obtaining an accessible public URL.
MCP AdvisorMCP Advisor & Installation - Use the right MCP server for your needs
Serper MCP ServerA Serper MCP Server
CursorThe AI Code Editor
Playwright McpPlaywright MCP server
Zhipu Web SearchZhipu Web Search MCP Server is a search engine specifically designed for large models. It integrates four search engines, allowing users to flexibly compare and switch between them. Building upon the web crawling and ranking capabilities of traditional search engines, it enhances intent recognition capabilities, returning results more suitable for large model processing (such as webpage titles, URLs, summaries, site names, site icons, etc.). This helps AI applications achieve "dynamic knowledge acquisition" and "precise scenario adaptation" capabilities.
Amap Maps高德地图官方 MCP Server
Visual Studio Code - Open Source ("Code - OSS")Visual Studio Code
BlenderBlenderMCP connects Blender to Claude AI through the Model Context Protocol (MCP), allowing Claude to directly interact with and control Blender. This integration enables prompt assisted 3D modeling, scene creation, and manipulation.
Jina AI MCP ToolsA Model Context Protocol (MCP) server that integrates with Jina AI Search Foundation APIs.
DeepChatYour AI Partner on Desktop
MiniMax MCPOfficial MiniMax Model Context Protocol (MCP) server that enables interaction with powerful Text to Speech, image generation and video generation APIs.
Baidu Map百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
ChatWiseThe second fastest AI chatbot™
Tavily Mcp
Howtocook Mcp基于Anduin2017 / HowToCook (程序员在家做饭指南)的mcp server,帮你推荐菜谱、规划膳食,解决“今天吃什么“的世纪难题; Based on Anduin2017/HowToCook (Programmer's Guide to Cooking at Home), MCP Server helps you recommend recipes, plan meals, and solve the century old problem of "what to eat today"