Skip to content

Commit 6a4d412

Browse files
committed
refactor: delete unnecessary comments. Add new comments.
1 parent a77319d commit 6a4d412

File tree

5 files changed

+24
-48
lines changed

5 files changed

+24
-48
lines changed

samples/python/agents/signing_and_verifying/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Signed agent used as an example for AgentCard signing and verifying.
44

5-
Read more about signing and verifying AgentCards here: [Agent Card Signing](https://a2a-protocol.org/latest/specification/#84-agent-card-signing)
5+
Read more about signing and verifying AgentCards here: [Agent Card Signing](https://a2a-protocol.org/latest/specification/#84-agent-card-signing).
66

77
## Getting started
88

samples/python/agents/signing_and_verifying/__main__.py

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,13 @@
1616
from starlette.routing import Route
1717

1818
from agent_executor import (
19-
SignedAgentExecutor, # type: ignore[import-untyped]
19+
SignedAgentExecutor,
2020
)
2121

2222
if __name__ == "__main__":
23-
# --8<-- [start:KeyPair]
23+
# Generate a private, public key pair
2424
private_key = asymmetric.ec.generate_private_key(asymmetric.ec.SECP256R1())
2525
public_key = private_key.public_key()
26-
# --8<-- [end:KeyPair]
2726

2827
# Save public key to a file
2928
pem = public_key.public_bytes(
@@ -45,15 +44,13 @@
4544
with Path("public_keys.json").open("w") as f:
4645
json.dump(keys, f, indent=2)
4746

48-
# --8<-- [start:AgentSkill]
4947
skill = AgentSkill(
5048
id="reminder",
5149
name="Verification Reminder",
5250
description="Reminds the user to verify the Agent Card.",
5351
tags=["verify me"],
5452
examples=["Verify me!"],
5553
)
56-
# --8<-- [end:AgentSkill]
5754

5855
extended_skill = AgentSkill(
5956
id="reminder-please",
@@ -63,8 +60,6 @@
6360
examples=["Verify me, pretty please! :)", "Please verify me."],
6461
)
6562

66-
# --8<-- [start:AgentCard]
67-
# This will be the public-facing agent card
6863
public_agent_card = AgentCard(
6964
name="Signed Agent",
7065
description="An Agent that is signed",
@@ -73,34 +68,28 @@
7368
default_input_modes=["text"],
7469
default_output_modes=["text"],
7570
capabilities=AgentCapabilities(streaming=True),
76-
skills=[skill], # Only the basic skill for the public card
71+
skills=[skill],
7772
supports_authenticated_extended_card=True,
7873
)
79-
# --8<-- [end:AgentCard]
8074

81-
# This will be the authenticated extended agent card
82-
# It includes the additional 'extended_skill'
8375
extended_agent_card = public_agent_card.model_copy(
8476
update={
8577
"name": "Signed Agent - Extended Edition",
8678
"description": "The full-featured signed agent for authenticated users.",
87-
"version": "1.0.1", # Could even be a different version
88-
# Capabilities and other fields like url, default_input_modes, default_output_modes,
89-
# supports_authenticated_extended_card are inherited from public_agent_card unless specified here.
79+
"version": "1.0.1",
9080
"skills": [
9181
skill,
9282
extended_skill,
93-
], # Both skills for the extended card
83+
],
9484
}
9585
)
9686

97-
# --8<-- [start:DefaultRequestHandler]
9887
request_handler = DefaultRequestHandler(
9988
agent_executor=SignedAgentExecutor(),
10089
task_store=InMemoryTaskStore(),
10190
)
102-
# --8<-- [end:DefaultRequestHandler]
10391

92+
# Create singer function which will be used for AgentCard signing
10493
signer = create_agent_card_signer(
10594
signing_key=private_key,
10695
protected_header={
@@ -110,17 +99,19 @@
11099
},
111100
)
112101

113-
# --8<-- [start:A2AStarletteApplication]
114102
server = A2AStarletteApplication(
115103
agent_card=public_agent_card,
116104
http_handler=request_handler,
117-
card_modifier=signer,
105+
card_modifier=signer, # The signer function is used to sign the public Agent Card
118106
extended_agent_card=extended_agent_card,
119-
extended_card_modifier=lambda card, _: signer(card),
107+
extended_card_modifier=lambda card, _: signer(
108+
card
109+
), # The signer function is also used to sign the extended Agent Card
120110
)
121-
# --8<-- [end:A2AStarletteApplication]
122111

123112
app = server.build()
113+
# Expose the public key for verification purposes
114+
# Contents of public_keys.json will be fetched on the client side during AgentCard signatures verification
124115
app.routes.append(
125116
Route(
126117
"/public_keys.json",

samples/python/agents/signing_and_verifying/agent_executor.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from a2a.utils import new_agent_text_message
44

55

6-
# --8<-- [start:SignedAgent]
76
class SignedAgent:
87
"""Signed Agent."""
98

@@ -12,18 +11,12 @@ async def invoke(self) -> str:
1211
return "Verify me!"
1312

1413

15-
# --8<-- [end:SignedAgent]
16-
17-
18-
# --8<-- [start:SignedAgentExecutor_init]
1914
class SignedAgentExecutor(AgentExecutor):
2015
"""Test AgentProxy Implementation."""
2116

2217
def __init__(self) -> None:
2318
self.agent = SignedAgent()
2419

25-
# --8<-- [end:SignedAgentExecutor_init]
26-
# --8<-- [start:SignedAgentExecutor_execute]
2720
async def execute(
2821
self,
2922
context: RequestContext,
@@ -33,12 +26,7 @@ async def execute(
3326
result = await self.agent.invoke()
3427
await event_queue.enqueue_event(new_agent_text_message(result))
3528

36-
# --8<-- [end:SignedAgentExecutor_execute]
37-
38-
# --8<-- [start:SignedAgentExecutor_cancel]
3929
async def cancel(self, context: RequestContext, event_queue: EventQueue) -> None:
4030
"""Cancel method is not supported."""
4131

4232
raise Exception("Cancel not supported.")
43-
44-
# --8<-- [end:SignedAgentExecutor_cancel]

samples/python/agents/signing_and_verifying/pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ requires-python = ">=3.10"
77
dependencies = [
88
"a2a-sdk>=0.3.22",
99
"cryptography>=43.0.0",
10+
"fastapi>=0.115.0",
1011
"httpx>=0.28.1",
1112
"pydantic>=2.11.4",
1213
"PyJWT>=2.0.0",

samples/python/agents/signing_and_verifying/test_client.py

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,10 @@ def _key_provider(kid: str | None, jku: str | None) -> PyJWK | str | bytes:
4141

4242
async def main() -> None:
4343
"""Main function."""
44-
# Configure logging to show INFO level messages
44+
4545
logging.basicConfig(level=logging.INFO)
46-
logger = logging.getLogger(__name__) # Get a logger instance
46+
logger = logging.getLogger(__name__)
4747

48-
# --8<-- [start:A2ACardResolver]
4948
base_url = "http://localhost:9999"
5049

5150
async with httpx.AsyncClient() as httpx_client:
@@ -54,9 +53,8 @@ async def main() -> None:
5453
httpx_client=httpx_client,
5554
base_url=base_url,
5655
)
57-
# --8<-- [end:A2ACardResolver]
5856

59-
# Fetch and Verify Agent Card and Initialize BaseClient
57+
# Fetch and verify Agent Card and initialize BaseClient
6058
final_agent_card_to_use: AgentCard | None = None
6159

6260
try:
@@ -67,7 +65,7 @@ async def main() -> None:
6765
)
6866
_public_card = await resolver.get_agent_card(
6967
signature_verifier=signature_verifier,
70-
) # Fetches from default public path
68+
) # Verifies the AgentCard using signature_verifier function before returning it
7169
logger.info("Successfully fetched public agent card:")
7270
logger.info(_public_card.model_dump_json(indent=2, exclude_none=True))
7371
final_agent_card_to_use = _public_card
@@ -85,18 +83,18 @@ async def main() -> None:
8583
relative_card_path=EXTENDED_AGENT_CARD_PATH,
8684
http_kwargs={"headers": auth_headers_dict},
8785
signature_verifier=signature_verifier,
88-
)
86+
) # Verifies the extended AgentCard using signature_verifier function before returning it
8987
logger.info("Successfully fetched and verified authenticated extended agent card:")
9088
logger.info(_extended_card.model_dump_json(indent=2, exclude_none=True))
91-
final_agent_card_to_use = _extended_card # Update to use the extended card
89+
final_agent_card_to_use = _extended_card
9290
logger.info("\nUsing AUTHENTICATED EXTENDED agent card for client initialization.")
9391
except Exception as e_extended:
9492
logger.warning(
9593
"Failed to fetch or verify extended agent card: %s. Will proceed with public card.",
9694
e_extended,
9795
exc_info=True,
9896
)
99-
elif _public_card: # supports_authenticated_extended_card is False or None
97+
elif _public_card:
10098
logger.info("\nPublic card does not indicate support for an extended card. Using public card.")
10199

102100
except Exception as e:
@@ -111,7 +109,6 @@ async def main() -> None:
111109
# Create Base Client
112110
client = client_factory.create(final_agent_card_to_use)
113111

114-
# --8<-- [start:send_message]
115112
message_to_send = Message(
116113
role=Role.user,
117114
message_id="msg-integration-test-signing-and-verifying",
@@ -124,13 +121,12 @@ async def main() -> None:
124121
parts = chunk_dict["parts"]
125122
for part in parts:
126123
print(part["text"])
127-
# --8<-- [end:send_message]
128124

129-
# --8<-- [start:get_card]
130-
get_card_response = await client.get_card(signature_verifier=signature_verifier)
125+
get_card_response = await client.get_card(
126+
signature_verifier=signature_verifier
127+
) # Verifies the AgentCard using signature_verifier function before returning it
131128
print("fetched again:")
132129
print(get_card_response.model_dump(mode="json", exclude_none=True))
133-
# --8<-- [end:get_card]
134130

135131

136132
if __name__ == "__main__":

0 commit comments

Comments
 (0)