This repository was archived by the owner on Mar 3, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy patha2a_mcp_server.py
More file actions
1081 lines (901 loc) · 36.2 KB
/
a2a_mcp_server.py
File metadata and controls
1081 lines (901 loc) · 36.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Enhanced MCP A2A Bridge with improved task management and result retrieval.
This script implements a bridge between the MCP protocol and A2A protocol,
allowing MCP clients to interact with A2A agents.
Supports multiple transport types:
- stdio: Standard input/output transport
- streamable-http: Recommended HTTP transport
- sse: Server-Sent Events transport
Configure the transport type using the MCP_TRANSPORT environment variable.
"""
import asyncio
import os
import uuid
from typing import Any, AsyncGenerator, Dict, List, Optional, Union
import json
import logging
import atexit
import httpx
# Set required environment variable for FastMCP 2.8.1+
os.environ.setdefault('FASTMCP_LOG_LEVEL', 'INFO')
from fastmcp import Context, FastMCP
from pydantic import BaseModel, Field
from common.types import (
AgentCard,
AgentCapabilities,
AgentSkill,
Artifact,
DataPart,
Message,
Part,
TextPart,
TaskState,
TaskStatus,
TaskStatusUpdateEvent,
TaskArtifactUpdateEvent,
JSONRPCResponse,
SendTaskRequest,
SendTaskResponse,
SendTaskStreamingRequest,
SendTaskStreamingResponse,
GetTaskRequest,
GetTaskResponse,
TaskQueryParams,
CancelTaskRequest,
CancelTaskResponse
)
from common.client.client import A2AClient
from common.server.task_manager import InMemoryTaskManager
from persistence_utils import save_to_json, load_from_json
# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Create a FastMCP server
mcp = FastMCP("A2A Bridge Server")
# File paths for persistent storage
DATA_DIR = os.environ.get("A2A_MCP_DATA_DIR", ".")
REGISTERED_AGENTS_FILE = os.path.join(DATA_DIR, "registered_agents.json")
TASK_AGENT_MAPPING_FILE = os.path.join(DATA_DIR, "task_agent_mapping.json")
# Load stored data from disk if it exists
stored_agents = load_from_json(REGISTERED_AGENTS_FILE)
stored_tasks = load_from_json(TASK_AGENT_MAPPING_FILE)
# Initialize in-memory dictionaries with stored data
registered_agents = {}
task_agent_mapping = {}
# Register function to save data on exit
def save_data_on_exit():
logger.info("Saving data before exit...")
# Convert registered_agents to a serializable format
agents_data = {url: agent.model_dump() for url, agent in registered_agents.items()}
save_to_json(agents_data, REGISTERED_AGENTS_FILE)
save_to_json(task_agent_mapping, TASK_AGENT_MAPPING_FILE)
logger.info("Data saved successfully")
atexit.register(save_data_on_exit)
# Periodically save data (every 5 minutes)
async def periodic_save():
while True:
await asyncio.sleep(300) # 5 minutes
logger.info("Performing periodic data save...")
agents_data = {url: agent.model_dump() for url, agent in registered_agents.items()}
save_to_json(agents_data, REGISTERED_AGENTS_FILE)
save_to_json(task_agent_mapping, TASK_AGENT_MAPPING_FILE)
logger.info("Periodic save completed")
# Load transport configuration from environment variables
DEFAULT_TRANSPORT = "stdio"
TRANSPORT_TYPES = ["stdio", "streamable-http", "sse"]
# MCP server configuration
MCP_TRANSPORT = os.environ.get("MCP_TRANSPORT", DEFAULT_TRANSPORT).lower()
MCP_HOST = os.environ.get("MCP_HOST", "0.0.0.0")
MCP_PORT = int(os.environ.get("MCP_PORT", "8000"))
MCP_PATH = os.environ.get("MCP_PATH", "/mcp") # For streamable-http
MCP_SSE_PATH = os.environ.get("MCP_SSE_PATH", "/sse") # For sse
# A2A server configuration
A2A_HOST = os.environ.get("A2A_HOST", "0.0.0.0")
A2A_PORT = int(os.environ.get("A2A_PORT", "41241"))
# Validate transport type
if MCP_TRANSPORT not in TRANSPORT_TYPES:
print(f"Warning: Invalid transport type '{MCP_TRANSPORT}'. Using default: {DEFAULT_TRANSPORT}")
MCP_TRANSPORT = DEFAULT_TRANSPORT
class AgentInfo(BaseModel):
"""Information about an A2A agent."""
url: str = Field(description="URL of the A2A agent")
name: str = Field(description="Name of the A2A agent")
description: str = Field(description="Description of the A2A agent")
class A2ABridgeTaskManager(InMemoryTaskManager):
"""Task manager that forwards tasks to A2A agents."""
def __init__(self):
super().__init__()
self.agent_clients = {} # Maps agent URLs to A2AClient instances
def get_or_create_client(self, agent_url: str) -> A2AClient:
"""Get an existing client or create a new one."""
if agent_url not in self.agent_clients:
self.agent_clients[agent_url] = A2AClient(url=agent_url) # Use named parameter
return self.agent_clients[agent_url]
async def on_send_task(self, request: SendTaskRequest) -> SendTaskResponse:
"""Handle a task send request by forwarding to the appropriate A2A agent."""
task_id = request.params.id
# Extract the agent URL from metadata
agent_url = request.params.metadata.get("agent_url") if request.params.metadata else None
if not agent_url:
# No agent URL provided, return error
return SendTaskResponse(
id=request.id,
error={
"code": -32602,
"message": "Agent URL not provided in task metadata",
}
)
client = self.get_or_create_client(agent_url)
# Forward the message to the A2A agent
try:
# Create payload as a single dictionary
payload = {
"id": task_id,
"message": request.params.message,
}
if request.params.sessionId:
payload["sessionId"] = request.params.sessionId
if request.params.metadata:
payload["metadata"] = request.params.metadata
result = await client.send_task(payload)
# Store the task result
self.tasks[task_id] = result
# Return the response
return SendTaskResponse(id=request.id, result=result)
except Exception as e:
return SendTaskResponse(
id=request.id,
error={
"code": -32603,
"message": f"Error forwarding task to A2A agent: {str(e)}",
}
)
async def on_send_task_subscribe(
self, request: SendTaskStreamingRequest
) -> AsyncGenerator[SendTaskStreamingResponse, None] | JSONRPCResponse:
"""Handle a task subscription request by forwarding to the appropriate A2A agent."""
task_id = request.params.id
# Extract the agent URL from metadata
agent_url = request.params.metadata.get("agent_url") if request.params.metadata else None
if not agent_url:
# No agent URL provided, return error
return JSONRPCResponse(
id=request.id,
error={
"code": -32602,
"message": "Agent URL not provided in task metadata",
}
)
client = self.get_or_create_client(agent_url)
# Set up SSE consumer
sse_event_queue = await self.setup_sse_consumer(task_id=task_id)
# Start forwarding the task in a background task
asyncio.create_task(self._forward_task_stream(
client=client,
request=request,
task_id=task_id,
))
# Return the SSE consumer
return self.dequeue_events_for_sse(
request_id=request.id,
task_id=task_id,
sse_event_queue=sse_event_queue,
)
async def _forward_task_stream(
self, client: A2AClient, request: SendTaskStreamingRequest, task_id: str
):
"""Forward a task stream to an A2A agent and relay the responses."""
try:
# Create payload as a single dictionary
payload = {
"id": task_id,
"message": request.params.message,
}
if request.params.sessionId:
payload["sessionId"] = request.params.sessionId
if request.params.metadata:
payload["metadata"] = request.params.metadata
# Send the task and subscribe to updates
stream = client.send_task_subscribe(payload)
# Process the stream events
async for event in stream:
# Forward the event to our SSE queue
await self.enqueue_events_for_sse(task_id, event)
# If this is the final event, break
if hasattr(event, "final") and event.final:
break
except Exception as e:
# Create an error event and enqueue it
error_event = TaskStatusUpdateEvent(
id=task_id,
status=TaskStatus(
state=TaskState.FAILED,
message=Message(
role="agent",
parts=[TextPart(text=f"Error forwarding task: {str(e)}")],
),
),
final=True,
)
await self.enqueue_events_for_sse(task_id, error_event)
async def fetch_agent_card(url: str) -> AgentCard:
"""
Fetch the agent card from the agent's URL.
First try the main URL, then the well-known location.
"""
async with httpx.AsyncClient() as client:
# First try the main endpoint
try:
response = await client.get(url)
if response.status_code == 200:
try:
data = response.json()
if isinstance(data, dict) and "name" in data and "url" in data:
return AgentCard(**data)
except json.JSONDecodeError:
pass # Not a valid JSON response, try the well-known URL
except Exception:
pass # Connection error, try the well-known URL
# Try the well-known location
well_known_url = f"{url.rstrip('/')}/.well-known/agent.json"
try:
response = await client.get(well_known_url)
if response.status_code == 200:
try:
data = response.json()
return AgentCard(**data)
except json.JSONDecodeError:
raise ValueError(f"Invalid JSON in agent card from {well_known_url}")
except httpx.RequestError as e:
raise ValueError(f"Failed to fetch agent card from {well_known_url}: {str(e)}")
# If we can't get the agent card, create a minimal one with default values
return AgentCard(
name="Unknown Agent",
url=url,
version="0.1.0",
capabilities=AgentCapabilities(streaming=False),
skills=[
AgentSkill(
id="unknown",
name="Unknown Skill",
description="Unknown agent capabilities",
)
],
)
@mcp.tool()
async def register_agent(url: str, ctx: Context) -> Dict[str, Any]:
"""
Register an A2A agent with the bridge server.
Args:
url: URL of the A2A agent
Returns:
Dictionary with registration status
"""
try:
# Fetch the agent card directly
agent_card = await fetch_agent_card(url)
# Store the agent information
agent_info = AgentInfo(
url=url,
name=agent_card.name,
description=agent_card.description or "No description provided",
)
registered_agents[url] = agent_info
# Save to disk immediately
agents_data = {url: agent.model_dump() for url, agent in registered_agents.items()}
save_to_json(agents_data, REGISTERED_AGENTS_FILE)
await ctx.info(f"Successfully registered agent: {agent_card.name}")
return {
"status": "success",
"agent": agent_info.model_dump(),
}
except Exception as e:
return {
"status": "error",
"message": f"Failed to register agent: {str(e)}",
}
@mcp.tool()
async def list_agents() -> List[Dict[str, Any]]:
"""
List all registered A2A agents.
Returns:
List of registered agents
"""
return [agent.model_dump() for agent in registered_agents.values()]
@mcp.tool()
async def unregister_agent(url: str, ctx: Context = None) -> Dict[str, Any]:
"""
Unregister an A2A agent from the bridge server.
Args:
url: URL of the A2A agent to unregister
Returns:
Dictionary with unregistration status
"""
if url not in registered_agents:
return {
"status": "error",
"message": f"Agent not registered: {url}",
}
try:
# Get agent name before removing it
agent_name = registered_agents[url].name
# Remove from registered agents
del registered_agents[url]
# Clean up any task mappings related to this agent
# Create a list of task_ids to remove to avoid modifying the dictionary during iteration
tasks_to_remove = []
for task_id, agent_url in task_agent_mapping.items():
if agent_url == url:
tasks_to_remove.append(task_id)
# Now remove the task mappings
for task_id in tasks_to_remove:
del task_agent_mapping[task_id]
# Save changes to disk immediately
agents_data = {url: agent.model_dump() for url, agent in registered_agents.items()}
save_to_json(agents_data, REGISTERED_AGENTS_FILE)
save_to_json(task_agent_mapping, TASK_AGENT_MAPPING_FILE)
if ctx:
await ctx.info(f"Successfully unregistered agent: {agent_name}")
return {
"status": "success",
"message": f"Successfully unregistered agent: {agent_name}",
"removed_tasks": len(tasks_to_remove),
}
except Exception as e:
return {
"status": "error",
"message": f"Error unregistering agent: {str(e)}",
}
@mcp.tool()
async def send_message(
agent_url: str,
message: str,
session_id: Optional[str] = None,
ctx: Context = None,
) -> Dict[str, Any]:
"""
Send a message to an A2A agent.
Args:
agent_url: URL of the A2A agent
message: Message to send
session_id: Optional session ID for multi-turn conversations
Returns:
Agent's response with task_id for future reference
"""
if agent_url not in registered_agents:
return {
"status": "error",
"message": f"Agent not registered: {agent_url}",
}
# Create a client for the agent
client = A2AClient(url=agent_url)
try:
# Generate a task ID
task_id = str(uuid.uuid4())
# Store the mapping of task_id to agent_url for later reference
task_agent_mapping[task_id] = agent_url
# Create the message
a2a_message = Message(
role="user",
parts=[TextPart(text=message)],
)
if ctx:
await ctx.info(f"Sending message to agent: {message}")
# Create payload as a single dictionary
payload = {
"id": task_id,
"message": a2a_message,
}
if session_id:
payload["sessionId"] = session_id
# Send the task with the payload
result = await client.send_task(payload)
# Save task mapping to disk
save_to_json(task_agent_mapping, TASK_AGENT_MAPPING_FILE)
# Debug: Print the raw response for analysis
if ctx:
await ctx.info(f"Raw response: {result}")
# Create a response dictionary with as much info as we can extract
response = {
"status": "success",
"task_id": task_id,
}
# Add any available fields from the result
if hasattr(result, "sessionId"):
response["session_id"] = result.sessionId
else:
response["session_id"] = None
# Try to get the state
try:
if hasattr(result, "status") and hasattr(result.status, "state"):
response["state"] = result.status.state
else:
response["state"] = "unknown"
except Exception as e:
response["state"] = f"error_getting_state: {str(e)}"
# Try to extract response message
try:
if hasattr(result, "status") and hasattr(result.status, "message") and result.status.message:
response_text = ""
for part in result.status.message.parts:
if part.type == "text":
response_text += part.text
if response_text:
response["message"] = response_text
except Exception as e:
response["message_error"] = f"Error extracting message: {str(e)}"
# Try to get artifacts
try:
if hasattr(result, "artifacts") and result.artifacts:
artifacts_data = []
for artifact in result.artifacts:
artifact_data = {
"name": artifact.name if hasattr(artifact, "name") else "unnamed_artifact",
"contents": [],
}
for part in artifact.parts:
if part.type == "text":
artifact_data["contents"].append({
"type": "text",
"text": part.text,
})
elif part.type == "data":
artifact_data["contents"].append({
"type": "data",
"data": part.data,
})
artifacts_data.append(artifact_data)
response["artifacts"] = artifacts_data
except Exception as e:
response["artifacts_error"] = f"Error extracting artifacts: {str(e)}"
return response
except Exception as e:
return {
"status": "error",
"message": f"Error sending message: {str(e)}",
}
@mcp.tool()
async def get_task_result(
task_id: str,
history_length: Optional[int] = None,
ctx: Context = None,
) -> Dict[str, Any]:
"""
Retrieve the result of a task from an A2A agent.
Args:
task_id: ID of the task to retrieve
history_length: Optional number of history items to include (null for all)
Returns:
Task result including status, message, and artifacts if available
"""
if task_id not in task_agent_mapping:
return {
"status": "error",
"message": f"Task ID not found: {task_id}",
}
agent_url = task_agent_mapping[task_id]
# Create a client for the agent
client = A2AClient(url=agent_url)
try:
# Create the request payload
payload = {
"id": task_id,
"historyLength": history_length
}
if ctx:
await ctx.info(f"Retrieving task result for task_id: {task_id}")
# Send the get task request
result = await client.get_task(payload)
# Debug: Print the raw response for analysis
if ctx:
await ctx.info(f"Raw task result: {result}")
# Create a response dictionary with as much info as we can extract
response = {
"status": "success",
"task_id": task_id,
}
# Try to extract task data
try:
if hasattr(result, "result"):
task = result.result
# Add basic task info
if hasattr(task, "sessionId"):
response["session_id"] = task.sessionId
else:
response["session_id"] = None
# Add task status
if hasattr(task, "status"):
status = task.status
if hasattr(status, "state"):
response["state"] = status.state
# Extract message from status
if hasattr(status, "message") and status.message:
response_text = ""
for part in status.message.parts:
if part.type == "text":
response_text += part.text
if response_text:
response["message"] = response_text
# Extract artifacts
if hasattr(task, "artifacts") and task.artifacts:
artifacts_data = []
for artifact in task.artifacts:
artifact_data = {
"name": artifact.name if hasattr(artifact, "name") else "unnamed_artifact",
"contents": [],
}
for part in artifact.parts:
if part.type == "text":
artifact_data["contents"].append({
"type": "text",
"text": part.text,
})
elif part.type == "data":
artifact_data["contents"].append({
"type": "data",
"data": part.data,
})
artifacts_data.append(artifact_data)
response["artifacts"] = artifacts_data
# Extract message history if available
if hasattr(task, "history") and task.history:
history_data = []
for message in task.history:
message_data = {
"role": message.role,
"parts": [],
}
for part in message.parts:
if part.type == "text":
message_data["parts"].append({
"type": "text",
"text": part.text,
})
elif hasattr(part, "data"):
message_data["parts"].append({
"type": "data",
"data": part.data,
})
history_data.append(message_data)
response["history"] = history_data
else:
response["error"] = "No result in response"
except Exception as e:
response["parsing_error"] = f"Error parsing task result: {str(e)}"
return response
except Exception as e:
return {
"status": "error",
"message": f"Error retrieving task result: {str(e)}",
}
@mcp.tool()
async def cancel_task(
task_id: str,
ctx: Context = None,
) -> Dict[str, Any]:
"""
Cancel a running task on an A2A agent.
Args:
task_id: ID of the task to cancel
Returns:
Cancellation result
"""
if task_id not in task_agent_mapping:
return {
"status": "error",
"message": f"Task ID not found: {task_id}",
}
agent_url = task_agent_mapping[task_id]
# Create a client for the agent
client = A2AClient(url=agent_url)
try:
# Create the request payload
payload = {
"id": task_id
}
if ctx:
await ctx.info(f"Cancelling task: {task_id}")
# Send the cancel task request
result = await client.cancel_task(payload)
# Debug: Print the raw response for analysis
if ctx:
await ctx.info(f"Raw cancellation result: {result}")
# Create a response dictionary
if hasattr(result, "error"):
return {
"status": "error",
"task_id": task_id,
"message": result.error.message,
"code": result.error.code
}
elif hasattr(result, "result"):
return {
"status": "success",
"task_id": task_id,
"message": "Task cancelled successfully"
}
else:
return {
"status": "unknown",
"task_id": task_id,
"message": "Unexpected response format"
}
except Exception as e:
return {
"status": "error",
"message": f"Error cancelling task: {str(e)}",
}
@mcp.tool()
async def send_message_stream(
agent_url: str,
message: str,
session_id: Optional[str] = None,
ctx: Context = None,
) -> Dict[str, Any]:
"""
Send a message to an A2A agent and stream the response.
Args:
agent_url: URL of the A2A agent
message: Message to send
session_id: Optional session ID for multi-turn conversations
Returns:
Stream of agent's responses
"""
if agent_url not in registered_agents:
return {
"status": "error",
"message": f"Agent not registered: {agent_url}",
}
# Create a client for the agent
client = A2AClient(url=agent_url)
try:
# Generate a task ID
task_id = str(uuid.uuid4())
# Store the mapping of task_id to agent_url for later reference
task_agent_mapping[task_id] = agent_url
# Save the task mapping to disk
save_to_json(task_agent_mapping, TASK_AGENT_MAPPING_FILE)
# Create the message
a2a_message = Message(
role="user",
parts=[TextPart(text=message)],
)
if ctx:
await ctx.info(f"Sending message to agent (streaming): {message}")
# Start progress indication
if ctx:
await ctx.info("Processing...")
# Dictionary to accumulate streaming responses
complete_response = {
"status": "success",
"task_id": task_id,
"session_id": session_id,
"state": "working",
"messages": [],
"artifacts": [],
}
# Create payload as a single dictionary
payload = {
"id": task_id,
"message": a2a_message,
}
if session_id:
payload["sessionId"] = session_id
# Send the task and subscribe to updates
stream = client.send_task_streaming(payload)
# Process and report stream events
try:
all_events = []
async for event in stream:
# Save all events for debugging
all_events.append({
"type": str(type(event)),
"dir": str(dir(event)),
})
if hasattr(event, "result"):
if hasattr(event.result, "status"):
# It's a TaskStatusUpdateEvent
status_event = event.result
# Update the state
if hasattr(status_event, "status") and hasattr(status_event.status, "state"):
complete_response["state"] = status_event.status.state
# Extract any message
if hasattr(status_event, "status") and hasattr(status_event.status, "message") and status_event.status.message:
message_text = ""
for part in status_event.status.message.parts:
if part.type == "text":
message_text += part.text
if message_text:
complete_response["messages"].append(message_text)
if ctx:
await ctx.info(f"Agent: {message_text}")
# If this is the final event, set session ID
if hasattr(status_event, "final") and status_event.final:
complete_response["session_id"] = getattr(status_event, "sessionId", session_id)
elif hasattr(event.result, "artifact"):
# It's a TaskArtifactUpdateEvent
artifact_event = event.result
# Extract artifact content
artifact_data = {
"name": artifact_event.artifact.name if hasattr(artifact_event.artifact, "name") else "unnamed",
"contents": [],
}
for part in artifact_event.artifact.parts:
if part.type == "text":
artifact_data["contents"].append({
"type": "text",
"text": part.text,
})
elif part.type == "data":
artifact_data["contents"].append({
"type": "data",
"data": part.data,
})
complete_response["artifacts"].append(artifact_data)
if ctx:
await ctx.info(f"Received artifact: {artifact_data['name']}")
else:
# Unknown event type, try to extract what we can
complete_response["unknown_events"] = complete_response.get("unknown_events", []) + [
{
"type": str(type(event)),
"dir": str(dir(event))
}
]
# Include debug info
complete_response["_debug_info"] = {
"all_events": all_events
}
return complete_response
except Exception as e:
return {
"status": "error",
"message": f"Error processing stream events: {str(e)}",
"_debug_info": {
"all_events": all_events
}
}
except Exception as e:
return {
"status": "error",
"message": f"Error sending message (stream): {str(e)}",
}
class CustomA2AServer:
"""
A minimal A2A server implementation that uses the task manager.
"""
def __init__(
self,
agent_card: AgentCard,
task_manager: A2ABridgeTaskManager,
host: str = "0.0.0.0",
port: int = 41241,
):
self.agent_card = agent_card
self.task_manager = task_manager
self.host = host
self.port = port
async def start_async(self):
"""Start the A2A server asynchronously."""
# In a real implementation, this would start a FastAPI server
# For now, just log that it's "started"
print(f"A2A server 'started' at {self.host}:{self.port}")
# Keep the server "running"
while True:
await asyncio.sleep(3600) # Sleep for an hour
def start(self):
"""Start the A2A server."""
asyncio.create_task(self.start_async())
def setup_a2a_server():
"""Set up the A2A server with our task manager."""
# Create a sample agent card
agent_card = AgentCard(
name="MCP Bridge Agent",
description="A bridge between MCP and A2A protocols",
url=f"http://{A2A_HOST}:{A2A_PORT}",
version="0.1.0",
# Add the required capabilities field
capabilities=AgentCapabilities(
streaming=True,
pushNotifications=False,
stateTransitionHistory=False,
),
# Add the required skills field with at least one skill
skills=[
AgentSkill(
id="mcp-bridge",
name="MCP Bridge",
description="Allows MCP clients to communicate with A2A agents",
tags=["bridge", "proxy", "mcp", "a2a"],
examples=[
"Send a message to an A2A agent",
"Register an A2A agent with the bridge",
"List all registered A2A agents",
],
inputModes=["text/plain"],
outputModes=["text/plain", "application/json"],
)
],
)
# Create our custom task manager
task_manager = A2ABridgeTaskManager()
# Create and return the A2A server
return CustomA2AServer(
agent_card=agent_card,
task_manager=task_manager,
host=A2A_HOST,
port=A2A_PORT,
)
async def main_async():
"""
Main async function to start both the MCP and A2A servers.
"""
# Load stored data into memory
load_registered_agents()
# Start periodic save task