tyc-cli 0.1.1 → 0.2.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  # tyc-cli
2
2
 
3
- > 天眼查 OpenAPI 业务语义层命令行工具 —— 为人类与 AI Agent 而生的企业数据查询利器
3
+ > 天眼查 MCP 命令行工具 —— 为人类与 AI Agent 而生的企业数据查询利器
4
4
 
5
5
  [![npm version](https://img.shields.io/npm/v/tyc-cli.svg)](https://www.npmjs.com/package/tyc-cli)
6
6
  [![npm download](https://img.shields.io/npm/dm/tyc-cli.svg)](https://www.npmjs.com/package/tyc-cli)
@@ -11,71 +11,19 @@
11
11
 
12
12
  ## 📖 项目简介
13
13
 
14
- `tyc-cli` 是基于天眼查 OpenAPI 的命令行工具,旨在帮助开发者和 AI Agent 快速访问企业工商信息、知识产权、司法风险、董监高画像等全维度商业数据。
14
+ `tyc-cli` 是天眼查 MCP Server 的官方命令行客户端。通过 MCP 协议(JSON-RPC 2.0 over
15
+ Streamable HTTP)调用天眼查 167 个业务语义聚合工具,覆盖企业工商、知产、司法风险、
16
+ 董监高等全维度商业数据。
15
17
 
16
- **核心能力**:
18
+ **核心特点**:
17
19
 
18
- - 🎯 **6 大业务分类**:企业基础信息(含股权图谱 / 集团 / 企业搜索 / 财务分析 / 企业报告 / 地理园区子分类)· 风险合规 · 知识产权(含建筑资质子分类)· 经营与公示(含投资机构 / 私募基金子分类)· 历史信息 · 董监高
19
- - 🔧 **167 个聚合工具**:每个工具内部自动并发调多个 tyc 原子 API,按业务语义合并输出
20
- - 🤖 **AI Agent 友好**:tyc 英文 key 透传 / 时间戳自动格式化 / 自动注入 `_summary` / `_empty` / `_warnings` 元数据
21
- - 🔒 **安全可控**:Authorization 透传不解析,配置文件本地化,敏感字段不入仓
22
-
23
- ---
24
-
25
- ## 🌟 为什么选择 tyc-cli?
26
-
27
- ### 🤖 为 AI Agent 原生设计
28
-
29
- - **tyc 英文 key 透传**:返回值顶层全为 `name` / `items` / `creditCode` / `total` 等英文字段,避免中英混用造成的 LLM 理解抖动
30
- - **时间戳自动格式化**:毫秒时间戳值自动转为 `Asia/Shanghai` 字符串(`yyyy-MM-dd`),key 名不变,便于 Agent 直接消费
31
- - **空结果归一化**:tyc 的 `经查无结果`(error_code 300000)自动归一为 `{items: [], total: 0, _empty: true}` + 友好摘要,便于 Agent 分支判断
32
- - **三种输出格式**:紧凑 JSON / 缩进 JSON (`--pretty`) / Markdown 表格 (`--md`),适配不同 Agent 上屏需求
33
- - **确定性错误码**:失败/熔断/无权限有清晰区分,可基于退出码自动重试
34
-
35
- ### 👤 为人类开发者设计
36
-
37
- - **简洁命令**:`tyc company registration-info "企业名称"`(自动剥离分类前缀,告别 `tyc company company-registration-info` 冗余)
38
- - **三种输出 mode**:
39
- - 默认紧凑 JSON:适合 `jq` 管道
40
- - `--pretty`:缩进 2 空格的 JSON,调试友好
41
- - `--md`:Markdown 表格,复制即用
42
- - **`--verbose` 调试**:打印 HTTP 请求详情到 stderr,定位问题
43
- - **`tyc --help` / `tyc <分类> --help`**:动态展开 6 分类下的全部命令
44
-
45
- ### 🏢 为企业级应用设计
46
-
47
- - **配置驱动**:统一配置 `~/.tyc/config.json`,支持多环境切换
48
- - **静态命令树**:构建时从 SSOT (`api-registry.yaml`) 生成命令,无需联网自省
49
- - **无状态**:每次调用独立鉴权,可水平扩展用于批处理脚本
50
-
51
- ### 🚀 零门槛上手
52
-
53
- - **3 分钟安装**:`npm install -g tyc-cli`
54
- - **一行命令查询**:无需编写代码,命令行直达数据
55
- - **MIT 协议**:可自由二次开发与商用分发
56
-
57
- ### 🛡️ 安全可控
58
-
59
- - **Authorization 透传**:CLI 不解析、不验签、不上报,原样发给天眼查 OpenAPI
60
- - **配置本地化**:token 仅保存在 `~/.tyc/config.json`,权限 600
61
- - **敏感字段不入仓**:`.gitignore` 排除 `.env` 与本地配置
62
-
63
- ---
64
-
65
- ## ⚡ 功能特性
66
-
67
- ### 6 大业务分类
68
-
69
- | 分类 | Go 包名 | 工具数 | 适合场景 |
70
- |------|---------|--------|---------|
71
- | 企业基础信息 | `company` | 52 | 工商核验、股东、年报、财务概览、股权图谱、集团、企业搜索、财务分析、信用报告、园区/经纬度 |
72
- | 风险合规 | `risk` | 36 | 失信、被执行、行政处罚、破产、欠税 |
73
- | 知识产权 | `intellectual_property` | 14 | 专利、商标、软著、知产出质、建筑资质 |
74
- | 经营与公示 | `operation` | 32 | 招投标、资质、许可、舆情、招聘、投资机构、私募基金 |
75
- | 历史信息 | `history` | 18 | 历史工商、历史司法、历史投资 |
76
- | 董监高 | `executive` | 15 | 高管个人画像、控制企业、合作伙伴 |
77
-
78
- 完整工具清单查看:`tyc <category> --help` 或本仓库 [`api-registry.yaml`](api-registry.yaml)。
20
+ - 🧠 **MCP 客户端架构**:CLI 只做协议转换与参数透传;多源合并、时间戳格式化、
21
+ 空结果归一化、`_summary` 注入等业务逻辑由 MCP Server 完成
22
+ - 🔌 **即插即用**:默认连接官方 MCP 端点 `https://ai-mcp.tianyancha.com/mcp`;
23
+ 支持 `--url` 指向私有部署
24
+ - 🔄 **Session 复用**:`Mcp-Session-Id` 本地缓存 24 小时,后续调用零 initialize 开销
25
+ - 🎯 **6 大业务分类 / 167 个工具**:企业基础信息 · 风险合规 · 知识产权 · 经营与公示 · 历史信息 · 董监高
26
+ - 🤖 **AI Agent 友好**:tyc 英文 key 透传 / 时间戳格式化 / `_summary / _empty / _warnings` 元数据
79
27
 
80
28
  ---
81
29
 
@@ -83,16 +31,16 @@
83
31
 
84
32
  ### 1. 环境准备
85
33
 
86
- - **Node.js**:≥ 18.0.0(推荐 LTS)
34
+ - **Node.js** ≥ 18.0.0(推荐 LTS)
87
35
  - **天眼查 API Token**:联系天眼查商务获取或自行注册
88
36
 
89
- ### 2. 安装工具
37
+ ### 2. 安装
90
38
 
91
39
  ```bash
92
40
  # 全局安装(推荐)
93
41
  npm install -g tyc-cli
94
42
 
95
- # 或本地安装后 npm link
43
+ # 或源码安装
96
44
  git clone https://github.com/tianyancha-tech/tyc-cli.git
97
45
  cd tyc-cli
98
46
  npm install && npm run build && npm link
@@ -101,413 +49,287 @@ npm install && npm run build && npm link
101
49
  ### 3. 初始化配置
102
50
 
103
51
  ```bash
52
+ # 连接官方 MCP(默认)
104
53
  tyc init --authorization "YOUR_API_TOKEN"
105
- # Authorization 保存在 ~/.tyc/config.json(与 MCP Server 共享)
54
+
55
+ # 连接自建 MCP
56
+ tyc init --authorization "YOUR_API_TOKEN" --url "http://your-mcp-host:8080/mcp"
57
+
58
+ # 仅写配置、不校验(离线环境或先配好稍后上线)
59
+ tyc init --authorization "YOUR_API_TOKEN" --no-verify
106
60
  ```
107
61
 
108
- ### 4. 开启查询
62
+ > `tyc init` 保存配置后会立即向 MCP 发一次 `initialize`:成功则打印 `已建立 MCP session`,
63
+ > 失败则退出码 1 并提示连通性问题。加 `--no-verify` 可跳过校验。
109
64
 
110
- ```bash
111
- # 企业工商信息
112
- tyc company registration-info "北京百度网讯科技有限公司"
65
+ 配置存于 `~/.tyc/config.json`(权限 600):
113
66
 
114
- # 董监高失信被执行(双参数)
115
- tyc executive personnel-dishonest "北京字节跳动科技有限公司" --humanName "张一鸣"
67
+ ```json
68
+ {
69
+ "url": "https://ai-mcp.tianyancha.com/mcp",
70
+ "headers": {
71
+ "Authorization": "YOUR_API_TOKEN"
72
+ }
73
+ }
74
+ ```
116
75
 
117
- # Markdown 友好输出
118
- tyc company registration-info "北京百度网讯科技有限公司" --md
76
+ ### 4. 开始查询
119
77
 
120
- # 缩进 JSON 调试
121
- tyc risk dishonest-info "..." --pretty --verbose
78
+ ```bash
79
+ tyc company registration-info "北京百度网讯科技有限公司"
80
+ tyc risk dishonest-info "..." --md
81
+ tyc executive personnel-dishonest "..." --humanName "张三"
122
82
  ```
123
83
 
124
84
  ---
125
85
 
126
86
  ## 📖 命令手册
127
87
 
128
- ### 基础管理命令
88
+ ### 基础命令
129
89
 
130
90
  | 命令 | 说明 |
131
91
  |------|------|
132
- | `tyc init --authorization <token>` | 配置 Authorization,保存到 `~/.tyc/config.json` |
92
+ | `tyc init --authorization <token>` | 写入 `headers.Authorization`;保存后会立即向 MCP 发一次 `initialize` 校验连通性 |
93
+ | `tyc init --url <url>` | 设置 MCP endpoint |
94
+ | `tyc init --header K=V` | 注入自定义 header(可重复);值留空则删除该 key |
95
+ | `tyc init --no-verify` | 仅写配置,跳过连通性校验(离线配置场景) |
96
+ | `tyc init --clear-session` | 清除本地 session 缓存 |
133
97
  | `tyc --help` | 显示 6 个分类总览 |
134
98
  | `tyc <category> --help` | 显示某分类下全部命令 |
135
99
  | `tyc <category> <method> --help` | 显示具体命令的入参说明 |
136
- | `tyc --version` | 显示当前版本号 |
137
100
 
138
101
  ### 全局选项
139
102
 
140
103
  | 选项 | 说明 |
141
104
  |------|------|
142
- | `--pretty` | 缩进 2 空格的 JSON 输出(调试友好) |
143
- | `--md` | Markdown 表格化输出(适合人类阅读 / Agent 上屏) |
144
- | `--verbose` | 输出 HTTP 请求详情到 stderr |
105
+ | `--pretty` | 缩进 2 空格 JSON 输出(调试友好) |
106
+ | `--md` | Markdown 表格化输出(人类阅读 / Agent 上屏) |
107
+ | `--verbose` | 打印 MCP 请求详情到 stderr(URL / Mcp-Session-Id / 掩码 Authorization / 响应原文) |
145
108
 
146
- > 三个输出模式互斥优先级:`--md` > `--pretty` > 默认紧凑 JSON。
109
+ 三种输出互斥优先级:`--md > --pretty > 默认`。
147
110
 
148
- ### 数据查询调用格式
111
+ ### 环境变量覆盖
149
112
 
150
- ```
151
- tyc <分类> <方法-kebab> <位置参数> [--可选参数 值]
152
- ```
153
-
154
- - **分类**:6 个 Go 包名(`company` / `risk` / `intellectual_property` / `operation` / `history` / `executive`)
155
- - **方法**:自动从 tool name 推导(`get_company_registration_info` → `registration-info`;`get_personnel_dishonest` → `personnel-dishonest`,自动剥离分类前缀)
156
- - **位置参数**:第一个必填参数(通常是 `searchKey`)
157
- - **可选参数**:如 `--humanName`、`--searchKey2`、`--id`、`--applicant` 等
113
+ | 变量 | 作用 |
114
+ |------|------|
115
+ | `TYC_MCP_ENDPOINT` | 临时覆盖 `url`(优先级高于 config.json) |
116
+ | `TYC_AUTHORIZATION` | config 中缺省 Authorization 时兜底 |
158
117
 
159
118
  ---
160
119
 
161
- ## 📚 查询指令手册(节选典型场景)
120
+ ## 📚 查询指令手册
162
121
 
163
- ### 1️⃣ company(企业基础信息,52 个工具)
122
+ ### 企业基础信息(company,52)
164
123
 
165
124
  ```bash
166
- # 工商登记基础(多源聚合:ic/baseinfoV2 + ic/companyType)
167
- tyc company registration-info "北京百度网讯科技有限公司"
168
-
169
- # 实际控制人
170
- tyc company actual-controller "..."
171
-
172
- # 受益所有人 (UBO)
173
- tyc company beneficial-owners "..."
174
-
175
- # 主要人员
176
- tyc company key-personnel "..."
177
-
178
- # 企业年报
179
- tyc company annual-reports "..."
180
-
181
- # 财务数据(5 源聚合:上市优先 stock/* + 非上市回退 ic/annualreport)
182
- tyc company financial-data "..."
183
-
184
- # 上市信息
185
- tyc company listing-info "..."
186
-
187
- # 三要素核验
188
- tyc company accuracy "..." --legalPersonName "梁志祥"
125
+ tyc company registration-info "北京百度网讯科技有限公司" # 工商登记
126
+ tyc company actual-controller "..." # 实际控制人
127
+ tyc company beneficial-owners "..." # UBO
128
+ tyc company key-personnel "..." # 主要人员
129
+ tyc company annual-reports "..." # 企业年报
130
+ tyc company financial-data "..." # 财务数据(上市/非上市自动回退)
131
+ tyc company accuracy "..." --legalPersonName "梁志祥" # 三要素核验
132
+ tyc company equity-tree "..." # 股权图谱
133
+ tyc company relation-path "A" --searchKey2 "B" # 双企业最短路径
134
+ tyc company group-info "..." # 集团信息(serial 串行执行)
189
135
  ```
190
136
 
191
- ### 2️⃣ risk(风险合规,36 个工具)
137
+ ### 风险合规(risk,36)
192
138
 
193
139
  ```bash
194
- # 失信被执行
195
- tyc risk dishonest-info "..."
196
-
197
- # 被执行人
198
- tyc risk judgment-debtor-info "..."
199
-
200
- # 限制高消费
201
- tyc risk high-consumption-restriction "..."
202
-
203
- # 行政处罚
204
- tyc risk administrative-penalty "..."
205
-
206
- # 破产重整
207
- tyc risk bankruptcy-reorganization "..."
208
-
209
- # 司法拍卖 / 裁判文书 / 立案信息 / ...
210
-
211
- # 综合风险总览
212
- tyc risk overview "..."
213
-
214
- # 风险详情(基于 ID)
215
- tyc risk detail "RISK_ID"
216
-
217
- # 司法解析
218
- tyc risk judicial-case "..."
140
+ tyc risk dishonest-info "..." # 失信被执行
141
+ tyc risk judgment-debtor-info "..." # 被执行人
142
+ tyc risk high-consumption-restriction "..." # 限高
143
+ tyc risk administrative-penalty "..." # 行政处罚
144
+ tyc risk bankruptcy-reorganization "..." # 破产重整
145
+ tyc risk overview "..." # 综合风险总览
146
+ tyc risk judicial-case "..." # 司法解析
219
147
  ```
220
148
 
221
- ### 3️⃣ intellectual_property(知识产权,14 个工具)
149
+ ### 知识产权(intellectual_property,14)
222
150
 
223
151
  ```bash
224
- # 专利 / 商标 / 软著 / 作品著作权
225
152
  tyc intellectual_property patent-info "..."
226
153
  tyc intellectual_property trademark-info "..."
227
154
  tyc intellectual_property software-copyright-info "..."
228
- tyc intellectual_property copyright-work-info "..."
229
-
230
- # 网站备案 + 公众号
231
- tyc intellectual_property internet-service-info "..."
232
-
233
- # 创新力评分
234
- tyc intellectual_property ipr-score "..."
235
-
236
- # 专利搜索(搜索类)
155
+ tyc intellectual_property ipr-score "..." # 创新力评分
237
156
  tyc intellectual_property search-patents "新能源" --applicant "宁德时代"
238
-
239
- # 商标详情(基于注册号)
240
- tyc intellectual_property trademark-detail "TM12345"
157
+ tyc intellectual_property construction-qualifications "..." # 建筑资质
241
158
  ```
242
159
 
243
- ### 4️⃣ operation(经营与公示,32 个工具)
160
+ ### 经营与公示(operation,32)
244
161
 
245
162
  ```bash
246
- # 招投标
247
163
  tyc operation bidding-info "..."
248
-
249
- # 资质证书 / 行政许可 / 电信许可
250
164
  tyc operation qualifications "..."
251
165
  tyc operation administrative-license "..."
252
- tyc operation telecom-license "..."
253
-
254
- # 信用评价(纳税信用 + 债券评级)
255
- tyc operation credit-evaluation "..."
256
-
257
- # 融资记录
258
- tyc operation financing-records "..."
259
-
260
- # 新闻舆情
261
166
  tyc operation news-sentiment "..."
262
-
263
- # 招聘动态
264
167
  tyc operation recruitment-info "..."
265
-
266
- # 抽查检查 / 双随机抽查
267
- tyc operation spot-check-info "..."
268
- tyc operation random-check "..."
168
+ tyc operation invest-agency-profile "红杉资本" # 投资机构
169
+ tyc operation private-fund-profile "..." # 私募基金
269
170
  ```
270
171
 
271
- ### 5️⃣ history(历史信息,18 个工具)
172
+ ### 历史信息(history,18)
272
173
 
273
174
  ```bash
274
- # 历史工商 / 历史股东 / 历史投资
275
175
  tyc history historical-registration "..."
276
176
  tyc history historical-shareholders "..."
277
- tyc history historical-investments "..."
278
-
279
- # 历史司法
280
177
  tyc history historical-judicial-docs "..."
281
- tyc history historical-dishonest "..."
282
- tyc history historical-judgment-debtor "..."
283
-
284
- # 历史信息总览
285
178
  tyc history historical-overview "..."
286
179
  ```
287
180
 
288
- ### 6️⃣ executive(董监高 · 双参数实体强锚定,15 个工具)
181
+ ### 董监高(executive,15) · 双参数实体强锚定
289
182
 
290
183
  ```bash
291
- # 董监高现状(11 个核心工具)
292
184
  tyc executive personnel-dishonest "..." --humanName "张三"
293
- tyc executive personnel-judgment-debtor "..." --humanName "张三"
294
- tyc executive personnel-high-consumption-ban "..." --humanName "张三"
295
- tyc executive personnel-controlled-companies "..." --humanName "张三"
296
- tyc executive personnel-related-companies "..." --humanName "张三"
297
-
298
- # 历史维度
299
- tyc executive personnel-historical-dishonest "..." --humanName "张三"
300
-
301
- # 人员画像深度(4 个 TYC 扩展工具)
302
185
  tyc executive person-profile "..." --humanName "张三"
303
186
  tyc executive person-partners "..." --humanName "张三"
304
187
  tyc executive person-risk-overview "..." --humanName "张三"
305
- tyc executive person-judicial-assistance "..." --humanName "张三"
306
188
  ```
307
189
 
308
- ### 7️⃣ company 子分类(33 个工具)
309
-
310
- ```bash
311
- # 股权与关系图谱(原 equity_relation 7 个,已并入 company)
312
- tyc company equity-tree "..."
313
- tyc company controlled-companies "..."
314
- tyc company parent-company "..."
315
- tyc company relation-graph "..."
316
- tyc company relation-path "企业 A" --searchKey2 "企业 B" # 双企业最短路径
317
- tyc company shareholder-change "..."
318
-
319
- # 集团信息(原 group 4 个,serial 串行执行)
320
- tyc company group-info "..."
321
- tyc company group-members "..."
322
- tyc company group-investors "..."
323
- tyc company group-shareholders "..."
324
-
325
- # 企业搜索(原 company_search 4 个)
326
- tyc company companies "百度" --industry "互联网" # 关键词 / 行业地区
327
- tyc company companies-by-tag "人工智能"
328
- tyc company companies-by-ranking "胡润榜"
329
-
330
- # 财务分析(原 financial_analysis 11 个,仅上市公司)
331
- tyc company income-statement "..."
332
- tyc company balance-sheet "..."
333
- tyc company cash-flow-statement "..."
334
- tyc company financial-summary "..."
335
- tyc company financial-main-indicators "..."
336
- tyc company share-structure "..."
337
- tyc company stock-shareholders "..."
338
- tyc company stock-executives "..."
339
- tyc company stock-prospectus "..."
340
- tyc company stock-violations "..."
341
- tyc company listed-companies "新能源"
342
-
343
- # 企业报告(原 enterprise_report 2 个)
344
- tyc company enterprise-report-basic "..."
345
- tyc company enterprise-report-professional "..."
346
-
347
- # 地理与园区(原 geography_park 5 个)
348
- tyc company park-info "中关村软件园"
349
- tyc company park-companies "中关村软件园"
350
- tyc company nearby-companies 116.391 --latitude 39.907 --radius 1000
351
- tyc company location "..." # 企业经纬度
352
- tyc company logo "..." # 企业 Logo
353
- ```
354
-
355
- ### 8️⃣ operation 子分类(合并自原 2 个分类,9 个工具)
356
-
357
- ```bash
358
- # 投资机构(原 investment_agency 5 个,searchKey = 投资机构名)
359
- tyc operation invest-agency-profile "红杉资本"
360
- tyc operation invest-agency-news "红杉资本"
361
- tyc operation invest-agency-public-investments "红杉资本"
362
- tyc operation invest-agency-funds "红杉资本"
363
- tyc operation invest-agency-events "红杉资本"
364
-
365
- # 私募基金(原 private_fund 4 个)
366
- tyc operation private-fund-profile "..."
367
- tyc operation private-fund-executives "..."
368
- tyc operation private-fund-products "..."
369
- tyc operation private-fund-related "..."
370
- ```
371
-
372
- ### 9️⃣ intellectual_property 子分类(合并自原 1 个分类,4 个工具)
373
-
374
- ```bash
375
- # 建筑资质(原 construction_qualification 4 个)
376
- tyc intellectual_property construction-qualifications "..."
377
- tyc intellectual_property construction-registered-personnel "..."
378
- tyc intellectual_property construction-projects "..."
379
- tyc intellectual_property construction-bad-conduct "..."
380
- ```
381
-
382
- > 完整 6 分类的命令清单可通过 `tyc <分类> --help` 实时查看,或浏览 [`api-registry.yaml`](api-registry.yaml)。
190
+ 完整命令清单:`tyc <category> --help`。
383
191
 
384
192
  ---
385
193
 
386
- ## ⚙️ 配置说明
387
-
388
- ### 配置文件路径
194
+ ## 🏗️ 架构
389
195
 
390
196
  ```
391
- ~/.tyc/config.json
197
+ ┌─────────────┐ ┌─────────────────────────────┐ ┌─────────────────┐
198
+ │ tyc-cli │ ──JSON──▶│ 天眼查 MCP Server │ ──HTTP──▶│ tyc OpenAPI │
199
+ │ (npm / TS) │ ◀───RPC──│ (ai-mcp.tianyancha.com/mcp) │ ◀───────│ │
200
+ └─────────────┘ └─────────────────────────────┘ └─────────────────┘
201
+ │ │
202
+ │ └─ 多源并发聚合 · 时间戳格式化 · _summary 注入 · 空结果归一化
203
+ │
204
+ └─ 仅命令树 · 参数透传 · Session 管理 · --md/--pretty 呈现
392
205
  ```
393
206
 
394
- ### 字段解析
207
+ **CLI 的职责**:
395
208
 
396
- | 字段 | 类型 | 说明 |
397
- |------|------|------|
398
- | `authorization` | string | 天眼查 OpenAPI Token,原样透传到下游 |
399
- | `baseUrl` | string(可选) | 自定义 tyc OpenAPI 域名,默认 `https://open.api.tianyancha.com` |
209
+ 1. 解析命令行(commander)
210
+ 2. 组装 `tools/call` JSON-RPC 请求,透传 `Authorization` header
211
+ 3. Session 管理(`initialize` + 24h 缓存 + 失效重建)
212
+ 4. 解析 MCP Streamable HTTP 响应(纯 JSON 或 SSE)
213
+ 5. 格式化输出(紧凑 JSON / `--pretty` / `--md`)
400
214
 
401
- ### 配置命令
215
+ **CLI 不做**:
402
216
 
403
- ```bash
404
- # 设置/更新 Authorization
405
- tyc init --authorization "YOUR_API_TOKEN"
406
-
407
- # 手动编辑(不推荐)
408
- vim ~/.tyc/config.json
409
- ```
410
-
411
- ### 安全性提示
412
-
413
- - 配置文件权限默认 600(用户只读写)
414
- - token 不入 git,`.gitignore` 已排除常见敏感路径
415
- - `--verbose` 模式下,token 在日志中以 `xxxx****xxxx` 形式打码
217
+ - 不解析 Authorization,不验签,不计费
218
+ - 不合并多源,不做时间戳格式化
219
+ - 不生成 `_summary` / `_empty` / `_warnings`(由 MCP Server 注入)
220
+ - 不缓存业务结果
416
221
 
417
222
  ---
418
223
 
419
- ## 🏗️ 目录结构
224
+ ## 📂 目录结构
420
225
 
421
226
  ```
422
227
  tyc-cli/
423
- ├── api-registry.yaml # SSOT:167 个工具的注册元数据(构建时输入)
424
- ├── package.json # bin: tyc / entry: dist/index.js
228
+ ├── package.json # bin: tyc · entry: dist/index.js
425
229
  ├── tsconfig.json
426
- ├── .eslintrc.cjs
427
230
  ├── LICENSE # MIT
428
231
  ├── README.md # 本文件
429
232
  ├── CHANGELOG.md
430
233
  │
431
- ├── scripts/
432
- │ └── build-registry.ts # 构建时:YAML → src/generated/t1_1-registry.json
433
- │
434
234
  └── src/
435
235
  ├── index.ts # CLI 入口(commander 注册)
436
- ├── types.ts # Tool / Param / Source / Registry 类型
437
- ├── client.ts # tyc OpenAPI HTTP 客户端
438
- ├── config.ts # ~/.tyc/config.json 读写
439
- ├── registry.ts # 加载 t1_1-registry.json
440
- ├── aggregator.ts # 多源并发/串行调度 + condition 求值 + params_template 渲染
441
- ├── transformer.ts # 多源合并 + 时间戳格式化 + 元数据注入(_summary/_empty/_warnings)
442
- ├── utils/
443
- │ └── jsonToMarkdown.ts # JSON → Markdown 表格化(--md 选项使用)
236
+ ├── types.ts # Catalog / Session / MCP 类型
237
+ ├── config.ts # ~/.tyc/config.json 读写 · 环境变量兜底
238
+ ├── session.ts # ~/.tyc/session.json 读写 · 24h TTL
239
+ ├── mcpClient.ts # MCP JSON-RPC client · SSE 解析 · 失效重建
240
+ ├── registry.ts # 读取打包内 catalog.json(命令树元数据)
241
+ ├── catalog.json # 命令元数据:name / group / cliMethod / params
444
242
  ├── commands/
445
- │ ├── init.ts # tyc init 命令
446
- │ └── category.ts # 动态注册 6 分类 × N 方法子命令
447
- └── generated/
448
- └── t1_1-registry.json # 构建产物(gitignored,npm pack 不含)
243
+ │ ├── init.ts # tyc init
244
+ │ └── category.ts # 动态注册 6 分类 × N 方法
245
+ └── utils/
246
+ └── jsonToMarkdown.ts # --md 选项的 Markdown 渲染
449
247
  ```
450
248
 
451
249
  ---
452
250
 
453
- ## 🔁 与 MCP Server 的关系
251
+ ## ⚙️ Session 管理
454
252
 
455
- `tyc-cli` 配套的 MCP Server(基于 Go 实现的 [apimcp](https://github.com/tianyancha-tech/apimcp))暴露**完全相同的 167 个工具**到 AI Agent。两者:
253
+ | 场景 | 行为 |
254
+ |------|------|
255
+ | 首次调用 | `initialize` → 读 `Mcp-Session-Id` header → 存 `~/.tyc/session.json` |
256
+ | 24h 内复用 | 直接用缓存 `sessionId`,跳过 `initialize` |
257
+ | 缓存过期(>24h) | 自动 re-initialize,用户无感 |
258
+ | 服务端主动失效(404/410/"session not found") | 删缓存 → 重建 → 重试 1 次 |
259
+ | `tyc init` 变更 url / Authorization | 配置写入后自动清掉旧 session |
260
+
261
+ `~/.tyc/session.json` 示例:
262
+
263
+ ```json
264
+ {
265
+ "url": "https://ai-mcp.tianyancha.com/mcp",
266
+ "sessionId": "mcp-session-xxx",
267
+ "initializedAt": 1777272039739,
268
+ "protocolVersion": "2024-11-05"
269
+ }
270
+ ```
456
271
 
457
- | 维度 | tyc-cli | MCP Server (apimcp) |
458
- |-----|--------------|---------------------|
459
- | 协议 | 命令行 / npm 包 | JSON-RPC 2.0 over Streamable HTTP |
460
- | 实现 | TypeScript | Go |
461
- | SSOT | `api-registry.yaml` | 共享同一份 yaml |
462
- | 输出结构 | 完全一致(tyc 英文 key + 时间戳格式化 + 项目元数据) | 同 |
463
- | 用途 | 命令行直查 / 脚本批处理 / Agent 工具调用 | Agent MCP 协议接入 / Web 中间层 |
272
+ ---
464
273
 
465
- CLI 不经过 MCP Server,直接以 HTTP 客户端身份调 tyc OpenAPI;TypeScript 端用 `aggregator.ts` + `transformer.ts` 重现了 Go 端的多源合并与元数据注入逻辑,**保证两端输出 1:1 等价**。
274
+ ## 📋 错误码
466
275
 
467
- ---
276
+ | 退出码 | 含义 | 处理建议 |
277
+ |-------|------|---------|
278
+ | 0 | 成功 | — |
279
+ | 1 | 请求失败 / 配置缺失 / 业务错误 | 查 stderr;常见:未 `tyc init`、MCP 不可达、tyc 无权限 |
468
280
 
469
- ## 📐 SSOT 同步
281
+ 下游 tyc OpenAPI 错误码(由 MCP Server 归一化后呈现):
470
282
 
471
- 本仓库的 `api-registry.yaml` 是 167 工具的 SSOT。`scripts/build-registry.ts` 在 `npm run build` 前自动读取此文件,生成 `src/generated/t1_1-registry.json` 作为 CLI 运行时数据源。
283
+ | error_code | CLI 表现 |
284
+ |-----------|---------|
285
+ | 0 | 正常输出 |
286
+ | 300000(经查无结果) | 成功退出 + `{items: [], total: 0, _empty: true}` + `_summary` |
287
+ | 300005(无权限) | 透传错误 + exit 1 |
288
+ | 其他 | 透传错误 + exit 1 |
472
289
 
473
- 如果你 fork 了上游 [apimcp](https://github.com/tianyancha-tech/apimcp) 大仓库做二次开发,需保持两边 yaml 同步:
290
+ ---
474
291
 
475
- ```bash
476
- # 从 monorepo 同步(cli/t1_1 子目录视角)
477
- cp ../../conf/api-registry.yaml ./api-registry.yaml
478
- npm run build
479
- ```
292
+ ## 🔁 与 MCP Server 的关系
293
+
294
+ | 维度 | `tyc-cli` | 天眼查 MCP Server |
295
+ |-----|---------|-------------------|
296
+ | 协议 | MCP client(JSON-RPC over Streamable HTTP) | MCP server |
297
+ | 实现 | TypeScript | Go |
298
+ | 职责 | 命令树 · 参数透传 · 格式化输出 | 多源聚合 · 时间戳格式化 · 元数据注入 · Authorization 透传至 OpenAPI |
299
+ | 运维 | 用户本地安装 | 官方托管 `ai-mcp.tianyancha.com`,或用户自建 |
300
+
301
+ CLI 和 MCP Server 共享同一 167 工具清单;工具元数据从打包内的 `catalog.json`
302
+ 读取,保证命令树冷启动零网络开销。
480
303
 
481
304
  ---
482
305
 
483
- ## 📋 错误码
306
+ ## 🧪 开发自测
484
307
 
485
- | 退出码 | 含义 | 处理建议 |
486
- |-------|------|---------|
487
- | 0 | 成功 | — |
488
- | 1 | 请求失败 | 查 stderr 详情,可能是网络/熔断/参数 |
489
- | 1 | 配置缺失 | 运行 `tyc init --authorization ...` |
308
+ 如果你 fork 了本项目做二次开发,可以跑打包内的测试脚本验证:
490
309
 
491
- 下游 tyc OpenAPI 错误码:
310
+ ```bash
311
+ # 默认连本地自建 MCP(需要你自己起 apimcp)
312
+ bash test/t1_1/cli/run_t1_1.sh
313
+
314
+ # 打线上官方 MCP(只需要一个有效 Authorization)
315
+ bash test/t1_1/cli/run_t1_1.sh -o
316
+
317
+ # 线上 + 详细日志
318
+ bash test/t1_1/cli/run_t1_1.sh -o -v
492
319
 
493
- | error_code | 含义 | CLI 表现 |
494
- |-----------|------|---------|
495
- | 0 | 成功 | 正常输出 |
496
- | 300000 | 经查无结果 | 自动归一为 `{items: [], total: 0, _empty: true}` + `_summary` 友好文案 |
497
- | 300005 | 无权限(token 不含此接口) | exit 1 + stderr 详情 |
498
- | 其他 | 各类业务/系统错误 | exit 1 + 透传错误信息 |
320
+ # 环境变量覆盖
321
+ MCP_URL=https://my-mcp.example.com/mcp AUTH_TOKEN=xxx bash test/t1_1/cli/run_t1_1.sh
322
+ ```
323
+
324
+ `-o` = `--online`,切到 `https://ai-mcp.tianyancha.com/mcp`;`-v` = `--verbose`。
325
+ 单分类脚本加 `-p` 可独立触发 preflight:`bash test/t1_1/cli/test_company.sh -p -o`。
499
326
 
500
327
  ---
501
328
 
502
329
  ## 🤝 贡献
503
330
 
504
- 欢迎 Issue / PR!主要协作流程:
505
-
506
- 1. Fork 本仓库
507
- 2. 编辑 `api-registry.yaml` 新增工具条目(参考已有条目格式)
508
- 3. `npm run build` 验证生成的命令树
509
- 4. `npm run lint` 通过
510
- 5. 提交 PR
331
+ 欢迎 Issue / PR!CLI 代码改动主要集中在 `src/`,工具清单由 MCP Server 侧 SSOT
332
+ 同步生成(`catalog.json`)。如果你发现命令树与服务端实际工具集不一致,提 Issue 即可。
511
333
 
512
334
  ---
513
335
 
@@ -515,4 +337,4 @@ npm run build
515
337
 
516
338
  本项目采用 [MIT License](LICENSE)。
517
339
 
518
- 数据来源:天眼查 OpenAPI(用户需自行获取并合规使用 token)。本工具不存储、不转发、不解析用户的查询数据,所有调用直连天眼查接口。
340
+ 数据来源:天眼查 OpenAPI(用户需自行获取并合规使用 token)。本工具不存储、不转发、不解析用户的查询数据,所有调用经天眼查 MCP Server 转发。