A2A Client

medium10 mina2a-client.ts

Your agent should delegate to a remote A2A agent as if it were a local tool.

What it shows

  • Read the agent card and send a message with A2AClient
  • Turn the remote agent into a tool with asTool()

The code

a2a-client.ts
import { A2AClient } from '@cogitator-ai/a2a';
import { Agent, Cogitator } from '@cogitator-ai/core';

const apiKey = process.env.GOOGLE_API_KEY;
if (!apiKey) throw new Error('Set GOOGLE_API_KEY');

const client = new A2AClient('http://localhost:3100');

const card = await client.agentCard();
console.log(`${card.name}: ${card.description} (streaming: ${card.capabilities.streaming})`);

const task = await client.sendMessage({
  role: 'user',
  parts: [{ type: 'text', text: 'Make this concise: "In my personal opinion, I think it is very cold today."' }],
});
console.log('Task', task.id, task.status.state);
const reply = task.artifacts[0]?.parts.find((part) => part.type === 'text');
if (reply?.type === 'text') console.log(reply.text);

const fetched = await client.getTask(task.id);
console.log('History length:', fetched.history.length);

const orchestrator = new Agent({
  name: 'orchestrator',
  model: 'google/gemini-3.5-flash-lite',
  instructions: 'Delegate writing tasks to the writing_assistant tool. Forward the request as-is.',
  tools: [
    client.asTool({
      name: 'writing_assistant',
      description: 'Remote writing assistant reachable over A2A',
    }),
  ],
  maxIterations: 3,
});

const cog = new Cogitator({ llm: { providers: { google: { apiKey } } } });
const result = await cog.run(orchestrator, {
  input: 'Fix the grammar: "Me and him goes to the store yesterday for buy some foods."',
});
console.log(result.output);

await cog.close();

Run it

install · bash
pnpm add @cogitator-ai/a2a @cogitator-ai/core
EnvironmentGOOGLE_API_KEY
run · bash
GOOGLE_API_KEY=your-key npx tsx a2a-client.ts

Or run the full example from a clone of the repository (keys in .env):

repository · bash
npx tsx examples/a2a/02-a2a-client.ts

Start the A2A server recipe first; the client talks to http://localhost:3100.

Learn more