Ir para o conteúdo

Exemplos de código

Clientes prontos para copiar e colar para os dois transportes do contrato de leitura do IRIS. Substitua a chave placeholder iris_xxx pela chave emitida pelo seu operador em tempo de execução — nunca comite uma chave real.

REST

curl

export IRIS_BASE_URL="https://<iris-host>"
export IRIS_API_KEY="iris_xxx"

# Search/list brand summaries (keyset page)
curl -sS "$IRIS_BASE_URL/v1/brands?q=cafe&limit=5" \
  -H "X-API-Key: $IRIS_API_KEY"

# Full detail for one application number
curl -sS "$IRIS_BASE_URL/v1/brands/123456" \
  -H "X-API-Key: $IRIS_API_KEY"

TypeScript (fetch)

const baseUrl = process.env.IRIS_BASE_URL!;
const apiKey = process.env.IRIS_API_KEY!; // "iris_xxx"

async function searchBrands(q: string, limit = 20) {
  const url = new URL(`${baseUrl}/v1/brands`);
  url.searchParams.set("q", q);
  url.searchParams.set("limit", String(limit));

  const res = await fetch(url, { headers: { "X-API-Key": apiKey } });
  if (!res.ok) {
    const { error } = await res.json();
    throw new Error(`${res.status} ${error.code}: ${error.message}`);
  }
  return res.json() as Promise<{ items: unknown[]; nextCursor: string | null }>;
}

const page = await searchBrands("cafe", 5);
console.log(page.items.length, "results; nextCursor =", page.nextCursor);

MCP

Um cliente mínimo @modelcontextprotocol/sdk que se conecta ao endpoint Streamable HTTP com o header X-API-Key e, em seguida, chama ambas as ferramentas. Isso espelha o smoke over-the-wire em deploy/smoke/mcp-smoke.ts, reduzido a um exemplo de connect + callTool.

import { Client } from "@modelcontextprotocol/sdk/client/index.js";
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";

const url = process.env.MCP_URL ?? "https://mcp.obviouy.com";
const apiKey = process.env.IRIS_API_KEY!; // "iris_xxx"

// The X-API-Key header is attached to every transport request via requestInit.
const transport = new StreamableHTTPClientTransport(new URL(url), {
  requestInit: { headers: { "X-API-Key": apiKey } },
});

const client = new Client({ name: "iris-mcp-example", version: "1.0.0" });
await client.connect(transport); // runs the JSON-RPC initialize handshake

try {
  // search_brands — same input + output as REST GET /v1/brands
  const search = await client.callTool({
    name: "search_brands",
    arguments: { q: "cafe", limit: 5 },
  });
  // Tool payloads come back as a JSON string in the first text content block.
  const firstText = (r: typeof search) =>
    (r.content as Array<{ type: string; text?: string }>).find((c) => c.type === "text")?.text;
  const page = JSON.parse(firstText(search) ?? "{}");
  console.log(page.items.length, "results; nextCursor =", page.nextCursor);

  // get_brand_detail — same input + output as REST GET /v1/brands/:nroSolicitud
  const detail = await client.callTool({
    name: "get_brand_detail",
    arguments: { nroSolicitud: "123456" },
  });
  console.log(firstText(detail)); // full detail JSON, or "null" when unknown
} finally {
  await client.close();
}

Ambas as ferramentas exigem o escopo brands:read (ou uma chave irrestrita, de escopo vazio). Uma chave ausente ou inválida faz a chamada de ferramenta retornar um resultado de erro — nunca dados do corpus. Veja o guia MCP e o guia de autenticação.

Veja também