Skip to content

fix(a2a): report agent reachability from observed calls - #7751

Open
marekdano wants to merge 3 commits into
mainfrom
7603-a2a-agent-reachable
Open

marekdano wants to merge 3 commits into
mainfrom
7603-a2a-agent-reachable

Conversation

@marekdano

Copy link
Copy Markdown
Collaborator

Closes #7603

Summary

reachable on an A2A agent was true by default and no code changed it. Clients showed every agent as reachable.

The gateway now records an observation each time it calls an agent:

Outcome of the call reachable
Any HTTP response, including a non-200 or a JSON-RPC error true
Transport failure false
Request blocked by the outbound URL policy unchanged

reachable is now nullable. null means no observation. A disabled agent and an agent nobody has called both report null.

A new nullable last_seen field records when the gateway made the observation. It is on a2a_agents and A2AAgentRead.

Changes

  • db.py, schemas.py: nullable reachable, new last_seen.
  • a2a_service.py: invoke_agent writes both fields in the existing post-call write. set_agent_state clears both fields on deactivation.
  • Migration c3e5a7b9d1f2 adds last_seen and sets reachable to null for existing agents. The old value came from a column default, not from an observation.
  • CHANGELOG.md entry.

Notes for reviewers

  • API clients that read reachable as a boolean must handle null.
  • The timed Agent Card check from the issue comment is out of scope, as the issue decided. Keep [BUG]: A2A agent not reachable #7603 open or link a follow-up if you want that tracked.
  • A reachable response does not prove that the agent works. A JSON-RPC error still counts as reachable and still counts as a failed invocation in metrics.

Testing

  • Unit tests cover each invoke outcome, the policy-block case, and the deactivation and activation behavior.
  • Migration upgrade, downgrade and upgrade round trip checked on SQLite.

@vishu-bh vishu-bh left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Recording reachability from actual calls makes sense, and the new timestamp gives clients useful context. There are four gaps to address before merging: MCP tool calls bypass the observation writes, the Admin UI treats unknown status as a failure, local serialization errors mark agents unreachable, and downgrade leaves the column nullable.

The focused service tests pass. Please also add a live gateway regression covering observations through both the A2A API and MCP tool calls, as required by AGENTS.md.

db_agent = get_for_update(ts_db, DbA2AAgent, agent_id)
if db_agent and getattr(db_agent, "enabled", False):
db_agent.last_interaction = end_time
if reachable_observation is not None:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P1] Please record observations for A2A tool calls as well.

This write only runs through invoke_agent(). The MCP A2A branch in ToolService.invoke_tool() sends HTTP requests directly at tool_service.py:7276, and _call_a2a_agent() does the same at line 8952. Neither path updates these fields. A successful tool call can therefore leave a new agent unobserved or preserve an old unreachable status.

The helper reproduction returned HTTP 200 after one outbound call, while reachable and last_seen stayed None. Please share the observation writer across these paths and cover tool-call success and transport failure.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed! The A2A branch of ToolService.invoke_tool() and _call_a2a_agent() now use the same writer as invoke_agent(). The writer is record_a2a_reachability() in a2a_reachability.py.

Any HTTP response marks the agent reachable. A connection failure or a timeout marks it unreachable.

Tests: test_a2a_tool_call_records_reachability covers success, HTTP 500, connect failure, and both timeout types. A live test calls the agent through tools/call and checks reachable and lastSeen. The live test fails when I disable the writes in tool_service.py.

Comment thread mcpgateway/schemas.py
config: Dict[str, Any]
enabled: bool
reachable: bool
reachable: Optional[bool] = Field(default=None, description="Last observed reachability. None means the gateway has made no observation.")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Please give the new null state an explicit display in the Admin UI.

agents_partial.html:146 currently renders it as "Unreachable", while a2aAgents.js:145 renders it as "Offline". Since the migration resets every existing agent to null, users will see a failure before the gateway makes any observation. Rendering the table with reachable=None confirms this behavior.

Please show an "Unknown" or "Not checked" state in both views and add a regression test.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed! agents_partial.html and a2aAgents.js now show null as "Not checked". The table no longer shows "Unreachable", and the detail view no longer shows "Offline".

Tests: test_agents_partial_reachability_label renders None, True, and False. A new JS test checks that reachable: null shows "Not checked" and not "Offline".

Comment thread mcpgateway/services/a2a_service.py Outdated
try:
async with get_isolated_http_client(follow_redirects=False) as client:
http_response = await client.post(pinned.endpoint_url, json=prepared.request_data, headers=pinned.headers, extensions=pinned.extensions)
except Exception:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Please limit this handler to transport failures.

HTTPX can also fail locally before it sends a request. Passing a NaN parameter raises a serialization ValueError; the reproduction sends zero requests but still changes reachable to False and advances last_seen. That records an observation about an agent the gateway never contacted.

Please preserve the previous observation when local request construction or serialization fails, and add coverage for that case.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed! is_transport_failure() accepts only httpx.TransportError and timeouts. A request that fails before the gateway sends it, such as the NaN serialization error, now keeps the previous reachable and last_seen.

Tests: test_invoke_records_reachability has a local-serialization-error case. It checks that neither field changes. The same rule applies to the tool-call paths.

if "last_seen" not in [column["name"] for column in inspector.get_columns("a2a_agents")]:
return
op.execute(sa.text("UPDATE a2a_agents SET reachable = :value WHERE reachable IS NULL").bindparams(value=True))
with op.batch_alter_table("a2a_agents") as batch_op:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Please restore the original NOT NULL constraint during downgrade.

