Building AI Agents with A2A and MCP Protocol: A Hands-on Implementation Guide

This article will focus on building AI agents hands-on with A2A and MCP protocol in 4 different languages, deploying them, setting them up with A2A and MCP clients as well as Java-based clients. This will focus more on coding and hands-on implementatio…


This content originally appeared on DEV Community and was authored by vishalmysore

This article will focus on building AI agents hands-on with A2A and MCP protocol in 4 different languages, deploying them, setting them up with A2A and MCP clients as well as Java-based clients. This will focus more on coding and hands-on implementation rather than theoretical knowledge as it's already available abundantly.

Understanding A2A and MCP

A2A is primarily for agent-to-agent communication and coordination, while MCP is focused on agent-to-tool integration. In other words, "A2A enables agents to talk to each other; MCP lets agents trigger tools." For a detailed comparison including schema differences, read more here: MCP vs A2A: Similarities and Differences

Quick Links

Chapter 1: Server Implementation in Multiple Languages

We will build servers that cater to both A2A and MCP protocols. This chapter demonstrates how to implement the same functionality across different JVM languages, showcasing the flexibility and interoperability of the A2A/MCP approach.

Java + Spring Boot Implementation

Create a Spring Boot application with the EnableAgent annotation:

package io.github.vishalmysore;

import io.github.vishalmysore.tools4ai.EnableAgent;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableAgent
public class Application {
}

Create a service as a normal Spring bean:


import com.t4a.annotations.Action;
import com.t4a.annotations.Agent;

import com.t4a.api.JavaMethodAction;

import com.t4a.detect.ActionCallback;
import com.t4a.processor.AIProcessor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Service
@Agent(groupName ="food preferences", groupDescription = "Provide persons name and then find out what does that person like")
@Slf4j
public class SimpleService {

    /**
     * Each action has access to AIProcessor and ActionCallback which are autowired by tools4ai
     */
    private ActionCallback callback;

    /**
     * Each action has access to AIProcessor and ActionCallback which are autowired by tools4ai
     */
    private AIProcessor processor;
    public SimpleService(){
      log.info(" Created Simple Service");
    }

    @Action(description = "Get the favourite food of a person")
    public String whatThisPersonFavFood(String name) {
        if("vishal".equalsIgnoreCase(name))
        return "Paneer Butter Masala";
        else if ("vinod".equalsIgnoreCase(name)) {
            return "aloo kofta";
        }else
            return "something yummy";
    }

}

When you start the server, it exposes the method whatThisPersonFavFood in both A2A and MCP protocols.
View the agent card at: https://localhost:7860/.well-known/agent.json

Detailed instructions: A2A MCP Spring Guide

Implementations in Other JVM Languages

Each implementation showcases how the A2A and MCP protocols can be leveraged in different JVM languages, each bringing its own strengths to the table. These implementations demonstrate the protocol's language-agnostic nature and the ability to create a polyglot agent ecosystem.

Scala Implementation

The Scala implementation demonstrates how to leverage Scala's powerful type system and functional programming features while maintaining protocol compatibility:

@Agent(groupName = "IceCream", groupDescription = "An agent that provides ice cream related information")
class IceCreamAgent {

  @Action(description = "get template before ordering icecream")
  def getTemperature(city: String): String = {
    s"The current temperature in $city is 25°C "
  }

}

View Scala Implementation

Kotlin

@Agent(
    groupName = "GeoSpatial Route Planner Agent",
    groupDescription = "Provides advanced route planning, traffic analysis, and custom map generation services"
)
class RoutePlannerAgent {
    @Action(description = "Generates custom map with points of interest")
    fun generateCustomMap(points: List<String>, radius: Double): String {
        return "Custom map generated with ${points.size} points of interest within $radius mile radius"
    }

    @Action(description = "Shows nearby locations of specified type")
    fun findNearbyPlaces(location: String, placeType: String, radius: Double): String {
        return "Found nearby $placeType locations around $location within $radius miles - MTR Coffee, NR Colony Coffee Shop, Cafe Coffee House"
    }
}

View Kotlin Implementation

Groovy

