Skip to content

Commit c43118c

Browse files
committed
padronize get_agent to receive filter as an argument
1 parent dd6cc20 commit c43118c

4 files changed

Lines changed: 38 additions & 25 deletions

File tree

src/sap_cloud_sdk/agentgateway/_lob.py

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,13 @@
2727
list_mcp_fragments,
2828
list_a2a_fragments,
2929
)
30-
from sap_cloud_sdk.agentgateway._models import Agent, AgentCard, MCPTool, MCPToolFilter
30+
from sap_cloud_sdk.agentgateway._models import (
31+
Agent,
32+
AgentCard,
33+
AgentCardFilter,
34+
MCPTool,
35+
MCPToolFilter,
36+
)
3137
from sap_cloud_sdk.agentgateway._token_cache import _GatewayUrlCache, _TokenCache
3238
from sap_cloud_sdk.agentgateway.exceptions import (
3339
AgentGatewaySDKError,
@@ -499,8 +505,7 @@ async def get_agent_cards_lob(
499505
tenant_subdomain: str,
500506
system_token: str,
501507
timeout: float,
502-
agent_names: list[str] | None = None,
503-
ord_ids: list[str] | None = None,
508+
filter: AgentCardFilter | None = None,
504509
) -> list[Agent]:
505510
"""List A2A agents and their agent cards using LoB flow.
506511
@@ -517,15 +522,13 @@ async def get_agent_cards_lob(
517522
tenant_subdomain: Tenant subdomain for multi-tenant lookup.
518523
system_token: Pre-fetched raw system token for authentication.
519524
timeout: HTTP timeout in seconds.
520-
agent_names: Optional list of agent card names to include (matched
521-
against the `name` field in the fetched agent card JSON).
522-
Applied after fetching. If empty or None, all are included.
523-
ord_ids: Optional list of ORD IDs to include (extracted from URL).
524-
Applied before fetching. If empty or None, all are included.
525+
filter: Optional AgentCardFilter narrowing results by agent card name
526+
or ORD ID. If None or empty, all A2A fragments are included.
525527
526528
Returns:
527529
List of Agent objects, each containing ORD ID and fetched AgentCard.
528530
"""
531+
f = filter or AgentCardFilter()
529532
loop = asyncio.get_running_loop()
530533

531534
logger.info("Listing A2A fragments for tenant '%s'", tenant_subdomain)
@@ -538,13 +541,13 @@ async def get_agent_cards_lob(
538541
return []
539542

540543
# Pre-fetch filter: ORD ID is extractable from the URL without fetching the card
541-
if ord_ids:
542-
ord_ids_set = set(ord_ids)
544+
if f.ord_ids:
545+
ord_ids_set = set(f.ord_ids)
543546
fragments = [
544-
f
545-
for f in fragments
547+
fr
548+
for fr in fragments
546549
if _ord_id_from_url(
547-
{k.lower(): v for k, v in f.properties.items()}.get("url", "")
550+
{k.lower(): v for k, v in fr.properties.items()}.get("url", "")
548551
)
549552
in ord_ids_set
550553
]
@@ -583,8 +586,8 @@ async def get_agent_cards_lob(
583586
)
584587

585588
# Post-fetch filter: agent card name is only known after fetching
586-
if agent_names:
587-
agent_names_set = set(agent_names)
589+
if f.agent_names:
590+
agent_names_set = set(f.agent_names)
588591
agents = [a for a in agents if a.agent_card.raw.get("name") in agent_names_set]
589592

590593
logger.info(

src/sap_cloud_sdk/agentgateway/agw_client.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -445,13 +445,11 @@ async def list_agent_cards(
445445

446446
tenant = self._resolve_tenant_subdomain()
447447
auth = await self.get_system_auth()
448-
f = filter or AgentCardFilter()
449448
return await get_agent_cards_lob(
450449
tenant,
451450
auth.access_token,
452451
self._config.timeout,
453-
agent_names=f.agent_names or None,
454-
ord_ids=f.ord_ids or None,
452+
filter=filter,
455453
)
456454
except AgentGatewaySDKError:
457455
raise

tests/agentgateway/unit/test_agw_client.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -983,8 +983,7 @@ async def test_returns_agents_from_lob_flow(self):
983983
"my-tenant",
984984
"system-token",
985985
60.0,
986-
agent_names=None,
987-
ord_ids=None,
986+
filter=None,
988987
)
989988

990989
@pytest.mark.asyncio
@@ -1015,8 +1014,9 @@ async def test_passes_filter_arguments(self):
10151014
"my-tenant",
10161015
"token",
10171016
60.0,
1018-
agent_names=["Billing Agent"],
1019-
ord_ids=["sap.s4:agent:v1"],
1017+
filter=AgentCardFilter(
1018+
agent_names=["Billing Agent"], ord_ids=["sap.s4:agent:v1"]
1019+
),
10201020
)
10211021

10221022
@pytest.mark.asyncio

tests/agentgateway/unit/test_lob.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,13 @@
2424
_fetch_agent_card,
2525
call_mcp_tool_lob,
2626
)
27-
from sap_cloud_sdk.agentgateway._models import Agent, AgentCard, MCPTool, MCPToolFilter
27+
from sap_cloud_sdk.agentgateway._models import (
28+
Agent,
29+
AgentCard,
30+
AgentCardFilter,
31+
MCPTool,
32+
MCPToolFilter,
33+
)
2834
from sap_cloud_sdk.agentgateway._token_cache import _GatewayUrlCache, _TokenCache
2935
from sap_cloud_sdk.agentgateway.config import ClientConfig
3036
from sap_cloud_sdk.agentgateway.exceptions import AgentGatewaySDKError, MCPServerNotFoundError
@@ -1107,7 +1113,10 @@ async def _cards_by_ord(fragment_url, token, timeout):
11071113
),
11081114
):
11091115
result = await get_agent_cards_lob(
1110-
"tenant-sub", "token", 60.0, agent_names=["Billing Agent"]
1116+
"tenant-sub",
1117+
"token",
1118+
60.0,
1119+
filter=AgentCardFilter(agent_names=["Billing Agent"]),
11111120
)
11121121

11131122
assert len(result) == 1
@@ -1132,7 +1141,10 @@ async def test_filters_by_ord_ids(self):
11321141
) as mock_fetch,
11331142
):
11341143
result = await get_agent_cards_lob(
1135-
"tenant-sub", "token", 60.0, ord_ids=["ord-2"]
1144+
"tenant-sub",
1145+
"token",
1146+
60.0,
1147+
filter=AgentCardFilter(ord_ids=["ord-2"]),
11361148
)
11371149

11381150
assert len(result) == 1

0 commit comments

Comments
 (0)