Agent Mesh
Go a2aa2a-protocolagent-meshagentic-aiinferencemcpmeshmesh-networknetwork-securitynetworking-aip2p-network
chaos-agent: port to LangChain 1.x
Antonio Ojea0edd60d51b57
diff --git a/cmd/chaos-agent/README.md b/cmd/chaos-agent/README.mdindex 7978628e..225abb39 100644--- a/cmd/chaos-agent/README.md+++ b/cmd/chaos-agent/README.md@@ -8,7 +8,7 @@ This provides a realistic workload for the SAM mesh scale testing experiment. Ra1. It connects to the `sam-node` MCP endpoint via SSE (or `sam-box` proxy in the microVM).2. It dynamically converts all discovered MCP tools into LangChain `Tool` objects with JSON schemas.-3. It initializes a LangChain `AgentExecutor` using the SAM mesh's OpenAI-compatible inference endpoint (`/v1/chat/completions`).+3. It initializes a LangChain `create_agent` tool-calling agent using the SAM mesh's OpenAI-compatible inference endpoint (`/v1/chat/completions`).4. It is given an adversarial prompt to explore all tools, fuzz them with extreme inputs, and find bugs.5. The LangChain framework automatically handles the multi-turn reasoning and tool-calling loop until the agent achieves its goal or hits the max iteration limit.diff --git a/cmd/chaos-agent/agent.py b/cmd/chaos-agent/agent.pyindex 9521906c..c3145f8f 100644--- a/cmd/chaos-agent/agent.py+++ b/cmd/chaos-agent/agent.py@@ -8,8 +8,8 @@ import timefrom typing import Any, Dictfrom langchain_openai import ChatOpenAI-from langchain.agents import AgentExecutor, create_tool_calling_agent-from langchain_core.prompts import ChatPromptTemplate+from langchain.agents import create_agent+from langchain_core.messages import HumanMessagefrom langchain_core.tools import toolfrom openai import AsyncOpenAI@@ -22,6 +22,32 @@ def tool_schema(spec):return getattr(spec, "input_schema", None) or getattr(spec, "inputSchema", {})+# AgentExecutor counted model+tool iterations; a LangGraph agent counts graph+# steps, and one iteration is two of those.+MAX_ITERATIONS = 500+++async def run_round(agent, prompt):+ """Drive one agent run to completion, echoing each turn as it happens.++ Streaming the updates is what verbose=True used to give us: the tool calls+ and their results are the whole point of a chaos run, so they are printed+ as they arrive rather than summarised at the end.+ """+ final = ""+ async for step in agent.astream(+ {"messages": [HumanMessage(content=prompt)]},+ config={"recursion_limit": 2 * MAX_ITERATIONS},+ stream_mode="updates",+ ):+ for update in step.values():+ for msg in (update or {}).get("messages", []):+ msg.pretty_print()+ if msg.type == "ai" and not getattr(msg, "tool_calls", None):+ final = msg.content+ return final++async def pick_model(inference_url, requested):"""Ask the mesh what it will serve this agent rather than guessing a name.@@ -101,27 +127,21 @@ async def main():print(f"mesh offered model: {model}", file=sys.stderr)llm = ChatOpenAI(model=model,- openai_api_base=args.inference_url,- openai_api_key=args.auth if args.auth else "none",+ base_url=args.inference_url,+ api_key=args.auth if args.auth else "none",default_headers=headers)- # Define the agent prompt# No system turn: Gemma and several other instruction-tuned models# reject the system role outright with a 400, and the mesh may hand# this agent any model at all. The persona goes in the human turn,# which every model accepts.- prompt_template = ChatPromptTemplate.from_messages([- ("human", "You are an autonomous adversarial AI agent. You have access to tools.\n\n{input}"),- ("placeholder", "{agent_scratchpad}"),- ])+ prompt = f"You are an autonomous adversarial AI agent. You have access to tools.\n\n{args.prompt}"- # Create the LangChain Agent+ # create_agent runs the model/tool loop until the model stops+ # calling tools; it replaced AgentExecutor in LangChain 1.x.print("Initializing LangChain Tool-Calling Agent...")- agent = create_tool_calling_agent(llm, lc_tools, prompt_template)-- # AgentExecutor runs the ReAct/Tool loop automatically!- agent_executor = AgentExecutor(agent=agent, tools=lc_tools, verbose=True, max_iterations=500)+ agent = create_agent(model=llm, tools=lc_tools)print(f"\n--- Starting Chaos Monkey Agent Loop ---")print(f"Instruction: {args.prompt}\n")@@ -132,9 +152,9 @@ async def main():print(f"\n--- Round {round_num} ---")started = time.monotonic()try:- result = await agent_executor.ainvoke({"input": args.prompt})+ output = await run_round(agent, prompt)print("\n--- Final Agent Result ---")- print(result["output"])+ print(output)except Exception as e:# A crash is a data point, not a reason to stop: an agent# that dies on the first refusal stops testing anything.diff --git a/cmd/chaos-agent/requirements.txt b/cmd/chaos-agent/requirements.txtindex 71f44a0c..bb9d5f1a 100644--- a/cmd/chaos-agent/requirements.txt+++ b/cmd/chaos-agent/requirements.txt@@ -1,10 +1,9 @@-openai>=1.40,<3+openai>=3.11.0,<4# Pinned to a major version: 1.x spells the transport streamablehttp_client and# 2.x spells it streamable_http_client, so an unpinned floor silently changes# the API underneath a sandbox image.-mcp>=2.0,<3-# Pinned below 1.0 deliberately: LangChain 1.x removed AgentExecutor and-# create_tool_calling_agent from langchain.agents, so an unpinned install-# builds an image whose agent fails at import rather than at run time.-langchain>=0.3,<1-langchain-openai>=0.2,<1+mcp>=2.2.0,<3+# LangChain 1.x replaced AgentExecutor with create_agent, which is what the+# agent is written against; 0.x no longer receives security fixes.+langchain>=1.4.0,<2+langchain-openai>=1.6.2,<2