← AI Agents Open Protocols
🤖 AI Agents

A2A Dynamic Agent Discovery at Runtime

Source: mortalapps.com
TL;DR
  • A2A dynamic agent discovery is the process of resolving specialist agent endpoints at runtime based on semantic capability matching rather than static routing.
  • It solves the brittleness of hardcoded agent addresses in decentralized systems, allowing supervisors to find the best available specialist for a specific task context.
  • In production, it enables elastic scaling of agentic microservices and decoupled development cycles between different agent teams.
  • The outcome is a resilient supervisor-specialist network that adapts to environment changes and agent availability without manual configuration updates.
  • Tradeoff: Introduces discovery latency and requires robust fallback logic for when no suitable specialist is resolved.

Why This Matters

In complex multi-agent ecosystems, a2a dynamic agent discovery is the mechanism that prevents architectural stagnation. Traditional software systems rely on service discovery (like Consul or Kubernetes DNS) to resolve IP addresses for known service names. However, agentic systems require a higher level of abstraction: resolving capabilities. When a supervisor agent encounters a task, it may not know which specialist is best suited for the specific nuances of the request. Hardcoding these relationships creates a 'distributed monolith' where updating one agent requires reconfiguring the entire network. This approach was invented to mirror human organizational structures, where a manager identifies a specialist based on their current skills and availability rather than a fixed list created months prior.

If ignored in production, systems become fragile. If a specialist agent is redeployed with a new schema or a different endpoint, the supervisor will fail. Furthermore, without dynamic discovery, you cannot easily perform A/B testing on different specialist versions or route tasks based on real-time cost and performance metrics. A2A dynamic agent discovery allows for 'just-in-time' binding, which is essential for enterprise-grade AI where agents are frequently updated, retired, or specialized. You should use this when your system exceeds three distinct agents or when agents are managed by different teams. Hardcoded routing is only acceptable for simple, static pipelines where the logic is unlikely to change and low-latency execution is the absolute priority over flexibility.

Core Concepts

To implement runtime discovery, engineers must master several architectural primitives that bridge the gap between networking and semantics.

  • Agent Card (Manifest): A machine-readable JSON/YAML document describing an agent's capabilities, supported tools, input schemas, and cost metrics.
  • Capability Registry: A centralized or distributed database (often a vector store) where agent cards are indexed for search.
  • Semantic Resolution: The process of using an LLM or embedding model to match a natural language task description against the capabilities listed in the registry.
  • Discovery TTL (Time-to-Live): The duration for which a supervisor caches a resolved agent endpoint before re-querying the registry.
  • Contextual Matching: A resolution strategy that considers the current conversation state, user permissions, and environmental variables when selecting an agent.
  • Handshake Protocol: The initial exchange between supervisor and specialist to confirm version compatibility and resource availability after discovery.

How It Works

The discovery lifecycle is triggered when a supervisor determines it cannot fulfill a request using its internal tools. This sequence must be completed in milliseconds to maintain a responsive user experience.

1. Intent Extraction and Query Formulation

The supervisor analyzes the current task and extracts key requirements: the required domain (e.g., 'legal', 'finance'), the necessary tools (e.g., 'sql-query', 'pdf-parse'), and constraints (e.g., 'latency < 500ms'). This is transformed into a discovery query.

2. Registry Lookup and Semantic Filtering

The query is sent to the Capability Registry. The registry performs a two-stage search:

  1. Hard Filtering: Removes agents that do not meet strict requirements (e.g., wrong protocol version or insufficient security clearance).
  2. Semantic Ranking: Uses vector similarity to rank the remaining agents based on how well their 'Agent Card' descriptions match the task intent.

3. Health and Policy Validation

The registry or supervisor checks the real-time health of the top-ranked candidates. This includes checking if the agent is online and if the current user has the RBAC permissions required to invoke that specific agent.

4. Resolution and Binding

The supervisor receives a list of candidate endpoints. It selects the optimal candidate (often the highest-ranked healthy agent) and 'binds' to it, establishing a session. The endpoint URL and metadata are cached locally based on the TTL.

