Agent Zero AI framework
feat: A2A Client/Server Implementation
Rafael Uzarowskid84d24b3bede
diff --git a/prompts/agent.system.tool.a2a_chat.md b/prompts/agent.system.tool.a2a_chat.mdnew file mode 100644index 000000000..de9d192fc--- /dev/null+++ b/prompts/agent.system.tool.a2a_chat.md@@ -0,0 +1,73 @@+### a2a_chat:+This tool lets Agent Zero chat with any other FastA2A-compatible agent.+It automatically keeps conversation **context** (so each subsequent call+continues the same dialogue) and supports optional file attachments.++#### What the tool can do+* Start a brand-new conversation with a remote agent.+* Continue an existing conversation transparently (context handled for you).+* Send text plus optional file URIs (images, docs, etc.).+* Receive the assistant’s reply as plain text.++#### Arguments+* `agent_url` (string, required) – Base URL of the *remote* agent.+ • Accepts `host:port`, `http://host:port`, or full path ending in `/a2a`.+* `message` (string, required) – The text you want to send.+* `attachments` (list[string], optional) – URIs pointing to files you want+ to send along with the message (can be http(s):// or file path).+* `reset` (boolean, optional) – Set to `true` to start a **new** conversation+ with the same `agent_url` (clears stored context). Default `false`.++> Leave **context_id** out – the tool handles it internally.++#### Usage – first message+##### Request+```json+{+ "thoughts": [+ "I want to ask the weather-bot for today’s forecast."+ ],+ "headline": "Ask remote agent (weather-bot)",+ "tool_name": "a2a_chat",+ "tool_args": {+ "agent_url": "http://weather.example.com:8000/a2a",+ "message": "Hello! What’s the forecast for Berlin today?",+ "attachments": [],+ "reset": false+ }+}+```+##### Response (assistant-side)+```plaintext+☀️ It will be sunny with a high of 22 °C.+```++#### Usage – follow-up (context automatically preserved)+##### Request+```json+{+ "thoughts": [+ "Need tomorrow’s forecast too."+ ],+ "headline": "Follow-up question",+ "tool_name": "a2a_chat",+ "tool_args": {+ "agent_url": "http://weather.example.com:8000/a2a",+ "message": "And tomorrow?",+ "attachments": [],+ "reset": false+ }+}+```+##### Response+```plaintext+🌦️ Partly cloudy with showers, high 18 °C.+```++#### Notes+1. **New conversation** – omit previous `agent_url` or use a *different* URL.+2. **Attachments** – supply absolute URIs ("http://…", "file:/…").+3. The tool stores session IDs per `agent_url` inside the current+ `AgentContext` – no manual handling required.+4. Use `"reset": true` to forget previous context and start a new chat.+5. The remote agent must implement FastA2A v0.2+ protocol.diff --git a/python/helpers/fasta2a_client.py b/python/helpers/fasta2a_client.pynew file mode 100644index 000000000..94f511d4c--- /dev/null+++ b/python/helpers/fasta2a_client.py@@ -0,0 +1,209 @@+import uuid+from typing import Any, Dict, List, Optional+from python.helpers.print_style import PrintStyle++try:+ from fasta2a.client import A2AClient # type: ignore+ import httpx # type: ignore+ FASTA2A_CLIENT_AVAILABLE = True+except ImportError:+ FASTA2A_CLIENT_AVAILABLE = False+ PrintStyle.warning("FastA2A client not available. Agent-to-agent communication disabled.")++_PRINTER = PrintStyle(italic=True, font_color="cyan", padding=False)+++class AgentConnection:+ """Helper class for connecting to and communicating with other Agent Zero instances via FastA2A."""++ def __init__(self, agent_url: str, timeout: int = 30, token: Optional[str] = None):+ """Initialize connection to an agent.++ Args:+ agent_url: The base URL of the agent (e.g., "https://agent.example.com")+ timeout: Request timeout in seconds+ """+ if not FASTA2A_CLIENT_AVAILABLE:+ raise RuntimeError("FastA2A client not available")++ # Ensure scheme is present+ if not agent_url.startswith(('http://', 'https://')):+ agent_url = 'http://' + agent_url++ self.agent_url = agent_url.rstrip('/')+ self.timeout = timeout+ # Auth headers+ if token is None:+ import os+ token = os.getenv("A2A_TOKEN")+ headers = {}+ if token:+ headers["Authorization"] = f"Bearer {token}"+ headers["X-API-KEY"] = token+ self._http_client = httpx.AsyncClient(timeout=timeout, headers=headers) # type: ignore+ self._a2a_client = A2AClient(base_url=self.agent_url, http_client=self._http_client) # type: ignore+ self._agent_card: Optional[Dict[str, Any]] = None+ # Track conversation context automatically+ self._context_id: Optional[str] = None++ async def get_agent_card(self) -> Dict[str, Any]:+ """Retrieve the agent card from the remote agent."""+ if self._agent_card is None:+ try:+ response = await self._http_client.get(f"{self.agent_url}/.well-known/agent.json")+ response.raise_for_status()+ self._agent_card = response.json()+ _PRINTER.print(f"Retrieved agent card from {self.agent_url}")+ _PRINTER.print(f"Agent: {self._agent_card.get('name', 'Unknown')}")+ _PRINTER.print(f"Description: {self._agent_card.get('description', 'No description')}")+ except Exception as e:+ # Fallback: if URL contains '/a2a', try root path without it+ if "/a2a" in self.agent_url:+ root_url = self.agent_url.split("/a2a", 1)[0]+ try:+ response = await self._http_client.get(f"{root_url}/.well-known/agent.json")+ response.raise_for_status()+ self._agent_card = response.json()+ _PRINTER.print(f"Retrieved agent card from {root_url}")+ except Exception:+ pass # swallow, will re-raise below+ _PRINTER.print(f"[!] Could not connect to {self.agent_url}\n → Ensure the server is running and reachable.\n → Full error: {e}")+ raise RuntimeError(f"Could not retrieve agent card: {e}")++ return self._agent_card # type: ignore++ async def send_message(+ self,+ message: str,+ attachments: Optional[List[str]] = None,+ context_id: Optional[str] = None,+ metadata: Optional[Dict[str, Any]] = None+ ) -> Dict[str, Any]:+ """Send a message to the remote agent and return task response."""+ if not self._agent_card:+ await self.get_agent_card()++ # Re-use context automatically if caller did not supply one+ if context_id is None:+ context_id = self._context_id++ # Build message parts+ parts = [{'kind': 'text', 'text': message}]++ if attachments:+ for attachment in attachments:+ file_part = {'kind': 'file', 'file': {'uri': attachment}}+ parts.append(file_part) # type: ignore++ # Construct A2A message+ a2a_message = {+ 'role': 'user',+ 'parts': parts,+ 'kind': 'message',+ 'message_id': str(uuid.uuid4())+ }++ if context_id is not None:+ a2a_message['context_id'] = context_id++ # Send using the message/send method (not send_task)+ try:+ response = await self._a2a_client.send_message(+ message=a2a_message, # type: ignore+ metadata=metadata,+ configuration={'accepted_output_modes': ['application/json', 'text/plain'], 'blocking': True} # type: ignore+ )++ # Persist context id for subsequent calls+ try:+ ctx = response.get('result', {}).get('context_id') # type: ignore[index]+ if isinstance(ctx, str):+ self._context_id = ctx+ except Exception:+ pass # ignore if structure differs+ return response # type: ignore+ except Exception as e:+ _PRINTER.print(f"[A2A] Error sending message: {e}")+ raise++ async def get_task(self, task_id: str) -> Dict[str, Any]:+ """Get the status and results of a task.++ Args:+ task_id: The ID of the task to query++ Returns:+ Dictionary containing the task information+ """+ try:+ response = await self._a2a_client.get_task(task_id) # type: ignore+ return response # type: ignore+ except Exception as e:+ _PRINTER.print(f"Failed to get task {task_id}: {e}")+ raise RuntimeError(f"Failed to get task: {e}")++ async def wait_for_completion(self, task_id: str, poll_interval: int = 2, max_wait: int = 300) -> Dict[str, Any]:+ """Wait for a task to complete and return the final result.++ Args:+ task_id: The ID of the task to wait for+ poll_interval: How often to check task status (seconds)+ max_wait: Maximum time to wait (seconds)++ Returns:+ Dictionary containing the completed task information+ """+ import asyncio++ waited = 0+ while waited < max_wait:+ task_info = await self.get_task(task_id)++ if 'result' in task_info:+ task = task_info['result']+ status = task.get('status', {})+ state = status.get('state', 'unknown')++ if state in ['completed', 'failed', 'canceled']:+ _PRINTER.print(f"Task {task_id} finished with state: {state}")+ return task_info+ else:+ _PRINTER.print(f"Task {task_id} status: {state}")++ await asyncio.sleep(poll_interval)+ waited += poll_interval++ raise TimeoutError(f"Task {task_id} did not complete within {max_wait} seconds")++ async def close(self):+ """Close the HTTP client connection."""+ await self._http_client.aclose()++ async def __aenter__(self):+ """Async context manager entry."""+ return self++ async def __aexit__(self, exc_type, exc_val, exc_tb):+ """Async context manager exit."""+ await self.close()+++async def connect_to_agent(agent_url: str, timeout: int = 30) -> AgentConnection:+ """Create a connection to a remote agent.++ Args:+ agent_url: The base URL of the agent+ timeout: Request timeout in seconds++ Returns:+ AgentConnection instance+ """+ connection = AgentConnection(agent_url, timeout)+ # Verify connection by retrieving agent card+ await connection.get_agent_card()+ return connection+++def is_client_available() -> bool:+ """Check if FastA2A client is available."""+ return FASTA2A_CLIENT_AVAILABLEdiff --git a/python/helpers/fasta2a_server.py b/python/helpers/fasta2a_server.pynew file mode 100644index 000000000..23792055f--- /dev/null+++ b/python/helpers/fasta2a_server.py@@ -0,0 +1,325 @@+# noqa: D401 (docstrings) – internal helper+import asyncio+import uuid+import atexit+from typing import Any, List+import contextlib++from python.helpers import settings+from starlette.middleware.base import BaseHTTPMiddleware+from starlette.requests import Request+from starlette.responses import PlainTextResponse+from starlette.middleware import Middleware++# Local imports+from python.helpers.print_style import PrintStyle+from agent import AgentContext, UserMessage+from initialize import initialize_agent++# Attempt to import FastA2A – fall back to stubs so linters stay quiet+try:+ from fasta2a import Worker # type: ignore+ from fasta2a.broker import InMemoryBroker # type: ignore+ from fasta2a.storage import InMemoryStorage # type: ignore+ from fasta2a.schema import Message, Artifact, AgentProvider, Skill # type: ignore++ FASTA2A_AVAILABLE = True+except ImportError: # pragma: no cover – library not installed+ FASTA2A_AVAILABLE = False++ class Worker: # type: ignore+ """Stub so type-checkers don’t complain when FastA2A is absent."""++ pass++ # Minimal stubs for type checkers+ Message = Artifact = AgentProvider = Skill = Any # type: ignore+ InMemoryBroker = InMemoryStorage = object # type: ignore++_PRINTER = PrintStyle(italic=True, font_color="purple", padding=False)++if FASTA2A_AVAILABLE:+ class AgentZeroWorker(Worker): # type: ignore[misc]+ """Agent Zero implementation of FastA2A Worker."""++ async def run_task(self, params: Any) -> None: # params: TaskSendParams+ """Execute a task by processing the message through Agent Zero."""+ try:+ task_id = params['id']+ context_id = params['context_id']+ message = params['message']++ _PRINTER.print(f"[A2A] Processing task {task_id} in context {context_id}")++ # Convert A2A message to Agent Zero format+ agent_message = self._convert_message(message)++ # Get or create Agent Zero context+ context = AgentContext.get(context_id)+ if not context:+ # Create new context for this A2A conversation+ cfg = initialize_agent()+ context = AgentContext(cfg, id=context_id)++ # Log user message so it appears instantly in UI chat window+ context.log.log(+ type="user", # type: ignore[arg-type]+ heading="Remote user message",+ content=agent_message.message,+ kvps={"from": "A2A"},+ temp=False,+ )++ # Process message through Agent Zero (includes response)+ task = context.communicate(agent_message)+ result_text = await task.result()++ # Build A2A message from result+ response_message: Message = { # type: ignore+ 'role': 'agent',+ 'parts': [{'kind': 'text', 'text': str(result_text)}],+ 'kind': 'message',+ 'message_id': str(uuid.uuid4())+ }++ await self.storage.update_task( # type: ignore[attr-defined]+ task_id=task_id,+ state='completed',+ new_messages=[response_message]+ )++ _PRINTER.print(f"[A2A] Completed task {task_id}")++ except Exception as e:+ _PRINTER.print(f"[A2A] Error processing task {params.get('id', 'unknown')}: {e}")+ await self.storage.update_task(+ task_id=params.get('id', 'unknown'),+ state='failed'+ )++ async def cancel_task(self, params: Any) -> None: # params: TaskIdParams+ """Cancel a running task."""+ task_id = params['id']+ _PRINTER.print(f"[A2A] Cancelling task {task_id}")+ await self.storage.update_task(task_id=task_id, state='canceled') # type: ignore[attr-defined]++ def build_message_history(self, history: List[Any]) -> List[Message]: # type: ignore+ # Not used in this simplified implementation+ return []++ def build_artifacts(self, result: Any) -> List[Artifact]: # type: ignore+ # No artifacts for now+ return []++ def _convert_message(self, a2a_message: Message) -> UserMessage: # type: ignore+ """Convert A2A message to Agent Zero UserMessage."""+ # Extract text from message parts+ text_parts = [part.get('text', '') for part in a2a_message.get('parts', []) if part.get('kind') == 'text']+ message_text = '\n'.join(text_parts)++ # Extract file attachments+ attachments = []+ for part in a2a_message.get('parts', []):+ if part.get('kind') == 'file':+ file_info = part.get('file', {})+ if 'uri' in file_info:+ attachments.append(file_info['uri'])++ return UserMessage(+ message=message_text,+ attachments=attachments+ )+++class DynamicA2AProxy:+ """Dynamic proxy for FastA2A server that allows reconfiguration."""++ _instance = None++ def __init__(self):+ self.app = None+ self._async_lock = asyncio.Lock()+ self._startup_done: bool = False+ self._worker_bg_task: asyncio.Task | None = None++ if FASTA2A_AVAILABLE:+ self._configure()+ self._register_shutdown()++ @staticmethod+ def get_instance():+ if DynamicA2AProxy._instance is None:+ DynamicA2AProxy._instance = DynamicA2AProxy()+ return DynamicA2AProxy._instance++ def _configure(self):+ """Configure the FastA2A application with Agent Zero integration."""+ try:+ # Import inside the method to handle missing dependencies gracefully+ from fasta2a import FastA2A # type: ignore+ from fasta2a.broker import InMemoryBroker # type: ignore+ from fasta2a.storage import InMemoryStorage # type: ignore++ storage = InMemoryStorage() # type: ignore[arg-type]+ broker = InMemoryBroker() # type: ignore[arg-type]++ # Define Agent Zero's skills+ skills: List[Skill] = [{ # type: ignore+ "id": "general_assistance",+ "name": "General AI Assistant",+ "description": "Provides general AI assistance including code execution, file management, web browsing, and problem solving",+ "tags": ["ai", "assistant", "code", "files", "web", "automation"],+ "examples": [+ "Write and execute Python code",+ "Manage files and directories",+ "Browse the web and extract information",+ "Solve complex problems step by step",+ "Install software and manage systems"+ ],+ "input_modes": ["text/plain", "application/octet-stream"],+ "output_modes": ["text/plain", "application/json"]+ }]++ provider: AgentProvider = { # type: ignore+ "organization": "Agent Zero",+ "url": "https://github.com/frdel/agent-zero"+ }++ # Authentication middleware (Bearer or X-API-KEY)+ class A2AAuthMiddleware(BaseHTTPMiddleware):+ async def dispatch(self, request: Request, call_next): # type: ignore[override]+ cfg = settings.get_settings()+ expected = cfg.get("a2a_token") or cfg.get("mcp_server_token")+ if not expected:+ return await call_next(request) # no auth configured++ auth_header = request.headers.get("Authorization", "")+ if auth_header.startswith("Bearer ") and auth_header.split(" ", 1)[1] == expected:+ return await call_next(request)++ api_key = request.headers.get("X-API-KEY") or request.query_params.get("api_key")+ if api_key == expected:+ return await call_next(request)++ # Fallback: check token in mount path (root_path contains the prefix stripped by DispatcherMiddleware)+ root_path = request.scope.get("root_path", "") # type: ignore[attr-defined]+ if root_path and f"/t-{expected}" in root_path:+ return await call_next(request)++ return PlainTextResponse("Unauthorized", 401)++ middleware_list = [Middleware(A2AAuthMiddleware)]++ self.app = FastA2A( # type: ignore+ storage=storage,+ broker=broker,+ name="Agent Zero",+ description=(+ "A general AI assistant that can execute code, manage files, browse the web, and "+ "solve complex problems in an isolated Linux environment."+ ),+ version="1.0.0",+ provider=provider,+ skills=skills,+ lifespan=None, # We manage lifespan manually+ middleware=middleware_list,+ )++ # Store for later lazy startup (needs active event-loop)+ self._storage = storage # type: ignore[attr-defined]+ self._broker = broker # type: ignore[attr-defined]+ self._worker = AgentZeroWorker(broker=broker, storage=storage) # type: ignore[attr-defined]++ _PRINTER.print("[A2A] FastA2A server configured successfully")++ except Exception as e:+ _PRINTER.print(f"[A2A] Failed to configure FastA2A server: {e}")+ self.app = None++ # ---------------------------------------------------------------------+ # Shutdown handling+ # ---------------------------------------------------------------------++ def _register_shutdown(self):+ """Register an atexit hook to gracefully stop worker & task manager."""++ def _sync_shutdown():+ try:+ if not self._startup_done or not FASTA2A_AVAILABLE:+ return+ loop = asyncio.new_event_loop()+ loop.run_until_complete(self._async_shutdown())+ loop.close()+ except Exception:+ pass # ignore errors during interpreter shutdown++ atexit.register(_sync_shutdown)++ async def _async_shutdown(self):+ """Async shutdown: cancel worker task & close task manager."""+ if self._worker_bg_task and not self._worker_bg_task.done():+ self._worker_bg_task.cancel()+ with contextlib.suppress(asyncio.CancelledError):+ await self._worker_bg_task+ try:+ await self.app.task_manager.__aexit__(None, None, None) # type: ignore[attr-defined]+ except Exception:+ pass++ async def _startup(self):+ """Ensure TaskManager and Worker are running inside current event-loop."""+ if self._startup_done or not FASTA2A_AVAILABLE:+ return+ self._startup_done = True++ # Start task manager+ await self.app.task_manager.__aenter__() # type: ignore[attr-defined]++ async def _worker_loop():+ async with self._worker.run(): # type: ignore[attr-defined]+ await asyncio.Event().wait()++ # fire-and-forget background task – keep reference+ self._worker_bg_task = asyncio.create_task(_worker_loop())+ _PRINTER.print("[A2A] Worker & TaskManager started")++ async def reconfigure(self):+ """Reconfigure the FastA2A server (placeholder for future use)."""+ async with self._async_lock:+ if FASTA2A_AVAILABLE:+ self._configure()++ async def __call__(self, scope, receive, send):+ """ASGI application interface."""+ if self.app is None:+ # FastA2A not available, return 503+ response = b'HTTP/1.1 503 Service Unavailable\r\n\r\nFastA2A not available'+ await send({+ 'type': 'http.response.start',+ 'status': 503,+ 'headers': [[b'content-type', b'text/plain']],+ })+ await send({+ 'type': 'http.response.body',+ 'body': response,+ })+ return++ # Lazy-start background components the first time we get a request+ if FASTA2A_AVAILABLE and not self._startup_done:+ await self._startup()++ # Delegate to FastA2A app+ await self.app(scope, receive, send)+++def is_available():+ """Check if FastA2A is available and properly configured."""+ return FASTA2A_AVAILABLE and DynamicA2AProxy.get_instance().app is not None+++def get_proxy():+ """Get the FastA2A proxy instance."""+ if not FASTA2A_AVAILABLE:+ return None+ return DynamicA2AProxy.get_instance()diff --git a/python/helpers/settings.py b/python/helpers/settings.pyindex 4ea9a23eb..f9fe45d9a 100644--- a/python/helpers/settings.py+++ b/python/helpers/settings.py@@ -67,7 +67,7 @@ class Settings(TypedDict):memory_memorize_enabled: boolmemory_memorize_consolidation: boolmemory_memorize_replace_threshold: float-+api_keys: dict[str, str]@@ -493,6 +493,25 @@ def convert_out(settings: Settings) -> SettingsOutput:})+ # -------- A2A Section --------+ a2a_fields: list[SettingsField] = [+ {+ "id": "show_a2a_connection",+ "title": "Show A2A connection info",+ "description": "Display the URL (including token) other agents can use to connect via FastA2A.",+ "type": "button",+ "value": "Show",+ }+ ]++ a2a_section: SettingsSection = {+ "id": "a2a_server",+ "title": "A2A Connection",+ "description": "Share this connection string with other agents.",+ "fields": a2a_fields,+ "tab": "external",+ }+if runtime.is_dockerized():auth_fields.append({@@ -880,7 +899,7 @@ def convert_out(settings: Settings) -> SettingsOutput:# TTS fieldstts_fields: list[SettingsField] = []-+tts_fields.append({"id": "tts_kokoro",@@ -1028,6 +1047,7 @@ def convert_out(settings: Settings) -> SettingsOutput:speech_section,api_keys_section,auth_section,+ a2a_section,mcp_client_section,mcp_server_section,backup_section,diff --git a/python/tools/a2a_chat.py b/python/tools/a2a_chat.pynew file mode 100644index 000000000..6ee664449--- /dev/null+++ b/python/tools/a2a_chat.py@@ -0,0 +1,57 @@+from python.helpers.tool import Tool, Response+from python.helpers.print_style import PrintStyle++try:+ from python.helpers.fasta2a_client import connect_to_agent, is_client_available # type: ignore+except ImportError: # pragma: no cover – client helper missing+ is_client_available = lambda: False # type: ignore+++class A2AChatTool(Tool):+ """Communicate with another FastA2A-compatible agent."""++ async def execute(self, **kwargs):+ if not is_client_available():+ return Response(message="FastA2A client not available on this instance.", break_loop=False)++ agent_url: str | None = kwargs.get("agent_url") # required+ user_message: str | None = kwargs.get("message") # required+ attachments = kwargs.get("attachments", None) # optional list[str]+ reset = bool(kwargs.get("reset", False))+ if not agent_url or not isinstance(agent_url, str):+ return Response(message="agent_url argument missing", break_loop=False)+ if not user_message or not isinstance(user_message, str):+ return Response(message="message argument missing", break_loop=False)++ # Retrieve or create session cache on the Agent instance+ sessions: dict[str, str] = self.agent.get_data("_a2a_sessions") or {}++ # Handle reset flag – start fresh conversation+ if reset and agent_url in sessions:+ sessions.pop(agent_url, None)++ context_id = None if reset else sessions.get(agent_url)+ try:+ async with await connect_to_agent(agent_url) as conn:+ task_resp = await conn.send_message(user_message, attachments=attachments, context_id=context_id)+ task_id = task_resp.get("result", {}).get("id") # type: ignore[index]+ if not task_id:+ return Response(message="Remote agent failed to create task.", break_loop=False)+ final = await conn.wait_for_completion(task_id)+ new_context_id = final["result"].get("context_id") # type: ignore[index]+ if isinstance(new_context_id, str):+ sessions[agent_url] = new_context_id+ # persist back to agent data+ self.agent.set_data("_a2a_sessions", sessions)+ # Extract latest assistant text+ history = final["result"].get("history", [])+ assistant_text = ""+ if history:+ last_parts = history[-1].get("parts", [])+ assistant_text = "\n".join(+ p.get("text", "") for p in last_parts if p.get("kind") == "text"+ )+ return Response(message=assistant_text or "(no response)", break_loop=False)+ except Exception as e:+ PrintStyle.error(f"A2A chat error: {e}")+ return Response(message=f"A2A chat error: {e}", break_loop=False)diff --git a/requirements.txt b/requirements.txtindex 738145f1b..799335b10 100644--- a/requirements.txt+++ b/requirements.txt@@ -5,6 +5,7 @@ docker==7.1.0duckduckgo-search==6.1.12faiss-cpu==1.11.0fastmcp==2.3.4+fasta2a==0.5.0flask[async]==3.0.3flask-basicauth==0.2.0flaredantic==0.1.4diff --git a/run_ui.py b/run_ui.pyindex 07ae1c82f..f3451939c 100644--- a/run_ui.py+++ b/run_ui.py@@ -1,27 +1,33 @@from datetime import timedeltaimport osimport secrets-import sysimport timeimport socketimport structfrom functools import wrapsimport threading-import signal-from typing import overridefrom flask import Flask, request, Response, sessionfrom flask_basicauth import BasicAuthimport initialize-from python.helpers import errors, files, git, mcp_server+from python.helpers import files, git, mcp_serverfrom python.helpers.files import get_abs_pathfrom python.helpers import runtime, dotenv, processfrom python.helpers.extract_tools import load_classes_from_folderfrom python.helpers.api import ApiHandlerfrom python.helpers.print_style import PrintStyle+# Try to import fasta2a_server, but handle gracefully if it fails+try:+ from python.helpers import fasta2a_server+ FASTA2A_AVAILABLE = True+except ImportError:+ FASTA2A_AVAILABLE = False+ fasta2a_server = None+# Set the new timezone to 'UTC'os.environ["TZ"] = "UTC"+os.environ["TOKENIZERS_PARALLELISM"] = "false"# Apply the timezone changeif hasattr(time, 'tzset'):time.tzset()@@ -171,7 +177,7 @@ def run():from werkzeug.serving import WSGIRequestHandlerfrom werkzeug.serving import make_serverfrom werkzeug.middleware.dispatcher import DispatcherMiddleware- from a2wsgi import ASGIMiddleware, WSGIMiddleware+ from a2wsgi import ASGIMiddlewarePrintStyle().print("Starting server...")@@ -214,13 +220,27 @@ def run():for handler in handlers:register_api_handler(webapp, handler)- # add the webapp and mcp to the app- app = DispatcherMiddleware(- webapp,- {- "/mcp": ASGIMiddleware(app=mcp_server.DynamicMcpProxy.get_instance()), # type: ignore- },- )+ # add the webapp, mcp, and a2a to the app+ middleware_routes = {+ "/mcp": ASGIMiddleware(app=mcp_server.DynamicMcpProxy.get_instance()), # type: ignore+ }++ # Add A2A server if available+ if FASTA2A_AVAILABLE and fasta2a_server and fasta2a_server.is_available():+ a2a_proxy = fasta2a_server.get_proxy()+ if a2a_proxy:+ from python.helpers import settings as _s+ cfg = _s.get_settings()+ token = cfg.get("a2a_token") or cfg.get("mcp_server_token")+ mount_path = f"/a2a/t-{token}"+ middleware_routes[mount_path] = ASGIMiddleware(app=a2a_proxy) # type: ignore+ # Also mount at /a2a for agent card and convenience+ middleware_routes["/a2a"] = ASGIMiddleware(app=a2a_proxy) # type: ignore+ PrintStyle().print(f"FastA2A server enabled at {mount_path} and /a2a")+ else:+ PrintStyle().debug("FastA2A server not available")++ app = DispatcherMiddleware(webapp, middleware_routes) # type: ignorePrintStyle().debug(f"Starting server at http://{host}:{port} ...")diff --git a/test_fasta2a_client.py b/test_fasta2a_client.pynew file mode 100644index 000000000..386964bd4--- /dev/null+++ b/test_fasta2a_client.py@@ -0,0 +1,56 @@+import asyncio+import sys+from typing import Any, Dict++from python.helpers.fasta2a_client import (+ connect_to_agent,+ is_client_available,+)+++async def chat_loop(base_url: str):+ if not is_client_available():+ print("FastA2A client library not available – install `fasta2a httpx` first.")+ return++ # Establish connection (agent card is fetched internally)+ async with await connect_to_agent(base_url) as conn:+ print(f"[✓] Connected to {base_url}")+ print('Type "exit" to quit.')++ while True:+ try:+ user_msg = input("You: ").strip()+ except (EOFError, KeyboardInterrupt):+ print()+ break++ if user_msg.lower() in {"exit", "quit"}:+ break++ try:+ # Send message & get immediate task response+ task_response: Dict[str, Any] = await conn.send_message(message=user_msg)+ task_id = task_response.get("result", {}).get("id")+ if not task_id:+ print("[!] No task ID returned – response: ", task_response)+ continue++ # Wait for the task to complete and fetch final result+ final = await conn.wait_for_completion(task_id)+ latest_history = final["result"].get("history", [])+ assistant_parts = latest_history[-1].get("parts", []) if latest_history else []+ assistant_text = "\n".join(+ p.get("text", "") for p in assistant_parts if p.get("kind") == "text"+ )+ print(f"Assistant: {assistant_text}\n")+ except Exception as e:+ print(f"[!] Error: {e}")+++if __name__ == "__main__":+ if len(sys.argv) < 2:+ print("Usage: python test_fasta2a_client.py <AGENT_BASE_URL>")+ sys.exit(1)++ asyncio.run(chat_loop(sys.argv[1]))diff --git a/webui/components/settings/external/a2a-connection.html b/webui/components/settings/external/a2a-connection.htmlnew file mode 100644index 000000000..fbc39885d--- /dev/null+++ b/webui/components/settings/external/a2a-connection.html@@ -0,0 +1,47 @@+<html>++<head>+ <title>Connection to A0 A2A Server</title>+</head>++<body>+ <div x-data>+ <p>Agent Zero A2A Server enables FastA2A protocol communication with other agents.</p>+ <p>Other agents can connect using the URL below (replace host if needed):</p>++ <h3>A2A Connection URL</h3>+ <div id="a2a-connection-example"></div>++ <script>+ setTimeout(() => {+ const url = window.location.origin;+ // Try to get a2a_token first, fallback to mcp_server_token+ let tokenField = null;+ try {+ const allFields = settingsModalProxy.settings.sections.flatMap(s => s.fields);+ tokenField = allFields.find(f => f.id === 'a2a_token') || allFields.find(f => f.id === 'mcp_server_token');+ } catch (e) { }+ const token = tokenField ? tokenField.value : '';+ const connectionUrl = `${url}/a2a/t-${token}`;++ const editor = ace.edit("a2a-connection-example");+ const dark = localStorage.getItem("darkMode");+ editor.setTheme(dark !== "false" ? "ace/theme/github_dark" : "ace/theme/tomorrow");+ editor.session.setMode("ace/mode/text");+ editor.setValue(connectionUrl);+ editor.clearSelection();+ editor.setReadOnly(true);+ }, 0);+ </script>+ </div>++ <style>+ #a2a-connection-example {+ width: 100%;+ height: 3em;+ }+ </style>++</body>++</html>diff --git a/webui/js/settings.js b/webui/js/settings.jsindex f58cb1c97..7b82cb58c 100644--- a/webui/js/settings.js+++ b/webui/js/settings.js@@ -1,4 +1,3 @@-const settingsModalProxy = {isOpen: false,settings: {},@@ -291,6 +290,9 @@ const settingsModalProxy = {openModal("settings/backup/backup.html");} else if (field.id === "backup_restore") {openModal("settings/backup/restore.html");+ } else if (field.id === "show_a2a_connection") {+ console.log('Opening A2A connection modal...');+ openModal("settings/external/a2a-connection.html");}}};diff --git a/webui/public/a2a_server.svg b/webui/public/a2a_server.svgnew file mode 100644index 000000000..626a60668--- /dev/null+++ b/webui/public/a2a_server.svg@@ -0,0 +1,4 @@+<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">+ <path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.72"/>+ <path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.72-1.72"/>+</svg>