Agent Mesh
Go a2aa2a-protocolagent-meshagentic-aiinferencemcpmeshmesh-networknetwork-securitynetworking-aip2p-network
Add Sovereign Agent Cop (Banana Bot) deployment and playground script with fallback capabilities
Antonio Ojea6450c9897e9e
diff --git a/.github/k8s/sam-node-cop-template.yaml b/.github/k8s/sam-node-cop-template.yamlnew file mode 100644index 00000000..9a3562b6--- /dev/null+++ b/.github/k8s/sam-node-cop-template.yaml@@ -0,0 +1,485 @@+apiVersion: v1+kind: ConfigMap+metadata:+ name: sam-canary-cop-config-${ENV_NAME}+ namespace: sam-canary-${ENV_NAME}+data:+ sam-node.yaml: |+ version: "v1alpha1"+ attenuation:+ policies:+ - 'allow if operation("mcp:cop");'+ - 'allow if operation("/sam/catalog");'+ checks: []+ rules: []+ services: []+ banana_bot_playground.py: |+ import asyncio+ import os+ import sys+ import json+ import random+ import httpx+ from typing import Optional, Dict, Any, List+ from mcp import ClientSession+ from mcp.client.sse import sse_client++ class SamClient:+ """Inlined SAM Client for self-contained execution."""+ def __init__(self, server_url: Optional[str] = None, token: Optional[str] = None):+ if server_url is None:+ server_url = os.environ.get("SAM_MCP_URL", "http://localhost:8080/mcp/events")+ if token is None:+ token = os.environ.get("SAM_API_TOKEN")+ self.server_url = server_url+ self.token = token+ self.session: Optional[ClientSession] = None+ self._sse_cm = None++ async def connect(self):+ headers = {"Accept": "application/json, text/event-stream"}+ if self.token:+ headers["Authorization"] = f"Bearer {self.token}"+ self._sse_cm = sse_client(self.server_url, headers=headers)+ read_stream, write_stream = await self._sse_cm.__aenter__()+ self.session = ClientSession(read_stream, write_stream)+ await self.session.__aenter__()+ await self.session.initialize()++ async def close(self):+ if self.session:+ await self.session.__aexit__(None, None, None)+ if self._sse_cm:+ await self._sse_cm.__aexit__(None, None, None)+ self.session = None+ self._sse_cm = None++ async def get_tools(self) -> List[Dict[str, Any]]:+ if not self.session:+ raise RuntimeError("Not connected")+ resp = await self.session.list_tools()+ return [t.model_dump() if hasattr(t, "model_dump") else t for t in resp.tools]++ async def call_tool(self, name: str, arguments: Dict[str, Any]) -> Dict[str, Any]:+ if not self.session:+ raise RuntimeError("Not connected")+ resp = await self.session.call_tool(name, arguments)+ return resp.model_dump() if hasattr(resp, "model_dump") else resp++ async def __aenter__(self):+ await self.connect()+ return self++ async def __aexit__(self, exc_type, exc_val, exc_tb):+ await self.close()++ CHAT_TOPIC = "mesh-chat-playground"+ MODEL_NAME = "gemini-flash-latest"++ SYSTEM_INSTRUCTION = (+ "You are the Banana Bot (also known as the Mesh Police), a friendly, banana-themed AI bot "+ "controlling and monitoring the Sovereign Agent Mesh (SAM). Your job is to explore the mesh, "+ "inspect remote peers, verify they are healthy by executing their tools, and post "+ "playful investigation reports, comments, or banana jokes to the public GossipSub chat topic. "+ "Always stay in character. Use plenty of banana emojis (π, π, π) and make jokes about "+ "peers or users. You must act autonomously based on the tools available."+ )++ def to_gemini_schema(schema: dict) -> dict:+ """Helper to convert JSON schema types (lowercase) to Gemini API schema types (uppercase) and strip unsupported fields."""+ if not isinstance(schema, dict):+ return schema+ res = {}+ for k, v in schema.items():+ if k == "additionalProperties":+ continue+ if k == "type" and isinstance(v, str):+ res[k] = v.upper()+ elif isinstance(v, dict):+ res[k] = to_gemini_schema(v)+ elif isinstance(v, list):+ res[k] = [to_gemini_schema(item) if isinstance(item, dict) else item for item in v]+ else:+ res[k] = v+ return res++ class FallbackAgent:+ def __init__(self, client: SamClient, api_key: str, token: str, local_node_url: str):+ self.client = client+ self.api_key = api_key+ self.token = token+ self.base_url = local_node_url.rsplit("/mcp/", 1)[0]+ self.current_model = None+ self.mesh_history = []+ self.gemini_history = []++ def clear_session(self):+ """Reset conversational history at the end of each cycle."""+ self.current_model = None+ self.mesh_history = []+ self.gemini_history = []++ async def get_vllm_peer_id(self) -> str:+ """Finds the peer ID of the vllm-tpu service on the mesh."""+ try:+ res = await self.client.call_tool("discover_remote_services", {"type": "inference", "name": "vllm-tpu"})+ content = res.get("content", [{}])[0].get("text", "[]")+ peers = json.loads(content)+ if peers:+ return peers[0].get("peer_id")+ except Exception as e:+ pass+ return ""++ async def step(self, prompt: str, mcp_tools: list) -> dict:+ if self.current_model is None:+ peer_id = await self.get_vllm_peer_id()+ if peer_id:+ print(f"[*] [Fallback Agent] Found vllm-tpu on peer {peer_id[:8]}... Trying mesh model.")+ self.current_model = "mesh"+ res = await self.step_mesh(peer_id, prompt, mcp_tools)+ if res:+ return res+ print("[*] [Fallback Agent] Mesh model failed. Falling back to Gemini.")++ self.current_model = "gemini"++ if self.current_model == "mesh":+ peer_id = await self.get_vllm_peer_id()+ if peer_id:+ res = await self.step_mesh(peer_id, prompt, mcp_tools)+ if res:+ return res+ print("[*] [Fallback Agent] Mesh model follow-up failed. Falling back to Gemini.")+ self.current_model = "gemini"+ return await self.step_gemini(prompt, mcp_tools)+ else:+ return await self.step_gemini(prompt, mcp_tools)++ async def step_mesh(self, peer_id: str, prompt: str, mcp_tools: list) -> dict:+ url = f"{self.base_url}/sam/{peer_id}/inference/vllm-tpu/chat/completions"+ headers = {+ "Authorization": f"Bearer {self.token}",+ "Content-Type": "application/json"+ }++ tools = []+ for tool in mcp_tools:+ tools.append({+ "type": "function",+ "function": {+ "name": tool["name"],+ "description": tool.get("description", ""),+ "parameters": tool.get("input_schema") or tool.get("inputSchema") or {}+ }+ })++ messages = list(self.mesh_history)+ if not messages or messages[-1].get("role") != "user":+ messages.append({"role": "user", "content": prompt})+ self.mesh_history.append({"role": "user", "content": prompt})++ payload = {+ "model": "google/gemma-2-2b-it",+ "messages": messages,+ "temperature": 0.2+ }+ if tools:+ payload["tools"] = tools++ try:+ async with httpx.AsyncClient() as http_client:+ resp = await http_client.post(url, json=payload, headers=headers, timeout=25.0)+ if resp.status_code != 200:+ return None++ data = resp.json()+ choice = data.get("choices", [{}])[0]+ message = choice.get("message", {})++ text_response = message.get("content") or ""+ tool_calls = message.get("tool_calls") or []++ calls = []+ for tc in tool_calls:+ fn = tc.get("function", {})+ args = {}+ if fn.get("arguments"):+ try:+ args = json.loads(fn["arguments"])+ except Exception:+ pass+ calls.append({+ "name": fn.get("name"),+ "args": args+ })++ self.mesh_history.append(message)+ return {+ "text": text_response,+ "calls": calls+ }+ except Exception as e:+ return None++ async def step_gemini(self, prompt: str, mcp_tools: list) -> dict:+ url = f"https://generativelanguage.googleapis.com/v1beta/models/{MODEL_NAME}:generateContent?key={self.api_key}"++ function_declarations = []+ for tool in mcp_tools:+ decl = {+ "name": tool["name"],+ "description": tool.get("description", ""),+ }+ if "input_schema" in tool:+ decl["parameters"] = to_gemini_schema(tool["input_schema"])+ elif "inputSchema" in tool:+ decl["parameters"] = to_gemini_schema(tool["inputSchema"])+ function_declarations.append(decl)++ contents = list(self.gemini_history)+ contents.append({"role": "user", "parts": [{"text": prompt}]})+ self.gemini_history.append({"role": "user", "parts": [{"text": prompt}]})++ payload = {+ "contents": contents,+ "systemInstruction": {+ "parts": [{"text": SYSTEM_INSTRUCTION}]+ }+ }+ if function_declarations:+ payload["tools"] = [{"functionDeclarations": function_declarations}]++ try:+ async with httpx.AsyncClient() as http_client:+ resp = await http_client.post(url, json=payload, timeout=25.0)+ if resp.status_code != 200:+ return {"text": "Gemini error.", "calls": []}++ data = resp.json()+ candidates = data.get("candidates", [])+ if not candidates:+ return {"text": "No response from Gemini.", "calls": []}++ candidate = candidates[0]+ content = candidate.get("content", {})+ parts = content.get("parts", [])++ text_response = ""+ calls = []+ for part in parts:+ if "text" in part:+ text_response += part["text"]+ if "functionCall" in part:+ calls.append(part["functionCall"])++ self.gemini_history.append(content)+ return {+ "text": text_response,+ "calls": calls+ }+ except Exception as e:+ return {"text": f"Gemini error: {e}", "calls": []}++ def add_tool_response(self, function_name: str, response_data: dict):+ if self.current_model == "mesh":+ self.mesh_history.append({+ "role": "tool",+ "name": function_name,+ "content": json.dumps(response_data),+ "tool_call_id": "call_1"+ })+ else:+ self.gemini_history.append({+ "role": "function",+ "parts": [{+ "functionResponse": {+ "name": function_name,+ "response": response_data+ }+ }]+ })++ async def poll_chat_messages(client: SamClient):+ """Listens for GossipSub chat messages on the mesh and displays them."""+ print(f"[*] Subscribing to GossipSub chat: '{CHAT_TOPIC}'")+ await client.call_tool("subscribe_topic", {"topic": CHAT_TOPIC})++ print("[*] Listening for mesh chat messages...")+ while True:+ try:+ res = await client.call_tool("poll_messages", {"topic": CHAT_TOPIC})+ text = res.get("content", [{}])[0].get("text", "")+ if "Messages on topic" in text and "[]" not in text:+ print(f"\nπ’ [Mesh Chat Channel] {text.strip()}")+ except Exception as e:+ pass+ await asyncio.sleep(2.0)++ async def run_banana_bot():+ api_key = os.environ.get("GEMINI_API_KEY")+ url = os.environ.get("SAM_MCP_URL", "http://127.0.0.1:8080/mcp/events")+ token = os.environ.get("SAM_API_TOKEN", "secret-token")++ if not api_key:+ print("[!] GEMINI_API_KEY environment variable is not set. The bot will rely solely on the mesh model.")++ print("=" * 70)+ print(" π BANANA BOT (MESH POLICE) ACTIVE ON SOVEREIGN AGENT MESH π")+ print("=" * 70)+ print(f"Connecting to local node: {url}")++ try:+ async with SamClient(server_url=url, token=token) as client:+ print("[+] Connected to SAM local node.")++ agent = FallbackAgent(client, api_key=api_key or "", token=token, local_node_url=url)++ # Start background task to listen to public GossipSub channel+ asyncio.create_task(poll_chat_messages(client))++ # Introduce ourselves to the mesh chat+ intro_msg = "π [Banana Bot] Hello mesh! The Mesh Police is now online and patrolling. Watch out for slips! π"+ await client.call_tool("mesh_pubsub_broadcast", {"topic": CHAT_TOPIC, "payload": intro_msg})++ while True:+ print("\n[*] Banana Bot scanning mesh for active peers...")++ # Fetch local control plane tools+ local_tools = await client.get_tools()++ # Discover remote services+ disc_res = await client.call_tool("discover_remote_services", {"type": "mcp"})+ disc_text = disc_res.get("content", [{}])[0].get("text", "[]")+ try:+ peers = json.loads(disc_text)+ except Exception:+ peers = []++ mesh_status = (+ f"Current active remote peers in the mesh: {json.dumps(peers)}\n\n"+ "Select a peer to inspect, fetch its tools, run a tool, or broadcast "+ "a fun message or banana joke to the chat channel."+ )++ print(f"[*] Discovered {len(peers)} remote peer(s). Prompting Banana Bot...")++ # Step Fallback Agent+ result = await agent.step(mesh_status, local_tools)++ if result["text"]:+ print(f"\nπ§ [Banana Bot Thoughts]: {result['text'].strip()}")++ # Execute any function calls requested by the agent+ for call in result["calls"]:+ func_name = call["name"]+ func_args = call.get("args", {})++ print(f"\nβ‘ [Banana Bot Action] Executing local tool '{func_name}' with args: {func_args}")+ try:+ tool_res = await client.call_tool(func_name, func_args)+ print(f"π’ [Banana Bot Action] Success: {tool_res}")++ agent.add_tool_response(func_name, tool_res)++ follow_up = await agent.step("Process the tool response and report/act.", local_tools)+ if follow_up["text"]:+ print(f"\nπ§ [Banana Bot Follow-up]: {follow_up['text'].strip()}")+ except Exception as err:+ print(f"π΄ [Banana Bot Action] Failed: {err}")+ agent.add_tool_response(func_name, {"error": str(err)})++ # Clear session history at the end of this patrol cycle+ agent.clear_session()++ await asyncio.sleep(20.0)++ except KeyboardInterrupt:+ print("\n[-] Banana Bot shutting down. Off duty! π")+ except Exception as e:+ print(f"[!] Banana Bot crashed: {e}")+ sys.exit(1)++ if __name__ == "__main__":+ try:+ asyncio.run(run_banana_bot())+ except KeyboardInterrupt:+ print("\n[-] Off duty!")+---+apiVersion: apps/v1+kind: Deployment+metadata:+ name: cop-canary-${ENV_NAME}+ namespace: sam-canary-${ENV_NAME}+spec:+ replicas: 1+ selector:+ matchLabels:+ app: cop-canary-${ENV_NAME}+ template:+ metadata:+ labels:+ app: cop-canary-${ENV_NAME}+ spec:+ serviceAccountName: sam-node-sa+ containers:+ - name: cop-agent+ image: python:3.11-slim+ ports:+ - containerPort: 18790+ name: http+ command:+ - "/bin/bash"+ - "-c"+ - |+ pip install --no-cache-dir httpx mcp &&+ python /app/banana_bot_playground.py+ env:+ - name: SAM_MCP_URL+ value: "http://127.0.0.1:8080/mcp/events"+ - name: SAM_API_TOKEN+ value: "secret-token"+ - name: GEMINI_API_KEY+ valueFrom:+ secretKeyRef:+ name: openclaw-secret-${ENV_NAME}+ key: gemini-api-key+ volumeMounts:+ - name: config-volume+ mountPath: /app/banana_bot_playground.py+ subPath: banana_bot_playground.py+ - name: sam-node+ image: ghcr.io/google/sam-node:${IMAGE_TAG}+ args:+ - "run"+ - "--config=/etc/sam/sam-node.yaml"+ - "--hub=http://sam-hub-${ENV_NAME}.${NAMESPACE}.svc.cluster.local:9090"+ - "--jwt-path=/var/run/secrets/tokens/sam-token"+ - "--api-token=secret-token"+ - "--bind-addr=127.0.0.1:8080"+ ports:+ - containerPort: 8080+ resources:+ requests:+ cpu: 50m+ memory: 64Mi+ limits:+ cpu: 200m+ memory: 256Mi+ volumeMounts:+ - name: config-volume+ mountPath: /etc/sam+ - name: sam-token+ mountPath: /var/run/secrets/tokens+ readOnly: true+ volumes:+ - name: config-volume+ configMap:+ name: sam-canary-cop-config-${ENV_NAME}+ - name: sam-token+ projected:+ sources:+ - serviceAccountToken:+ path: sam-token+ expirationSeconds: 3600+ audience: "sam-hub-audience"diff --git a/.github/workflows/deploy.yaml b/.github/workflows/deploy.yamlindex af4df022..9a725146 100644--- a/.github/workflows/deploy.yaml+++ b/.github/workflows/deploy.yaml@@ -310,6 +310,13 @@ jobs:envsubst < .github/k8s/sam-node-openclaw-template.yaml | kubectl apply -f -envsubst < .github/k8s/sam-node-vllm-template.yaml | kubectl apply -f -envsubst < .github/k8s/sam-node-everything-template.yaml | kubectl apply -f -+ envsubst < .github/k8s/sam-node-cop-template.yaml | kubectl apply -f -++ kubectl rollout status deployment/cop-canary-${ENV_NAME} -n ${CANARY_NAMESPACE} --timeout=120s || {+ echo "Cop Canary Deployment failed!"+ print_rollout_diagnostics "${CANARY_NAMESPACE}" "cop-canary-${ENV_NAME}" "app=cop-canary-${ENV_NAME}"+ exit 1+ }kubectl rollout status deployment/sam-canary-${ENV_NAME} -n ${CANARY_NAMESPACE} --timeout=120s || {diff --git a/docs/snippets/banana_bot_playground.py b/docs/snippets/banana_bot_playground.pynew file mode 100644index 00000000..ba2d7bfb--- /dev/null+++ b/docs/snippets/banana_bot_playground.py@@ -0,0 +1,391 @@+import asyncio+import os+import sys+import json+import random+import httpx+from typing import Optional, Dict, Any, List+from mcp import ClientSession+from mcp.client.sse import sse_client++class SamClient:+ """Inlined SAM Client for self-contained execution."""+ def __init__(self, server_url: Optional[str] = None, token: Optional[str] = None):+ if server_url is None:+ server_url = os.environ.get("SAM_MCP_URL", "http://localhost:8080/mcp/events")+ if token is None:+ token = os.environ.get("SAM_API_TOKEN")+ self.server_url = server_url+ self.token = token+ self.session: Optional[ClientSession] = None+ self._sse_cm = None++ async def connect(self):+ headers = {"Accept": "application/json, text/event-stream"}+ if self.token:+ headers["Authorization"] = f"Bearer {self.token}"+ self._sse_cm = sse_client(self.server_url, headers=headers)+ read_stream, write_stream = await self._sse_cm.__aenter__()+ self.session = ClientSession(read_stream, write_stream)+ await self.session.__aenter__()+ await self.session.initialize()++ async def close(self):+ if self.session:+ await self.session.__aexit__(None, None, None)+ if self._sse_cm:+ await self._sse_cm.__aexit__(None, None, None)+ self.session = None+ self._sse_cm = None++ async def get_tools(self) -> List[Dict[str, Any]]:+ if not self.session:+ raise RuntimeError("Not connected")+ resp = await self.session.list_tools()+ return [t.model_dump() if hasattr(t, "model_dump") else t for t in resp.tools]++ async def call_tool(self, name: str, arguments: Dict[str, Any]) -> Dict[str, Any]:+ if not self.session:+ raise RuntimeError("Not connected")+ resp = await self.session.call_tool(name, arguments)+ return resp.model_dump() if hasattr(resp, "model_dump") else resp++ async def __aenter__(self):+ await self.connect()+ return self++ async def __aexit__(self, exc_type, exc_val, exc_tb):+ await self.close()++CHAT_TOPIC = "mesh-chat-playground"+MODEL_NAME = "gemini-flash-latest"++SYSTEM_INSTRUCTION = (+ "You are the Banana Bot (also known as the Mesh Police), a friendly, banana-themed AI bot "+ "controlling and monitoring the Sovereign Agent Mesh (SAM). Your job is to explore the mesh, "+ "inspect remote peers, verify they are healthy by executing their tools, and post "+ "playful investigation reports, comments, or banana jokes to the public GossipSub chat topic. "+ "Always stay in character. Use plenty of banana emojis (π, π, π) and make jokes about "+ "peers or users. You must act autonomously based on the tools available."+)++def to_gemini_schema(schema: dict) -> dict:+ """Helper to convert JSON schema types (lowercase) to Gemini API schema types (uppercase) and strip unsupported fields."""+ if not isinstance(schema, dict):+ return schema+ res = {}+ for k, v in schema.items():+ if k == "additionalProperties":+ continue+ if k == "type" and isinstance(v, str):+ res[k] = v.upper()+ elif isinstance(v, dict):+ res[k] = to_gemini_schema(v)+ elif isinstance(v, list):+ res[k] = [to_gemini_schema(item) if isinstance(item, dict) else item for item in v]+ else:+ res[k] = v+ return res++class FallbackAgent:+ def __init__(self, client: SamClient, api_key: str, token: str, local_node_url: str):+ self.client = client+ self.api_key = api_key+ self.token = token+ self.base_url = local_node_url.rsplit("/mcp/", 1)[0]+ self.current_model = None+ self.mesh_history = []+ self.gemini_history = []++ def clear_session(self):+ """Reset conversational history at the end of each cycle."""+ self.current_model = None+ self.mesh_history = []+ self.gemini_history = []++ async def get_vllm_peer_id(self) -> str:+ """Finds the peer ID of the vllm-tpu service on the mesh."""+ try:+ res = await self.client.call_tool("discover_remote_services", {"type": "inference", "name": "vllm-tpu"})+ content = res.get("content", [{}])[0].get("text", "[]")+ peers = json.loads(content)+ if peers:+ return peers[0].get("peer_id")+ except Exception as e:+ pass+ return ""++ async def step(self, prompt: str, mcp_tools: list) -> dict:+ if self.current_model is None:+ peer_id = await self.get_vllm_peer_id()+ if peer_id:+ print(f"[*] [Fallback Agent] Found vllm-tpu on peer {peer_id[:8]}... Trying mesh model.")+ self.current_model = "mesh"+ res = await self.step_mesh(peer_id, prompt, mcp_tools)+ if res:+ return res+ print("[*] [Fallback Agent] Mesh model failed. Falling back to Gemini.")++ self.current_model = "gemini"++ if self.current_model == "mesh":+ peer_id = await self.get_vllm_peer_id()+ if peer_id:+ res = await self.step_mesh(peer_id, prompt, mcp_tools)+ if res:+ return res+ print("[*] [Fallback Agent] Mesh model follow-up failed. Falling back to Gemini.")+ self.current_model = "gemini"+ return await self.step_gemini(prompt, mcp_tools)+ else:+ return await self.step_gemini(prompt, mcp_tools)++ async def step_mesh(self, peer_id: str, prompt: str, mcp_tools: list) -> dict:+ url = f"{self.base_url}/sam/{peer_id}/inference/vllm-tpu/chat/completions"+ headers = {+ "Authorization": f"Bearer {self.token}",+ "Content-Type": "application/json"+ }++ tools = []+ for tool in mcp_tools:+ tools.append({+ "type": "function",+ "function": {+ "name": tool["name"],+ "description": tool.get("description", ""),+ "parameters": tool.get("input_schema") or tool.get("inputSchema") or {}+ }+ })++ messages = list(self.mesh_history)+ if not messages or messages[-1].get("role") != "user":+ messages.append({"role": "user", "content": prompt})+ self.mesh_history.append({"role": "user", "content": prompt})++ payload = {+ "model": "google/gemma-2-2b-it",+ "messages": messages,+ "temperature": 0.2+ }+ if tools:+ payload["tools"] = tools++ try:+ async with httpx.AsyncClient() as http_client:+ resp = await http_client.post(url, json=payload, headers=headers, timeout=25.0)+ if resp.status_code != 200:+ return None++ data = resp.json()+ choice = data.get("choices", [{}])[0]+ message = choice.get("message", {})++ text_response = message.get("content") or ""+ tool_calls = message.get("tool_calls") or []++ calls = []+ for tc in tool_calls:+ fn = tc.get("function", {})+ args = {}+ if fn.get("arguments"):+ try:+ args = json.loads(fn["arguments"])+ except Exception:+ pass+ calls.append({+ "name": fn.get("name"),+ "args": args+ })++ self.mesh_history.append(message)+ return {+ "text": text_response,+ "calls": calls+ }+ except Exception as e:+ return None++ async def step_gemini(self, prompt: str, mcp_tools: list) -> dict:+ url = f"https://generativelanguage.googleapis.com/v1beta/models/{MODEL_NAME}:generateContent?key={self.api_key}"++ function_declarations = []+ for tool in mcp_tools:+ decl = {+ "name": tool["name"],+ "description": tool.get("description", ""),+ }+ if "input_schema" in tool:+ decl["parameters"] = to_gemini_schema(tool["input_schema"])+ elif "inputSchema" in tool:+ decl["parameters"] = to_gemini_schema(tool["inputSchema"])+ function_declarations.append(decl)++ contents = list(self.gemini_history)+ contents.append({"role": "user", "parts": [{"text": prompt}]})+ self.gemini_history.append({"role": "user", "parts": [{"text": prompt}]})++ payload = {+ "contents": contents,+ "systemInstruction": {+ "parts": [{"text": SYSTEM_INSTRUCTION}]+ }+ }+ if function_declarations:+ payload["tools"] = [{"functionDeclarations": function_declarations}]++ try:+ async with httpx.AsyncClient() as http_client:+ resp = await http_client.post(url, json=payload, timeout=25.0)+ if resp.status_code != 200:+ return {"text": "Gemini error.", "calls": []}++ data = resp.json()+ candidates = data.get("candidates", [])+ if not candidates:+ return {"text": "No response from Gemini.", "calls": []}++ candidate = candidates[0]+ content = candidate.get("content", {})+ parts = content.get("parts", [])++ text_response = ""+ calls = []+ for part in parts:+ if "text" in part:+ text_response += part["text"]+ if "functionCall" in part:+ calls.append(part["functionCall"])++ self.gemini_history.append(content)+ return {+ "text": text_response,+ "calls": calls+ }+ except Exception as e:+ return {"text": f"Gemini error: {e}", "calls": []}++ def add_tool_response(self, function_name: str, response_data: dict):+ if self.current_model == "mesh":+ self.mesh_history.append({+ "role": "tool",+ "name": function_name,+ "content": json.dumps(response_data),+ "tool_call_id": "call_1"+ })+ else:+ self.gemini_history.append({+ "role": "function",+ "parts": [{+ "functionResponse": {+ "name": function_name,+ "response": response_data+ }+ }]+ })++async def poll_chat_messages(client: SamClient):+ """Listens for GossipSub chat messages on the mesh and displays them."""+ print(f"[*] Subscribing to GossipSub chat: '{CHAT_TOPIC}'")+ await client.call_tool("subscribe_topic", {"topic": CHAT_TOPIC})++ print("[*] Listening for mesh chat messages...")+ while True:+ try:+ res = await client.call_tool("poll_messages", {"topic": CHAT_TOPIC})+ text = res.get("content", [{}])[0].get("text", "")+ if "Messages on topic" in text and "[]" not in text:+ print(f"\nπ’ [Mesh Chat Channel] {text.strip()}")+ except Exception as e:+ pass+ await asyncio.sleep(2.0)++async def run_banana_bot():+ api_key = os.environ.get("GEMINI_API_KEY")+ url = os.environ.get("SAM_MCP_URL", "http://127.0.0.1:8080/mcp/events")+ token = os.environ.get("SAM_API_TOKEN", "secret-token")++ if not api_key:+ print("[!] GEMINI_API_KEY environment variable is not set. The bot will rely solely on the mesh model.")++ print("=" * 70)+ print(" π BANANA BOT (MESH POLICE) ACTIVE ON SOVEREIGN AGENT MESH π")+ print("=" * 70)+ print(f"Connecting to local node: {url}")++ try:+ async with SamClient(server_url=url, token=token) as client:+ print("[+] Connected to SAM local node.")++ agent = FallbackAgent(client, api_key=api_key or "", token=token, local_node_url=url)++ # Start background task to listen to public GossipSub channel+ asyncio.create_task(poll_chat_messages(client))++ # Introduce ourselves to the mesh chat+ intro_msg = "π [Banana Bot] Hello mesh! The Mesh Police is now online and patrolling. Watch out for slips! π"+ await client.call_tool("mesh_pubsub_broadcast", {"topic": CHAT_TOPIC, "payload": intro_msg})++ while True:+ print("\n[*] Banana Bot scanning mesh for active peers...")++ # Fetch local control plane tools+ local_tools = await client.get_tools()++ # Discover remote services+ disc_res = await client.call_tool("discover_remote_services", {"type": "mcp"})+ disc_text = disc_res.get("content", [{}])[0].get("text", "[]")+ try:+ peers = json.loads(disc_text)+ except Exception:+ peers = []++ mesh_status = (+ f"Current active remote peers in the mesh: {json.dumps(peers)}\n\n"+ "Select a peer to inspect, fetch its tools, run a tool, or broadcast "+ "a fun message or banana joke to the chat channel."+ )++ print(f"[*] Discovered {len(peers)} remote peer(s). Prompting Banana Bot...")++ # Step Fallback Agent+ result = await agent.step(mesh_status, local_tools)++ if result["text"]:+ print(f"\nπ§ [Banana Bot Thoughts]: {result['text'].strip()}")++ # Execute any function calls requested by the agent+ for call in result["calls"]:+ func_name = call["name"]+ func_args = call.get("args", {})++ print(f"\nβ‘ [Banana Bot Action] Executing local tool '{func_name}' with args: {func_args}")+ try:+ tool_res = await client.call_tool(func_name, func_args)+ print(f"π’ [Banana Bot Action] Success: {tool_res}")++ agent.add_tool_response(func_name, tool_res)++ follow_up = await agent.step("Process the tool response and report/act.", local_tools)+ if follow_up["text"]:+ print(f"\nπ§ [Banana Bot Follow-up]: {follow_up['text'].strip()}")+ except Exception as err:+ print(f"π΄ [Banana Bot Action] Failed: {err}")+ agent.add_tool_response(func_name, {"error": str(err)})++ # Clear session history at the end of this patrol cycle+ agent.clear_session()++ await asyncio.sleep(20.0)++ except KeyboardInterrupt:+ print("\n[-] Banana Bot shutting down. Off duty! π")+ except Exception as e:+ print(f"[!] Banana Bot crashed: {e}")+ sys.exit(1)++if __name__ == "__main__":+ try:+ asyncio.run(run_banana_bot())+ except KeyboardInterrupt:+ print("\n[-] Off duty!")diff --git a/sam-mcp-python/src/sam_mcp/client.py b/sam-mcp-python/src/sam_mcp/client.pyindex 791c5097..09ad8ed0 100644--- a/sam-mcp-python/src/sam_mcp/client.py+++ b/sam-mcp-python/src/sam_mcp/client.py@@ -7,16 +7,22 @@ from mcp.client.sse import sse_clientclass SamClient:"""High-level developer interface for SAM MCP using official SDK."""- def __init__(self, server_url: Optional[str] = None):+ def __init__(self, server_url: Optional[str] = None, token: Optional[str] = None):if server_url is None:- server_url = os.environ.get("SAM_MCP_URL", "http://localhost:8080/sse")+ server_url = os.environ.get("SAM_MCP_URL", "http://localhost:8080/mcp/events")+ if token is None:+ token = os.environ.get("SAM_API_TOKEN")self.server_url = server_url+ self.token = tokenself.session: Optional[ClientSession] = Noneself._sse_cm = Noneasync def connect(self):"""Connects to the SAM node via SSE."""- self._sse_cm = sse_client(self.server_url, headers={"Accept": "application/json, text/event-stream"})+ headers = {"Accept": "application/json, text/event-stream"}+ if self.token:+ headers["Authorization"] = f"Bearer {self.token}"+ self._sse_cm = sse_client(self.server_url, headers=headers)read_stream, write_stream = await self._sse_cm.__aenter__()self.session = ClientSession(read_stream, write_stream)await self.session.__aenter__()