5. Failure and Fallback Handling

If no agents match the criteria, or if the selected agent fails the initial handshake, the supervisor enters fallback mode. This may involve broadening the search criteria, escalating to a human-in-the-loop, or returning a graceful degradation message to the user.

Architecture

The architecture consists of four primary components interacting over a secure network. The Supervisor Agent acts as the requester and orchestrator. It initiates the flow by sending a query to the Discovery Registry, which is a specialized service containing the Agent Card Store (metadata) and a Vector Index (for semantic search). The Specialist Agents are the providers; they periodically push updates to the Registry to keep their status and capabilities current.

Data flows start at the Supervisor, which sends a 'ResolveRequest' to the Registry. The Registry returns a 'ResolutionResponse' containing a list of Agent URIs and their associated metadata. The Supervisor then initiates a direct 'A2A Handshake' with the chosen Specialist. Execution occurs directly between the Supervisor and Specialist to minimize latency, with the Registry only involved in the initial resolution phase. If a Specialist becomes unavailable, the Supervisor detects the failure and re-queries the Registry for an alternative.

The Discovery Resolution Sequence

In a production A2A environment, the resolution sequence must be deterministic yet flexible. The supervisor does not simply 'ask an LLM' who to call; it follows a multi-stage pipeline. First, the Constraint Solver filters the registry for agents that support the specific A2A protocol version (e.g., v1.2) and the required security headers. Second, the Semantic Matcher calculates the cosine similarity between the task embedding and the agent's capability embeddings.

Third, a Heuristic Scorer applies weights to non-semantic factors. For example, an agent with a lower 'Cost-Per-Token' or 'P99 Latency' might be preferred over one with a slightly higher semantic match. The final score is a weighted sum: Score = (α * SemanticSim) + (β * ReliabilityScore) - (γ * Cost). This ensures that the system optimizes for business value, not just linguistic similarity.

Semantic Capability Filtering

Traditional keyword matching fails in agent discovery because 'financial analysis' and 'stock market reporting' are semantically similar but might require different tools. We implement semantic filtering by embedding the capabilities array of the Agent Card into a high-dimensional space.

When a supervisor queries the registry, it generates an embedding of the 'Task Intent'. The registry performs a K-Nearest Neighbors (KNN) search. To prevent 'hallucinated' matches, we apply a similarity threshold (e.g., 0.85). If the top match is below this threshold, the registry returns an empty set, signaling the supervisor to use a generalist fallback or ask the user for clarification. This prevents the supervisor from routing a medical query to a weather agent simply because it was the 'closest' available option.

Runtime Context Injection

Discovery is not just about what an agent *can* do, but what it *should* do in the current context. A2A discovery supports 'Contextual Constraints'. For example, if a user is in the 'EU' region, the supervisor may add a constraint to the discovery query: region == 'EU'. The registry then filters for agents whose Agent Cards declare compliance with EU data residency.

This injection happens at the 'Query Formulation' stage. The supervisor merges the static task requirements with dynamic environmental variables. This allows the same supervisor code to resolve different specialists depending on whether it is running in a production, staging, or regional environment, without any changes to its core logic.

Security Handshakes and Identity Verification

Discovery introduces a significant attack surface: 'Agent Spoofing'. A malicious agent could register itself with the capability 'Process Payments' to intercept sensitive data. To mitigate this, the A2A protocol requires a Post-Discovery Handshake.

Once the supervisor receives an endpoint from the registry, it sends a 'Challenge' message. The specialist must respond with a verifiable credential (e.g., a JWT signed by a trusted Identity Provider like Entra ID) that matches the identity_provider field in its Agent Card. The supervisor validates this signature before sending any task data. If the signature is invalid, the supervisor reports the endpoint to the registry as 'compromised' and attempts to resolve a different agent.

Fallback and Graceful Degradation Logic