The upgrade makes reachable nullable, but the downgrade only replaces existing null values and removes last_seen. After a SQLite upgrade/downgrade round trip, the column remains nullable and accepts a new NULL value. The original schema rejects that value.

Please set nullable=False in this batch after clearing the nulls, and assert the restored constraint in the round-trip test.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed! The downgrade now sets nullable=False on reachable after it replaces the null values.

Tests: test_downgrade_restores_the_not_null_constraint runs upgrade then downgrade on SQLite. It asserts that the column is NOT NULL and that inserting NULL raises IntegrityError.

@marekdano marekdano assigned vishu-bh and unassigned marekdano Dec 1, 2026
@marekdano
marekdano requested a review from vishu-bh December 1, 2026 14:51

@vishu-bh vishu-bh left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The MCP observations, unknown UI state, and downgrade constraint now have fixes and regression coverage. The new live gateway tests also pass. Two items remain: local protocol errors still change reachability, and the migration needs to follow the current main branch head. Details are inline.

>>> is_transport_failure(ValueError("Out of range float values are not JSON compliant"))
False
"""
return isinstance(error, (httpx.TransportError, asyncio.TimeoutError))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Local protocol errors still change reachability

The NaN case is fixed, but this check still includes httpx.LocalProtocolError because it inherits from TransportError. HTTPX raises it when a configured header contains CRLF, before sending any HTTP request bytes. I reproduced that with the actual invocation: zero request bytes were written, but reachable changed from True to False and last_seen advanced.

Please exclude local protocol errors from these observations and add a regression test. The direct httpx.TransportError catch in tool_service.py:7332 needs the same treatment.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed!
is_transport_failure() now returns false for httpx.LocalProtocolError, so a malformed header no longer changes reachable or last_seen. It also returns false for httpx.UnsupportedProtocol, because HTTPX raises that error before it sends a request.

The direct httpx.TransportError catch in tool_service.py now calls is_transport_failure() before it records an observation. All three paths follow the same rule: the A2A API, the MCP tool call, and _call_a2a_agent().

Tests:

  • test_is_transport_failure covers LocalProtocolError and UnsupportedProtocol.
  • test_invoke_records_reachability has a local-protocol-error case. It checks that neither field changes.
  • test_a2a_tool_call_records_reachability has the same case for the tool-call path. It checks that no observation is recorded.

import sqlalchemy as sa

revision: str = "c3e5a7b9d1f2" # pragma: allowlist secret
down_revision: Union[str, Sequence[str], None] = "b9d2f4a6c8e1" # pragma: allowlist secret

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P1] Update the migration parent after syncing with main

Main now includes c4e8a1d7f2b9, which also follows b9d2f4a6c8e1. Combining the two migration sets produces two Alembic heads, so resolving the code conflicts alone will not make the combined branch ready.

Please make this migration follow the current main head and update the expected parent in its test. After resolving the merge conflicts, verify that alembic heads reports one head and that the upgrade still passes.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I rebased on main and resolved the conflicts in CHANGELOG.md, db.py, and schemas.py. The migration c3e5a7b9d1f2 now follows c4e8a1d7f2b9, and the expected parent in test_a2a_agents_last_seen_migration.py matches. alembic reports one head. The migration tests pass, including the upgrade, the idempotent second run, and the downgrade round trip.

The model and the read schema now carry reachable and last_seen next to drifted and unverified from #7663.

  The reachable field of an A2A agent was True by default and no code
  changed it, so clients showed every agent as reachable.

  Record an observation each time the gateway calls an agent:
  - Any HTTP response marks the agent reachable, including a non-200
    and a JSON-RPC error. The success flag is not used.
  - A transport failure marks the agent unreachable.
  - A request that the outbound URL policy blocks changes nothing.

  Add a nullable last_seen field so clients can tell how old the
  observation is. Make reachable nullable. Null means no observation.
  Disabling an agent sets both fields to null.

  Migration c3e5a7b9d1f2 adds last_seen and sets reachable to null for
  existing agents, because the old default was never an observation.

Signed-off-by: Marek Dano <mk.dano@gmail.com>
  Address review feedback on the reachability change.

  Record observations for MCP tool calls. The A2A branch of
  ToolService.invoke_tool() and _call_a2a_agent() now use the same
  writer as invoke_agent(). The writer is in a2a_reachability.py.

  Mark an agent unreachable only for a connection failure or a timeout.
  A request that fails before the gateway sends it, such as a
  serialization error, keeps the previous observation.

  Show a null reachable value as "Not checked" in the Admin UI table
  and the agent detail view. The table and the detail view showed it as
  a failure.

  Restore the NOT NULL constraint on reachable in the downgrade.

  Add a live gateway test for observations through the A2A API and MCP
  tool calls.

Signed-off-by: Marek Dano <mk.dano@gmail.com>
  httpx raises LocalProtocolError, for example for a header with CRLF,
  before it sends any request bytes. Recording it marked the agent
  unreachable although the gateway never contacted the agent.

  Treat LocalProtocolError and UnsupportedProtocol as local failures in
  is_transport_failure(). Apply the same check at the direct
  httpx.TransportError catch in the MCP tool-call path.

  Make the last_seen migration follow c4e8a1d7f2b9, the current head on
  main, so Alembic reports one head.

  Closes #7603

Signed-off-by: Marek Dano <mk.dano@gmail.com>
@marekdano
marekdano force-pushed the 7603-a2a-agent-reachable branch from 4358a64 to 1c64117 Compare December 4, 2026 09:50

@vishu-bh vishu-bh left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 🚀

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG]: A2A agent not reachable

2 participants