A2AJava: Quick Start Guide to Agent Development
May 6, 2025 ยท View on GitHub
Table of Contents
- Getting Started
- Creating Your First Agent
- Advanced Configuration
- Protocol Details
- Error Handling & Best Practices
- Advanced Features
Getting Started
Installation
Add the following Maven dependency to your project:
<dependency>
<groupId>io.github.vishalmysore</groupId>
<artifactId>a2ajava</artifactId>
<version>0.0.7.1</version>
</dependency>
Basic Configuration
- Create
tools4ai.propertiesin your resources folder:
agent.provider=gemini # or openai
Creating Your First Agent
1. Basic Agent Structure
@Agent(groupName = "ticket-booking",
groupDescription = "Handles airline ticket booking operations")
public class BookingAgent {
@Action(description = "Book a flight ticket")
public String bookFlight(String from, String to, String date) {
// Your implementation here
}
}
2. Adding Real-time Updates
@Action(description = "Book a flight ticket")
public String bookFlight(String from, String to, String date) {
actionCallback.sendtStatus("Starting booking process", ActionState.WORKING);
try {
// Booking logic here
actionCallback.sendtStatus("Booking completed", ActionState.COMPLETED);
return "Booking confirmed";
} catch (Exception e) {
actionCallback.sendtStatus("Booking failed: " + e.getMessage(), ActionState.ERROR);
throw e;
}
}
3. Handling Different Message Types
@Action(description = "Process booking request")
public void processBooking(Message message) {
for (Part part : message.getParts()) {
switch (part) {
case TextPart textPart -> handleTextBooking(textPart);
case DataPart dataPart -> handleStructuredBooking(dataPart);
case FilePart filePart -> handleFileAttachment(filePart);
}
}
}
Advanced Configuration
AI Model Integration
- Configure the AI Provider:
# tools4ai.properties
agent.provider=gemini
- Implement a Custom Transformer:
@Override
public PromptTransformer getPromptTransformer() {
return new GeminiV2PromptTransformer();
}
Protocol Support
Your agent automatically supports both:
- A2A Protocol (Agent-to-Agent communication)
- MCP Protocol (Model Context Protocol for AI model interaction)
The framework exposes:
/.well-known/agent.json- A2A protocol endpoint/mcp/tools- MCP protocol endpoint
Error Handling & Best Practices
1. Proper Error Handling
@Action(description = "Process transaction")
public TransactionResult processTransaction(Transaction tx) {
try {
actionCallback.sendtStatus("Validating", ActionState.WORKING);
validateTransaction(tx);
actionCallback.sendtStatus("Processing", ActionState.WORKING);
return processValidTransaction(tx);
} catch (Exception e) {
actionCallback.sendtStatus("Failed: " + e.getMessage(), ActionState.ERROR);
return new TransactionResult(false, e.getMessage());
}
}
2. Best Practices
- Group related actions using meaningful
@AgentgroupNames - Provide clear action descriptions
- Use appropriate parameter types
- Implement proper status updates
- Follow security guidelines
- Use HTTPS in production
- Implement authentication
- Secure sensitive data
Advanced Features
1. Dynamic Tool Discovery
- Automatic tool registration
- Parameter type inference
- JSON schema generation
- AI-friendly descriptions
2. Message Types Support
- TextPart: Plain text
- FilePart: File transfers
- DataPart: Structured data
3. Real-time Updates
Both protocols support status updates through:
ActionCallback(A2A)MCPActionCallback(MCP)
Security Considerations
- Authentication Setup
agentCard.setAuthentication(new Authentication(new String[]{"Bearer"}));
- HTTPS Configuration
- OAuth2 Integration (if needed)
- Proper input validation
Future Development Areas
-
AI Provider Integration
- Additional AI providers
- Enhanced configuration options
- Custom transformations
-
Protocol Enhancements
- Extended message types
- Improved real-time capabilities
- Enhanced streaming
-
Developer Experience
- Additional tools
- Enhanced debugging
- Comprehensive logging