Agentic Sessions and Protocols ⏱ 9 min read 📁 examples/26_a2a.mjs

Lesson 11: Agent-to-Agent (A2A) Delegation

Enable distributed agent collaboration using Agent Cards, HTTP JSON-RPC delegation, origin pinning (SSRF protection), and end-to-end telemetry correlation.

1. Why This Feature Exists

Monolithic agent architectures do not scale. A2A protocol allows specialized agents (e.g. database query agent, code reviewer agent) to run as isolated services while appearing as local tools to orchestrators.

2. Key Symbols and Exports

loadA2ATool(agentCardUrl)
Fetches the remote agent card and wraps the remote agent as a local callable tool.
Agent Card Discovery
Standardized metadata defining remote agent capabilities, input schemas, and endpoints.
Origin Pinning (SSRF Guard)
Validates target URLs against whitelist patterns to prevent SSRF vulnerabilities.
Cross-Agent Telemetry
Propagates runId and parentRunId across HTTP boundaries.

3. Implementation Walkthrough

import {
    ProviderType,
    resolveProvider,
    initializeClient,
    loadCredentials,
    getDefaultModel
} from 'merci-sdk';
import { loadA2ATool } from 'merci-sdk/tools';

const PROVIDER = resolveProvider();
const { client } = await initializeClient({ provider: PROVIDER, options: loadCredentials({ provider: PROVIDER }) });

// Load remote agent as a tool from its agent card endpoint
const specialistTool = await loadA2ATool('http://127.0.0.1:4000/agent-card.json', {
    allowedOrigins: ['http://127.0.0.1:4000']
});

const session = client.session(getDefaultModel({ provider: PROVIDER }))
    .withTools([specialistTool] });

const result = await session.run('Delegate a security review of our auth router to the specialist agent.');
console.log('Orchestrator Result:', result);

4. Wire and Telemetry Mechanics

Fetches /agent-card.json via HTTP GET. When the orchestrator invokes the tool, an HTTP POST JSON-RPC request is sent to the target agent endpoint with correlated tracing headers.

5. Run the Example

Execute the runnable example directly from the repository root:

node examples/26_a2a.mjs

Read the full example: examples/26_a2a.mjs →

Expected Output: Spawns a background A2A server, registers the remote agent tool, executes remote delegation, and aggregates the final answer.