Skip to content
mentoor

Topic 4 of 5 · 6 min read

Message contracts and agent-to-agent protocols

Goal: Define the message schema two agents exchange and check a sample message against it for missing or ambiguous fields.

When agents send each other free text, the receiver has to guess; a message contract with named fields, allowed values and task states lets the receiver check before it acts. The contract is the same idea as an API schema.

Check yourself · 1 of 3

What is the main job of an agent-to-agent message schema?

Choose one answer

Why it matters

An orchestrator tells a worker "update the report, it is urgent". Which report? Urgent compared to what? The worker guesses, and the guess is hard to see later. A message with task_id, target, priority and deadline leaves nothing to guess, and a receiver can reject an invalid one instead of acting on it.

What a contract contains

  • Identity: task_id, from, to, so replies match requests.
  • Type: what kind of message it is (request, result, error, question).
  • Payload fields: names, types, which are required, and allowed values as enums.
  • State: where the task is (submitted, working, input-required, completed, failed, canceled). A2A defines task states like these.
  • Failure shape: an error with a code and whether retry is safe.
  • Version: a schema_version so changes do not break old agents.

Write it as JSON Schema, then validate every incoming message before the receiver acts. Reject, and reply with what was wrong.

Two protocols to know

MCP (Model Context Protocol) connects an agent to tools and data: a database, a file system, a ticket system. A2A (Agent2Agent) connects agents to each other, including agents from different vendors: an agent publishes a card describing what it can do, and others send it tasks and read their state. They solve different problems and are used together: MCP below the agent, A2A between agents.

Before and after

Weak message:

{"task": "fix the bug in checkout, high priority", "files": "checkout stuff"}

Better:

{
  "schema_version": "1.0",
  "task_id": "T-114",
  "type": "request",
  "from": "orchestrator",
  "to": "worker-2",
  "goal": "Fix the rounding error in cart totals",
  "files": ["src/checkout/total.ts"],
  "priority": "high",
  "deadline": "2026-12-03T12:00:00Z",
  "reply_state": ["completed", "failed", "input-required"]
}

What goes wrong

  • Free text in a typed slot. A priority of "kind of urgent" cannot be sorted or checked.
  • Missing required fields. A message with no task_id cannot be matched to its reply.
  • Ambiguous values. deadline: "tomorrow" has no time zone.
  • No error shape. The worker fails and returns prose, so the orchestrator cannot tell failure from success.

Examples

Catching a missing field

{"type": "result", "status": "completed", "summary": "Totals fixed"}

The schema requires task_id on every result. This message has none, so the orchestrator cannot match it to a request. The receiver replies with an error that names the missing field instead of guessing.

Writing the schema

{
  "type": "object",
  "required": ["task_id", "type", "priority"],
  "properties": {
    "task_id": {"type": "string"},
    "type": {"enum": ["request", "result", "error"]},
    "priority": {"enum": ["low", "normal", "high"]}
  },
  "additionalProperties": false
}

additionalProperties: false rejects misspelled fields such as priorty.

Key points

  • A message contract names fields, types, required fields and allowed values so the receiver does not guess.
  • Validate every incoming message against the schema and reject invalid ones with the reason.
  • Task states such as submitted, working, input-required, completed and failed make progress and blocking visible.
  • Define an error shape that says whether retry is safe.
  • MCP connects an agent to tools; A2A connects agents to each other. They are used together.

Common mistakes

  • Letting agents exchange free-text messages for work that needs exact values.
  • Skipping validation because the sender is another model that usually follows the format.
  • Using vague values such as "soon" or "tomorrow" for dates and priorities.
  • Mixing up MCP and A2A and expecting a tool protocol to route tasks between agents.

Check yourself · 2 of 3

Which pair describes MCP and A2A correctly?

Choose one answer

Check yourself · 3 of 3

A worker sends back {"type":"result","summary":"done"}. The schema requires task_id and status. What should the orchestrator do?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic