@auth/agent

中文文档
0.6.2发布于 2026/06/12MITTypeScriptES Module构建来源已验证近 30 天下载 1.7万

Client SDK for the Agent Auth Protocol — agent identity, registration, and capability-based authorization

原始使用文档
文档目录13 节
  1. Installation
  2. Quick Start
  3. AI Framework Integration
  4. Vercel AI SDK
  5. OpenAI Function Calling
  6. Anthropic Claude
  7. Error Handling
  8. SDK Tools
  9. Filtering Tools
  10. Subpath Import
  11. Constraints (Section 2.13)
  12. Storage
  13. License

@auth/agent

Client SDK for the Agent Auth Protocol — agent identity, registration, and capability-based authorization.

Installation

npm install @auth/agent

Quick Start

import { AgentAuthClient } from "@auth/agent";

const client = new AgentAuthClient({
  directoryUrl: "https://directory.example.com",
});

// Discover a provider
const config = await client.discoverProvider("https://api.example.com");

// Connect an agent with constrained capabilities
const agent = await client.connectAgent({
  provider: "https://api.example.com",
  capabilities: ["read_data", { name: "transfer_money", constraints: { amount: { max: 1000 } } }],
  name: "my-assistant",
});

// Execute a capability
const result = await client.executeCapability({
  agentId: agent.agentId,
  capability: "read_data",
  arguments: { id: "user-123" },
});

AI Framework Integration

Vercel AI SDK

toAISDKTools auto-imports jsonSchema from the ai package. Pass it explicitly if preferred:

import { generateText } from "ai";
import { AgentAuthClient, getAgentAuthTools, toAISDKTools } from "@auth/agent";

const client = new AgentAuthClient();
const tools = await toAISDKTools(getAgentAuthTools(client));

const { text } = await generateText({
  model: openai("gpt-4o"),
  tools,
  prompt: "Transfer $50 to Alice",
});

To pass jsonSchema explicitly (avoids the dynamic import):

import { generateText, jsonSchema } from "ai";
import { AgentAuthClient, getAgentAuthTools, toAISDKTools } from "@auth/agent";

const client = new AgentAuthClient();
const tools = await toAISDKTools(getAgentAuthTools(client), { jsonSchema });

OpenAI Function Calling

import { AgentAuthClient, getAgentAuthTools, toOpenAITools } from "@auth/agent";

const client = new AgentAuthClient();
const { definitions, execute } = toOpenAITools(getAgentAuthTools(client), {
  strict: true, // structured outputs — prevents hallucinated arguments
});

const res = await openai.chat.completions.create({
  model: "gpt-4o",
  tools: definitions,
  messages,
});

for (const call of res.choices[0].message.tool_calls ?? []) {
  const result = await execute(call.function.name, JSON.parse(call.function.arguments));
}

Anthropic Claude

import { AgentAuthClient, getAgentAuthTools, toAnthropicTools } from "@auth/agent";

const client = new AgentAuthClient();
const { definitions, processToolUse } = toAnthropicTools(getAgentAuthTools(client));

const res = await anthropic.messages.create({
  model: "claude-sonnet-4-20250514",
  max_tokens: 1024,
  tools: definitions,
  messages,
});

const toolUseBlocks = res.content.filter((b) => b.type === "tool_use");
if (toolUseBlocks.length > 0) {
  const results = await processToolUse(toolUseBlocks);
  messages.push({ role: "assistant", content: res.content }, { role: "user", content: results });
}

Error Handling

All adapters wrap tool execution errors as structured { error, code } objects instead of throwing. This lets models recover gracefully:

{ "error": "Capability not granted", "code": "capability_not_granted" }

SDK Tools

The SDK exposes protocol tools that map to the agent lifecycle:

ToolDescription
list_providersList discovered/configured providers
search_providersSearch registry by intent
discover_providerLook up a provider by URL
list_capabilitiesList provider capabilities
describe_capabilityGet full capability definition
connect_agentRegister an agent (with optional constraints)
execute_capabilityExecute a granted capability
request_capabilityRequest additional capabilities
agent_statusCheck agent status and grants
sign_jwtSign an agent JWT manually
disconnect_agentRevoke an agent
reactivate_agentReactivate an expired agent
rotate_agent_keyRotate agent keypair
rotate_host_keyRotate host keypair
enroll_hostEnroll host with enrollment token

Filtering Tools

Use filterTools to expose only the tools your agent needs:

import { getAgentAuthTools, filterTools } from "@auth/agent";

const allTools = getAgentAuthTools(client);

const minimal = filterTools(allTools, { only: ["execute_capability", "agent_status"] });
const safe = filterTools(allTools, { exclude: ["sign_jwt", "rotate_host_key"] });

Subpath Import

For lighter imports when you only need tools + adapters (no client, crypto, or storage):

import { getAgentAuthTools, toOpenAITools, filterTools } from "@auth/agent/tools";

Constraints (Section 2.13)

Pass constraints when connecting or requesting capabilities to restrict argument values:

await client.connectAgent({
  provider: "https://api.example.com",
  capabilities: [
    "read_data",
    {
      name: "transfer_money",
      constraints: {
        amount: { max: 1000, min: 1 },
        currency: { in: ["USD", "EUR"] },
      },
    },
  ],
});

Constraint grants are returned in capabilityGrants[].constraints.

Storage

The SDK uses pluggable storage for persisting host identity and agent connections:

import { AgentAuthClient } from "@auth/agent";

const client = new AgentAuthClient({
  storage: myCustomStorage, // implements Storage interface
});

Built-in: MemoryStorage (default, non-persistent). For long-running apps, implement the Storage interface with your preferred backend (database, filesystem, KV store, etc.):

Method groupDescription
getHostIdentity / setHostIdentity / deleteHostIdentityHost keypair and identity
getAgentConnection / setAgentConnection / deleteAgentConnectionPer-agent connection state
getProviderConfig / setProviderConfig / listProviderConfigsCached provider discovery docs

The CLI package (@auth/agent-cli) includes a file-based FileStorage implementation with optional encryption at rest — see packages/cli/src/storage.ts for a reference implementation.

License

MIT

版本与发布记录

@auth/agent 共发布 9 个版本,首次发布于 2026/03/18;过去 12 个月发布了 9 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.6.2latest2026/06/124 个月前
0.5.12026/05/165 个月前
0.4.62026/04/077 个月前
0.4.52026/03/297 个月前
0.4.42026/03/297 个月前
0.4.32026/03/247 个月前

关于 @auth/agent 的常见问题

@auth/agent 是什么?

Client SDK for the Agent Auth Protocol — agent identity, registration, and capability-based authorization

如何安装 @auth/agent?

在项目目录运行 npm install @auth/agent,或使用 pnpm add / yarn add / bun add。

@auth/agent 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@auth/agent 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@auth/agent 可以商用吗?

@auth/agent 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@auth/agent 的下载量有多少?

根据 npm 官方下载统计,@auth/agent 在 2026/10/07 – 2026/11/05 这 30 天内被下载 16,668 次,日均约 556 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@auth/agent 还在维护吗?

最新版本 0.6.2 发布于 2026/06/12(4 个月前);至今共发布 9 个版本,其中过去 12 个月发布了 9 个;首次发布于 2026/03/18。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。