Skip to content

perf(agentgateway): parallelize MCP fragment and A2A agent card fetching - #334

Open
cassiofariasmachado wants to merge 6 commits into
mainfrom
perf/parallelize-mcp-list-tools-requests
Open

cassiofariasmachado wants to merge 6 commits into
mainfrom
perf/parallelize-mcp-list-tools-requests

Conversation

@cassiofariasmachado

Copy link
Copy Markdown
Member

Disclaimer: Do not include SAP-internal or customer-specific information in this PR (e.g. internal system URLs, customer names, tenant IDs, or confidential configurations). This is a public repository.

Description

get_mcp_tools_lob and get_agent_cards_lob previously fetched each fragment sequentially with await inside a for loop. With N fragments each taking 3–4 s, total load time scaled as O(N × latency) — ~95 s for 22 fragments.

This PR replaces both sequential loops with asyncio.gather(return_exceptions=True), fetching all fragments concurrently. Wall-clock time drops to the latency of the single slowest fragment (~4 s regardless of fragment count).

Changes:

  • get_mcp_tools_lob: two-pass approach — validate URLs into a pending list up front, then asyncio.gather over all of them. Uses isinstance(result, BaseException) (not Exception) to correctly catch CancelledError and other BaseException subclasses returned by gather(return_exceptions=True). The final log now reports len(pending) (fragments that had a valid URL) rather than len(fragments) (all fragments including URL-less ones). Timing is logged: Loaded N MCP tool(s) from M fragment(s) in X.XXs.
  • get_agent_cards_lob: same pattern applied — was missed by prior art (issue Parallelize fragment connections inside list_mcp_tools so all fragments are handled concurrently #301 / PR perf(agentgateway): parallelize LoB MCP tool listing across fragments #322). pending_cards collects (fragment_name, url, ord_id) tuples; asyncio.gather fetches all concurrently; errors are isolated per-fragment.

Related Issue

Closes #301

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update
  • Code refactoring
  • Dependency update

Actually: performance improvement (non-breaking)

How to Test

python -m pytest tests/agentgateway/unit/test_lob.py -s --log-cli-level=INFO

New tests verify:

  1. All fragments are dispatched concurrently (test_fragments_fetched_concurrently) — uses asyncio.sleep to confirm all N tasks start before any finish
  2. Per-fragment failure isolation — one failing fragment does not abort the others
  3. Fragment count in log excludes URL-less fragments

Checklist

  • I have read the Contributing Guidelines
  • I have verified that my changes solve the issue
  • I have added/updated automated tests to cover my changes
  • All tests pass locally
  • I have verified that my code follows the Code Guidelines
  • I have updated documentation (if applicable)
  • I have added type hints for all public APIs
  • My code does not contain sensitive information (credentials, tokens, etc.)
  • I have followed Conventional Commits for commit messages

Additional Notes

PR #322 addressed get_mcp_tools_lob only. This PR also fixes get_agent_cards_lob with the same pattern, and adds a timing log line to make the improvement observable in production logs.

@cassiofariasmachado
cassiofariasmachado requested a review from a team as a code owner September 14, 2026 23:34
Comment thread src/sap_cloud_sdk/agentgateway/_lob.py
Comment thread src/sap_cloud_sdk/agentgateway/_lob.py
@NicoleMGomes

Copy link
Copy Markdown
Contributor

Missing user-guide and version update

@NicoleMGomes

Copy link
Copy Markdown
Contributor

Great work!!

WhatsApp Image 2026-08-12 at 16 28 59

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.

Parallelize fragment connections inside list_mcp_tools so all fragments are handled concurrently

2 participants