> 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/example-mcp-agent/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

```js
import { DNotifier } from "@dnotifier-realtime/dnotifier";

async function main() {
  const userId = "demo-user";
  const notifier = new DNotifier({
    appId: process.env.DNOTIFIER_APP_ID,
    secret: process.env.DNOTIFIER_SECRET,
    transport: "http",
    userId,
    permissionEnforcement: "backend",
    onAgentEvent: (event) => {
      console.log("[agent]", event.type, {
        toolId: event.toolId,
        mcpServerId: event.mcpServerId,
      });
    },
  });

  await notifier.connect();

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

  const allTools = mermaid.tools();
  console.log(
    "Discovered:",
    allTools.map((t) => t.name).join(", ")
  );

  const verify = allTools.find((t) => t.name === "mermaid.verify");
  const describe = allTools.find((t) => t.name === "mermaid.describe");
  const renderAscii = allTools.find((t) => t.name === "mermaid.render_ascii");
  if (!verify || !describe || !renderAscii) {
    throw new Error("Expected verify, describe, and render_ascii tools");
  }

  // Optional: call an MCP tool directly (no LLM)
  const sample = "flowchart LR\n  A[Start] --> B[Done]";
  const direct = await verify.execute(
    { source: sample },
    { executionId: "manual", agentId: "manual", toolId: verify.id }
  );
  console.log("Direct verify:", String(direct).slice(0, 200));

  const agent = DNotifier.defineAgent({
    name: "mermaid-assistant",
    description: "Validates and explains Mermaid diagrams using MCP tools.",
    instructions:
      "You help with Mermaid diagrams. Use mermaid.verify, mermaid.describe, and mermaid.render_ascii when needed. Keep answers concise.",
    provider: "open_ai",
    model: "gpt-4o",
    observability: true,
    tools: [verify, describe, renderAscii],
    permissions: {
      "mermaid.verify": ["execute"],
      "mermaid.describe": ["execute"],
      "mermaid.render_ascii": ["execute"],
    },
  });

  const result = await notifier.runAgent(agent, {
    input:
      "Create a Mermaid diagram for a 3-tier web architecture and render it in ASCII.",
    userId,
    maxToolRounds: 6,
  });

  console.log("finishReason:", result.finishReason);
  console.log("executionId:", result.executionId);
  console.log("output:\n", result.output);
}

main().catch((err) => {
  console.error(err?.name || "Error", err?.message || err);
  process.exitCode = 1;
});
```

## Learn more

* [**Tools & MCP — JavaScript**](https://dnotifier.gitbook.io/product-docs/workflows-and-agents/tools-and-mcp/javascript-typescript.md)
* [**Local tool agent example**](https://dnotifier.gitbook.io/product-docs/workflows-and-agents/example-tool-agent/javascript-typescript.md)
