In Part 4 of this series, we added tool calling to our AI agent, allowing it to access real-time information like weather forecasts and current dates. However, we discovered another limitation: when asked to book flights or hotels, the agent couldn’t access those systems because we’d need to hardcode every API integration into our application.
Hardcoding API integrations creates maintenance challenges. Every new service requires code changes, recompilation, and redeployment. As your organization adds more systems (booking platforms, inventory systems, CRM tools), your AI agent becomes increasingly difficult to maintain and extend.
In this post, we’ll add Model Context Protocol (MCP) to our AI agent, allowing it to dynamically discover and use tools from external services without code changes or redeployment. You’ll learn how MCP enables you to expose your legacy systems, enterprise applications, or microservices to AI agents without rewriting them.
Overview of the Solution
The Integration Challenge
Hardcoding tool integrations has several problems:
- Every new API requires code changes and redeployment
- Tools are tightly coupled to the AI agent application
- Different teams can’t independently develop and deploy their tools
- Testing and versioning tools becomes complex
- Scaling to dozens or hundreds of integrations becomes unmanageable
We need a way to add new capabilities to our AI agent without modifying its code.
We’ll solve this with Model Context Protocol (MCP):
- Create MCP servers that expose tools via a standardized protocol
- Configure MCP client in our AI agent to discover available tools
- Let the AI automatically call tools from any connected MCP server
- Add new capabilities by simply starting new MCP servers
What is Model Context Protocol?
Model Context Protocol (MCP) is an open protocol that standardizes how AI applications connect to external tools and data sources. Think of it as a universal adapter that lets AI agents discover and use tools from any service that implements the protocol.
Key benefits:
- Standardized Interface: All tools use the same protocol, regardless of implementation
- Dynamic Discovery: AI agents automatically discover available tools from connected servers
- Loose Coupling: Tools and AI agents can be developed, deployed, and scaled independently
- Multiple Transports: Supports SSE (Server-Sent Events), Streamable HTTP, stdio, and custom transports
- Language Agnostic: Servers can be written in any language (Java, Python, Node.js, etc.)
Architecture Overview
1 | AI Agent (MCP Client) |
Key Spring AI Components
- MCP Server Starter: Auto-configures MCP server capabilities in Spring Boot applications
- MCP Client Starter: Connects to MCP servers and registers their tools automatically
- ToolCallbackProvider: Spring AI interface for tool registration and invocation
Prerequisites
Before you start, ensure you have:
- Completed Part 4 of this series with the working
ai-agentapplication - Java 21 JDK installed (Amazon Corretto 21)
- Maven 3.6+ installed
- Docker Desktop running (for Testcontainers with PostgreSQL/PGVector)
- AWS CLI configured with access to Amazon Bedrock
Navigate to your project directory from Part 4:
1 | cd ai-agent |
MCP Server
We’ll create a separate Spring Boot application that acts as an MCP server, exposing travel-related tools (flight and hotel search/booking) that our AI agent can use.
Why Separate Server?
Separating the MCP server from the AI agent provides several benefits:
- Independent Deployment: Update travel services without redeploying the AI agent
- Team Autonomy: Different teams can own and maintain their own MCP servers
- Scalability: Scale travel services independently based on demand
- Reusability: Multiple AI agents or applications can use the same MCP server
Clone Travel Server
We’ll use a pre-built travel server that demonstrates MCP server capabilities:
1 | cd .. |
Explore Travel Server Structure
The travel server provides comprehensive travel management capabilities:
1 | # View the project structure |
Key components:
- HotelTools: Search and booking tools for hotels
- FlightTools: Search and booking tools for flights
- AirportTools: Airport information lookup
- Services: Business logic for travel operations
- Repositories: Data access layer with Spring Data JPA
The travel server is configured as an MCP server with these key dependencies in pom.xml:
1 | <dependency> |
And configuration in application.properties:
1 | # MCP Server properties |
These properties configure the MCP server identity and provide instructions that help AI agents understand the server’s capabilities.
Review HotelTools
Let’s examine how tools are exposed via MCP:
1 | cat src/main/java/com/example/travel/accommodations/HotelTools.java |
1 | package com.example.travel.accommodations; |
Key points:
- @Tool annotation: Marks methods as callable tools with detailed descriptions
- ToolCallbackProvider bean: Registers tools with Spring AI’s MCP server
- Separation of concerns: Tools delegate to service layer for business logic
- Clear descriptions: Help the AI understand when and how to use each tool
- Tools files pattern: Create separate Tools classes instead of annotating service methods directly, allowing you to expand complex objects into individual parameters and use descriptive method names that help the AI model understand which method to call with which parameters
Important: The real power of Spring AI’s MCP server starter is that you can add MCP capabilities to almost any existing Java application or microservice. By simply adding the starter dependency and annotating methods with
@Tool, you can expose your legacy systems, enterprise applications, or microservices to AI agents without rewriting them. This enables AI integration across your entire application landscape.
Start Travel Server
Open a new terminal and run the travel server. It uses Testcontainers for automatic PostgreSQL management:
1 | ./mvnw spring-boot:test-run |
You should see:
1 | Creating container for image: postgres:16 |
The server is now running on http://localhost:8082 with:
- Over 100 hotels across major cities
- Sample flights between popular destinations
- MCP server endpoints for tool discovery
Keep this terminal open - the travel server needs to run while we test the AI agent.
Test Travel Server
Open a new terminal and verify the server is working:
1 | # Search for hotels in Paris |
✅ Success! The travel server is running and responding to requests.
MCP Client
Now we’ll configure our AI agent to connect to the travel server via MCP, automatically discovering and using its tools.
Add MCP Client Dependency
Navigate back to your AI agent project:
Open pom.xml and add the MCP client dependency to the <dependencies> section:
1 | <!-- MCP Client --> |
Configure MCP Client
Add MCP client configuration to src/main/resources/application.properties:
1 | cat >> src/main/resources/application.properties << 'EOF' |
This configuration:
- Enables automatic tool callback registration
- Connects to the travel server via SSE (Server-Sent Events) transport
- Names the connection “travel” for identification in logs
Update ChatService with MCP
Update ChatService to use tools from MCP servers:
src/main/java/com/example/ai/agent/service/ChatService.java
1 | ... |
The key change is adding ToolCallbackProvider tools parameter and .defaultToolCallbacks(tools) method. Spring AI automatically:
- Connects to configured MCP servers
- Discovers available tools
- Registers them with the ChatClient
- Makes them available to the AI model
Testing MCP Integration
Let’s test the complete MCP integration with travel bookings:
1 | ./mvnw spring-boot:test-run |
You should see in the logs:
1 | ... |
Test with REST API:
1 | # Search for hotels in Paris |
✅ Success! The AI agent now has access to travel booking capabilities through MCP without any hardcoded integrations.
You can also test in the UI at http://localhost:8080 - ask about flights, hotels, or complete travel planning.