@Agent(groupName = "Property Maintenance", groupDescription = "Provides lawn mowing and snow cleaning services")
class MaintenanceAgent {
    @Action(description = "Schedule a lawn mowing service")
    String scheduleLawnMowing(String address, String preferredDate) {
        return "Lawn mowing service scheduled for ${address} on ${preferredDate}"
    }

    @Action(description = "Get lawn mowing service price estimation")
    String getLawnMowingPrice(String lawnSize) {
        return "Estimated price for ${lawnSize} lawn is \$50"
    }

    @Action(description = "Schedule snow removal service")
    String scheduleSnowRemoval(String address, String urgency) {
        return "Snow removal service scheduled for ${address} with ${urgency} priority"
    }

    @Action(description = "Get snow removal price estimation")
    String getSnowRemovalPrice(String drivewaySize, String snowDepth) {
        return "Estimated price for ${drivewaySize} driveway with ${snowDepth} snow is \$75"
    }
}

View Groovy Implementation

Language Selection Guide

  • Complex data processing: Use Scala with Spark
  • Clean async operations: Use Kotlin's coroutines
  • Enterprise integrations: Use Java's ecosystem
  • Rapid prototyping: Use Groovy

Chapter 2: Adding Security

This chapter demonstrates how to add Spring-based security to agents. Code available at A2A MCP Security Demo.

@Log
@Service
@Agent(groupName = "car booking", groupDescription = "actions related to car booking")
public class CarBookingAgent {
    @PreAuthorize("hasRole('USER')")
    @Action(description = "Book a car for the given details")
    public String bookCar(String carType, String pickupLocation, String dropLocation) {
        return "Car of type " + carType + " has been booked from " + pickupLocation + " to " + dropLocation;
    }

    @PreAuthorize("hasRole('ADMIN')")
    @Action(description = "Cancel a car booking")
    public String cancelCarBooking(String bookingId) {
        return "Car booking with ID " + bookingId + " has been cancelled";
    }

    @Action(description = "Get booking status of a car")
    public String getBookingStatus(String bookingId) {
        return "The status of booking ID " + bookingId + " is confirmed";
    }

    // Additional methods...
}

Detailed security configuration guide: RBAC Security Integration
Live demo: A2A MCP Security Demo

This class elegantly combines agentic design with Spring Security to create a secure and modular AI-triggerable service. By annotating the class with @agent and individual methods with @Action, it exposes business logic (like booking or canceling a car) as structured agent tools—ready for invocation via A2A or MCP protocols. The use of @PreAuthorize from Spring Security adds robust role-based access control, ensuring that only authorized users can trigger sensitive actions (e.g., cancellations restricted to admins). This fusion provides a massively advantageous pattern for building scalable, secure, and AI-integrated APIs—where LLMs or agents can safely execute backend logic while respecting enterprise-grade authorization.

Chapter 3: Building Integrated Servers and Clients

In this chapter, we'll build a complete server and client implementation in Java that works with both A2A and MCP protocols. This demonstrates how a single codebase can serve both protocols, maximizing interoperability and reducing development overhead. The complete source code is available at: MCP Server Client Repository

Server Implementation

@Service
@Agent(groupName = "libraryOperations", groupDescription = "Manage library operations like holding books and getting books")
@Slf4j
public class LibraryService {
    private ActionCallback callback;
    private AIProcessor processor;

    @Action(description = "Hold a book for a user")
    public String holdBook(String bookName, String userName) {
        return String.format("Book '%s' has been held for user '%s'", bookName, userName);
    }

    @Action(description = "Get a book's availability status")
    public String getBookStatus(String bookName) {
        if ("Effective Java".equalsIgnoreCase(bookName)) {
            return "Available";
        } else if ("Clean Code".equalsIgnoreCase(bookName)) {
            return "Checked out";
        }
        return "Not in catalog";
    }

    // Additional methods...
}

A2A Client Implementation

public class A2AClient {
    public static void main(String[] args) {
        A2AAgent a2aagent = new A2AAgent();
        a2aagent.connect("http://localhost:7860/");
        Object task = a2aagent.remoteMethodCall("get me list of books");
    }
}

MCP Client Implementation

public class MCPClient {
    public static void main(String[] args) {
        MCPAgent mcpAgent = new MCPAgent();
        mcpAgent.connect("https://vishalmysore-a2amcpspring.hf.space/");
        AgentInfo mcpCard = mcpAgent.getInfo();
        CallToolResult result = (CallToolResult) mcpAgent.remoteMethodCall("holdBook", 
            "vishal needs to read harry potter book 1");
    }
}

