// npm install @modelcontextprotocol/sdk
// NOVODID_API_TOKEN must be provided through a private environment or secret manager.
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
const token = process.env.NOVODID_API_TOKEN;
if (!token || token.length > 4096) {
  console.error("Configure NOVODID_API_TOKEN privately.");
  process.exit(1);
}
const client = new Client({
  name: "novodid-readonly-recipe",
  version: "1.0.0",
});
try {
  await client.connect(
    new StreamableHTTPClientTransport(new URL("https://mcp.novodid.com/mcp"), {
      requestInit: { headers: { Authorization: `Bearer ${token}` } },
    }),
  );
  const tools = await client.listTools();
  if (!tools.tools.some((tool) => tool.name === "list_dids"))
    throw Error("Required scope unavailable");
  const result = await client.callTool({ name: "list_dids", arguments: {} });
  if (result.isError) throw Error("Read refused");
  console.log(
    JSON.stringify(result.structuredContent ?? result.content, null, 2),
  );
} catch {
  console.error(
    "MCP read failed. Check token, dids:read, account access and network; do not retry writes automatically.",
  );
  process.exitCode = 1;
} finally {
  await client.close();
}
