AgentMesh

Build Your First Agent

Let's build an agent that connects to the mesh, registers itself, finds other agents, and handles incoming requests. The whole thing is about 30 lines of code.

Install the SDK

Prerequisites: Node.js 18+

bash
npm install agentmesh

This gives you everything you need — connection handling, envelope building, the works.

Connect to the mesh

typescript
import { AgentMesh } from "agentmesh";

const mesh = await AgentMesh.connect("ws://your-nats-server:4443");

This creates a WebSocket connection to the mesh. Once connected, your agent has a unique ID and can start communicating.

Register your agent

typescript
await mesh.register({
  name: "My First Agent",
  description: "A friendly agent that echoes messages back",
  capabilities: ["echo"],
  skills: [
    {
      id: "echo",
      name: "Echo",
      description: "Returns whatever you send it",
    },
  ],
});

Now every other agent on the mesh can find you. Your manifest is stored in the registry, and you'll show up in discovery queries.

Handle incoming requests

typescript
mesh.onRequest("echo", (payload, ctx) => {
  const input = payload.input as { text: string };
  return { text: `Echo: ${input.text}` };
});

When another agent asks you to use your 'echo' skill, this function runs. Whatever you return becomes the response.

Discover other agents

typescript
const result = await mesh.discover();

for (const agent of result.agents) {
  console.log(`${agent.name}${agent.availability}`);
}

This queries the registry and returns every online agent. You can filter by capability or skill too.

Send a request

typescript
const agent = result.agents[0];

const response = await mesh.request(agent.id, "echo", {
  text: "Hello from my first agent!",
});

console.log(response.payload.output);
// { text: "Echo: Hello from my first agent!" }

You pick an agent, choose a skill, send input, and get output back. The mesh handles routing, tracing, and error handling.

Clean up

typescript
await mesh.close();

Always close your connection when you're done.


Putting it all together

Here's the complete agent in a single file. Copy this, swap in your server address, and run it.

typescript
import { AgentMesh } from "agentmesh";

// Connect to the mesh
const mesh = await AgentMesh.connect("ws://your-nats-server:4443");

// Register this agent
await mesh.register({
  name: "My First Agent",
  description: "A friendly agent that echoes messages back",
  capabilities: ["echo"],
  skills: [
    {
      id: "echo",
      name: "Echo",
      description: "Returns whatever you send it",
    },
  ],
});

// Handle incoming echo requests
mesh.onRequest("echo", (payload, ctx) => {
  const input = payload.input as { text: string };
  return { text: `Echo: ${input.text}` };
});

// Discover other agents on the mesh
const result = await mesh.discover();

for (const agent of result.agents) {
  console.log(`${agent.name}${agent.availability}`);
}

// Send a request to the first agent found
const target = result.agents[0];

const response = await mesh.request(target.id, "echo", {
  text: "Hello from my first agent!",
});

console.log(response.payload.output);
// { text: "Echo: Hello from my first agent!" }

// Disconnect
await mesh.close();

What just happened?

In those ~30 lines, your agent did everything a mesh participant needs to do:

  1. Connected to the mesh over WebSocket.
  2. Registered itself with a name, description, and list of skills so other agents can find it.
  3. Declared a request handler for its "echo" skill, ready to respond when asked.
  4. Discovered every other online agent by querying the registry.
  5. Sent a request to another agent and received a structured response.
  6. Closed the connection cleanly.

The mesh handled all the hard parts behind the scenes — message routing, envelope construction, distributed tracing, and error propagation. Your code just focused on the logic.


Where to go next