← Developers
Level 1: code Framework

TypeScript: Vercel AI SDK

The guard wrapped in an AI SDK tool definition.

Configuration and reference code for TypeScript: Vercel AI SDK

The guard

securdGuard.ts
// securdGuard.ts
// Held and blocked names resolve to the policy's block page address.
// Put that address (from the console, Sites > your site) in SECURD_BLOCK_ADDRS.
import { promises as dns } from "node:dns";

const BLOCK_ADDRS = new Set((process.env.SECURD_BLOCK_ADDRS ?? "").split(",").filter(Boolean));

export class SecurdHeld extends Error {
  constructor(public host: string, public agentRole: string) {
    super(`${host} is held or blocked for ${agentRole}`);
  }
}

export async function securdGuard(host: string, agentRole: string): Promise<void> {
  let answers: string[];
  try {
    answers = (await dns.lookup(host, { all: true })).map((a) => a.address);
  } catch {
    console.warn(JSON.stringify({ event: "securd.unresolved", agentRole, host }));
    throw new SecurdHeld(host, agentRole);
  }
  if (answers.some((a) => BLOCK_ADDRS.has(a))) {
    console.warn(JSON.stringify({ event: "securd.held", agentRole, host }));
    throw new SecurdHeld(host, agentRole);
  }
}

A guarded tool

tools.ts
import { tool } from "ai";
import { z } from "zod";
import { securdGuard, SecurdHeld } from "./securdGuard";

const AGENT_ROLE = "chat-assistant";

export const fetchJson = tool({
  description: "GET JSON from an approved API.",
  parameters: z.object({ url: z.string().url() }),
  execute: async ({ url }) => {
    const host = new URL(url).hostname;
    try {
      await securdGuard(host, AGENT_ROLE);
    } catch (e) {
      if (e instanceof SecurdHeld) return { status: "held", host, reason: e.message };
      throw e;
    }
    return (await fetch(url)).json();
  },
});

Serverless note

Serverless platforms do not expose the resolver configuration. Use a DoH virtual site with the local proxy pattern, or run the agent on infrastructure where the resolver can be set. See the DoH page.

Evaluate Agent DNS with your own agent traffic

Deploy on a single policy in learning mode and review the recorded destinations with your team.