DEV Community

vishalmysore
vishalmysore

Posted on

Building a Multi-Agent System with A2A and MCP Protocols in Java

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:

  1. A Movies Server (Port 7861) - Handling movie-related operations
  2. A Library Server (Port 7862) - Managing library-related tasks
  3. 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

Image description

Image description

Image description

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
Enter fullscreen mode Exit fullscreen mode

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);
    }
}
Enter fullscreen mode Exit fullscreen mode

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
    }
}
Enter fullscreen mode Exit fullscreen mode

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);
    }
}
Enter fullscreen mode Exit fullscreen mode

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
    }
}
Enter fullscreen mode Exit fullscreen mode

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
    }
}
Enter fullscreen mode Exit fullscreen mode

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
    }
}
Enter fullscreen mode Exit fullscreen mode

Running the System

Starting the Servers

  1. Start the Movies Server:
mvn spring-boot:run -Dspring-boot.run.main-class=example.movies.server.MoviesServer -Dtools4ai.properties.path=tools4ai_movies.properties
Enter fullscreen mode Exit fullscreen mode
  1. Start the Library Server:
mvn spring-boot:run -Dspring-boot.run.main-class=example.library.server.LibraryServer -Dtools4ai.properties.path=tools4ai_library.properties
Enter fullscreen mode Exit fullscreen mode

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/
Enter fullscreen mode Exit fullscreen mode

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/
Enter fullscreen mode Exit fullscreen mode

Integration with Claude Desktop

The system can be integrated with Claude Desktop for enhanced AI capabilities:

  1. 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
        }
    }
}
Enter fullscreen mode Exit fullscreen mode
  1. Download and configure the MCP Connector JAR for seamless integration.

Benefits of Multi-Agent Architecture

  1. Separation of Concerns: Each server handles specific domain operations
  2. Scalability: Services can be scaled independently
  3. Protocol Flexibility: Support for both A2A and MCP enables diverse integration options
  4. Independent Development: Teams can work on different agents without interference
  5. Easy Maintenance: Modular design simplifies updates and troubleshooting

Best Practices

  1. Always specify the main class when running servers
  2. Use appropriate ports for different services
  3. Implement proper error handling in both clients
  4. Document API endpoints and tool capabilities
  5. Maintain separate configuration files for each server
  6. Implement proper logging for debugging
  7. 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)