Failure is a first-class citizen in dynamic discovery. There are three primary failure modes: 'Registry Unavailable', 'No Match Found', and 'Selected Agent Unresponsive'.

  1. Registry Unavailable: Supervisors should maintain a 'Local Cache' of recently resolved agents. If the registry is down, the supervisor falls back to the cache. If the cache is empty, it uses a 'Default Specialist' (a hardcoded generalist LLM).
  2. No Match Found: The supervisor can attempt 'Query Relaxation'. It removes the most restrictive constraints (e.g., lowering the similarity threshold or removing specific tool requirements) and retries the lookup.
  3. Selected Agent Unresponsive: The supervisor implements a 'Circuit Breaker'. If an agent fails to respond within the timeout, it is marked as 'Down' in the local cache, and the supervisor immediately resolves the next best candidate from the registry's original response list.

Code Example

A supervisor-side discovery client that resolves a specialist based on task intent and environment region.
Python
import os
import requests
from typing import Optional, Dict, Any

REGISTRY_URL = os.environ.get("AGENT_REGISTRY_URL", "https://registry.internal.corp")
API_KEY = os.environ.get("REGISTRY_API_KEY")

def resolve_specialist(intent: str, region: str) -> Optional[Dict[str, Any]]:
    """
    Resolves a specialist agent via semantic lookup with regional constraints.
    """
    payload = {
        "query": intent,
        "constraints": {
            "region": region,
            "min_protocol_version": "1.2"
        },
        "limit": 1
    }
    
    try:
        response = requests.post(
            f"{REGISTRY_URL}/v1/discover",
            json=payload,
            headers={"Authorization": f"Bearer {API_KEY}"},
            timeout=2.0 # Discovery must be fast
        )
        response.raise_for_status()
        results = response.json().get("agents", [])
        
        if not results:
            print(f"No agent found for intent: {intent}")
            return None
            
        return results[0]
    except Exception as e:
        # Log error and trigger fallback logic
        print(f"Discovery failed: {e}")
        return None

# Example usage
task = "Analyze the Q3 tax implications for our Dublin office."
agent = resolve_specialist(task, region="EU")
if agent:
    print(f"Resolved to: {agent['name']} at {agent['endpoint']}")
Expected Output
Resolved to: TaxSpecialist-EU at https://tax-agent.eu-west-1.aws.internal

Key Takeaways

Dynamic discovery decouples supervisors from specialists, enabling independent scaling and updates.
The process relies on 'Agent Cards' which provide the semantic and technical metadata for resolution.
Semantic matching must be combined with hard constraints (version, region, security) for production reliability.
Caching and circuit breakers are essential to mitigate the latency and availability risks of a central registry.
Security is maintained through post-discovery handshakes and identity verification using signed credentials.
Enterprise governance is enforced by filtering discovery results based on RBAC and data residency tags.

Frequently Asked Questions

What is the difference between discovery and routing? +
Discovery is the process of finding *where* an agent is and *what* it can do. Routing is the actual act of sending the message to that endpoint. Discovery happens once (or periodically), while routing happens for every message.
What happens if the discovery registry is down? +
The supervisor should fall back to a local cache of previously discovered agents. If the cache is empty, it should use a pre-configured 'Default Specialist' or return a service-unavailable error.
How do I handle versioning in dynamic discovery? +
Include a `min_protocol_version` in your discovery query. The registry will filter out any agents that do not support that version, preventing runtime serialization errors.
Can I discover agents across different cloud providers? +
Yes, as long as both agents follow the A2A protocol and the registry has visibility into both environments. This is a key use case for cross-platform agentic workflows.
How does discovery work with sensitive data? +
The registry itself only stores metadata (Agent Cards), not sensitive data. The supervisor uses discovery to find a compliant agent, and the actual sensitive data is only sent during the secure A2A session.
Is semantic search better than keyword search for discovery? +
Yes, because it understands intent. A query for 'money transfer' can match an agent with 'payment processing' capabilities, which keyword search would miss.
What is a typical TTL for a discovered agent? +
In most production systems, a TTL of 5 to 15 minutes is recommended. This provides a good balance between reducing registry load and reacting to agent deployments.
How do I prevent 'Agent Spoofing' in discovery? +
The supervisor must perform a cryptographic handshake with the discovered agent. The agent must prove its identity using a certificate or token that matches its registry entry.