By keeping the client-side interface consistent across both A2A and MCP protocols, this architecture delivers a massive advantage in developer experience, maintainability, and scalability. Both A2AClient and MCPClient follow nearly identical patterns—initialize the agent, connect to the server, and invoke remoteMethodCall()—making it seamless for developers to switch or integrate protocols without rewriting logic or learning new APIs. This uniformity allows organizations to abstract away protocol differences, enabling agentic systems to scale across different communication models (agent-to-agent or agent-to-tool) with minimal friction. It also simplifies onboarding, testing, and debugging, empowering teams to build secure, intelligent systems faster with reduced cognitive overhead.

Chapter 4: Multi-Server Setup

Source code: MCP A2A Multi-Agent

Server Configuration

  • Movies Server (Port 7861)
  • Library Server (Port 7862)

Starting Servers

Movies Server:

mvn spring-boot:run -Pmovies

Library Server:

mvn spring-boot:run -Plibrary

Configuration example:

spring.application.name=spring-boot
server.port=7862
a2a.persistence=cache
tools4ai.properties.path=tools4ai_library.properties

Chapter 5: Agentic Mesh

Source code: MCP Agentic Mesh

Server Configuration

  • Dessert Server (Port 7863)
  • Gym Server (Port 7864)
  • Restaurant Server (Port 7865)
  • Yoga Server (Port 7866)

Agent Catalog Implementation

AgentCatalog agentCatalog = new AgentCatalog();
agentCatalog.addAgent("http://localhost:7862/");
agentCatalog.addAgent("http://localhost:7863/");
String answer = agentCatalog.processQuery("what is the recipe for Gulab Jamun").getTextResult();

Testing with cURL

# Get tools list for Dessert Server
curl -H "Content-Type: application/json" \
-d '{"jsonrpc":"2.0","method":"tools/list","params":{},"id":9}' \
http://localhost:7863/

Claude Desktop Integration

Configure in C:\Users\<yourusername>\AppData\Roaming\Claude\claude_desktop_config.json:

{
    "mcpServers": {
        "yogaserver": {
            "command": "java",
            "args": [
                "-jar",
                "PATH_TO_YOUR_JAR/mcp-connector-full.jar",
                "http://localhost:7866"
            ],
            "timeout": 30000
        }
    }
}

Chapters 4 and 5 showcase how to scale an agentic system from individual task-specific servers to a fully coordinated multi-agent mesh. In Chapter 4, the setup demonstrates how to spin up multiple dedicated MCP+A2A servers—such as a Movies Server and Library Server—each running independently on separate ports, making it easy to modularize capabilities. Chapter 5 then takes this further by introducing the Agentic Mesh pattern, where multiple specialized servers (e.g., Dessert, Gym, Yoga) collaborate through a centralized AgentCatalog. This allows any client or LLM to route queries intelligently across agents—creating a distributed, scalable, and loosely coupled AI ecosystem. With support for cURL testing, Claude Desktop integration, and cross-agent orchestration, these chapters offer a practical blueprint for building robust AI-powered systems using open-source protocols like A2A and MCP.

Chapter 6: Agentic Mesh Patterns

Different mesh patterns are implemented in A2A Java Mesh Implementation

Interactive Pattern Demos

Chapter 7: Implementation Patterns and Advanced Integration

This chapter explores the various architectural patterns for implementing sophisticated agent networks. Each pattern is designed to address specific use cases and requirements in distributed AI systems. The implementation patterns we'll discuss have been battle-tested in production environments and offer different trade-offs in terms of scalability, resilience, and complexity.

Actor-Based Mesh Pattern: Foundation for Distributed Intelligence

public class IntelligentAgent extends AbstractBehavior<AgentMessage> {
    private final ActorRef<AgentMessage> supervisor;
    private final Map<String, ActorRef<AgentMessage>> knownAgents;

    public Behavior<AgentMessage> onMessage(AgentMessage msg) {
        return switch (msg) {
            case DiscoveryRequest req -> handleDiscovery(req);
            case TaskRequest task -> processTask(task);
            case CollaborationInvite invite -> joinCollaboration(invite);
        };
    }
}

