@tronsfey/ucli 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +349 -0
- package/README.zh.md +340 -0
- package/assets/logo.svg +138 -0
- package/dist/index.js +550 -0
- package/dist/index.js.map +1 -0
- package/package.json +44 -0
- package/skill.md +223 -0
package/README.md
ADDED
|
@@ -0,0 +1,349 @@
|
|
|
1
|
+
<h1 align="center">ucli</h1>
|
|
2
|
+
|
|
3
|
+
<p align="center">
|
|
4
|
+
<a href="https://www.npmjs.com/package/@tronsfey/ucli"><img src="https://img.shields.io/npm/v/@tronsfey/ucli?color=2563eb" alt="npm version"/></a>
|
|
5
|
+
<img src="https://img.shields.io/badge/Commander.js-ESM-38bdf8" alt="Commander.js"/>
|
|
6
|
+
<img src="https://img.shields.io/badge/node-%3E%3D18-38bdf8" alt="node"/>
|
|
7
|
+
<img src="https://img.shields.io/badge/license-MIT-22c55e" alt="license"/>
|
|
8
|
+
</p>
|
|
9
|
+
|
|
10
|
+
<p align="center">
|
|
11
|
+
English | <a href="./README.zh.md">中文</a>
|
|
12
|
+
</p>
|
|
13
|
+
|
|
14
|
+
---
|
|
15
|
+
|
|
16
|
+
## Overview
|
|
17
|
+
|
|
18
|
+
`@tronsfey/ucli` is the client component of ucli. It gives AI agents (and humans) a simple interface to:
|
|
19
|
+
|
|
20
|
+
- **Discover** OpenAPI services registered on a ucli server
|
|
21
|
+
- **Execute** API operations without ever handling credentials directly
|
|
22
|
+
- **Cache** specs locally to reduce round-trips
|
|
23
|
+
- **Invoke** MCP server tools via `ucli mcp run`
|
|
24
|
+
|
|
25
|
+
Auth credentials (bearer tokens, API keys, OAuth2 secrets, MCP headers/env) are stored encrypted on the server and injected at runtime — they are **never written to disk** or visible in process listings.
|
|
26
|
+
|
|
27
|
+
## How It Works
|
|
28
|
+
|
|
29
|
+
```mermaid
|
|
30
|
+
sequenceDiagram
|
|
31
|
+
participant Agent as AI Agent / User
|
|
32
|
+
participant CLI as ucli
|
|
33
|
+
participant Server as ucli-server
|
|
34
|
+
participant Cache as Local Cache
|
|
35
|
+
participant API as Target API
|
|
36
|
+
|
|
37
|
+
Agent->>CLI: ucli configure --server URL --token JWT
|
|
38
|
+
CLI->>CLI: Save config (OS config dir)
|
|
39
|
+
|
|
40
|
+
Agent->>CLI: ucli services list
|
|
41
|
+
CLI->>Cache: Check local cache (TTL)
|
|
42
|
+
alt Cache miss
|
|
43
|
+
CLI->>Server: GET /api/v1/oas (Bearer JWT)
|
|
44
|
+
Server-->>CLI: [ { name, description, ... } ]
|
|
45
|
+
CLI->>Cache: Write cache entry
|
|
46
|
+
end
|
|
47
|
+
Cache-->>CLI: OAS list
|
|
48
|
+
CLI-->>Agent: Table / JSON output
|
|
49
|
+
|
|
50
|
+
Agent->>CLI: ucli run --service payments --operation createPayment --params '{...}'
|
|
51
|
+
CLI->>Server: GET /api/v1/oas/payments (Bearer JWT)
|
|
52
|
+
Server-->>CLI: OAS spec + decrypted authConfig (TLS)
|
|
53
|
+
CLI->>CLI: Inject authConfig as ENV vars
|
|
54
|
+
CLI->>API: spawn @tronsfey/openapi2cli (ENV: auth creds)
|
|
55
|
+
API-->>CLI: HTTP response
|
|
56
|
+
CLI-->>Agent: Formatted output (JSON / table / YAML)
|
|
57
|
+
|
|
58
|
+
Agent->>CLI: ucli mcp run my-server get_weather --city Beijing
|
|
59
|
+
CLI->>Server: GET /api/v1/mcp/my-server (Bearer JWT)
|
|
60
|
+
Server-->>CLI: McpEntry + decrypted authConfig (TLS)
|
|
61
|
+
CLI->>CLI: Inject auth as headers/env into mcp2cli config
|
|
62
|
+
CLI->>MCP: @tronsfey/mcp2cli (programmatic, no subprocess)
|
|
63
|
+
MCP-->>CLI: Tool result (JSON)
|
|
64
|
+
CLI-->>Agent: Output
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## Installation
|
|
68
|
+
|
|
69
|
+
```bash
|
|
70
|
+
npm install -g @tronsfey/ucli
|
|
71
|
+
# or
|
|
72
|
+
pnpm add -g @tronsfey/ucli
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## Quick Start
|
|
76
|
+
|
|
77
|
+
```bash
|
|
78
|
+
# 1. Configure (get server URL and JWT from your admin)
|
|
79
|
+
ucli configure --server http://localhost:3000 --token <group-jwt>
|
|
80
|
+
|
|
81
|
+
# 2. List available services
|
|
82
|
+
ucli services list
|
|
83
|
+
|
|
84
|
+
# 3. Inspect a service's operations
|
|
85
|
+
ucli services info payments
|
|
86
|
+
|
|
87
|
+
# 4. Run an operation
|
|
88
|
+
ucli run --service payments --operation getPetById --params '{"petId": 42}'
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
## Command Reference
|
|
92
|
+
|
|
93
|
+
### `configure`
|
|
94
|
+
|
|
95
|
+
Store the server URL and group JWT locally.
|
|
96
|
+
|
|
97
|
+
```bash
|
|
98
|
+
ucli configure --server <url> --token <jwt>
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
| Flag | Required | Description |
|
|
102
|
+
|------|----------|-------------|
|
|
103
|
+
| `--server` | Yes | ucli server URL (e.g. `https://gateway.example.com`) |
|
|
104
|
+
| `--token` | Yes | Group JWT issued by the server admin |
|
|
105
|
+
|
|
106
|
+
Config is stored in the OS-appropriate config directory:
|
|
107
|
+
- Linux/macOS: `~/.config/ucli/`
|
|
108
|
+
- Windows: `%APPDATA%\ucli\`
|
|
109
|
+
|
|
110
|
+
---
|
|
111
|
+
|
|
112
|
+
### `services list`
|
|
113
|
+
|
|
114
|
+
List all OpenAPI services available to your group.
|
|
115
|
+
|
|
116
|
+
```bash
|
|
117
|
+
ucli services list [--format table|json|yaml] [--refresh]
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
| Flag | Default | Description |
|
|
121
|
+
|------|---------|-------------|
|
|
122
|
+
| `--format` | `table` | Output format: `table`, `json`, or `yaml` |
|
|
123
|
+
| `--refresh` | `false` | Bypass local cache and fetch fresh from server |
|
|
124
|
+
|
|
125
|
+
**Example output (table):**
|
|
126
|
+
|
|
127
|
+
```
|
|
128
|
+
NAME DESCRIPTION CACHE TTL
|
|
129
|
+
payments Payments service API 3600s
|
|
130
|
+
inventory Inventory management 1800s
|
|
131
|
+
crm CRM operations 7200s
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
**Example output (json):**
|
|
135
|
+
|
|
136
|
+
```json
|
|
137
|
+
[
|
|
138
|
+
{ "name": "payments", "description": "Payments service API", "cacheTtl": 3600 },
|
|
139
|
+
{ "name": "inventory", "description": "Inventory management", "cacheTtl": 1800 }
|
|
140
|
+
]
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
---
|
|
144
|
+
|
|
145
|
+
### `services info <name>`
|
|
146
|
+
|
|
147
|
+
Show detailed information about a specific service, including its available operations.
|
|
148
|
+
|
|
149
|
+
```bash
|
|
150
|
+
ucli services info <service-name> [--format table|json|yaml]
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
| Argument/Flag | Description |
|
|
154
|
+
|---------------|-------------|
|
|
155
|
+
| `<name>` | Service name from `services list` |
|
|
156
|
+
| `--format` | Output format (`table` default) |
|
|
157
|
+
|
|
158
|
+
---
|
|
159
|
+
|
|
160
|
+
### `run`
|
|
161
|
+
|
|
162
|
+
Execute a single API operation defined in an OpenAPI spec.
|
|
163
|
+
|
|
164
|
+
```bash
|
|
165
|
+
ucli run --service <name> --operation <operationId> [options]
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
| Flag | Required | Description |
|
|
169
|
+
|------|----------|-------------|
|
|
170
|
+
| `--service` | Yes | Service name (from `services list`) |
|
|
171
|
+
| `--operation` | Yes | `operationId` from the OpenAPI spec |
|
|
172
|
+
| `--params` | No | JSON string of parameters (path, query, body merged) |
|
|
173
|
+
| `--format` | No | Output format: `json` (default), `table`, `yaml` |
|
|
174
|
+
| `--query` | No | JMESPath expression to filter the response |
|
|
175
|
+
|
|
176
|
+
**Examples:**
|
|
177
|
+
|
|
178
|
+
```bash
|
|
179
|
+
# GET with path parameter
|
|
180
|
+
ucli run --service petstore --operation getPetById \
|
|
181
|
+
--params '{"petId": 42}'
|
|
182
|
+
|
|
183
|
+
# POST with body
|
|
184
|
+
ucli run --service payments --operation createPayment \
|
|
185
|
+
--params '{"amount": 100, "currency": "USD", "recipient": "acct_123"}' \
|
|
186
|
+
--format json
|
|
187
|
+
|
|
188
|
+
# GET with query parameter + JMESPath filter
|
|
189
|
+
ucli run --service inventory --operation listProducts \
|
|
190
|
+
--params '{"category": "electronics", "limit": 10}' \
|
|
191
|
+
--query 'items[?price < `50`].name'
|
|
192
|
+
|
|
193
|
+
# POST with data from file
|
|
194
|
+
ucli run --service crm --operation createContact \
|
|
195
|
+
--params "@./contact.json"
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
---
|
|
199
|
+
|
|
200
|
+
### `mcp list`
|
|
201
|
+
|
|
202
|
+
List all MCP servers available to your group.
|
|
203
|
+
|
|
204
|
+
```bash
|
|
205
|
+
ucli mcp list [--format table|json|yaml]
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
| Flag | Default | Description |
|
|
209
|
+
|------|---------|-------------|
|
|
210
|
+
| `--format` | `table` | Output format: `table`, `json`, or `yaml` |
|
|
211
|
+
|
|
212
|
+
---
|
|
213
|
+
|
|
214
|
+
### `mcp tools <server>`
|
|
215
|
+
|
|
216
|
+
List tools available on a specific MCP server.
|
|
217
|
+
|
|
218
|
+
```bash
|
|
219
|
+
ucli mcp tools <server-name> [--format table|json]
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
| Argument/Flag | Description |
|
|
223
|
+
|---------------|-------------|
|
|
224
|
+
| `<server-name>` | MCP server name from `mcp list` |
|
|
225
|
+
| `--format` | Output format (`table` default) |
|
|
226
|
+
|
|
227
|
+
---
|
|
228
|
+
|
|
229
|
+
### `mcp run <server> <tool> [args...]`
|
|
230
|
+
|
|
231
|
+
Execute a tool on an MCP server.
|
|
232
|
+
|
|
233
|
+
```bash
|
|
234
|
+
ucli mcp run <server-name> <tool-name> [args...]
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
Args are passed as `key=value` pairs and converted to a JSON object.
|
|
238
|
+
|
|
239
|
+
**Examples:**
|
|
240
|
+
|
|
241
|
+
```bash
|
|
242
|
+
# Call a weather tool
|
|
243
|
+
ucli mcp run weather get_forecast location="New York" units=metric
|
|
244
|
+
|
|
245
|
+
# Call a search tool
|
|
246
|
+
ucli mcp run search-server web_search query="ucli MCP" limit=5
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
---
|
|
250
|
+
|
|
251
|
+
### `refresh`
|
|
252
|
+
|
|
253
|
+
Force-refresh the local OAS cache from the server.
|
|
254
|
+
|
|
255
|
+
```bash
|
|
256
|
+
ucli refresh [--service <name>]
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
| Flag | Description |
|
|
260
|
+
|------|-------------|
|
|
261
|
+
| `--service` | Refresh only a specific service (omit to refresh all) |
|
|
262
|
+
|
|
263
|
+
---
|
|
264
|
+
|
|
265
|
+
### `help`
|
|
266
|
+
|
|
267
|
+
Show available commands and AI agent usage instructions.
|
|
268
|
+
|
|
269
|
+
```bash
|
|
270
|
+
ucli help
|
|
271
|
+
```
|
|
272
|
+
|
|
273
|
+
## Configuration
|
|
274
|
+
|
|
275
|
+
Config is managed via the `configure` command. Values are stored in the OS config dir using [conf](https://github.com/sindresorhus/conf).
|
|
276
|
+
|
|
277
|
+
| Key | Description |
|
|
278
|
+
|-----|-------------|
|
|
279
|
+
| `serverUrl` | ucli server URL |
|
|
280
|
+
| `token` | Group JWT for authenticating with the server |
|
|
281
|
+
|
|
282
|
+
## Caching
|
|
283
|
+
|
|
284
|
+
- OAS entries are cached locally as JSON files in the OS temp dir (`ucli/` subdirectory)
|
|
285
|
+
- Cache TTL per entry is set by the server admin via the `cacheTtl` field (seconds)
|
|
286
|
+
- Expired entries are automatically re-fetched on next access
|
|
287
|
+
- Force a refresh: `ucli refresh` or use `--refresh` flag on `services list`
|
|
288
|
+
|
|
289
|
+
## Auth Handling
|
|
290
|
+
|
|
291
|
+
Credentials are **never exposed** to the agent or written to disk:
|
|
292
|
+
|
|
293
|
+
1. CLI fetches the OAS entry from the server over TLS (includes decrypted `authConfig`)
|
|
294
|
+
2. `authConfig` is passed as **environment variables** to the `@tronsfey/openapi2cli` subprocess
|
|
295
|
+
3. The subprocess uses the credentials to call the target API
|
|
296
|
+
4. The in-memory `authConfig` is discarded after the subprocess exits
|
|
297
|
+
|
|
298
|
+
This means credentials never appear in:
|
|
299
|
+
- Process listings (`ps aux`)
|
|
300
|
+
- Shell history
|
|
301
|
+
- Log files
|
|
302
|
+
- The agent's context window
|
|
303
|
+
|
|
304
|
+
For MCP servers, auth (`http_headers` or `env`) is injected directly into the `@tronsfey/mcp2cli` programmatic config — it is **never passed as CLI arguments** (which would be visible in `ps`).
|
|
305
|
+
|
|
306
|
+
## For AI Agents
|
|
307
|
+
|
|
308
|
+
The recommended workflow for AI agents using `ucli` as a skill:
|
|
309
|
+
|
|
310
|
+
```bash
|
|
311
|
+
# Step 1: Discover available services
|
|
312
|
+
ucli services list --format json
|
|
313
|
+
|
|
314
|
+
# Step 2: Inspect a service to see available operations
|
|
315
|
+
ucli services info <service-name> --format json
|
|
316
|
+
|
|
317
|
+
# Step 3: Execute an operation
|
|
318
|
+
ucli run --service <name> --operation <operationId> \
|
|
319
|
+
--params '{ ... }' --format json
|
|
320
|
+
|
|
321
|
+
# Step 4: Filter results with JMESPath
|
|
322
|
+
ucli run --service inventory --operation listProducts \
|
|
323
|
+
--query 'items[?inStock == `true`] | [0:5]'
|
|
324
|
+
|
|
325
|
+
# Step 5: Chain operations (use output from one as input to another)
|
|
326
|
+
PRODUCT_ID=$(ucli run --service inventory --operation listProducts \
|
|
327
|
+
--query 'items[0].id' | tr -d '"')
|
|
328
|
+
ucli run --service orders --operation createOrder \
|
|
329
|
+
--params "{\"productId\": \"$PRODUCT_ID\", \"quantity\": 1}"
|
|
330
|
+
```
|
|
331
|
+
|
|
332
|
+
**Tips for agents:**
|
|
333
|
+
- Always run `services list` first to discover what's available
|
|
334
|
+
- Use `--format json` for programmatic parsing
|
|
335
|
+
- Use `--query` with JMESPath to extract specific fields
|
|
336
|
+
- Check pagination fields (`nextPage`, `totalCount`) for list operations
|
|
337
|
+
- If a service seems stale, run `ucli refresh --service <name>`
|
|
338
|
+
|
|
339
|
+
## Error Reference
|
|
340
|
+
|
|
341
|
+
| Error | Likely Cause | Resolution |
|
|
342
|
+
|-------|-------------|------------|
|
|
343
|
+
| `Unauthorized (401)` | JWT expired or revoked | Get a new token from the admin |
|
|
344
|
+
| `Service not found` | Service name misspelled or not in group | Run `services list` to see available services |
|
|
345
|
+
| `Operation not found` | Invalid `operationId` | Run `services info <name>` to see valid operations |
|
|
346
|
+
| `MCP server not found` | Server name misspelled or not in group | Run `ucli mcp list` to see available servers |
|
|
347
|
+
| `Tool not found` | Invalid tool name | Run `ucli mcp tools <server>` to see available tools |
|
|
348
|
+
| `Connection refused` | Server not running or wrong URL | Check server URL with `ucli configure` |
|
|
349
|
+
| `Cache error` | Temp dir permissions issue | Run `ucli refresh` to reset cache |
|
package/README.zh.md
ADDED
|
@@ -0,0 +1,340 @@
|
|
|
1
|
+
<h1 align="center">ucli</h1>
|
|
2
|
+
|
|
3
|
+
<p align="center">
|
|
4
|
+
<a href="https://www.npmjs.com/package/@tronsfey/ucli"><img src="https://img.shields.io/npm/v/@tronsfey/ucli?color=2563eb" alt="npm version"/></a>
|
|
5
|
+
<img src="https://img.shields.io/badge/Commander.js-ESM-38bdf8" alt="Commander.js"/>
|
|
6
|
+
<img src="https://img.shields.io/badge/node-%3E%3D18-38bdf8" alt="node"/>
|
|
7
|
+
<img src="https://img.shields.io/badge/license-MIT-22c55e" alt="license"/>
|
|
8
|
+
</p>
|
|
9
|
+
|
|
10
|
+
<p align="center">
|
|
11
|
+
<a href="./README.md">English</a> | 中文
|
|
12
|
+
</p>
|
|
13
|
+
|
|
14
|
+
---
|
|
15
|
+
|
|
16
|
+
## 概述
|
|
17
|
+
|
|
18
|
+
`@tronsfey/ucli` 是 ucli 的客户端组件,为 AI 智能体(和人类)提供简洁的接口来:
|
|
19
|
+
|
|
20
|
+
- **发现** 注册在 ucli 服务端上的 OpenAPI 服务
|
|
21
|
+
- **执行** API 操作,无需直接处理凭据
|
|
22
|
+
- **本地缓存** 规范,减少网络请求
|
|
23
|
+
- **调用** MCP 服务器工具,使用 `ucli mcp run`
|
|
24
|
+
|
|
25
|
+
认证凭据(Bearer Token、API 密钥、OAuth2 密钥、MCP 请求头/环境变量)在服务端加密存储,运行时以环境变量或请求头方式注入——**永不落盘**,也不会出现在进程列表中。
|
|
26
|
+
|
|
27
|
+
## 工作原理
|
|
28
|
+
|
|
29
|
+
```mermaid
|
|
30
|
+
sequenceDiagram
|
|
31
|
+
participant Agent as AI 智能体 / 用户
|
|
32
|
+
participant CLI as ucli
|
|
33
|
+
participant Server as ucli-server
|
|
34
|
+
participant Cache as 本地缓存
|
|
35
|
+
participant API as 目标 API
|
|
36
|
+
|
|
37
|
+
Agent->>CLI: ucli configure --server URL --token JWT
|
|
38
|
+
CLI->>CLI: 保存配置(OS 配置目录)
|
|
39
|
+
|
|
40
|
+
Agent->>CLI: ucli services list
|
|
41
|
+
CLI->>Cache: 检查本地缓存(TTL)
|
|
42
|
+
alt 缓存未命中
|
|
43
|
+
CLI->>Server: GET /api/v1/oas(Bearer JWT)
|
|
44
|
+
Server-->>CLI: [ { name, description, ... } ]
|
|
45
|
+
CLI->>Cache: 写入缓存
|
|
46
|
+
end
|
|
47
|
+
Cache-->>CLI: OAS 列表
|
|
48
|
+
CLI-->>Agent: 表格 / JSON 输出
|
|
49
|
+
|
|
50
|
+
Agent->>CLI: ucli run --service payments --operation createPayment --params '{...}'
|
|
51
|
+
CLI->>Server: GET /api/v1/oas/payments(Bearer JWT)
|
|
52
|
+
Server-->>CLI: OAS 规范 + 解密认证配置(TLS)
|
|
53
|
+
CLI->>CLI: 将认证配置注入 ENV 变量
|
|
54
|
+
CLI->>API: 启动 @tronsfey/openapi2cli(ENV 含认证凭据)
|
|
55
|
+
API-->>CLI: HTTP 响应
|
|
56
|
+
CLI-->>Agent: 格式化输出(JSON / 表格 / YAML)
|
|
57
|
+
|
|
58
|
+
Agent->>CLI: ucli mcp run my-server get_weather --city Beijing
|
|
59
|
+
CLI->>Server: GET /api/v1/mcp/my-server(Bearer JWT)
|
|
60
|
+
Server-->>CLI: McpEntry + 解密认证配置(TLS)
|
|
61
|
+
CLI->>CLI: 将认证信息注入 mcp2cli 配置(headers/env)
|
|
62
|
+
CLI->>MCP: @tronsfey/mcp2cli(程序化调用,非子进程)
|
|
63
|
+
MCP-->>CLI: 工具执行结果(JSON)
|
|
64
|
+
CLI-->>Agent: 输出
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## 安装
|
|
68
|
+
|
|
69
|
+
```bash
|
|
70
|
+
npm install -g @tronsfey/ucli
|
|
71
|
+
# 或
|
|
72
|
+
pnpm add -g @tronsfey/ucli
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## 快速开始
|
|
76
|
+
|
|
77
|
+
```bash
|
|
78
|
+
# 1. 配置(从管理员获取服务器 URL 和 JWT)
|
|
79
|
+
ucli configure --server http://localhost:3000 --token <group-jwt>
|
|
80
|
+
|
|
81
|
+
# 2. 列出可用服务
|
|
82
|
+
ucli services list
|
|
83
|
+
|
|
84
|
+
# 3. 查看服务的操作列表
|
|
85
|
+
ucli services info payments
|
|
86
|
+
|
|
87
|
+
# 4. 执行操作
|
|
88
|
+
ucli run --service payments --operation getPetById --params '{"petId": 42}'
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
## 命令参考
|
|
92
|
+
|
|
93
|
+
### `configure`
|
|
94
|
+
|
|
95
|
+
将服务器 URL 和群组 JWT 保存到本地。
|
|
96
|
+
|
|
97
|
+
```bash
|
|
98
|
+
ucli configure --server <url> --token <jwt>
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
| 参数 | 必填 | 说明 |
|
|
102
|
+
|------|------|------|
|
|
103
|
+
| `--server` | 是 | ucli 服务器 URL(如 `https://gateway.example.com`) |
|
|
104
|
+
| `--token` | 是 | 服务端管理员签发的群组 JWT |
|
|
105
|
+
|
|
106
|
+
配置存储在 OS 对应的配置目录:
|
|
107
|
+
- Linux/macOS:`~/.config/ucli/`
|
|
108
|
+
- Windows:`%APPDATA%\ucli\`
|
|
109
|
+
|
|
110
|
+
---
|
|
111
|
+
|
|
112
|
+
### `services list`
|
|
113
|
+
|
|
114
|
+
列出当前群组可访问的所有 OpenAPI 服务。
|
|
115
|
+
|
|
116
|
+
```bash
|
|
117
|
+
ucli services list [--format table|json|yaml] [--refresh]
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
| 参数 | 默认值 | 说明 |
|
|
121
|
+
|------|--------|------|
|
|
122
|
+
| `--format` | `table` | 输出格式:`table`、`json` 或 `yaml` |
|
|
123
|
+
| `--refresh` | `false` | 绕过本地缓存,从服务器重新拉取 |
|
|
124
|
+
|
|
125
|
+
**示例输出(table):**
|
|
126
|
+
|
|
127
|
+
```
|
|
128
|
+
NAME DESCRIPTION CACHE TTL
|
|
129
|
+
payments 支付服务 API 3600s
|
|
130
|
+
inventory 库存管理 1800s
|
|
131
|
+
crm CRM 操作 7200s
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
**示例输出(json):**
|
|
135
|
+
|
|
136
|
+
```json
|
|
137
|
+
[
|
|
138
|
+
{ "name": "payments", "description": "支付服务 API", "cacheTtl": 3600 },
|
|
139
|
+
{ "name": "inventory", "description": "库存管理", "cacheTtl": 1800 }
|
|
140
|
+
]
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
---
|
|
144
|
+
|
|
145
|
+
### `services info <name>`
|
|
146
|
+
|
|
147
|
+
显示指定服务的详细信息(包括可用操作列表)。
|
|
148
|
+
|
|
149
|
+
```bash
|
|
150
|
+
ucli services info <service-name> [--format table|json|yaml]
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
| 参数 | 说明 |
|
|
154
|
+
|------|------|
|
|
155
|
+
| `<name>` | 服务名称(来自 `services list`) |
|
|
156
|
+
| `--format` | 输出格式(默认 `table`) |
|
|
157
|
+
|
|
158
|
+
---
|
|
159
|
+
|
|
160
|
+
### `run`
|
|
161
|
+
|
|
162
|
+
执行 OpenAPI 规范中定义的单个 API 操作。
|
|
163
|
+
|
|
164
|
+
```bash
|
|
165
|
+
ucli run --service <name> --operation <operationId> [选项]
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
| 参数 | 必填 | 说明 |
|
|
169
|
+
|------|------|------|
|
|
170
|
+
| `--service` | 是 | 服务名称(来自 `services list`) |
|
|
171
|
+
| `--operation` | 是 | OpenAPI 规范中的 `operationId` |
|
|
172
|
+
| `--params` | 否 | JSON 字符串(路径参数、查询参数、请求体合并传入) |
|
|
173
|
+
| `--format` | 否 | 输出格式:`json`(默认)、`table`、`yaml` |
|
|
174
|
+
| `--query` | 否 | JMESPath 表达式,用于过滤响应 |
|
|
175
|
+
|
|
176
|
+
**示例:**
|
|
177
|
+
|
|
178
|
+
```bash
|
|
179
|
+
# GET 带路径参数
|
|
180
|
+
ucli run --service petstore --operation getPetById \
|
|
181
|
+
--params '{"petId": 42}'
|
|
182
|
+
|
|
183
|
+
# POST 带请求体
|
|
184
|
+
ucli run --service payments --operation createPayment \
|
|
185
|
+
--params '{"amount": 100, "currency": "CNY", "recipient": "acct_123"}' \
|
|
186
|
+
--format json
|
|
187
|
+
|
|
188
|
+
# 使用 JMESPath 过滤结果
|
|
189
|
+
ucli run --service inventory --operation listProducts \
|
|
190
|
+
--params '{"category": "electronics"}' \
|
|
191
|
+
--query 'items[?price < `500`].name'
|
|
192
|
+
|
|
193
|
+
# 从文件读取参数
|
|
194
|
+
ucli run --service crm --operation createContact \
|
|
195
|
+
--params "@./contact.json"
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
---
|
|
199
|
+
|
|
200
|
+
### `mcp list`
|
|
201
|
+
|
|
202
|
+
列出当前群组可访问的所有 MCP 服务器。
|
|
203
|
+
|
|
204
|
+
```bash
|
|
205
|
+
ucli mcp list [--format table|json|yaml]
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
---
|
|
209
|
+
|
|
210
|
+
### `mcp tools <server>`
|
|
211
|
+
|
|
212
|
+
列出指定 MCP 服务器上的可用工具。
|
|
213
|
+
|
|
214
|
+
```bash
|
|
215
|
+
ucli mcp tools <server-name> [--format table|json]
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
---
|
|
219
|
+
|
|
220
|
+
### `mcp run <server> <tool> [args...]`
|
|
221
|
+
|
|
222
|
+
在 MCP 服务器上执行指定工具。
|
|
223
|
+
|
|
224
|
+
```bash
|
|
225
|
+
ucli mcp run <server-name> <tool-name> [args...]
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
参数以 `key=value` 形式传入。
|
|
229
|
+
|
|
230
|
+
**示例:**
|
|
231
|
+
|
|
232
|
+
```bash
|
|
233
|
+
# 调用天气工具
|
|
234
|
+
ucli mcp run weather get_forecast location="北京" units=metric
|
|
235
|
+
|
|
236
|
+
# 调用搜索工具
|
|
237
|
+
ucli mcp run search-server web_search query="ucli MCP" limit=5
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
---
|
|
241
|
+
|
|
242
|
+
### `refresh`
|
|
243
|
+
|
|
244
|
+
强制从服务器刷新本地 OAS 缓存。
|
|
245
|
+
|
|
246
|
+
```bash
|
|
247
|
+
ucli refresh [--service <name>]
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
| 参数 | 说明 |
|
|
251
|
+
|------|------|
|
|
252
|
+
| `--service` | 仅刷新指定服务(不填则刷新所有) |
|
|
253
|
+
|
|
254
|
+
---
|
|
255
|
+
|
|
256
|
+
### `help`
|
|
257
|
+
|
|
258
|
+
显示命令列表及 AI 智能体使用说明。
|
|
259
|
+
|
|
260
|
+
```bash
|
|
261
|
+
ucli help
|
|
262
|
+
```
|
|
263
|
+
|
|
264
|
+
## 配置说明
|
|
265
|
+
|
|
266
|
+
配置通过 `configure` 命令管理,使用 [conf](https://github.com/sindresorhus/conf) 存储到 OS 配置目录。
|
|
267
|
+
|
|
268
|
+
| 键名 | 说明 |
|
|
269
|
+
|------|------|
|
|
270
|
+
| `serverUrl` | ucli 服务器 URL |
|
|
271
|
+
| `token` | 用于与服务端认证的群组 JWT |
|
|
272
|
+
|
|
273
|
+
## 缓存机制
|
|
274
|
+
|
|
275
|
+
- OAS 条目以 JSON 文件形式缓存到 OS 临时目录(`ucli/` 子目录)
|
|
276
|
+
- 每个条目的缓存 TTL 由服务端管理员通过 `cacheTtl` 字段设置(单位:秒)
|
|
277
|
+
- 过期条目在下次访问时自动重新拉取
|
|
278
|
+
- 强制刷新:`ucli refresh` 或在 `services list` 时添加 `--refresh`
|
|
279
|
+
|
|
280
|
+
## 认证处理
|
|
281
|
+
|
|
282
|
+
凭据**永不暴露**给智能体,也不会落盘:
|
|
283
|
+
|
|
284
|
+
1. CLI 通过 TLS 从服务器获取 OAS 条目(含解密后的 `authConfig`)
|
|
285
|
+
2. `authConfig` 以**环境变量**方式传递给 `@tronsfey/openapi2cli` 子进程
|
|
286
|
+
3. 子进程使用凭据调用目标 API
|
|
287
|
+
4. 子进程退出后,内存中的 `authConfig` 被丢弃
|
|
288
|
+
|
|
289
|
+
凭据不会出现在:
|
|
290
|
+
- 进程列表(`ps aux`)
|
|
291
|
+
- Shell 历史记录
|
|
292
|
+
- 日志文件
|
|
293
|
+
- 智能体的上下文窗口
|
|
294
|
+
|
|
295
|
+
对于 MCP 服务器,认证信息(`http_headers` 或 `env`)直接注入 `@tronsfey/mcp2cli` 的程序化配置中——**永不作为 CLI 参数传递**(否则会出现在 `ps` 列表中)。
|
|
296
|
+
|
|
297
|
+
## AI 智能体使用指南
|
|
298
|
+
|
|
299
|
+
AI 智能体将 `ucli` 作为技能使用时,推荐的工作流程:
|
|
300
|
+
|
|
301
|
+
```bash
|
|
302
|
+
# 第一步:发现可用服务
|
|
303
|
+
ucli services list --format json
|
|
304
|
+
|
|
305
|
+
# 第二步:查看服务支持的操作
|
|
306
|
+
ucli services info <service-name> --format json
|
|
307
|
+
|
|
308
|
+
# 第三步:执行操作
|
|
309
|
+
ucli run --service <name> --operation <operationId> \
|
|
310
|
+
--params '{ ... }' --format json
|
|
311
|
+
|
|
312
|
+
# 第四步:用 JMESPath 过滤结果
|
|
313
|
+
ucli run --service inventory --operation listProducts \
|
|
314
|
+
--query 'items[?inStock == `true`] | [0:5]'
|
|
315
|
+
|
|
316
|
+
# 第五步:链式操作(将前一个结果作为下一个的输入)
|
|
317
|
+
PRODUCT_ID=$(ucli run --service inventory --operation listProducts \
|
|
318
|
+
--query 'items[0].id' | tr -d '"')
|
|
319
|
+
ucli run --service orders --operation createOrder \
|
|
320
|
+
--params "{\"productId\": \"$PRODUCT_ID\", \"quantity\": 1}"
|
|
321
|
+
```
|
|
322
|
+
|
|
323
|
+
**智能体使用建议:**
|
|
324
|
+
- 始终先运行 `services list` 发现可用服务
|
|
325
|
+
- 使用 `--format json` 方便程序解析
|
|
326
|
+
- 使用 `--query` 配合 JMESPath 提取特定字段
|
|
327
|
+
- 注意列表操作的分页字段(`nextPage`、`totalCount`)
|
|
328
|
+
- 若服务数据疑似过期,执行 `ucli refresh --service <name>`
|
|
329
|
+
|
|
330
|
+
## 错误参考
|
|
331
|
+
|
|
332
|
+
| 错误 | 可能原因 | 解决方法 |
|
|
333
|
+
|------|---------|---------|
|
|
334
|
+
| `Unauthorized (401)` | JWT 已过期或被吊销 | 联系管理员获取新令牌 |
|
|
335
|
+
| `Service not found` | 服务名拼写错误或不在当前群组 | 运行 `services list` 查看可用服务 |
|
|
336
|
+
| `Operation not found` | 无效的 `operationId` | 运行 `services info <name>` 查看有效操作 |
|
|
337
|
+
| `MCP server not found` | MCP 服务器名拼写错误或不在当前群组 | 运行 `ucli mcp list` 查看可用服务器 |
|
|
338
|
+
| `Tool not found` | 无效的工具名 | 运行 `ucli mcp tools <server>` 查看可用工具 |
|
|
339
|
+
| `Connection refused` | 服务器未运行或 URL 错误 | 用 `ucli configure` 检查服务器 URL |
|
|
340
|
+
| `Cache error` | 临时目录权限问题 | 运行 `ucli refresh` 重置缓存 |
|