> For the complete documentation index, see [llms.txt](https://dnotifier.gitbook.io/product-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://dnotifier.gitbook.io/product-docs/workflows-and-agents/define-agent.md). Agents can also search and read this documentation through its [MCP server](https://dnotifier.gitbook.io/product-docs/~gitbook/mcp).

# Define an agent

An **agent** is a named unit of work. Define agents with **`defineAgent`** / **`define_agent`**, then either register them on a **workflow** or run a **tool agent** with standalone **`runAgent`** / **`run_agent`**.

## Classic agent (custom `run`)

```js
const myAgent = DNotifier.defineAgent({
  name: "my-agent",           // unique within your app
  provider: "open_ai",        // optional — default for ctx.sendAI in this agent
  model: "gpt-4o",            // optional — default for ctx.sendAI in this agent
  async run(ctx) {
    // ctx.input — workflow or runAgent input
    // ctx.state — shared across agents
    // ctx.sendAI, ctx.search, ctx.runAgent, ...
    return { done: true };
  },
});
```

Optional `provider` / `model` on the agent are applied automatically to `ctx.sendAI` unless the call passes its own values.

## Tool agent (SDK loop)

Pass `tools` (from `notifier.tools.register` or `mcp.tools()`). You may omit `run` — the SDK runs the tool agent for you:

```js
const agent = DNotifier.defineAgent({
  name: "math-assistant",
  instructions: "Use math.add when needed. Be concise.",
  provider: "open_ai",
  model: "gpt-4o",
  observability: true,
  tools: [addTool],
  permissions: { "math.add": ["execute"] },
});

await notifier.runAgent(agent, { input: "What is 2+2?" });
```

See [**Tools & MCP agents**](https://dnotifier.gitbook.io/product-docs/workflows-and-agents/tools-and-mcp.md) for full APIs and examples.

## Fields

| Field                | Classic  | Tool agent               | Description                                                                                                  |
| -------------------- | -------- | ------------------------ | ------------------------------------------------------------------------------------------------------------ |
| `name`               | required | required                 | Unique, stable agent id within your app (see note below)                                                     |
| `run`                | required | optional                 | Custom logic; installed automatically when `tools` is set                                                    |
| `tools`              | —        | required for tool agents | Array from `tools.register` / MCP                                                                            |
| `instructions`       | —        | recommended              | System guidance for the tool agent                                                                           |
| `description`        | optional | optional                 | Human-readable summary                                                                                       |
| `permissions`        | —        | optional                 | Tool name → allowed actions                                                                                  |
| `observability`      | —        | optional                 | Record standalone `runAgent` runs in the dashboard                                                           |
| `provider` / `model` | optional | optional                 | Defaults for LLM turns                                                                                       |
| `middleware`         | optional | optional                 | Agent hooks — see [Middleware](https://dnotifier.gitbook.io/product-docs/workflows-and-agents/middleware.md) |
| `capabilities`       | optional | optional                 | Capability flags — each defaults to `false`                                                                  |

{% hint style="warning" %}
**Agent names must be unique within your app.** The `name` is the agent's identity. If you define two agents with the same name in the same app, the one defined last replaces the earlier one, and callers (workflows, remote agents, MCP and A2A clients) will only reach it.

Best practice: treat `name` as a stable id, not a display title. Use a short, lowercase, hyphenated value such as `mermaid-diagram` or `billing-support-v2`, keep it the same across deployments, and put the human-friendly wording in `description`.
{% endhint %}

## Capabilities

Agents and workflows can declare what they support with these flags: `mcpServer`, `a2a`, `tools`, `rag`, `memory`, `workflows`, `humanApproval`, and `subagents`. Any you leave out are `false`; unknown flags are ignored.

```js
const agent = DNotifier.defineAgent({
  name: "support-agent",
  run: async (ctx) => ({ ok: true }),
  capabilities: { mcpServer: true }, // a2a is false
});

const workflow = new DNotifier.Workflow({
  name: "support",
  entry: async (ctx) => ctx.runAgent("support-agent"),
  capabilities: { a2a: true }, // mcpServer is false
});
```

Python uses the same keyword (`capabilities={"mcpServer": True}`), and Dart uses `capabilities: {'mcpServer': true}`.

With `mcpServer` or `a2a` turned on, a listening agent or workflow can be called from MCP clients such as Claude and Cursor, or from any A2A agent. See [**Serve agents over MCP**](https://dnotifier.gitbook.io/product-docs/workflows-and-agents/mcp-server.md) and [**Agent-to-agent (A2A)**](https://dnotifier.gitbook.io/product-docs/workflows-and-agents/a2a.md).

## Rules

| Rule              | Detail                                                                                          |
| ----------------- | ----------------------------------------------------------------------------------------------- |
| **Unique name**   | Each agent must have a distinct `name` within your app — a duplicate replaces the earlier agent |
| **Classic `run`** | Async function; return value goes to `ctx.runAgent("name")`                                     |
| **Tool agents**   | Each tool must have `id` and `name` from the SDK registries                                     |
| **Errors**        | Throw `WorkflowError` for expected validation failures                                          |

## What classic agents can do

Inside `run(ctx)`:

* Call **`ctx.sendAI`** for model steps
* Call **`ctx.search`** for RAG retrieval
* Read/write **`ctx.state`** for pipeline data
* Call **`ctx.runAgent`** to delegate (uncommon inside agents — usually done in `entry`)
* **`ctx.recordStep`** for custom observability

{% hint style="info" %}
Agents do not connect or authenticate themselves. The parent `DNotifier` must `connect()` before `runWorkflow` or `runAgent`.
{% endhint %}

## Registration

Agents are registered on the workflow — not globally:

```js
const workflow = new DNotifier.Workflow({
  name: "my-pipeline",
  entry: async (ctx) => { /* ... */ },
}).registerAgents({
  "my-agent": myAgent,
});
```

## Host an agent without a workflow

An agent can answer remote calls on its own. Call `listen` on a connected client that uses WebSocket transport:

```js
const agent = DNotifier.defineAgent({ name: "mermaid-diagram", tools, instructions });
agent.listen(notifier);   // answers invokes for "mermaid-diagram"
agent.stopListening();    // stop answering
```

Callers send an invoke with `agentName: "mermaid-diagram"` to this client's `userId`. The result or error is sent back automatically. Python uses `agent.listen(notifier)` / `agent.stop_listening()`, and Dart uses `agent.listen(notifier)` / `agent.stopListening()`. Workflows have the same `listen` / `stopListening` pair.

The same listening agent also answers MCP and A2A calls when those capabilities are on.

## Language guides

| Language                | Guide                                                                                                                            |
| ----------------------- | -------------------------------------------------------------------------------------------------------------------------------- |
| JavaScript / TypeScript | [javascript-typescript.md](https://dnotifier.gitbook.io/product-docs/workflows-and-agents/define-agent/javascript-typescript.md) |
| Dart / Flutter          | [dart-flutter.md](https://dnotifier.gitbook.io/product-docs/workflows-and-agents/define-agent/dart-flutter.md)                   |
| Python                  | [python.md](https://dnotifier.gitbook.io/product-docs/workflows-and-agents/define-agent/python.md)                               |

## Next steps

* [**Tools & MCP agents**](https://dnotifier.gitbook.io/product-docs/workflows-and-agents/tools-and-mcp.md)
* [**Middleware**](https://dnotifier.gitbook.io/product-docs/workflows-and-agents/middleware.md)
* [**Serve agents over MCP**](https://dnotifier.gitbook.io/product-docs/workflows-and-agents/mcp-server.md)
* [**Agent-to-agent (A2A)**](https://dnotifier.gitbook.io/product-docs/workflows-and-agents/a2a.md)
* [**Build a workflow**](https://dnotifier.gitbook.io/product-docs/workflows-and-agents/build-workflow.md)
* [**WorkflowContext reference**](https://dnotifier.gitbook.io/product-docs/workflows-and-agents/workflow-context.md)
