PHP library implementation of the A2A (AI Agent-to-Agent) Protocol v0.2.5.
composer require andreibesleaga/a2a-php
# Start the fully compliant A2A server
php -S localhost:8081 examples/complete_a2a_server.php
# Verify compliance with A2A-TCK
cd ../a2a-tck
python3 run_tck.py --sut-url http://localhost:8081 --category all
This implementation has been thoroughly tested against the official A2A Test Compatibility Kit (TCK):
- JSON-RPC 2.0 Transport: Complete compliance with error codes
- Agent Card: All required fields and validation
- Message Send: Text messages, task continuation, error handling
- Task Management: Get, cancel, state transitions, history
- Protocol Violations: Proper error responses
- Message Capabilities: Context continuation, multiple parts
- Push Notifications: Full CRUD operations (set/get/list/delete)
- Streaming Methods: SSE streaming, task resubscription
- Agent Card Extensions: Capabilities structure validation
- Error Handling: Comprehensive JSON-RPC error responses
- State Management: Proper task state transitions
- Validation: Input validation and type checking
- Logging: Structured logging with PSR-3 compliance
- Advanced Task Management: Custom task IDs, artifacts
- Event System: Real-time updates via Server-Sent Events
- Extension Support: Agent extensions and interfaces
- Transport Security: HTTPS support and security headers
- A2A Protocol v0.2.5: Complete implementation with all methods
- JSON-RPC 2.0: Transport layer with A2A-specific error codes
- Message Handling: Text, file, and data parts with validation
- Task Lifecycle: Full state management (pending β working β completed)
- Agent Discovery: AgentCard with capabilities and metadata
- Server-Sent Events (SSE): Real-time task updates
- Event-Driven Architecture: ExecutionEventBus for loose coupling
- Task Streaming: Live status and artifact updates
- Reconnection Logic: Automatic reconnection and resubscription
- AgentExecutor Framework: Complex multi-step agent logic
- RequestContext System: Execution state and context management
- Push Notifications: Complete CRUD API for notification configs
- Extension System: Agent extensions and additional interfaces
- Modern PHP 8.0+: Strict typing, enums, and modern patterns
- PSR Compliance: PSR-4 autoloading, PSR-3 logging
- Comprehensive Testing: Unit, integration, and end-to-end tests
- HTTP Client: Abstracted with Guzzle for flexibility
use A2A\Models\AgentCard;
use A2A\Models\AgentCapabilities;
// Create agent with full capabilities
$agentCard = new AgentCard(
name: 'my-agent-001',
description: 'Production-ready A2A agent',
url: 'https://my-agent.com/api',
version: '1.0.0'
);
// Add capabilities
$capabilities = new AgentCapabilities();
$capabilities->addInputMode('text');
$capabilities->addInputMode('file');
$capabilities->addOutputMode('text');
$capabilities->addOutputMode('data');
$agentCard->setCapabilities($capabilities);
// Add skills and metadata
$agentCard->addSkill('data-processing');
$agentCard->addSkill('file-analysis');
$agentCard->setMetadata('environment', 'production');
use A2A\A2AClient;
use A2A\Models\Message;
use A2A\Models\Parts\TextPart;
$client = new A2AClient($agentCard);
// Send a text message
$message = new Message();
$message->addPart(new TextPart('Process this data: user_id=123'));
$response = $client->sendMessage('https://other-agent.com/api', $message);
echo "Task ID: " . $response['result']['task']['id'] . "\n";
// Check agent availability
$isAlive = $client->ping('https://other-agent.com/api');
// Get remote agent information
$remoteCard = $client->getAgentCard('https://other-agent.com/api');
use A2A\A2AServer;
use A2A\TaskManager;
use A2A\Events\ExecutionEventBusImpl;
$server = new A2AServer($agentCard);
$taskManager = new TaskManager();
$eventBus = new ExecutionEventBusImpl();
// Add message handler
$server->addMessageHandler(function($message, $fromAgent, $context) use ($taskManager) {
// Create new task
$task = $taskManager->createTask($context->taskId, [
'message' => $message->getParts()[0]->getText(),
'from' => $fromAgent
]);
// Process in background
$task->setStatus(TaskState::WORKING);
// Return task reference
return ['task' => $task->toArray()];
});
// Handle incoming HTTP request
$request = json_decode(file_get_contents('php://input'), true);
$response = $server->handleRequest($request);
header('Content-Type: application/json');
echo json_encode($response);
use A2A\Models\Task;
use A2A\Models\TaskStatus;
use A2A\Enums\TaskState;
// Create task with custom ID
$taskId = 'user-request-' . uniqid();
$task = new Task($taskId, 'Process user data');
// Update task status
$task->setStatus(new TaskStatus(TaskState::WORKING));
$task->addToHistory('Started processing user data');
// Add artifacts
$task->addArtifact('processed_data.json', ['user_id' => 123, 'status' => 'active']);
// Mark as completed
$task->setStatus(new TaskStatus(TaskState::COMPLETED, final: true));
use A2A\Streaming\StreamingClient;
use A2A\Streaming\StreamingServer;
use A2A\Events\TaskStatusUpdateEvent;
// Client: Subscribe to task updates
$streamingClient = new StreamingClient('https://agent.com/stream');
$streamingClient->subscribeToTask($taskId, function($event) {
if ($event instanceof TaskStatusUpdateEvent) {
echo "Task {$event->getTaskId()} status: {$event->getStatus()->getState()}\n";
}
});
// Server: Publish task updates
$streamingServer = new StreamingServer($eventBus);
$streamingServer->start(); // Starts SSE endpoint
// Publish status update
$event = new TaskStatusUpdateEvent($taskId, $contextId, new TaskStatus(TaskState::COMPLETED));
$eventBus->publish($event);
// Set push notification config
$config = [
'webhook_url' => 'https://my-app.com/webhooks/a2a',
'events' => ['task.completed', 'task.failed'],
'secret' => 'webhook-secret-key'
];
$client->setPushNotificationConfig($taskId, $config);
// Get notification config
$config = $client->getPushNotificationConfig($taskId);
// List all configs
$configs = $client->listPushNotificationConfigs();
// Delete config
$client->deletePushNotificationConfig($taskId);
The implementation is fully tested against the official A2A Test Compatibility Kit:
# 1. Start the A2A server
php -S localhost:8081 examples/complete_a2a_server.php
# 2. Run compliance tests in another terminal
cd ../a2a-tck
# Test all categories
python3 run_tck.py --sut-url http://localhost:8081 --category all
# Test specific categories
python3 run_tck.py --sut-url http://localhost:8081 --category mandatory
python3 run_tck.py --sut-url http://localhost:8081 --category capabilities
python3 run_tck.py --sut-url http://localhost:8081 --category quality
python3 run_tck.py --sut-url http://localhost:8081 --category features
β
Mandatory Tests: 25/25 PASSED
β
Capability Tests: 14/14 PASSED
β
Quality Tests: 12/12 PASSED
β
Feature Tests: 15/15 PASSED
π Total: 70/70 tests passing (100% compliance)
# Run all tests
composer test
# Run with coverage
composer test -- --coverage-html coverage/
# Run specific test suites
./vendor/bin/phpunit tests/A2AServerTest.php
./vendor/bin/phpunit tests/ComprehensiveTest.php
./vendor/bin/phpunit tests/EndToEndTest.php
# PHPStan analysis (Level 8)
composer analyse
# Code style checking
composer style
# Fix code style automatically
composer style -- --fix
All A2A Protocol v0.2.5 methods are implemented:
- β
message/send
- Send messages with text, file, or data parts - β
message/stream
- Server-sent events for real-time updates - β
tasks/get
- Retrieve task status and history - β
tasks/cancel
- Cancel running tasks
- β
tasks/pushNotificationConfig/set
- Configure webhooks - β
tasks/pushNotificationConfig/get
- Retrieve configs - β
tasks/pushNotificationConfig/list
- List all configs - β
tasks/pushNotificationConfig/delete
- Remove configs
- β
tasks/resubscribe
- Reconnect to task streams - β
Agent discovery - Via
/.well-known/agent.json
- β Health checks - Standard ping/pong
- β AgentCard: All fields including extensions and interfaces
- β Message: All part types (text, file, data) with metadata
- β Task: Complete lifecycle with status, history, artifacts
- β Parts: TextPart, FilePart, DataPart, FileWithBytes, FileWithUri
- β Events: TaskStatusUpdate, TaskArtifactUpdate, custom events
- β JSON-RPC 2.0: Complete transport with error codes
- β Error Handling: All A2A-specific error codes implemented
- β Validation: Input validation for all message types
- β State Management: Proper task state transitions
- β Context Management: Task and message context handling
- β Streaming: Server-Sent Events with reconnection
- β Event System: Event bus with subscription management
- β Agent Execution: Framework for complex workflows
- β Extension System: Agent extensions and interfaces
- β Transport Security: HTTPS support with proper headers
- β Authentication: Framework for auth schemes
Complete JSON-RPC 2.0 error handling with A2A-specific codes:
use A2A\Exceptions\A2AException;
use A2A\Exceptions\TaskNotFoundException;
use A2A\Exceptions\InvalidRequestException;
try {
$task = $client->getTask('nonexistent-task');
} catch (TaskNotFoundException $e) {
// A2A Error Code: -32001
echo "Task not found: " . $e->getMessage();
} catch (InvalidRequestException $e) {
// JSON-RPC Error Code: -32600
echo "Invalid request: " . $e->getMessage();
} catch (A2AException $e) {
// General A2A protocol error
echo "A2A Error: " . $e->getMessage();
}
- -32700: Parse error (malformed JSON)
- -32600: Invalid request (missing required fields)
- -32601: Method not found
- -32602: Invalid params
- -32603: Internal error
- -32001: Task not found (A2A-specific)
- -32002: Agent not available (A2A-specific)
- -32003: Capability not supported (A2A-specific)
The /examples
directory contains comprehensive working examples:
basic_agent.php
- Simple agent implementationclient_server.php
- Client-server communicationcomplete_agent_communication.php
- Full agent-to-agent flow
complete_a2a_server.php
- Production-ready server (TCK compliant)streaming_example.php
- Real-time streaming and eventstask_management.php
- Task lifecycle managementenhanced_features.php
- Push notifications and extensions
protocol_compliance.php
- A2A protocol compliance demospecification_compliance.php
- Spec compliance verificationimplementation_status.php
- Feature status checker
# Start the complete server (recommended)
php -S localhost:8081 examples/complete_a2a_server.php
# Test agent communication
php examples/complete_agent_communication.php
# Check implementation status
php examples/implementation_status.php
- PHP 8.0+ with extensions:
curl
,json
,mbstring
- Composer for dependency management
- Web Server: nginx, Apache, or PHP built-in server
- Memory: 512MB+ recommended for production
FROM php:8.2-fpm-alpine
RUN apk add --no-cache git curl
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
WORKDIR /app
COPY composer.json composer.lock ./
RUN composer install --no-dev --optimize-autoloader
COPY . .
EXPOSE 8080
CMD ["php", "-S", "0.0.0.0:8080", "complete_a2a_server.php"]
server {
listen 80;
server_name your-a2a-agent.com;
root /var/www/a2a-php;
location / {
try_files $uri $uri/ @php;
}
location @php {
fastcgi_pass php-fpm:9000;
fastcgi_index complete_a2a_server.php;
fastcgi_param SCRIPT_FILENAME $document_root/examples/complete_a2a_server.php;
include fastcgi_params;
}
# Agent card endpoint
location /.well-known/agent.json {
add_header Content-Type application/json;
add_header Access-Control-Allow-Origin *;
}
}
- A2A Protocol Specification - Official protocol docs
- Test Compatibility Kit - Compliance testing
- PHP SDK API Docs - Complete API reference
- A2A Project - Main organization
- Discussions - Community forum
- Issues - Bug reports and features
- a2a-python - Python implementation
- a2a-js - JavaScript/TypeScript implementation
- a2a-samples - Example implementations
Please see contributing guidelines:
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature
) - Add tests for new functionality
- Ensure all tests pass (
composer test
) - Run static analysis (
composer analyse
) - Check code style (
composer style
) - Commit your changes (
git commit -m 'Add amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
git clone https://github.com/andreibesleaga/a2a-php.git
cd a2a-php
composer install
composer test
This project is licensed under the Apache License 2.0 - see the LICENSE file for details.