Exploring Model Context Protocol (MCP) With Spring AI
Build interoperable AI tools and services using Anthropic's open standard for connecting LLMs to external data and capabilities
1What is the Model Context Protocol?
The Model Context Protocol (MCP) is an open standard developed by Anthropic that provides a universal way to connect AI models to external data sources, tools, and services. Think of it as a "USB-C for AI" — a standardized interface that works across different AI applications.
Standardized
One protocol works across all MCP-compatible clients and servers
Client-Server
MCP Clients connect to MCP Servers that expose tools and resources
Extensible
Add new capabilities without modifying the core AI application
MCP vs Direct Tool Calling
With direct tools, each application must implement its own integrations. With MCP, you build an integration once and use it everywhere — in Claude Desktop, your Spring AI app, or any MCP-compatible client.
2MCP Architecture
MCP Communication Flow
AI Application
(Host)
MCP Client
(Spring AI)
MCP Server
(Tools/Resources)
Key Concepts
MCP Server
Exposes tools (functions the AI can call), resources (data it can read), and prompts (templates for interactions).
MCP Client
Connects to MCP servers, discovers available capabilities, and invokes them on behalf of the AI model.
Transport Layer
MCP supports multiple transports: stdio (standard I/O for local processes) and SSE (Server-Sent Events over HTTP).
3Using MCP Client in Spring AI
Step 1: Add Dependencies
<dependency><groupId>org.springframework.ai</groupId><artifactId>spring-ai-mcp-client-spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.ai</groupId><artifactId>spring-ai-openai-spring-boot-starter</artifactId></dependency>Step 2: Configure MCP Servers
Configure MCP servers in application.yml:
spring:ai:mcp:client:stdio:# File system server - provides file operationsfilesystem:command: npx
args:-"-y"-"@modelcontextprotocol/server-filesystem"-"/path/to/allowed/directory"# Brave search server - web search capabilitiesbrave-search:command: npx
args:-"-y"-"@anthropic/mcp-server-brave-search"env:BRAVE_API_KEY: ${BRAVE_API_KEY}Step 3: Use MCP Tools with ChatClient
importorg.springframework.ai.chat.client.ChatClient;importorg.springframework.ai.mcp.SyncMcpToolCallbackProvider;importorg.springframework.stereotype.Service;@ServicepublicclassMCPAgentService{privatefinalChatClient chatClient;publicMCPAgentService(ChatClient.Builder chatClientBuilder,SyncMcpToolCallbackProvider mcpToolProvider){// Register all MCP tools with the ChatClientthis.chatClient = chatClientBuilder
.defaultSystem("""
You are a helpful assistant with access to file system
operations and web search capabilities.
Use the available tools to help users with their requests.
""").defaultTools(mcpToolProvider)// All MCP server tools available.build();}publicStringprocess(String userMessage){return chatClient.prompt().user(userMessage).call().content();}}Auto-Discovery
Spring AI automatically discovers all tools exposed by configured MCP servers and makes them available to your ChatClient.
4Building Your Own MCP Server
Spring AI also lets you create MCP servers to expose your Spring services as tools:
Step 1: Add Server Dependency
<dependency><groupId>org.springframework.ai</groupId><artifactId>spring-ai-mcp-server-spring-boot-starter</artifactId></dependency>Step 2: Define Tools as Spring Beans
importorg.springframework.ai.tool.annotation.Tool;importorg.springframework.ai.tool.annotation.ToolParam;importorg.springframework.stereotype.Component;@ComponentpublicclassWeatherService{@Tool(description ="Get current weather for a city")publicWeatherResponsegetCurrentWeather(@ToolParam(description ="City name")String city,@ToolParam(description ="Temperature unit: celsius or fahrenheit")String unit){// Implement weather API callreturnnewWeatherResponse(city,22.5, unit,"Sunny");}@Tool(description ="Get 5-day weather forecast")publicList<ForecastDay>getWeatherForecast(@ToolParam(description ="City name")String city){// Return forecast datareturnList.of(newForecastDay("Monday",20,"Cloudy"),newForecastDay("Tuesday",23,"Sunny"));}}recordWeatherResponse(String city,double temperature,String unit,String condition){}recordForecastDay(String day,int tempHigh,String condition){}Step 3: Configure Server Transport
spring:ai:mcp:server:# Use Server-Sent Events for HTTP-based communicationtransport: sse
sse-endpoint: /mcp/sse
# Or use stdio for local process communication# transport: stdioStep 4: Run and Connect
Your MCP server is now available at http://localhost:8080/mcp/sse and can be connected to by any MCP client, including Claude Desktop or another Spring AI application.
5Best Practices & Use Cases
Common MCP Server Types
Database Access
Query PostgreSQL, MySQL, or MongoDB and let AI analyze results
API Integrations
Wrap REST APIs (Slack, GitHub, Jira) as MCP tools
Code Execution
Run Python scripts, execute shell commands safely
Business Logic
Expose domain-specific operations as AI-callable tools
Security Considerations
Pre-built MCP Servers
Check out github.com/modelcontextprotocol/servers for ready-to-use servers for GitHub, Slack, PostgreSQL, Puppeteer, and more.
What You've Learned
MCP Fundamentals
Standardized AI-tool connectivity
Architecture
Clients, servers, and transports
MCP Client
Connect to external MCP servers
MCP Server
Build your own tool servers
Use Cases
Database, APIs, and business logic
Security
Authentication and validation