Model Context Protocol with Spring AI, Building MCP Clients and Servers in Java A developer demonstrated how Java developers can build Model Context Protocol (MCP) clients and servers using Spring AI, which provides Boot starters and APIs for both sides of the architecture. The writeup explains how MCP standardizes AI application communication with external tools, resources, and prompts, replacing bespoke per-service integrations with a common protocol layer. The author frames MCP as a protocol rather than a model, positioning it between the LLM and external systems. In the previous article, we explored how to build AI agents with Spring AI using: LLMs ↓ RAG ↓ Tool Calling ↓ Memory ↓ Agent Workflows Tool calling gives an AI application the ability to interact with external capabilities. But another problem appears as AI systems become larger. Imagine you have: Customer Service Agent ↓ Order APIs Payment APIs CRM APIs Knowledge Base Email Service And another application has: Sales Agent ↓ CRM Calendar Email Customer Database And another has: Developer Agent ↓ Git Repository Issue Tracker CI/CD Documentation If every AI application implements every integration differently, the architecture quickly becomes difficult to maintain. This is where Model Context Protocol MCP becomes interesting. MCP provides a standardized way for AI applications to interact with external tools and resources. Spring AI provides support for both building MCP servers and consuming MCP servers from Spring Boot applications. In this article, we'll build a mental model for MCP and explore how Java developers can use it with Spring AI. MCP stands for: Model Context Protocol At a high level, MCP standardizes how an AI application communicates with external capabilities such as: Tools Resources Prompts Instead of every AI application inventing its own integration mechanism: AI Application ↓ Custom Tool Integration ↓ CRM we can have: AI Application ↓ MCP Client ↓ MCP Protocol ↓ MCP Server ↓ CRM The MCP server exposes capabilities through a standardized interface. The AI application doesn't need to understand every internal implementation detail of the external system. Suppose you build an AI assistant that needs access to: GitHub Slack PostgreSQL Google Calendar Internal APIs File Systems Without a standard protocol, your application might contain: GitHub Integration Slack Integration PostgreSQL Integration Calendar Integration Internal API Integration Each integration may have its own: Authentication Tool Schema Request Format Response Format Connection Management Error Handling Now imagine another AI application needs the same capabilities. You may end up rebuilding many of the same integrations. MCP addresses this by creating a common protocol for AI applications and external servers. Conceptually: AI Application │ MCP Client │ MCP Protocol │ ┌─────────────────┼─────────────────┐ ↓ ↓ ↓ MCP Server MCP Server MCP Server ↓ ↓ ↓ CRM GitHub Database This is one of the main ideas behind MCP. This distinction is important. MCP is not: An AI model It is a protocol for connecting AI applications with capabilities. Think of the stack like this: LLM ↓ AI Application ↓ MCP Client ↓ MCP Protocol ↓ MCP Server ↓ Tools / Resources ↓ External System The LLM performs reasoning. The MCP layer provides standardized communication. The external system performs the actual operation. MCP introduces two important roles. The MCP client lives inside the AI application. Its responsibility is to connect to MCP servers and interact with the capabilities they expose. For example: Spring Boot AI Application ↓ MCP Client ↓ Weather MCP Server The client can discover and use the server's available capabilities. The MCP server exposes capabilities. Weather MCP Server Tools: getWeather getForecast Resources: weather://cities Prompts: weather-analysis The server is responsible for implementing those capabilities. Spring AI provides Boot starters and APIs for both sides of this architecture. A simplified architecture looks like: User ↓ Spring Boot ↓ ChatClient ↓ MCP Client ↓ MCP Protocol ↓ MCP Server ↓ Tool ↓ External API User: What's the weather in Paris? The AI application can discover a weather tool exposed by an MCP server. The flow becomes: User ↓ LLM ↓ MCP Tool ↓ Weather MCP Server ↓ Weather API ↓ Tool Result ↓ LLM ↓ Final Answer At this point, you might ask: "Isn't this just tool calling?" There is an important distinction. Traditional Spring AI tool calling can expose application methods directly: @Tool public String getWeather String city { return weatherService.getWeather city ; } Your application owns the tool. With MCP: AI Application ↓ MCP Client ↓ Remote MCP Server ↓ Tool The tool can live outside the application. This creates a cleaner separation between: AI Application and: Capability Provider Spring AI integrates MCP tools into its tool-calling architecture, allowing applications to consume tools exposed by MCP servers. One of the most important MCP capabilities is the tool . A tool represents an action that an AI application can invoke. getWeather createTicket searchCustomers getOrder sendEmail A weather server might expose: getTemperature city A CRM server might expose: findCustomer email createLead customer updateLead leadId A developer server might expose: searchRepository query getBuildStatus createIssue title The MCP client can discover these tools and make them available to the AI application. MCP is not limited to actions. It can also expose resources . A resource represents information that an MCP client can access. customer://123 order://ORD-10291 file://README.md database://schema Think of the distinction as: Tool = Do something Resource = Access something Tool: createTicket Resource: customer://123 A server can expose both. MCP also supports prompts. A server can provide reusable prompt templates for specific tasks. Prompt: analyze-customer Input: customerId Or: Prompt: summarize-order Input: orderId This allows prompt templates to become part of the server-provided capabilities rather than being hardcoded independently in every client. Spring AI's MCP support includes annotations for tools, resources, and prompts. Let's build a simple MCP server. Imagine a weather service. Our application already has: @Service public class WeatherService { public String getTemperature String city { return "22°C"; } } We can expose this capability through an MCP tool. With Spring AI's annotation-based MCP support: @Service public class WeatherTools { @McpTool description = "Get the current temperature for a city" public String getTemperature @McpToolParam description = "City name", required = true String city { return weatherService.getTemperature city ; } } The MCP annotation model allows Spring services to expose capabilities as MCP operations. For a Spring Boot application, Spring AI provides MCP server starters.