Agent Mesh
Go a2aa2a-protocolagent-meshagentic-aiinferencemcpmeshmesh-networknetwork-securitynetworking-aip2p-network
agents: ask the mesh which model it serves instead of guessing
Antonio Ojead6ba1b561ce7
diff --git a/.gitignore b/.gitignoreindex 97f52808..eb65f7a5 100644--- a/.gitignore+++ b/.gitignore@@ -55,3 +55,8 @@ __pycache__/keys.dbmobile/logcat.txt+# Guest images built by scripts/build-rootfs.sh; gigabytes, and reproducible+# from the script, so they belong in a bucket rather than in history.+/rootfs.ext4+/rootfs.tar+diff --git a/cmd/chaos-agent/agent.py b/cmd/chaos-agent/agent.pyindex 6512fb99..9521906c 100644--- a/cmd/chaos-agent/agent.py+++ b/cmd/chaos-agent/agent.py@@ -3,23 +3,54 @@ import sysimport asyncioimport argparseimport json+import random+import timefrom typing import Any, Dictfrom langchain_openai import ChatOpenAIfrom langchain.agents import AgentExecutor, create_tool_calling_agentfrom langchain_core.prompts import ChatPromptTemplatefrom langchain_core.tools import tool+from openai import AsyncOpenAIfrom mcp.client.session import ClientSession-from mcp.client.sse import sse_client+from mcp.client.streamable_http import streamable_http_client+++def tool_schema(spec):+ """MCP 2.x renamed inputSchema to input_schema; tolerate both."""+ return getattr(spec, "input_schema", None) or getattr(spec, "inputSchema", {})+++async def pick_model(inference_url, requested):+ """Ask the mesh what it will serve this agent rather than guessing a name.++ Hardcoding a model name is how this agent used to 404 on every call: the+ catalog is per-agent, so the only name guaranteed to work is one the mesh+ just said it would serve.+ """+ if requested:+ return requested+ models = await AsyncOpenAI(base_url=inference_url, api_key="unused").models.list()+ if not models.data:+ raise SystemExit(+ "the mesh offered this agent no models; check that an inference "+ "provider is registered and that policy grants access to it"+ )+ return models.data[0].idasync def main():parser = argparse.ArgumentParser(description="Chaos Monkey Agent via LangChain & MCP")- parser.add_argument("--mcp-url", default="http://127.0.0.1:8080/mcp", help="MCP SSE endpoint")- parser.add_argument("--inference-url", default="http://127.0.0.1:8080/v1", help="OpenAI-compatible endpoint")- parser.add_argument("--auth", default="", help="Authorization header (e.g. Bearer <token>)")+ parser.add_argument("task", nargs="?", default="", help="Instruction to run; same positional interface as the agent harness, so one sandbox init serves both")+ parser.add_argument("--mcp-url", default=os.environ.get("SAM_MCP_URL", "http://mesh.sam.alt/mcp"), help="MCP endpoint; a mesh name the boundary resolves")+ parser.add_argument("--inference-url", default=os.environ.get("SAM_INFERENCE_URL", "http://mesh.sam.alt/v1"), help="OpenAI-compatible endpoint")+ parser.add_argument("--model", default=os.environ.get("SAM_MODEL", ""), help="Model to ask for; default is whatever the mesh offers first")+ parser.add_argument("--rounds", type=int, default=int(os.environ.get("CHAOS_ROUNDS", "0")), help="Rounds to run; 0 keeps going until stopped")+ parser.add_argument("--sleep", type=float, default=float(os.environ.get("CHAOS_SLEEP", "30")), help="Seconds between rounds, jittered")+ parser.add_argument("--auth", default="", help="Only needed outside a sandbox; inside one the boundary authenticates")parser.add_argument("--prompt", default="You are a chaos monkey testing a distributed mesh network. Use all tools available to you. Pass extreme, invalid, or adversarial inputs (like huge strings, negative numbers, SQL injection strings) to see if the tools or network crash. Report any errors you discover.", help="Agent instruction prompt")args = parser.parse_args()+ args.prompt = args.task or args.promptheaders = {}if args.auth:@@ -28,8 +59,16 @@ async def main():print(f"Connecting to MCP at {args.mcp_url}...")- # Connect to MCP via SSE- async with sse_client(args.mcp_url, headers=headers) as (read, write):+ # Connect to MCP over Streamable HTTP, which is what the mesh serves; the+ # older SSE transport is answered with 400. The transport takes no headers+ # of its own -- they belong to the HTTP client -- and inside a sandbox+ # there are none to send, because the boundary does the authenticating.+ transport_args = {}+ if headers:+ import httpx2++ transport_args["http_client"] = httpx2.AsyncClient(headers=headers)+ async with streamable_http_client(args.mcp_url, **transport_args) as (read, write):async with ClientSession(read, write) as session:await session.initialize()@@ -52,23 +91,28 @@ async def main():except Exception as e:return f"Tool Execution Error: {e}"# Append the actual schema to the description so the LLM knows how to use it- mcp_tool.description = f"{tool_desc}\nSchema: {t.inputSchema}"+ mcp_tool.description = f"{tool_desc}\nSchema: {tool_schema(t)}"return mcp_toollc_tools.append(make_tool(t.name, t.description))# Initialize LangChain Chat Model (OpenAI compatible)+ model = await pick_model(args.inference_url, args.model)+ print(f"mesh offered model: {model}", file=sys.stderr)llm = ChatOpenAI(- model="default",+ model=model,openai_api_base=args.inference_url,openai_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([- ("system", "You are an autonomous adversarial AI agent. You have access to tools."),- ("human", "{input}"),+ ("human", "You are an autonomous adversarial AI agent. You have access to tools.\n\n{input}"),("placeholder", "{agent_scratchpad}"),])@@ -82,15 +126,23 @@ async def main():print(f"\n--- Starting Chaos Monkey Agent Loop ---")print(f"Instruction: {args.prompt}\n")- try:- # Wrap in a loop to run for a while- for i in range(5):- print(f"\n--- Iteration {i+1} ---")+ round_num = 0+ while args.rounds == 0 or round_num < args.rounds:+ round_num += 1+ print(f"\n--- Round {round_num} ---")+ started = time.monotonic()+ try:result = await agent_executor.ainvoke({"input": args.prompt})print("\n--- Final Agent Result ---")print(result["output"])- except Exception as e:- print(f"Agent Loop crashed (this might be a successful chaos test!): {e}")+ 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.+ print(f"Round {round_num} crashed (this might be a successful chaos test!): {e}")+ print(f"Round {round_num} took {time.monotonic() - started:.1f}s", file=sys.stderr)+ # Jitter, so a fleet of these does not hit the providers on the+ # same tick and turn a soak test into a thundering herd.+ await asyncio.sleep(args.sleep * random.uniform(0.5, 1.5))if __name__ == "__main__":asyncio.run(main())diff --git a/cmd/chaos-agent/requirements.txt b/cmd/chaos-agent/requirements.txtindex 491e0494..71f44a0c 100644--- a/cmd/chaos-agent/requirements.txt+++ b/cmd/chaos-agent/requirements.txt@@ -1,4 +1,10 @@-openai-mcp-langchain-langchain-openai+openai>=1.40,<3+# 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,<1diff --git a/development/examples/agent-harness/agent.py b/development/examples/agent-harness/agent.pyindex d3a0b323..41dc5f99 100644--- a/development/examples/agent-harness/agent.py+++ b/development/examples/agent-harness/agent.py@@ -91,11 +91,33 @@ async def call_tool(session, name, arguments):return f"tool call failed: {exc}"+async def pick_model(client, requested):+ """Resolve the model to ask for, preferring whatever the mesh actually offers.++ An agent that hardcodes a model name is an agent that needs configuration,+ which is the thing this harness is trying not to need. The catalog is+ already per-agent -- it lists what this agent's policy allows -- so asking+ it is both the simplest option and the correct one.+ """+ if requested:+ return requested+ models = await client.models.list()+ if not models.data:+ raise SystemExit(+ "the mesh offered this agent no models; check that an inference "+ "provider is registered and that policy grants access to it"+ )+ return models.data[0].id++async def run(task, model, max_steps):# No api_key that means anything: the gateway authenticates this agent to# the mesh. The SDK requires the argument, so it gets a placeholder.client = AsyncOpenAI(base_url=f"{MESH}/v1", api_key="unused")+ model = await pick_model(client, model)+ print(f"mesh offered model: {model}", file=sys.stderr)+# Streamable HTTP, which is what the mesh serves. The older SSE transport# is answered with 400, and that arrives late enough to look like a network# problem rather than a protocol one.@@ -143,11 +165,16 @@ async def run(task, model, max_steps):def main():parser = argparse.ArgumentParser(description=__doc__)- parser.add_argument("task", help="What the agent should do")+ parser.add_argument(+ "task",+ nargs="?",+ default="Describe the tools you have and what each is for.",+ help="What the agent should do",+ )parser.add_argument("--model",- default=os.environ.get("SAM_MODEL", "default"),- help="Model to ask the mesh for; the mesh chooses the provider",+ default=os.environ.get("SAM_MODEL", ""),+ help="Model to ask the mesh for; default is whatever the mesh offers first",)parser.add_argument("--max-steps", type=int, default=10)args = parser.parse_args()diff --git a/scripts/build-rootfs.sh b/scripts/build-rootfs.shindex bfcfb7cc..3dee56d5 100755--- a/scripts/build-rootfs.sh+++ b/scripts/build-rootfs.sh@@ -45,8 +45,12 @@ CONTAINER_ID=$(docker create agent-rootfs)docker export $CONTAINER_ID > rootfs.tardocker rm $CONTAINER_ID-# Create the ext4 image-dd if=/dev/zero of=rootfs.ext4 bs=1M count=500+# Create the ext4 image. The size is the agent's, not the base system's: the+# harness needs a couple of hundred megabytes and a LangChain agent needs well+# over a gigabyte, and an image that is too small fails during the copy rather+# than at boot, which is a confusing place to find out.+ROOTFS_MB="${ROOTFS_MB:-500}"+dd if=/dev/zero of=rootfs.ext4 bs=1M count="${ROOTFS_MB}"mkfs.ext4 rootfs.ext4mkdir -p /tmp/rootfsdiff --git a/scripts/launch-microvms.sh b/scripts/launch-microvms.shindex f46a6f17..ba4dbfa3 100755--- a/scripts/launch-microvms.sh+++ b/scripts/launch-microvms.sh@@ -40,6 +40,20 @@ VM_VCPUS="${VM_VCPUS:-1}"# never a thousand at once. Zero is the right default for real work.SANDBOX_LINGER="${SANDBOX_LINGER:-0}"+# Anything the agent needs to know reaches it as a kernel cmdline pair, so it+# must not contain spaces: the kernel splits on them and init would see two+# variables. Model names and durations are safe; prompts are not, which is why+# the task stays in the init script.+AGENT_ENV=""+for v in SAM_MODEL CHAOS_SLEEP CHAOS_ROUNDS; do+ eval "val=\${$v:-}"+ [ -z "$val" ] && continue+ case "$val" in+ *" "*) echo "Error: $v must not contain spaces (kernel cmdline)" >&2; exit 1 ;;+ esac+ AGENT_ENV="$AGENT_ENV $v=$val"+done+if [ ! -d "$WORKDIR" ]; thenecho "Error: $WORKDIR does not exist. Did cloud-init finish?" >&2exit 1@@ -207,7 +221,7 @@ EOF# it is started with an empty environment.fc_put "$API_SOCKET" /boot-source "{\"kernel_image_path\": \"$WORKDIR/vmlinux.bin\",- \"boot_args\": \"console=ttyS0 reboot=k panic=1 pci=off ro SANDBOX_LINGER=${SANDBOX_LINGER}\"+ \"boot_args\": \"console=ttyS0 reboot=k panic=1 pci=off ro SANDBOX_LINGER=${SANDBOX_LINGER}${AGENT_ENV}\"}"fc_put "$API_SOCKET" /machine-config "{diff --git a/scripts/microvm-init.sh b/scripts/microvm-init.shindex 38ce8bd1..a6e929ca 100755--- a/scripts/microvm-init.sh+++ b/scripts/microvm-init.sh@@ -30,7 +30,16 @@ if ! touch /etc/.writable 2>/dev/null; thenfirm -f /etc/.writable 2>/dev/null || true-TASK="${AGENT_TASK:-Describe the tools you have and what each is for.}"+# The task is only forced when the operator sets one. Left unset, each agent+# image uses its own default, which is what lets the same init boot the polite+# harness and the adversarial agent without knowing which one it is holding.+# Note AGENT_TASK arrives on the kernel cmdline, so it cannot contain spaces;+# a real prompt belongs in the agent image, not here.+if [ -n "${AGENT_TASK:-}" ]; then+ set -- "${AGENT_TASK}"+else+ set --+fi# The kernel hands PID 1 an empty environment, so there is no PATH to inherit# and anything looked up by name is not found.@@ -40,7 +49,7 @@ export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin# reaches the console before whatever init was trying to say about why it# failed. Holding the process open long enough for the console to drain is the# difference between a diagnosis and a stack trace.-if ! /usr/local/bin/nano-init run vsock://2:1080 python3 /app/agent/agent.py "${TASK}" > /dev/console 2>&1; then+if ! /usr/local/bin/nano-init run vsock://2:1080 python3 /app/agent/agent.py "$@" > /dev/console 2>&1; thenecho "sandbox init failed; see above" > /dev/consolefi