Skip to main content
Glama

top-clients-by-bandwidth

Retrieve top N clients on a UniFi site sorted by bandwidth usage, with options for combined, transmit-only, or receive-only metrics.

Instructions

Top N clients by bandwidth on a site (combined / tx-only / rx-only). Requires Cloud Connector (UNIFI_API_KEY_OWNER).

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
nameYesSite host name (e.g., 'USM')
topNNoNumber of top clients to return (default: 10)
metricNoBandwidth metric: combined (tx+rx), tx-only, rx-onlycombined
limitNoMax clients to fetch from API (default: 200)

Implementation Reference

  • The main handler function for the top-clients-by-bandwidth tool. Checks connector availability, resolves the site context, fetches clients from the UniFi Cloud Connector API, ranks them by bandwidth metric (combined/tx/rx), and returns the top N clients with bandwidth stats.
    export async function topClientsByBandwidth(params: z.infer<typeof topClientsByBandwidthSchema>) {
      if (!isConnectorAvailable()) {
        return {
          site: params.name,
          error: "Cloud Connector unavailable. Set UNIFI_API_KEY_OWNER to enable per-client bandwidth analysis.",
          clients: [],
        };
      }
    
      const ctx = await resolveConnectorContext(params.name);
      if (!ctx) {
        return {
          site: params.name,
          error: `Site '${params.name}' not found or connector unavailable`,
          clients: [],
        };
      }
    
      const resp = await connectorClient.get<{ data: ConnectorClient[] }>(
        ctx.hostId,
        `network/integration/v1/sites/${ctx.localSiteId}/clients`,
        { limit: params.limit },
      );
    
      const clients = resp.data ?? [];
      const ranked = clients
        .map((c) => {
          const tx = c.uplink?.txRate ?? c.txRate ?? 0;
          const rx = c.uplink?.rxRate ?? c.rxRate ?? 0;
          const combined = tx + rx;
          const value = params.metric === "tx" ? tx : params.metric === "rx" ? rx : combined;
          const typeStr = typeof c.type === "string" ? c.type : "";
          return {
            name: c.name ?? c.hostname ?? c.macAddress ?? c.id ?? "unknown",
            ip: c.ipAddress ?? "",
            mac: c.macAddress ?? "",
            type: typeStr,
            // Derive isWired from `type` when boolean field is absent — UniFi
            // connector clients usually expose only the type discriminator.
            isWired: typeof c.isWired === "boolean" ? c.isWired : typeStr.toUpperCase() === "WIRED",
            txBps: tx,
            rxBps: rx,
            combinedBps: combined,
            value,
          };
        })
        .sort((a, b) => b.value - a.value)
        .slice(0, params.topN);
    
      const totalBps = clients.reduce((s, c) => {
        const tx = c.uplink?.txRate ?? c.txRate ?? 0;
        const rx = c.uplink?.rxRate ?? c.rxRate ?? 0;
        return s + tx + rx;
      }, 0);
    
      return {
        site: ctx.hostName,
        checkedAt: new Date().toISOString(),
        metric: params.metric,
        totalClients: clients.length,
        totalBpsAcrossSite: totalBps,
        topN: ranked.length,
        clients: ranked,
      };
    }
  • Zod schema defining the input parameters: name (site host name), topN (default 10), metric (combined/tx/rx, default combined), and limit (default 200).
    export const topClientsByBandwidthSchema = z.object({
      name: z.string().describe("Site host name (e.g., 'USM')"),
      topN: z.coerce.number().optional().default(10).describe("Number of top clients to return (default: 10)"),
      metric: z.enum(["combined", "tx", "rx"]).optional().default("combined")
        .describe("Bandwidth metric: combined (tx+rx), tx-only, rx-only"),
      limit: z.coerce.number().optional().default(200).describe("Max clients to fetch from API (default: 200)"),
    });
  • src/index.ts:102-104 (registration)
    Registration of the tool with name 'top-clients-by-bandwidth' using the MCP server's tool() function, associating it with the schema and handler via wrapToolHandler.
    tool("top-clients-by-bandwidth",
      "Top N clients by bandwidth on a site (combined / tx-only / rx-only). Requires Cloud Connector (UNIFI_API_KEY_OWNER).",
      topClientsByBandwidthSchema.shape, wrapToolHandler(topClientsByBandwidth));
  • TypeScript interface ConnectorClient defining the shape of client data returned from the UniFi Cloud Connector API.
    interface ConnectorClient {
      id?: string;
      name?: string;
      hostname?: string;
      ipAddress?: string;
      macAddress?: string;
      type?: string;
      isWired?: boolean;
      uplink?: { txRate?: number; rxRate?: number };
      txBytes?: number;
      rxBytes?: number;
      rxRate?: number;
      txRate?: number;
    }
Behavior3/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

No annotations are provided, so the description carries the full burden. It discloses the requirement and metric options, but does not mention potential behaviors like pagination, rate limits, or empty results. The tool is a read operation, so safety is implied but not confirmed.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is a single sentence that efficiently communicates purpose and requirements without unnecessary words. It is front-loaded and earns its place.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness3/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

With no output schema, the description should explain return values or structure, which it does not. However, the tool is a straightforward data retrieval, so the gap is moderate. It provides enough for a basic agent but incomplete for complex invocation.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

The input schema has 100% coverage with descriptions for all parameters. The description adds marginal value beyond the schema, restating metric options already defined. Baseline score of 3 is appropriate.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the tool returns 'Top N clients by bandwidth on a site' with explicit metric options (combined, tx-only, rx-only). It is specific and distinguishes from sibling tools like 'analyze-site-health' or 'list-devices'.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines3/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description mentions a prerequisite ('Requires Cloud Connector') but does not provide when-to-use guidance, when-not-to-use, or alternatives among siblings. It is implied via context but not explicit.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/us-all/unifi-mcp-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server