Back to Courses
    MCP Deep Dive

    Journey of MCP with Spring AI

    The Model Context Protocol: Building the "USB-C for AI" with Java and Spring

    The Agentic Era Challenge

    Why We Need a Universal Protocol

    We're firmly in the "agentic era" of AI, where the goal isn't just to make Large Language Models (LLMs) smarter, but to make them do things. Yet, there's a fundamental challenge: connecting our brilliant but isolated LLMs to the real world—APIs, databases, file systems—often results in a chaotic mess of custom code.

    It's the classic M×N integration nightmare, where connecting M models to N tools becomes an exponentially complex and brittle web of one-off solutions.

    What if there was a "USB-C for AI"?

    A single, standardized way to plug any model into any tool. That's exactly the promise of the Model Context Protocol (MCP).

    Introduced by Anthropic in November 2024, MCP is the open standard designed to bring order to this chaos. And for the massive community of Java developers, Spring AI has emerged as the key to unlocking this power, providing a robust, enterprise-grade framework to build these crucial bridges.

    Part 1

    What is the Model Context Protocol?

    Understanding the Architecture and Core Concepts

    1.1The Core Problem Solved

    The "USB-C for AI" analogy perfectly captures the value proposition. Before MCP, every time you wanted your AI to interact with a new service (like a weather API or a GitHub repo), you had to write custom integration code. This created tight coupling between your AI logic and specific tool APIs.

    MCP solves this by creating a standardized protocol for communication. The AI model (the "Client") and the external tool (the "Server") both speak the language of MCP. This decouples them, enabling a "plug-and-play" ecosystem of interoperable AI tools and services.

    1.2The MCP Architecture

    The protocol follows a client-server model with roles specifically tailored for AI applications:

    MCP Host

    The main AI application (Claude Desktop, custom chatbot, IDE). Orchestrates the whole process.

    MCP Client

    Lives inside the Host. Communicates with specific MCP Servers. Multiple clients can exist.

    MCP Server

    Exposes specific capabilities called "tools" to the outside world (e.g., GitHub, databases).

    Two Developer Roles in MCP

    RoleFocusExample Task
    AI Application DeveloperOrchestrating clients, integrating AI models, building UXBuilding a chatbot using database and web search servers
    MCP Server DeveloperWrapping domain services as MCP ServersCreating a PostgreSQL server exposing SQL execution

    1.3Core MCP Features

    MCP defines a rich, bidirectional set of features enabling sophisticated interactions:

    CategoryFeatureDescription
    Server FeaturesToolsCallable functions (e.g., get_weather)
    ResourcesData sources (files, database records)
    PromptsPre-defined prompt templates
    Client FeaturesSamplingAsk client's LLM to generate content
    ElicitationRequest additional user information
    Shared FeaturesProgressTrack long-running operations
    LoggingDebugging and operational insight
    CancellationStop in-progress operations
    Part 2

    Spring AI: MCP for the Java Ecosystem

    Enterprise-grade MCP development with Spring Boot

    2.1Why Spring AI for MCP?

    For Java developers, Spring AI is the most direct and robust path to building MCP-enabled applications. It abstracts away low-level complexity and integrates seamlessly into the familiar Spring Boot programming model.

    Auto-configuration

    Spring Boot handles boilerplate setup

    Dependency Management

    Consistent version management

    Spring Security

    Enterprise-grade security for servers

    Full Spring Ecosystem

    Spring Data, Cloud, and more

    2.2Spring AI MCP Starters

    True to Spring philosophy, getting started is as simple as adding a dependency:

    Maven Artifact IDPurpose
    spring-ai-starter-mcp-clientCore starter for MCP client applications
    spring-ai-starter-mcp-serverCore starter for MCP servers
    spring-ai-starter-mcp-server-webmvcHTTP/SSE transport with Spring MVC
    spring-ai-starter-mcp-server-webfluxHTTP/SSE transport with Spring WebFlux

    2.3The Magic of Annotations

    Instead of implementing complex interfaces, expose any method as an MCP tool with a simple annotation:

    Java Example
    @ServicepublicclassWeatherService{@Tool(description ="Find and retrieve the station_id for weather stations")publicStringgetStationIds(@ToolParam(description ="Station is typically just city")String station
    ){// Business logic to find station IDreturn stationId;}@Tool(description ="Retrieve current and forecast weather information")publicStringgetStationOverview(@ToolParam(description ="List of station ids")List<String> stationIds
    ){// Business logic to get weatherreturn stationOverview;}}

    How it works:

    With @Tool and @ToolParam, the framework handles tool discovery, serialization, and invocation. The LLM sees a tool with clear name, description, and typed parameters—all inferred from your Java code.

    Part 3

    Building Your First MCP Server

    Step-by-step tutorial for a Course Catalog server

    1Project Setup with Spring Initializr

    Head to start.spring.io and configure your project:

    Project

    Maven

    Language

    Java

    Spring Boot

    3.3.x or later

    Dependencies

    Model Context Protocol Server

    2Creating the Tool (CourseService)

    Create a service that exposes your tools:

    Java Example
    @ServicepublicclassCourseService{privateList<Course> courses =newArrayList<>();@Tool(name ="get_courses", description ="Get a list of available courses")publicList<Course>getCourses(){return courses;}publicrecordCourse(String title,String url){}@PostConstructprivatevoidinit(){
    courses.add(newCourse("Building Web Applications with Spring Boot","..."));
    courses.add(newCourse("Spring Boot Tutorial for Beginners","..."));}}

    3Registering Tools and Configuration

    Register the tools as a Bean in your main application class:

    Java Example
    @SpringBootApplicationpublicclassCoursesApplication{publicstaticvoidmain(String[] args){SpringApplication.run(CoursesApplication.class, args);}@BeanpublicList<ToolCallback>courseTools(CourseService courseService){returnList.of(ToolCallbacks.from(courseService));}}

    Application Properties:

    Properties Example
    spring.main.web-application-type=nonespring.ai.mcp.server.name=course-mcp-serverspring.ai.mcp.server.version=0.0.1spring.main.banner-mode=offlogging.pattern.console=

    4Build and Test with Claude Desktop

    Build the application and configure Claude Desktop to use your server:

    Bash Example
    mvn clean package

    Claude Desktop Configuration (JSON):

    Json Example
    {"course-mcp-server":{"command":"/path/to/your/java","args":["-jar","/path/to/your/courses.jar"]}}

    Test Your Server!

    After restarting Claude, ask: "What courses are available?" and watch it call your Java application to get the data!

    Part 4

    Production: Real-World Use Cases & Security

    Moving beyond demos to enterprise applications

    Top Use Cases for Spring AI MCP Servers

    Enterprise AI Integration

    Connect siloed data in CRMs, ERPs, and legacy systems. AI agents query NetSuite, Spring Batch jobs, and more through MCP.

    AI-Powered Data Analytics

    Business analysts ask questions in natural language, and AI uses MCP to generate reports from complex data sources.

    Intelligent Developer Tools

    Quick access to project info, build status, documentation through AI-powered MCP servers.

    Financial Services Automation

    Multi-agent systems access transaction data, run risk models, and flag suspicious activity in real-time.

    Security is Not an Afterthought

    Critical Warning:

    An MCP server is just like a REST API. If you're exposing tools or data you don't want public, you need proper security. Exposing an unauthenticated MCP server is asking for trouble.

    MCP Security Checklist

    Use OAuth 2.0 / OpenID Connect

    Integrate with enterprise identity providers (Okta, Azure AD)

    Implement Fine-Grained Access Control

    Control which users/clients can access which tools

    Validate All Inputs

    Never trust input from the client—sanitize and validate

    Use HTTPS for All Transports

    Ensure all communication is encrypted in transit

    Monitor and Log All Tool Calls

    Maintain audit trail for security and compliance

    Conclusion

    The Future is Agentic

    We've journeyed from the chaos of AI integration to the order brought by the Model Context Protocol. We saw how Spring AI makes this powerful standard accessible and enterprise-ready for Java developers.

    The combination of Spring's proven, enterprise-grade foundation and MCP's standardized protocol is a game-changer. It's not just about building cool demos; it's about building reliable, secure, and scalable AI applications that solve real business problems.

    Standardized Protocol

    MCP is the "USB-C for AI"

    Spring Integration

    Enterprise-ready with annotations

    Production Ready

    Security and scalability built-in

    💬 Comments & Discussion