Reactive Streams Mesh Pattern

public class ReactiveAgent {
    private final Flux<TaskRequest> incomingTasks;
    private final Sink<TaskResponse> outgoingResponses;

    public Flux<TaskResponse> processTaskStream() {
        return incomingTasks
            .flatMap(this::analyzeTask)
            .filter(this::canHandle)
            .switchIfEmpty(this::delegateToMesh)
            .map(this::executeTask);
    }
}

Microservices-Based Agent Pattern

@RestController
@Component
public class AgentController {
    @Autowired
    private AgentCapabilityService capabilities;

    @PostMapping("/collaborate")
    public ResponseEntity<CollaborationResponse> collaborate(
            @RequestBody CollaborationRequest request) {
        List<Agent> peers = discoveryService.findCapableAgents(request.getRequiredSkills());
        return coalitionService.formTemporaryAlliance(peers, request);
    }
}

Event-Driven Mesh Pattern

@KafkaListener(topics = "agent-collaboration")
public class EventDrivenAgent {
    @EventHandler
    public void handleTaskDistribution(TaskDistributionEvent event) {
        if (canHandleTask(event.getTask())) {
            TaskResult result = processTask(event.getTask());
            publishEvent(new TaskCompletionEvent(result));
        } else {
            publishEvent(new TaskRedirectionEvent(event, findBetterAgent()));
        }
    }
}

Graph-Based Mesh Pattern

@Node
public class GraphAgent {
    @Id private String agentId;
    @Property private Set<String> capabilities;

    @Relationship(type = "CAN_COLLABORATE_WITH")
    private Set<GraphAgent> collaborators;

    @Relationship(type = "DEPENDS_ON")
    private Set<GraphAgent> dependencies;

    public Optional<CollaborationPath> findOptimalCollaborationPath(TaskRequirement requirement) {
        return graphTraversalService.findShortestCapabilityPath(this, requirement);
    }
}

Chapters 6 and 7 dive deep into the design patterns that power large-scale agentic systems, starting with interactive mesh patterns and culminating in advanced implementation architectures. Chapter 6 introduces real-world Agentic Mesh Patterns—such as Supply Chain, Education, Healthcare, and more—brought to life via interactive demos that showcase how specialized agents can collaborate to solve complex workflows. Chapter 7 then transitions into battle-tested implementation patterns that developers can adopt depending on their infrastructure needs. Whether it's an Actor-Based Mesh for asynchronous decision-making, Reactive Streams for backpressure-aware processing, Microservices Agents for REST-based interoperability, Event-Driven Meshes using Kafka for decoupled collaboration, or a Graph-Based Mesh that models agents and dependencies as queryable graphs—these patterns provide a toolkit for building resilient, scalable, and intelligent multi-agent systems. Together, these chapters serve as both a practical guide and architectural reference for building the next generation of distributed AI ecosystems.

Implementation Considerations

Service Discovery and Registry

Use technologies like:

  • Consul
  • Eureka
  • Kubernetes service discovery

Communication Protocols and Interoperability

The choice of communication protocol is crucial for building a robust agentic mesh. Each protocol offers different advantages:

  • gRPC: Ideal for high-performance, low-latency communication between agents. Its binary protocol and HTTP/2 foundation make it perfect for microservices architectures.
  • Apache Thrift: Excellent for polyglot environments where agents are implemented in different languages. Offers strong type safety and efficient serialization.
  • Custom protocols over HTTP/2: When you need specialized behavior or optimizations for your specific use case. Built on HTTP/2's multiplexing and streaming capabilities.

These protocols should be selected based on your specific requirements for performance, language support, and ecosystem compatibility.

Security and Trust Architecture

Security in an agentic mesh requires a comprehensive approach that addresses authentication, authorization, and secure communication:

Authentication and Authorization

  • OAuth 2.0: Provides robust token-based authentication and authorization flows. Perfect for scenarios where agents need to access resources on behalf of users.
  • JWT tokens: Enables stateless authentication with rich claim support. Particularly useful for carrying agent capabilities and permissions.
  • Custom claims can encode agent capabilities, trust levels, and access permissions.

Transport Security

  • Mutual TLS: Ensures bidirectional trust between agents through certificate-based authentication.
  • Certificate rotation and management strategies for long-running agents.
  • Network policy enforcement for agent-to-agent communication.

