跳转到主要内容

概述

Tools API 提供对 Shannon 工具注册表的直接访问。您可以浏览可用工具、查看参数 Schema,以及绕过完整的任务工作流直接执行工具。

功能特性

  • 工具发现 — 浏览所有可用工具,支持按类别过滤
  • JSON Schema 查看 — 获取任意工具的完整参数 Schema
  • 直接执行 — 在 Agent 工作流之外直接调用工具
  • 危险工具拦截 — 被标记为危险的工具在 Gateway 层自动隐藏和拦截
  • 用量追踪 — 每次执行记录 Token 消耗和成本
  • Session 上下文 — 可选绑定到 Session(仅保留 session_iduser_id

安全模型

Shannon 对标记为 dangerous 的工具实施严格的安全边界:
  • List 端点自动应用 exclude_dangerous=true — 危险工具永远不会暴露
  • Get 和 Execute 端点对危险工具返回 403 Forbidden
  • 工具元数据在每个 Gateway 实例中缓存 5 分钟
  • 工具执行的 HTTP 客户端超时时间为 120 秒
危险工具无法通过任何公共 API 端点访问。此限制在 Gateway 层强制执行,客户端无法绕过。

列出工具

列出所有可用工具。危险工具会自动排除。
curl http://localhost:8080/api/v1/tools \
  -H "Authorization: Bearer <token>"

查询参数

参数类型必填描述
categorystring按类别过滤工具(如 "search""code"

按类别过滤示例

curl "http://localhost:8080/api/v1/tools?category=search" \
  -H "Authorization: Bearer <token>"

响应

[
  {
    "name": "web_search",
    "description": "Search the web for current information",
    "category": "search",
    "version": "1.0",
    "parameters": {
      "type": "object",
      "properties": {
        "query": {
          "type": "string",
          "description": "Search query"
        },
        "max_results": {
          "type": "integer",
          "description": "Maximum number of results",
          "default": 10
        }
      },
      "required": ["query"]
    },
    "timeout_seconds": 30,
    "cost_per_use": 0.004
  }
]

获取工具

获取指定工具的元数据和参数 Schema。
curl http://localhost:8080/api/v1/tools/web_search \
  -H "Authorization: Bearer <token>"

路径参数

参数类型必填描述
namestring工具名称

响应

{
  "name": "web_search",
  "description": "Search the web for current information",
  "category": "search",
  "version": "1.0",
  "parameters": {
    "type": "object",
    "properties": {
      "query": {
        "type": "string",
        "description": "Search query"
      },
      "max_results": {
        "type": "integer",
        "description": "Maximum number of results",
        "default": 10
      }
    },
    "required": ["query"]
  },
  "timeout_seconds": 30,
  "cost_per_use": 0.004
}

执行工具

使用提供的参数直接执行指定工具。
curl -X POST http://localhost:8080/api/v1/tools/web_search/execute \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer <token>" \
  -d '{
    "arguments": {
      "query": "latest AI news",
      "max_results": 5
    },
    "session_id": "optional-session-id"
  }'

路径参数

参数类型必填描述
namestring工具名称

请求体

字段类型必填描述
argumentsobject与工具参数 Schema 匹配的工具特定参数
session_idstring用于上下文绑定的 Session ID
提供 session_id 时,仅 session_iduser_id 会传递给工具。出于安全考虑,tenant_id 等其他 Session 字段会被剥离。

响应

{
  "success": true,
  "output": {
    "results": [
      {
        "title": "Major AI Breakthrough Announced",
        "url": "https://example.com/ai-news",
        "snippet": "Researchers announce a significant advancement in..."
      }
    ]
  },
  "text": "Found 5 results for 'latest AI news'",
  "error": null,
  "metadata": {
    "source": "searchapi"
  },
  "execution_time_ms": 1234,
  "usage": {
    "tokens": 7500,
    "cost_usd": 0.004
  }
}

响应字段

字段类型描述
successboolean执行是否成功完成
outputobject工具返回的结构化输出
textstring人类可读的结果摘要
errorstring | null执行失败时的错误消息
metadataobject工具特定的元数据(如数据来源)
execution_time_msinteger执行耗时(毫秒)
usage.tokensinteger本次执行的 Token 数量
usage.cost_usdnumber预估成本(美元)
用量以异步方式(fire-and-forget)记录到 token_usage 表。Token 数量为合成值:max(100, int(cost_usd / 0.000002))

工具响应对象

字段类型描述
namestring工具标识符
descriptionstring人类可读的描述
categorystring工具类别(如 "search""code""data"
versionstring工具版本
parametersobject (JSON Schema)工具的参数 Schema
timeout_secondsinteger允许的最大执行时间
cost_per_usenumber每次调用的预估成本(美元)

错误响应

状态码错误描述
400Bad request缺少工具名称或请求体无效
401Unauthorized缺少或无效的认证信息
403Forbidden工具被标记为危险,无法访问
404Tool not found指定名称的工具不存在
502Bad gatewayLLM Service(工具后端)不可用
所有错误遵循标准格式:
{
  "error": "error message here"
}

危险工具错误

尝试通过 Get 或 Execute 访问危险工具时返回:
{
  "error": "Tool not available via direct execution"
}

后续步骤