Google’s A2A protocol and Anthropic’s MCP framework both offer powerful mechanisms for building intelligent AI agents — but what if I told you we can combine both in a single Java application? The a2ajava framework simplifies AI agent creation, enabling seamless A2A communication while exposing reusable AI tools via MCP. By integrating both technologies, you can unlock unparalleled AI interoperability, allowing your Java applications to interact dynamically within the broader AI ecosystem.
Example Source code here
Want to build the next-generation AI agents? Let’s harness the combined strength of A2A and MCP for true AI-driven automation!
Test your knowledge on A2A and MCP
The a2ajava framework offers a powerful yet intuitive way to transform your Java applications into active participants in the AI landscape. By using simple annotations, you can define cohesive AI agents capable of communicating with others and expose specific functionalities as handy AI tools for broader consumption. Let’s explore this with our MathTutorAgent example.
Meet the MathTutorAgent: A Java-Based AI Helper
package com.example.mathtutor;
import com.t4a.annotations.Action;
import com.t4a.annotations.Agent;
import com.t4a.detect.ActionCallback;
import lombok.extern.java.Log;
import org.springframework.stereotype.Service;
@Log
@Service
@Agent(groupName = "Math Tutoring Capabilities")
public class MathTutorAgent {
private ActionCallback callback; // a2ajava handles the injection
@Action(description = "Add two numbers and get the result")
public double add(double num1, double num2) {
log.info(String.format("Adding %f and %f...", num1, num2));
return num1 + num2;
}
@Action(description = "Subtract one number from another")
public double subtract(double minuend, double subtrahend) {
log.info(String.format("Subtracting %f from %f...", subtrahend, minuend));
return minuend - subtrahend;
}
@Action(description = "Multiply two numbers together")
public double multiply(double factor1, double factor2) {
log.info(String.format("Multiplying %f by %f...", factor1, factor2));
return factor1 * factor2;
}
}
Agent Card for A2A: /well-known/agent.json
When the MathTutorAgent application runs, a2ajava automatically generates an Agent Card, typically available at http://localhost:YOUR_PORT/.well-known/agent.json. This card describes the agent and its capabilities for other A2A agents. For our MathTutorAgent, it might look something like this:
{
"name": "",
"description": "This agent provides capabilities for performing math tutoring related actions.",
"url": "http://localhost:YOUR_PORT",
"provider": {
"organization": "",
"url": ""
},
"version": "",
"documentationUrl": "",
"capabilities": {
"streaming": false,
"pushNotifications": false,
"stateTransitionHistory": false
},
"authentication": {
"schemes": [
"class java.lang.String"
],
"credentials": ""
},
"defaultInputModes": [
"class java.lang.String"
],
"defaultOutputModes": [
"class java.lang.String"
],
"skills": [
{
"id": null,
"name": "add",
"description": "Add two numbers and get the result",
"tags": null,
"examples": null,
"inputModes": null,
"outputModes": null
},
{
"id": null,
"name": "subtract",
"description": "Subtract one number from another",
"tags": null,
"examples": null,
"inputModes": null,
"outputModes": null
},
{
"id": null,
"name": "multiply",
"description": "Multiply two numbers together",
"tags": null,
"examples": null,
"inputModes": null,
"outputModes": null
}
]
}
This JSON output allows other A2A-compliant agents to understand the MathTutorAgent's purpose and the specific actions it can perform.
MCP Tools: /mcp/list-tools
Simultaneously, a2ajava exposes the @Action-annotated methods as individual tools via the /mcp/list-tools endpoint. This allows other AI systems to discover and use these specific mathematical operations. Here's how the add tool might be represented in the JSON response from this endpoint:
{
"tools": [
// ... other tools ...
{
"parameters": {
"type": "object",
"properties": {
"provideAllValuesInPlainEnglish": {
"type": "string",
"description": "{\n \"num1\": 0.0,\n \"num2\": 0.0\n}"
}
},
"required": [
"provideAllValuesInPlainEnglish"
],
"additionalProperties": false
},
"inputSchema": {
"type": "object",
"properties": {
"provideAllValuesInPlainEnglish": {
"type": "string",
"description": "{\n \"num1\": 0.0,\n \"num2\": 0.0\n}",
"additionalProperties": null,
"items": null
}
},
"required": [
"provideAllValuesInPlainEnglish"
]
},
"annotations": null,
"description": "Add two numbers and get the result",
"name": "add",
"type": null
},
// ... other tools for subtract and multiply ...
]
}
As you can see, each @Action translates into a distinct tool with a description and parameter schema, ready to be invoked by other AI systems.
The Dual Power of a2ajava
The a2ajava framework empowers Java developers to seamlessly integrate their applications into the AI ecosystem by supporting both the A2A protocol for agent-to-agent communication and the MCP protocol for exposing individual tools. By simply annotating your Java code, you can create sophisticated AI agents and reusable AI tools, all within the familiar Java environment.
Enhancing AI Agent Security with Spring Security
Combining Google A2A and Anthropic MCP with Spring using a2ajava provides a powerful mechanism for building AI applications while ensuring robust security. By leveraging Spring Security, developers gain access to advanced features that help secure AI-powered APIs and interactions.
Key Benefits of Spring Security in AI Agent Communication
✅ Robust Authentication & Authorization — Secure AI agents using OAuth2, JWT, or role-based access control.
✅ AI-Agent Identity Management — Ensure only authorized AI agents can communicate via A2A.
✅ Encrypted Data Exchange — Protect sensitive agent interactions using Spring Security’s encryption mechanisms.
✅ Audit Logging & Access Control — Maintain visibility and control over AI-powered requests and responses.
@RestController
@RequestMapping("/mcp/tools")
public class MCPController {
@PreAuthorize("hasRole('USER')")
@GetMapping("/add")
public ResponseEntity add(@RequestParam double num1, @RequestParam double num2) {
return ResponseEntity.ok(num1 + num2);
}
@PreAuthorize("hasRole('USER')")
@GetMapping("/subtract")
public ResponseEntity subtract(@RequestParam double a, @RequestParam double b) {
return ResponseEntity.ok(a - b);
}
}
📌 AI tools are now restricted to authenticated users using @PreAuthorize("hasRole('USER')").
How This Works Together
Spring Boot + A2A → Enables secure and scalable AI agent interactions.
Spring Security + MCP Tools → Ensures strict access control while exposing AI functionalities as tools.
Unified Integration → Provides encrypted messaging, authentication, and logging for AI-driven APIs.
@Configuration
@EnableWebSecurity
public class SecurityConfig {
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
.authorizeHttpRequests(auth -> auth
.requestMatchers("/mcp/tools/**").authenticated() // Secure AI tools
.anyRequest().permitAll()
)
.oauth2ResourceServer(OAuth2ResourceServerConfigurer::jwt) // Enable JWT authentication
.csrf().disable(); // Disable CSRF for API communication
return http.build();
}
}
📌 Using JWT authentication ensures only verified AI agents can access sensitive AI functionalities.
By combining A2A & MCP with Spring using a2ajava, developers can build powerful, secure AI-driven microservices that are ready for enterprise applications.