> 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/tools-and-mcp/javascript-typescript.md). Agents can also search and read this documentation through its [MCP server](https://dnotifier.gitbook.io/product-docs/~gitbook/mcp).

# JavaScript / TypeScript

Package: `@dnotifier-realtime/dnotifier`

## Setup

```js
import { DNotifier } from "@dnotifier-realtime/dnotifier";
import WebSocket from "ws"; // only if transport: "ws"

const notifier = new DNotifier({
  appId: process.env.DNOTIFIER_APP_ID,
  secret: process.env.DNOTIFIER_SECRET,
  transport: "http",
  userId: "user-123",
  permissionEnforcement: "backend", // or "disabled" for local demos
  onAgentEvent: (event) => {
    console.log(event.type, event.toolId, event.message);
  },
});

await notifier.connect();
```

## Register a local tool

```js
const add = notifier.tools.register({
  name: "math.add",
  description: "Add two numbers",
  inputSchema: {
    type: "object",
    properties: {
      a: { type: "number" },
      b: { type: "number" },
    },
    required: ["a", "b"],
  },
  async execute(input) {
    return { sum: Number(input.a) + Number(input.b) };
  },
});
```

`register` returns an in-memory tool (`id`, `name`, `execute`, …). Pass that object into `defineAgent({ tools: [...] })`.

## Connect MCP

```js
const mermaid = await notifier.mcp.connect({
  id: "mermaid",
  name: "Agentic Mermaid",
  transport: "http",
  endpoint: "https://agentic-mermaid.dev/mcp",
  // headers: { Authorization: "Bearer ..." }, // optional
});

const tools = mermaid.tools(); // cached list after discovery
const verify = tools.find((t) => t.name === "mermaid.verify");

// Optional direct call (bypasses the agent loop)
const result = await verify.execute(
  { source: "flowchart LR\n  A --> B" },
  { executionId: "manual", agentId: "manual", toolId: verify.id }
);
```

## Define a tool agent

```js
const agent = DNotifier.defineAgent({
  name: "math-assistant",
  description: "Does arithmetic with tools",
  instructions: "Use math.add when the user asks to add numbers. Be concise.",
  provider: "open_ai",
  model: "gpt-4o",
  observability: true,
  tools: [add],
  permissions: {
    "math.add": ["execute"],
  },
});
```

Omit `run` when `tools` is set — the SDK runs the tool agent for you.

## Run standalone

```js
const result = await notifier.runAgent(agent, {
  input: "What is 21 + 21?",
  userId: "user-123",
  maxToolRounds: 6,
});

console.log(result.output);
console.log(result.finishReason); // "stop"
console.log(result.executionId);
```

## Inside a workflow

```js
const workflow = new DNotifier.Workflow({
  name: "with-tools",
  observability: true,
  async entry(ctx) {
    return ctx.runAgent("math-assistant");
  },
}).registerAgents({
  "math-assistant": agent,
});

const { result } = await notifier.runWorkflow({
  workflow,
  input: "Add 3 and 5",
});
```

## Errors

Typed errors are exported from the package, including `ToolNotFoundError`, `ToolValidationError`, `PermissionDeniedError`, `MCPConnectionError`, `MCPToolError`, `LLMError`, and `AgentExecutionError`.

## Learn more

* [**Example: Local tool agent**](https://dnotifier.gitbook.io/product-docs/workflows-and-agents/example-tool-agent/javascript-typescript.md)
* [**Example: MCP Mermaid agent**](https://dnotifier.gitbook.io/product-docs/workflows-and-agents/example-mcp-agent/javascript-typescript.md)
* [**Define an agent**](https://dnotifier.gitbook.io/product-docs/workflows-and-agents/define-agent/javascript-typescript.md)
