// =============================================================================
// SERVICE REGISTRY (A2A Discovery)
// =============================================================================
// Central registry where agents register and discover each other.
//
// HOW IT WORKS:
// 1. Agent starts up → POST /register with its AgentCard
// 2. Other agents need a capability → GET /agents?capability=xxx
// 3. Registry returns matching agents with their endpoints
//
// USED BY:
// - Food agents (DoorDash, UberEats, Grubhub) - A2A
// - Stripe agent - AP2
// - MCP server - to discover all agents
// =============================================================================
import type { AgentCard } from '../../src/types.js';
import { createServer } from 'http';
// In-memory agent registry (real world: database)
const agents: Map<string, AgentCard> = new Map();
// =============================================================================
// HTTP SERVER
// =============================================================================
createServer((req, res) => {
const url = new URL(req.url || '/', `http://${req.headers.host}`);
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
if (req.method === 'OPTIONS') {
res.writeHead(200);
res.end();
return;
}
// Register an agent
if (req.method === 'POST' && url.pathname === '/register') {
let body = '';
req.on('data', chunk => body += chunk);
req.on('end', () => {
const card: AgentCard = JSON.parse(body);
agents.set(card.agentId, card);
console.log(`📝 Registered: ${card.name}`);
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ success: true }));
});
return;
}
// Get agents (optionally filtered by capability)
if (req.method === 'GET' && url.pathname === '/agents') {
const capability = url.searchParams.get('capability');
let results = Array.from(agents.values());
if (capability) results = results.filter(a => a.capabilities.includes(capability));
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(results));
return;
}
res.writeHead(404);
res.end();
}).listen(8004, () => console.log('📋 Registry running on port 8004'));