Create a multi-agent system that supports both Google's Agent-to-Agent (A2A) protocol and the Model Context Protocol (MCP) using Java and Spring Boot. We'll build a system with two specialized servers - one for movie operations and another for library operations - demonstrating how different agents can work together while maintaining separation of concerns.
Overview
Our multi-agent system consists of:
- A Movies Server (Port 7861) - Handling movie-related operations
- A Library Server (Port 7862) - Managing library-related tasks
- Java clients supporting both A2A and MCP protocols
Key Components
- A2A Protocol: Google's Agent-to-Agent protocol for AI agent communication
- MCP Protocol: Model Context Protocol for AI model interactions
- Spring Boot: Framework for building standalone Java applications
- Tools4AI: Library for implementing agent capabilities
Project Structure
src/
├── main/
│ ├── java/
│ │ └── example/
│ │ ├── client/
│ │ │ ├── A2AClient.java
│ │ │ └── MCPClient.java
│ │ ├── library/
│ │ │ └── server/
│ │ │ ├── LibraryServer.java
│ │ │ └── LibraryService.java
│ │ └── movies/
│ │ └── server/
│ │ ├── MoviesServer.java
│ │ └── MoviesService.java
│ └── resources/
│ ├── application-library.properties
│ ├── application-movies.properties
│ ├── tools4ai_library.properties
│ └── tools4ai_movies.properties
Implementation Guide
1. Setting Up the Movies Server
First, create the Movies server with Spring Boot:
@SpringBootApplication
@EnableAgent
@PropertySource("classpath:application-movies.properties")
public class MoviesServer {
public static void main(String[] args) {
SpringApplication.run(MoviesServer.class, args);
}
}
Create the service layer:
@Service
public class MoviesService {
// Implement movie-related operations
public void rentMovie(String movieName, String user) {
// Implementation
}
public void returnMovie(String movieName) {
// Implementation
}
}
2. Setting Up the Library Server
Similarly, create the Library server:
@SpringBootApplication
@EnableAgent
@PropertySource("classpath:application-library.properties")
public class LibraryServer {
public static void main(String[] args) {
SpringApplication.run(LibraryServer.class, args);
}
}
Create the library service:
@Service
public class LibraryService {
// Implement library-related operations
public void holdBook(String bookName, String user) {
// Implementation
}
public void returnBook(String bookName) {
// Implementation
}
}
3. Implementing the Clients
A2A Client Implementation
public class A2AClient {
private final String serverUrl;
public A2AClient(String serverUrl) {
this.serverUrl = serverUrl;
}
public void communicateWithAgent(String message) {
// Implement A2A protocol communication
}
}
MCP Client Implementation
public class MCPClient {
private final String serverUrl;
public MCPClient(String serverUrl) {
this.serverUrl = serverUrl;
}
public void executeModelOperation(String operation) {
// Implement MCP protocol communication
}
}
Running the System
Starting the Servers
- Start the Movies Server:
mvn spring-boot:run -Dspring-boot.run.main-class=example.movies.server.MoviesServer -Dtools4ai.properties.path=tools4ai_movies.properties
- Start the Library Server:
mvn spring-boot:run -Dspring-boot.run.main-class=example.library.server.LibraryServer -Dtools4ai.properties.path=tools4ai_library.properties
Interacting with the Servers
You can interact with both servers using curl commands:
Movies Server (Port 7861):
curl -H "Content-Type: application/json" `
-d '{
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"name": "rentMovie",
"arguments": {
"provideAllValuesInPlainEnglish": {
"name": "The Dark Knight",
"user": "John Doe"
}
}
},
"id": 25
}' `
http://localhost:7861/
Library Server (Port 7862):
curl -H "Content-Type: application/json" `
-d '{
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"name": "holdBook",
"arguments": {
"provideAllValuesInPlainEnglish": {
"name": "Harry Potter",
"user": "Jane Doe"
}
}
},
"id": 25
}' `
http://localhost:7862/
Integration with Claude Desktop
The system can be integrated with Claude Desktop for enhanced AI capabilities:
- Configure Claude Desktop client in
claude_desktop_config.json
:
{
"mcpServers": {
"mycustomserver": {
"command": "java",
"args": [
"-jar",
"PATH_TO_YOUR_JAR/mcp-connector-full.jar",
"http://localhost:7860"
],
"timeout": 30000
}
}
}
- Download and configure the MCP Connector JAR for seamless integration.
Benefits of Multi-Agent Architecture
- Separation of Concerns: Each server handles specific domain operations
- Scalability: Services can be scaled independently
- Protocol Flexibility: Support for both A2A and MCP enables diverse integration options
- Independent Development: Teams can work on different agents without interference
- Easy Maintenance: Modular design simplifies updates and troubleshooting
Best Practices
- Always specify the main class when running servers
- Use appropriate ports for different services
- Implement proper error handling in both clients
- Document API endpoints and tool capabilities
- Maintain separate configuration files for each server
- Implement proper logging for debugging
- Use appropriate security measures for production deployments
Conclusion
This multi-agent system demonstrates how to effectively combine A2A and MCP protocols in a Java-based environment. The separation into Movies and Library servers showcases how different AI agents can be orchestrated while maintaining clean architecture and clear responsibilities. The system is extensible and can be adapted for various use cases in AI agent orchestration.
For more information and updates, visit the project repository and documentation. The complete source code and examples are available in the repository.
Code for this article is here
Top comments (0)