How MCP Works
When you ask “Find hotels in Paris for 3 nights”, here’s what happens:
- AI analyzes the question: Recognizes it needs hotel search capabilities
- Tool discovery: Finds
findHotelsByCitytool from the travel MCP server - Parameter extraction: Extracts city=”Paris”, numberOfNights=3, and determines check-in date
- Tool invocation: Calls the travel server via MCP protocol
- Response synthesis: Formats hotel results into a natural language response
The AI automatically decides which tools to call from which servers, all through the standardized MCP protocol.
Let’s continue the chat. Alice returned from her Paris trip and wants to submit her expenses:
1 | curl -X POST http://localhost:8080/api/chat/message \ |
❌ Problem discovered: Alice has photos of her receipts on her phone, but the agent can only process text—it cannot analyze images or extract information from visual documents like receipts, invoices, or travel confirmations.
We’ll address this limitation in the next part of the series! Stay tuned!
Add More MCP Servers
The power of MCP is that you can add new capabilities without changing the AI agent code:
1 | # Start another MCP server on a different port |
You can add as many MCP servers as needed, each providing different capabilities (CRM, inventory, analytics, etc.).
Cleanup
To stop the applications:
- Press
Ctrl+Cin the AI agent terminal - Press
Ctrl+Cin the travel server terminal
The PostgreSQL containers will continue running (due to withReuse(true)). If necessary, stop and remove them:
1 | docker stop ai-agent-postgres |
(Optional) To remove all data and start fresh:
1 | docker volume prune |
Commit Changes
1 | git add . |
Conclusion
In this post, we’ve added dynamic tool integration to our AI agent through MCP:
- MCP Server: Created a separate travel service exposing hotel and flight tools
- MCP Client: Configured AI agent to discover and use tools from MCP servers
- Dynamic Discovery: AI automatically finds and calls tools without hardcoded integrations
- Loose Coupling: Travel services can be updated independently of the AI agent
- Scalability: Add new capabilities by starting new MCP servers
Our AI agent now has a complete, production-ready architecture: memory (Part 2), knowledge (Part 3), real-time information (Part 4), and dynamic tool integration (Part 5). It can remember conversations, answer policy questions, access current information, and integrate with any service that implements the MCP protocol—all essential capabilities for enterprise AI applications.
What’s Next
Explore advanced features like multi-modal support (image and document analysis) using various models.

Learn More
- Model Context Protocol Documentation
- Spring AI MCP Documentation
- MCP Java SDK
- Amazon Bedrock User Guide
- Part 1: Create an AI Agent
- Part 2: Add Memory
- Part 3: Add Knowledge
- Part 4: Add Tools
Let’s continue building intelligent Java applications with Spring AI!