Security Monitoring

  • Real-time threat detection for unusual agent behavior
  • Audit logging of all inter-agent communications
  • Automated response to security events

Monitoring and Observability in Agentic Mesh

Maintaining visibility into a distributed agent network requires comprehensive monitoring and observability solutions. Here's a detailed approach to implementing effective monitoring:

Metrics Collection and Analysis

  • Micrometer: Provides a vendor-neutral metrics facade that supports multiple monitoring systems.
    • Custom metrics for agent performance
    • Task execution timing
    • Communication latency measurements
    • Resource utilization tracking

Time-Series Monitoring

  • Prometheus: Ideal for collecting and analyzing time-series data from agents
    • Query language for complex metric analysis
    • Alert management for agent issues
    • Performance trending and capacity planning
    • Custom dashboards for agent health

Distributed Tracing

  • Jaeger or Zipkin: Essential for understanding request flow across multiple agents
    • End-to-end request visualization
    • Performance bottleneck identification
    • Error chain analysis
    • Service dependency mapping

Advanced Monitoring Patterns

  • Real-time agent health monitoring
  • Automated performance optimization
  • Predictive maintenance using collected metrics
  • AI-driven anomaly detection

Benefits and Applications

Enhanced Scalability

  • Horizontal scaling through specialized agents
  • Reduced monolithic system dependencies

Improved Resilience

  • Localized system failures
  • Prevention of cascade failures

Specialized Expertise

  • Domain-optimized agents
  • Improved system performance

Dynamic Adaptation

  • Runtime reconfiguration of agent collaborations
  • Zero-downtime updates

Challenges and Future Directions

Current challenges include:

  • Coordination overhead
  • Consistency management
  • Distributed debugging complexity

Future developments focus on:

  • Autonomous coordination algorithms
  • Inter-agent learning mechanisms
  • Standardized collaboration protocols

The evolution of the JVM ecosystem with Project Loom and GraalVM will open new opportunities for agentic mesh implementations.


This content originally appeared on DEV Community and was authored by vishalmysore


Print Share Comment Cite Upload Translate Updates
APA

vishalmysore | Sciencx (2025-06-07T23:02:11+00:00) Building AI Agents with A2A and MCP Protocol: A Hands-on Implementation Guide. Retrieved from https://www.scien.cx/2025/06/07/building-ai-agents-with-a2a-and-mcp-protocol-a-hands-on-implementation-guide/

MLA
" » Building AI Agents with A2A and MCP Protocol: A Hands-on Implementation Guide." vishalmysore | Sciencx - Saturday June 7, 2025, https://www.scien.cx/2025/06/07/building-ai-agents-with-a2a-and-mcp-protocol-a-hands-on-implementation-guide/
HARVARD
vishalmysore | Sciencx Saturday June 7, 2025 » Building AI Agents with A2A and MCP Protocol: A Hands-on Implementation Guide., viewed ,<https://www.scien.cx/2025/06/07/building-ai-agents-with-a2a-and-mcp-protocol-a-hands-on-implementation-guide/>
VANCOUVER
vishalmysore | Sciencx - » Building AI Agents with A2A and MCP Protocol: A Hands-on Implementation Guide. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/06/07/building-ai-agents-with-a2a-and-mcp-protocol-a-hands-on-implementation-guide/
CHICAGO
" » Building AI Agents with A2A and MCP Protocol: A Hands-on Implementation Guide." vishalmysore | Sciencx - Accessed . https://www.scien.cx/2025/06/07/building-ai-agents-with-a2a-and-mcp-protocol-a-hands-on-implementation-guide/
IEEE
" » Building AI Agents with A2A and MCP Protocol: A Hands-on Implementation Guide." vishalmysore | Sciencx [Online]. Available: https://www.scien.cx/2025/06/07/building-ai-agents-with-a2a-and-mcp-protocol-a-hands-on-implementation-guide/. [Accessed: ]
rf:citation
» Building AI Agents with A2A and MCP Protocol: A Hands-on Implementation Guide | vishalmysore | Sciencx | https://www.scien.cx/2025/06/07/building-ai-agents-with-a2a-and-mcp-protocol-a-hands-on-implementation-guide/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.