Skip to main content

Embed in Existing Service

Add agent capabilities to an Express app you already have — without restructuring your codebase.
Use agent.start() instead. It handles everything internally.

Why Embed?

You have an existing service — maybe an API, a webhook handler, or a background worker. You want to add AI capabilities without:
  • Rewriting your application structure
  • Running a separate agent process
  • Managing two deployments
attach() lets you keep your existing Express app and add agent capabilities as a layer on top.

Quick Start

What attach() Does

One call wires up everything:
  1. A2A endpoints — Mounts /.well-known/agent-card.json (agent discovery) and /send (capability invocation) on your Express app
  2. Mesh registration — Authenticates with the mesh, registers your agent’s capabilities, and starts heartbeating to stay discoverable
  3. Triggers — Activates triggers already registered on the agent via agent.add_triggers()
  4. Graceful shutdown — Listens for SIGTERM and SIGINT to disconnect cleanly before your process exits

Attach Configuration

AttachConfig

object
required
An Agent instance with capabilities already defined. Create with new Agent(\{ name, description, public_url? \}).
object
Mesh connection credentials. If omitted, the agent runs locally without mesh registration — useful for development and testing.

Triggers

Triggers let external events fire prompts into the mesh. When a trigger fires, the prompt template is filled with payload data and sent to the mesh for processing.

Webhook Triggers

Receive HTTP POST requests from external systems and convert them into mesh prompts. Use this to integrate with Zendesk, Stripe, GitHub, Slack, or any system that sends webhooks.

WebhookTrigger

string
required
Trigger type identifier. Must be "webhook".
string
required
Event name for logging and identification. Example: "ticket_created".
string
required
URL path where the webhook will be mounted. External systems POST to this endpoint. Example: "/webhooks/zendesk".
string
required
Prompt template sent to the mesh. Use double-brace placeholders for dynamic values. Example: "New ticket \"subject\" (ID: ticket_id). Triage it.".
object
Maps payload fields to prompt variables using JSONPath dot-notation.
When Zendesk POSTs to /webhooks/zendesk, the payload_map extracts ticket.id and ticket.subject from the JSON body, fills them into the prompt, and sends it to the mesh.

Schedule Triggers

Run prompts on a cron schedule. Use this for daily summaries, periodic checks, or scheduled maintenance tasks.

ScheduleTrigger

string
required
Trigger type identifier. Must be "schedule".
string
required
Event name for logging and identification. Example: "daily_summary".
string
required
Cron expression (5 fields: minute hour day-of-month month day-of-week). Example: "0 9 * * MON-FRI".
string
required
Prompt sent to the mesh when the schedule fires. Example: "Summarize all open tickets and send to Slack.".
Common cron patterns:
  • 0 9 * * * — Every day at 9:00 AM
  • 0 9 * * MON-FRI — Weekdays at 9:00 AM
  • */15 * * * * — Every 15 minutes
  • 0 0 1 * * — First day of every month at midnight

Emit Triggers

Listen for in-process events fired by your own application code. Use this when your app detects something that needs AI handling — like an SLA warning, a fraud signal, or a customer escalation.

EmitTrigger

string
required
Trigger type identifier. Must be "emit".
string
required
Event name. Your code fires this event using handle.emit(). Example: "sla_warning".
string
required
Prompt template sent to the mesh. Use double-brace placeholders. Example: "Ticket ticket_id is approaching SLA breach. Take action.".

The AgentHandle

attach() returns an AgentHandle for runtime interaction with the mesh.

AgentHandle

function
Fire-and-forget: emit an event that triggers a prompt to the mesh. Does not wait for a response. Signature: (event: string, payload: object) => void.
function
Request-response: emit an event and wait for the mesh to process it. Returns the result. Times out after 30 seconds. Signature: (event: string, payload: object) => Promise<unknown>.
function
Clean shutdown: stops cron timers, removes event listeners, disconnects from mesh, and closes the agent. Call this before your process exits. Signature: () => Promise<void>.
object
The underlying MeshConnector instance, or null if mesh config was omitted. Use to check connection status or access advanced mesh features.
object
The underlying Agent instance.

Full Example

An Express service with existing routes, agent capabilities, and multiple trigger types:

When to Use This

attach() is a convenience wrapper. Under the hood it calls agent.expose(app) and creates a MeshConnector — the same primitives you’d use manually.

What’s Next