national-stats-mcp 1.0.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/License +201 -0
- package/README.md +102 -0
- package/dbcode.json +42 -0
- package/dist/api-client.d.ts +121 -0
- package/dist/api-client.js +223 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +266 -0
- package/dist/types.d.ts +25 -0
- package/dist/types.js +3 -0
- package/package.json +35 -0
- package/wbcode.json +14 -0
- package/zb.json +226 -0
package/License
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
# 国家统计局MCP/Agent数据获取工具
|
|
2
|
+
|
|
3
|
+
## 项目概述
|
|
4
|
+
本项目是一个基于MCP(Model Context Protocol)协议的国家统计局数据获取服务,为AI Agent提供直接访问国家统计局API的功能。项目支持获取各种统计数据,包含关键词搜索、分类浏览、时间维度查询等功能,遵循国家统计局官方API接口最佳实践。
|
|
5
|
+
|
|
6
|
+
主要特点:
|
|
7
|
+
- ✅ 关键词搜索:直接搜索获取指标代码及最新值(推荐用于快速查询)
|
|
8
|
+
- ✅ 树形分类:可浏览和递归获取所有分类/指标体系
|
|
9
|
+
- ✅ 时间范围:可查询各数据库对应可用时间维度
|
|
10
|
+
- ✅ 标准API:遵循国家统计局接口文档的全部规范
|
|
11
|
+
- ✅ 容错机制:内置重试逻辑、请求延迟、数据验证等功能
|
|
12
|
+
|
|
13
|
+
## 环境要求
|
|
14
|
+
- Node.js 18+
|
|
15
|
+
- npm or yarn
|
|
16
|
+
|
|
17
|
+
## 安装指南
|
|
18
|
+
安装所需依赖,请运行以下命令:
|
|
19
|
+
```bash
|
|
20
|
+
npm install
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## 服务启动
|
|
24
|
+
启动MCP服务器:
|
|
25
|
+
```bash
|
|
26
|
+
# 构建项目
|
|
27
|
+
npm run build
|
|
28
|
+
|
|
29
|
+
# 启动服务器(STDIO模式 - 适用于Claude Desktop等)
|
|
30
|
+
npm start
|
|
31
|
+
|
|
32
|
+
# 或启动HTTP服务器(端口模式)
|
|
33
|
+
node dist/index.js --port 8080
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## 可用工具
|
|
37
|
+
|
|
38
|
+
### search_statistics(推荐)
|
|
39
|
+
通过关键词搜索国家统计局指标和数据。可以快速查找指标及其最新值。
|
|
40
|
+
- 参数:`keyword`(必需)- 搜索关键词,如 "GDP"、"人均可支配收入" 等
|
|
41
|
+
- 参数:`db`(可选)- 数据库筛选,空=全部,可选"年度数据"或"季度数据"
|
|
42
|
+
- 参数:`page`(可选)- 页码,从0开始
|
|
43
|
+
|
|
44
|
+
### get_statistics_categories
|
|
45
|
+
获取统计指标分类树,返回国家统计局分类体系(第一层)。
|
|
46
|
+
- 参数:`dbcode`(必需)- 数据库代码,`wdcode`(可选)- 维度代码
|
|
47
|
+
|
|
48
|
+
### get_statistics_leaf_categories
|
|
49
|
+
递归获取特定数据库的所有叶子指标节点(分类下全部可查询指标)。
|
|
50
|
+
- 参数:`dbcode`(必需)- 数据库代码
|
|
51
|
+
|
|
52
|
+
### get_statistics_time_options
|
|
53
|
+
获取指定数据库可选的时间范围维度。
|
|
54
|
+
- 参数:`dbcode`(必需)- 数据库代码
|
|
55
|
+
|
|
56
|
+
### get_statistics_data
|
|
57
|
+
获取国家统计局特定指标的数据。
|
|
58
|
+
- 参数:`zb`(必需)- 指标代码,`dbcode`(必需)- 数据库代码,`sj`(可选)- 时间范围
|
|
59
|
+
|
|
60
|
+
### batch_get_statistics
|
|
61
|
+
批量获取多个统计指标的数据。
|
|
62
|
+
- 参数:`queries`(必需)- 查询参数数组
|
|
63
|
+
|
|
64
|
+
## 模块介绍
|
|
65
|
+
- `src/index.ts`: MCP服务器主入口,实现MCP协议规范
|
|
66
|
+
- `src/api-client.ts`: 国家统计局API交互模块,提供完整的数据获取功能,包括搜索、分类、数据查询等
|
|
67
|
+
|
|
68
|
+
## 遵循国家统计局API标准
|
|
69
|
+
项目严格实现api_introduce.md中的接口规范,包含:
|
|
70
|
+
- 搜索接口 `/search.htm` 用于关键词查找和快速获取最新值
|
|
71
|
+
- 分类接口 `/easyquery.htm` 用于遍历指标分类树
|
|
72
|
+
- 数据接口 `/easyquery.htm` 用于查询历史时间序列
|
|
73
|
+
- 通过 `k1` 时间戳防缓存参数
|
|
74
|
+
- URL编码处理 `wds` 和 `dfwds` 参数
|
|
75
|
+
- 数据验证与错误处理最佳实践
|
|
76
|
+
|
|
77
|
+
## 数据编码对应
|
|
78
|
+
- `dbcode.json`: 存储数据库代码及其相应的名称
|
|
79
|
+
- `zb.json`: 包含用于查询不同类型国家数据的类别和子类别代码
|
|
80
|
+
- `wbcode.json`: 维度代码定义
|
|
81
|
+
|
|
82
|
+
## 为AI Agent的使用
|
|
83
|
+
本项目专门设计为MCP服务器,可以直接集成到AI Agent系统中,提供国家统计局数据查询能力。Agent可根据使用场景选择最合适的工具:
|
|
84
|
+
|
|
85
|
+
- **快速查询**:使用 `search_statistics` 直接通过关键词获取指标和最新值
|
|
86
|
+
- **分类浏览**:使用 `get_statistics_categories` 或 `get_statistics_leaf_categories` 查看指标体系
|
|
87
|
+
- **深度查询**:使用 `get_statistics_time_options` 和 `get_statistics_data` 获取历史序列数据
|
|
88
|
+
|
|
89
|
+
## 开发
|
|
90
|
+
进行开发时可使用:
|
|
91
|
+
```bash
|
|
92
|
+
npm run dev
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
## 贡献
|
|
96
|
+
欢迎对本代码进行修改。请确保遵循项目的编码标准。
|
|
97
|
+
|
|
98
|
+
## 开源协议
|
|
99
|
+
[MIT License](./License)
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
|
package/dbcode.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
[
|
|
2
|
+
{
|
|
3
|
+
"dbcode": "hgyd",
|
|
4
|
+
"name": "月度数据"
|
|
5
|
+
},
|
|
6
|
+
{
|
|
7
|
+
"dbcode": "hgjd",
|
|
8
|
+
"name": "季度数据"
|
|
9
|
+
},
|
|
10
|
+
{
|
|
11
|
+
"dbcode": "hgnd",
|
|
12
|
+
"name": "年度数据"
|
|
13
|
+
},
|
|
14
|
+
{
|
|
15
|
+
"dbcode": "fsyd",
|
|
16
|
+
"name": "分省月度数据"
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
"dbcode": "fsjd",
|
|
20
|
+
"name": "分省季度数据"
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
"dbcode": "fsnd",
|
|
24
|
+
"name": "分省年度数据"
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
"dbcode": "csyd",
|
|
28
|
+
"name": "城市月度数据"
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
"dbcode": "csnd",
|
|
32
|
+
"name": "城市年度数据"
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
"dbcode": "gatyd",
|
|
36
|
+
"name": "港澳台月度数据"
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
"dbcode": "gatnd",
|
|
40
|
+
"name": "港澳台年度数据"
|
|
41
|
+
}
|
|
42
|
+
]
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
export interface StatsApiResponse {
|
|
2
|
+
returncode: number;
|
|
3
|
+
returndata: {
|
|
4
|
+
hasdatacount: number;
|
|
5
|
+
datanodes: Array<{
|
|
6
|
+
code: string;
|
|
7
|
+
data: {
|
|
8
|
+
data: number | string;
|
|
9
|
+
dotcount: number;
|
|
10
|
+
hasdata: boolean;
|
|
11
|
+
strdata: string;
|
|
12
|
+
};
|
|
13
|
+
wds: Array<{
|
|
14
|
+
valuecode: string;
|
|
15
|
+
wdcode: string;
|
|
16
|
+
}>;
|
|
17
|
+
}>;
|
|
18
|
+
wdnodes: Array<{
|
|
19
|
+
wdcode: string;
|
|
20
|
+
wdname: string;
|
|
21
|
+
nodes: Array<{
|
|
22
|
+
code: string;
|
|
23
|
+
name: string;
|
|
24
|
+
unit: string;
|
|
25
|
+
dotcount?: number;
|
|
26
|
+
}>;
|
|
27
|
+
}>;
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
export interface StatsSearchResult {
|
|
31
|
+
pagecount: number;
|
|
32
|
+
pagecurrent: number;
|
|
33
|
+
result: Array<{
|
|
34
|
+
data: string;
|
|
35
|
+
db: string;
|
|
36
|
+
reg: string;
|
|
37
|
+
sj: string;
|
|
38
|
+
zb: string;
|
|
39
|
+
report: string;
|
|
40
|
+
}>;
|
|
41
|
+
}
|
|
42
|
+
export interface SearchResponse {
|
|
43
|
+
name: string;
|
|
44
|
+
value: string;
|
|
45
|
+
time: string;
|
|
46
|
+
db: string;
|
|
47
|
+
zbCode: string;
|
|
48
|
+
cnCode: string;
|
|
49
|
+
dbcode: string;
|
|
50
|
+
sjCode: string;
|
|
51
|
+
}
|
|
52
|
+
export type CategoryResponse = Array<{
|
|
53
|
+
id: string;
|
|
54
|
+
pid: string | number;
|
|
55
|
+
name: string;
|
|
56
|
+
isParent: boolean;
|
|
57
|
+
dbcode: string;
|
|
58
|
+
wdcode: string;
|
|
59
|
+
}>;
|
|
60
|
+
export interface TimeDimensionResponse {
|
|
61
|
+
returncode: number;
|
|
62
|
+
returndata: Array<{
|
|
63
|
+
wdcode: string;
|
|
64
|
+
wdname: string;
|
|
65
|
+
issj: boolean;
|
|
66
|
+
selcode: string;
|
|
67
|
+
nodes: Array<{
|
|
68
|
+
code: string;
|
|
69
|
+
name: string;
|
|
70
|
+
}>;
|
|
71
|
+
}>;
|
|
72
|
+
}
|
|
73
|
+
export declare class StatsApiClient {
|
|
74
|
+
private baseUrl;
|
|
75
|
+
private cnMap;
|
|
76
|
+
/**
|
|
77
|
+
* 搜索指标
|
|
78
|
+
*/
|
|
79
|
+
search(keyword: string, db?: string, page?: number): Promise<StatsSearchResult>;
|
|
80
|
+
/**
|
|
81
|
+
* 解析搜索结果
|
|
82
|
+
*/
|
|
83
|
+
parseSearchResult(result: StatsSearchResult): SearchResponse[];
|
|
84
|
+
/**
|
|
85
|
+
* 获取指标分类树
|
|
86
|
+
*/
|
|
87
|
+
getCategories(dbcode: string, wdcode?: string): Promise<CategoryResponse>;
|
|
88
|
+
/**
|
|
89
|
+
* 递归获取指标分类树的所有叶子节点(包含子节点)
|
|
90
|
+
*/
|
|
91
|
+
getLeafCategories(dbcode: string): Promise<CategoryResponse>;
|
|
92
|
+
/**
|
|
93
|
+
* 获取特定ID的分类节点
|
|
94
|
+
*/
|
|
95
|
+
private getTreeSpecificNode;
|
|
96
|
+
/**
|
|
97
|
+
* 获取时间维度信息
|
|
98
|
+
*/
|
|
99
|
+
getTimeDimensions(dbcode: string): Promise<TimeDimensionResponse>;
|
|
100
|
+
/**
|
|
101
|
+
* 获取统计数据
|
|
102
|
+
*/
|
|
103
|
+
getData(zb: string, dbcode: string, sj?: string): Promise<StatsApiResponse>;
|
|
104
|
+
/**
|
|
105
|
+
* 批量获取数据
|
|
106
|
+
*/
|
|
107
|
+
batchGet(queryParams: Array<{
|
|
108
|
+
zb: string;
|
|
109
|
+
dbcode: string;
|
|
110
|
+
sj: string;
|
|
111
|
+
}>): Promise<Array<{
|
|
112
|
+
query: {
|
|
113
|
+
zb: string;
|
|
114
|
+
dbcode: string;
|
|
115
|
+
sj: string;
|
|
116
|
+
};
|
|
117
|
+
result: StatsApiResponse | null;
|
|
118
|
+
error?: string;
|
|
119
|
+
}>>;
|
|
120
|
+
}
|
|
121
|
+
//# sourceMappingURL=api-client.d.ts.map
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.StatsApiClient = void 0;
|
|
7
|
+
const axios_1 = __importDefault(require("axios"));
|
|
8
|
+
// 国家统计局API客户端
|
|
9
|
+
class StatsApiClient {
|
|
10
|
+
baseUrl = 'https://data.stats.gov.cn';
|
|
11
|
+
cnMap = {
|
|
12
|
+
'B01': 'hgjd', // 季度
|
|
13
|
+
'C01': 'hgnd', // 年度
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* 搜索指标
|
|
17
|
+
*/
|
|
18
|
+
async search(keyword, db = '', page = 0) {
|
|
19
|
+
const params = new URLSearchParams({
|
|
20
|
+
s: encodeURIComponent(keyword),
|
|
21
|
+
m: 'searchdata',
|
|
22
|
+
db: encodeURIComponent(db),
|
|
23
|
+
p: page.toString(),
|
|
24
|
+
});
|
|
25
|
+
const searchUrl = `${this.baseUrl}/search.htm?${params}`;
|
|
26
|
+
try {
|
|
27
|
+
const response = await axios_1.default.get(searchUrl);
|
|
28
|
+
return response.data;
|
|
29
|
+
}
|
|
30
|
+
catch (error) {
|
|
31
|
+
if (axios_1.default.isAxiosError(error)) {
|
|
32
|
+
throw new Error(`API search request failed: ${error.response?.data || error.message}`);
|
|
33
|
+
}
|
|
34
|
+
throw new Error(`Request failed: ${error}`);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* 解析搜索结果
|
|
39
|
+
*/
|
|
40
|
+
parseSearchResult(result) {
|
|
41
|
+
return result.result.map(item => {
|
|
42
|
+
// 解析 report 字段获取参数
|
|
43
|
+
const reportParams = Object.fromEntries(item.report.split('&').map(p => {
|
|
44
|
+
const [key, value] = p.split('=');
|
|
45
|
+
return [key, value];
|
|
46
|
+
}));
|
|
47
|
+
// 确定数据库类型
|
|
48
|
+
const cnCode = reportParams.cn || 'B01'; // 默认为季度数据
|
|
49
|
+
const dbcode = this.cnMap[cnCode] || 'hgjd';
|
|
50
|
+
return {
|
|
51
|
+
name: item.zb,
|
|
52
|
+
value: item.data,
|
|
53
|
+
time: item.sj,
|
|
54
|
+
db: item.db,
|
|
55
|
+
zbCode: reportParams.zb || '',
|
|
56
|
+
cnCode,
|
|
57
|
+
dbcode,
|
|
58
|
+
sjCode: reportParams.sj || ''
|
|
59
|
+
};
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* 获取指标分类树
|
|
64
|
+
*/
|
|
65
|
+
async getCategories(dbcode, wdcode = 'zb') {
|
|
66
|
+
const params = {
|
|
67
|
+
m: 'getTree',
|
|
68
|
+
id: 'zb', // 从指标根节点开始
|
|
69
|
+
dbcode,
|
|
70
|
+
wdcode
|
|
71
|
+
};
|
|
72
|
+
try {
|
|
73
|
+
const response = await axios_1.default.post(`${this.baseUrl}/easyquery.htm`, new URLSearchParams(params));
|
|
74
|
+
return response.data;
|
|
75
|
+
}
|
|
76
|
+
catch (error) {
|
|
77
|
+
if (axios_1.default.isAxiosError(error)) {
|
|
78
|
+
throw new Error(`API category request failed: ${error.response?.data || error.message}`);
|
|
79
|
+
}
|
|
80
|
+
throw new Error(`Request failed: ${error}`);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* 递归获取指标分类树的所有叶子节点(包含子节点)
|
|
85
|
+
*/
|
|
86
|
+
async getLeafCategories(dbcode) {
|
|
87
|
+
let allLeaves = [];
|
|
88
|
+
const getRecursively = async (nodeId) => {
|
|
89
|
+
let retries = 0;
|
|
90
|
+
const maxRetries = 3;
|
|
91
|
+
while (retries < maxRetries) {
|
|
92
|
+
try {
|
|
93
|
+
// 添加延迟避免请求过于频繁
|
|
94
|
+
await new Promise(resolve => setTimeout(resolve, 200));
|
|
95
|
+
const categoryResp = await this.getTreeSpecificNode(nodeId, dbcode);
|
|
96
|
+
for (const node of categoryResp) {
|
|
97
|
+
if (!node.isParent) {
|
|
98
|
+
allLeaves.push(node);
|
|
99
|
+
}
|
|
100
|
+
else {
|
|
101
|
+
await getRecursively(node.id);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
catch (error) {
|
|
107
|
+
retries++;
|
|
108
|
+
console.log(`Category request failed for node ${nodeId}, attempt ${retries}/${maxRetries}.`);
|
|
109
|
+
if (retries >= maxRetries) {
|
|
110
|
+
throw error;
|
|
111
|
+
}
|
|
112
|
+
// 延迟重试
|
|
113
|
+
await new Promise(resolve => setTimeout(resolve, 1000 * retries));
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
await getRecursively('zb');
|
|
118
|
+
return allLeaves;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* 获取特定ID的分类节点
|
|
122
|
+
*/
|
|
123
|
+
async getTreeSpecificNode(id, dbcode, wdcode = 'zb') {
|
|
124
|
+
const params = {
|
|
125
|
+
m: 'getTree',
|
|
126
|
+
id,
|
|
127
|
+
dbcode,
|
|
128
|
+
wdcode
|
|
129
|
+
};
|
|
130
|
+
try {
|
|
131
|
+
const response = await axios_1.default.post(`${this.baseUrl}/easyquery.htm`, new URLSearchParams(params));
|
|
132
|
+
return response.data;
|
|
133
|
+
}
|
|
134
|
+
catch (error) {
|
|
135
|
+
if (axios_1.default.isAxiosError(error)) {
|
|
136
|
+
throw new Error(`API getTree request failed: ${error.response?.data || error.message}`);
|
|
137
|
+
}
|
|
138
|
+
throw new Error(`Request failed: ${error}`);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* 获取时间维度信息
|
|
143
|
+
*/
|
|
144
|
+
async getTimeDimensions(dbcode) {
|
|
145
|
+
const wds = '[]';
|
|
146
|
+
const params = {
|
|
147
|
+
m: 'getOtherWds',
|
|
148
|
+
dbcode,
|
|
149
|
+
rowcode: 'zb',
|
|
150
|
+
colcode: 'sj',
|
|
151
|
+
wds,
|
|
152
|
+
k1: Date.now(), // 添加时间戳防止缓存
|
|
153
|
+
h: 1
|
|
154
|
+
};
|
|
155
|
+
try {
|
|
156
|
+
const response = await axios_1.default.get(`${this.baseUrl}/easyquery.htm`, { params });
|
|
157
|
+
return response.data;
|
|
158
|
+
}
|
|
159
|
+
catch (error) {
|
|
160
|
+
if (axios_1.default.isAxiosError(error)) {
|
|
161
|
+
throw new Error(`API time dimension request failed: ${error.response?.data || error.message}`);
|
|
162
|
+
}
|
|
163
|
+
throw new Error(`Request failed: ${error}`);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* 获取统计数据
|
|
168
|
+
*/
|
|
169
|
+
async getData(zb, dbcode, sj = 'LAST30') {
|
|
170
|
+
const wds = JSON.stringify([{ "wdcode": "zb", "valuecode": zb }]);
|
|
171
|
+
const dfwds = JSON.stringify([{ "wdcode": "sj", "valuecode": sj }]);
|
|
172
|
+
const params = {
|
|
173
|
+
m: 'QueryData',
|
|
174
|
+
dbcode,
|
|
175
|
+
rowcode: 'zb',
|
|
176
|
+
colcode: 'sj',
|
|
177
|
+
// 编码数组形式的参数
|
|
178
|
+
wds: encodeURIComponent(wds),
|
|
179
|
+
dfwds: encodeURIComponent(dfwds),
|
|
180
|
+
k1: Date.now(), // 添加时间戳防止缓存
|
|
181
|
+
h: 1
|
|
182
|
+
};
|
|
183
|
+
const paramStr = Object.entries(params)
|
|
184
|
+
.map(([k, v]) => `${k}=${v}`)
|
|
185
|
+
.join('&');
|
|
186
|
+
try {
|
|
187
|
+
const response = await axios_1.default.get(`${this.baseUrl}/easyquery.htm?${paramStr}`);
|
|
188
|
+
return response.data;
|
|
189
|
+
}
|
|
190
|
+
catch (error) {
|
|
191
|
+
if (axios_1.default.isAxiosError(error)) {
|
|
192
|
+
throw new Error(`API query data request failed: ${error.response?.data || error.message}`);
|
|
193
|
+
}
|
|
194
|
+
throw new Error(`Request failed: ${error}`);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* 批量获取数据
|
|
199
|
+
*/
|
|
200
|
+
async batchGet(queryParams) {
|
|
201
|
+
const results = [];
|
|
202
|
+
for (const param of queryParams) {
|
|
203
|
+
try {
|
|
204
|
+
const result = await this.getData(param.zb, param.dbcode, param.sj);
|
|
205
|
+
results.push({
|
|
206
|
+
query: param,
|
|
207
|
+
result,
|
|
208
|
+
error: undefined
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
catch (error) {
|
|
212
|
+
results.push({
|
|
213
|
+
query: param,
|
|
214
|
+
result: null,
|
|
215
|
+
error: error.message
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
return results;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
exports.StatsApiClient = StatsApiClient;
|
|
223
|
+
//# sourceMappingURL=api-client.js.map
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.server = void 0;
|
|
5
|
+
const mcp_js_1 = require("@modelcontextprotocol/sdk/server/mcp.js");
|
|
6
|
+
const stdio_js_1 = require("@modelcontextprotocol/sdk/server/stdio.js");
|
|
7
|
+
const mcp_http_server_1 = require("mcp-http-server");
|
|
8
|
+
const api_client_1 = require("./api-client");
|
|
9
|
+
const zod_1 = require("zod");
|
|
10
|
+
const commander_1 = require("commander");
|
|
11
|
+
// 获取版本号,这里使用package.json中的版本
|
|
12
|
+
const VERSION = '1.0.0';
|
|
13
|
+
// 创建API客户端实例
|
|
14
|
+
const apiClient = new api_client_1.StatsApiClient();
|
|
15
|
+
// 创建MCP服务器
|
|
16
|
+
exports.server = new mcp_js_1.McpServer({
|
|
17
|
+
name: 'national-stats-mcp',
|
|
18
|
+
version: VERSION,
|
|
19
|
+
description: '国家统计局数据查询API'
|
|
20
|
+
});
|
|
21
|
+
// 设置服务器instructions
|
|
22
|
+
exports.server.instructions = `
|
|
23
|
+
该服务主要用于帮助用户查询国家统计局的统计数据。
|
|
24
|
+
|
|
25
|
+
主要工具包括:
|
|
26
|
+
- search_statistics: 通过关键词搜索国家统计局数据(推荐用于快速查询)
|
|
27
|
+
- get_statistics_categories: 获取统计指标分类
|
|
28
|
+
- get_statistics_leaf_categories: 获取所有叶子指标(递归获取)
|
|
29
|
+
- get_statistics_time_options: 获取特定数据库可选的时间范围
|
|
30
|
+
- get_statistics_data: 获取特定统计指标的数据
|
|
31
|
+
- batch_get_statistics: 批量获取统计数据
|
|
32
|
+
|
|
33
|
+
使用说明:
|
|
34
|
+
- 对于快速查询,直接使用search_statistics根据关键词查找数据和指标code
|
|
35
|
+
- 如需浏览分类体系,调用get_statistics_categories获取分类树
|
|
36
|
+
- 要获取特定分类下的所有叶子指标,使用get_statistics_leaf_categories
|
|
37
|
+
- 查看可用时间范围调用get_statistics_time_options
|
|
38
|
+
- 根据指标代码查询具体数据使用get_statistics_data
|
|
39
|
+
|
|
40
|
+
配置选项:
|
|
41
|
+
- dbcode: 数据库代码,hgnd (年度数据)、hgjd (季度数据) 等
|
|
42
|
+
- zb: 指标代码,通过分类或搜索获取
|
|
43
|
+
- sj: 时间范围,如 LAST6 (最近6个季度)、2025 (指定年份) 等
|
|
44
|
+
`;
|
|
45
|
+
// 注册搜索统计指标的工具(最推荐使用)
|
|
46
|
+
exports.server.tool('search_statistics', '通过关键词搜索国家统计局指标和数据。可以快速查找指标及其最新值。', {
|
|
47
|
+
keyword: zod_1.z.string().describe('搜索关键词,如 "GDP"、"人均可支配收入" 等'),
|
|
48
|
+
db: zod_1.z.string().optional().default('').describe('数据库筛选,空=全部,可选 "年度数据" 或 "季度数据"'),
|
|
49
|
+
page: zod_1.z.number().optional().default(0).describe('页码,从0开始'),
|
|
50
|
+
}, async (args) => {
|
|
51
|
+
const { keyword, db = '', page = 0 } = args;
|
|
52
|
+
try {
|
|
53
|
+
// 执行搜索
|
|
54
|
+
const searchResult = await apiClient.search(keyword, db, page);
|
|
55
|
+
// 解析搜索结果
|
|
56
|
+
const parsedResults = apiClient.parseSearchResult(searchResult);
|
|
57
|
+
return {
|
|
58
|
+
content: [
|
|
59
|
+
{
|
|
60
|
+
type: 'text',
|
|
61
|
+
text: JSON.stringify({
|
|
62
|
+
pagecount: searchResult.pagecount,
|
|
63
|
+
pagecurrent: searchResult.pagecurrent,
|
|
64
|
+
results: parsedResults
|
|
65
|
+
}, null, 2)
|
|
66
|
+
}
|
|
67
|
+
],
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
catch (error) {
|
|
71
|
+
return {
|
|
72
|
+
content: [
|
|
73
|
+
{
|
|
74
|
+
type: 'text',
|
|
75
|
+
text: `Error: ${error.message}`,
|
|
76
|
+
},
|
|
77
|
+
],
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
// 注册获取统计分类的工具
|
|
82
|
+
exports.server.tool('get_statistics_categories', '获取国家统计局的统计指标分类,返回分类树结构(第一层)', {
|
|
83
|
+
dbcode: zod_1.z.string()
|
|
84
|
+
.describe('数据库代码,指定数据类型:hgnd(年度数据), hgjd(季度数据)等'),
|
|
85
|
+
wdcode: zod_1.z.string()
|
|
86
|
+
.default('zb')
|
|
87
|
+
.describe('维度代码,默认为zb(指标)'),
|
|
88
|
+
}, async (args) => {
|
|
89
|
+
const { dbcode, wdcode = 'zb' } = args;
|
|
90
|
+
try {
|
|
91
|
+
const data = await apiClient.getCategories(dbcode, wdcode);
|
|
92
|
+
return {
|
|
93
|
+
content: [
|
|
94
|
+
{
|
|
95
|
+
type: 'text',
|
|
96
|
+
text: JSON.stringify(data, null, 2)
|
|
97
|
+
}
|
|
98
|
+
],
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
catch (error) {
|
|
102
|
+
return {
|
|
103
|
+
content: [
|
|
104
|
+
{
|
|
105
|
+
type: 'text',
|
|
106
|
+
text: `Error: ${error.message}`,
|
|
107
|
+
},
|
|
108
|
+
],
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
// 注册获取统计指标叶子节点的工具(递归获取所有叶子指标)
|
|
113
|
+
exports.server.tool('get_statistics_leaf_categories', '递归获取国家统计局特定数据库的所有叶子指标节点(可查看分类下所有可查询指标)', {
|
|
114
|
+
dbcode: zod_1.z.string()
|
|
115
|
+
.describe('数据库代码,指定数据类型:hgnd(年度数据), hgjd(季度数据)等'),
|
|
116
|
+
}, async (args) => {
|
|
117
|
+
const { dbcode } = args;
|
|
118
|
+
try {
|
|
119
|
+
const data = await apiClient.getLeafCategories(dbcode);
|
|
120
|
+
return {
|
|
121
|
+
content: [
|
|
122
|
+
{
|
|
123
|
+
type: 'text',
|
|
124
|
+
text: JSON.stringify(data, null, 2)
|
|
125
|
+
}
|
|
126
|
+
],
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
catch (error) {
|
|
130
|
+
return {
|
|
131
|
+
content: [
|
|
132
|
+
{
|
|
133
|
+
type: 'text',
|
|
134
|
+
text: `Error: ${error.message}`,
|
|
135
|
+
},
|
|
136
|
+
],
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
// 注册获取统计时间维度选项的工具
|
|
141
|
+
exports.server.tool('get_statistics_time_options', '获取指定数据库可选的时间范围维度', {
|
|
142
|
+
dbcode: zod_1.z.string()
|
|
143
|
+
.describe('数据库代码,指定数据类型:hgnd(年度数据), hgjd(季度数据)等'),
|
|
144
|
+
}, async (args) => {
|
|
145
|
+
const { dbcode } = args;
|
|
146
|
+
try {
|
|
147
|
+
const data = await apiClient.getTimeDimensions(dbcode);
|
|
148
|
+
return {
|
|
149
|
+
content: [
|
|
150
|
+
{
|
|
151
|
+
type: 'text',
|
|
152
|
+
text: JSON.stringify(data, null, 2)
|
|
153
|
+
}
|
|
154
|
+
],
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
catch (error) {
|
|
158
|
+
return {
|
|
159
|
+
content: [
|
|
160
|
+
{
|
|
161
|
+
type: 'text',
|
|
162
|
+
text: `Error: ${error.message}`,
|
|
163
|
+
},
|
|
164
|
+
],
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
});
|
|
168
|
+
// 注册获取统计数据的工具
|
|
169
|
+
exports.server.tool('get_statistics_data', '获取国家统计局特定指标的数据', {
|
|
170
|
+
zb: zod_1.z.string().describe('指标代码,通过分类或搜索获取'),
|
|
171
|
+
dbcode: zod_1.z.string()
|
|
172
|
+
.describe('数据库代码,指定数据类型:hgnd(年度数据), hgjd(季度数据)等'),
|
|
173
|
+
sj: zod_1.z.string()
|
|
174
|
+
.default('LAST30')
|
|
175
|
+
.describe('时间范围,例如 LAST6 (最近6季度), 2025 (指定年份), LAST30 (最近30年) 等'),
|
|
176
|
+
}, async (args) => {
|
|
177
|
+
const { zb, dbcode, sj = 'LAST30' } = args;
|
|
178
|
+
try {
|
|
179
|
+
const data = await apiClient.getData(zb, dbcode, sj);
|
|
180
|
+
return {
|
|
181
|
+
content: [
|
|
182
|
+
{
|
|
183
|
+
type: 'text',
|
|
184
|
+
text: JSON.stringify(data, null, 2)
|
|
185
|
+
}
|
|
186
|
+
],
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
catch (error) {
|
|
190
|
+
return {
|
|
191
|
+
content: [
|
|
192
|
+
{
|
|
193
|
+
type: 'text',
|
|
194
|
+
text: `Error: ${error.message}`,
|
|
195
|
+
},
|
|
196
|
+
],
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
});
|
|
200
|
+
// 注册批量获取统计数据的工具
|
|
201
|
+
exports.server.tool('batch_get_statistics', '批量获取国家统计局多个指标的数据', {
|
|
202
|
+
queries: zod_1.z.array(zod_1.z.object({
|
|
203
|
+
zb: zod_1.z.string().describe('指标代码'),
|
|
204
|
+
dbcode: zod_1.z.string().describe('数据库代码'),
|
|
205
|
+
sj: zod_1.z.string().optional().default('LAST30').describe('时间范围'),
|
|
206
|
+
})).describe('查询参数数组')
|
|
207
|
+
}, async (args) => {
|
|
208
|
+
const { queries } = args;
|
|
209
|
+
try {
|
|
210
|
+
const results = await apiClient.batchGet(queries.map(q => ({
|
|
211
|
+
zb: q.zb,
|
|
212
|
+
dbcode: q.dbcode,
|
|
213
|
+
sj: q.sj || 'LAST30'
|
|
214
|
+
})));
|
|
215
|
+
return {
|
|
216
|
+
content: [
|
|
217
|
+
{
|
|
218
|
+
type: 'text',
|
|
219
|
+
text: JSON.stringify(results, null, 2)
|
|
220
|
+
}
|
|
221
|
+
],
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
catch (error) {
|
|
225
|
+
return {
|
|
226
|
+
content: [
|
|
227
|
+
{
|
|
228
|
+
type: 'text',
|
|
229
|
+
text: `Error: ${error.message}`,
|
|
230
|
+
},
|
|
231
|
+
],
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
});
|
|
235
|
+
// 启动服务器
|
|
236
|
+
async function startServer() {
|
|
237
|
+
const program = new commander_1.Command();
|
|
238
|
+
program
|
|
239
|
+
.option('-p, --port <number>', 'Port to listen on for HTTP/SSE mode')
|
|
240
|
+
.option('-h, --host <host>', 'Host to listen on for HTTP/SSE mode', 'localhost')
|
|
241
|
+
.parse();
|
|
242
|
+
const options = program.opts();
|
|
243
|
+
// 通过端口参数判断是否使用HTTP模式还是STDIO模式
|
|
244
|
+
if (options.port) {
|
|
245
|
+
await (0, mcp_http_server_1.startSseAndStreamableHttpMcpServer)({
|
|
246
|
+
host: options.host,
|
|
247
|
+
port: parseInt(options.port),
|
|
248
|
+
createMcpServer: async ({ headers }) => {
|
|
249
|
+
return exports.server;
|
|
250
|
+
},
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
else {
|
|
254
|
+
// 使用STDIO模式
|
|
255
|
+
const transport = new stdio_js_1.StdioServerTransport();
|
|
256
|
+
await exports.server.connect(transport);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
if (require.main === module) {
|
|
260
|
+
startServer().catch((error) => {
|
|
261
|
+
console.error('Failed to start server:', error);
|
|
262
|
+
process.exit(1);
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
exports.default = exports.server;
|
|
266
|
+
//# sourceMappingURL=index.js.map
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export interface McpServer {
|
|
2
|
+
new (options: {
|
|
3
|
+
name: string;
|
|
4
|
+
version: string;
|
|
5
|
+
capabilities: {
|
|
6
|
+
resources?: Record<string, any>;
|
|
7
|
+
tools?: Record<string, any>;
|
|
8
|
+
};
|
|
9
|
+
instructions?: string;
|
|
10
|
+
}): McpServer;
|
|
11
|
+
tool(name: string, description: string, parameters: any, implementation: (args: any) => Promise<any>): void;
|
|
12
|
+
connect(transport: any): Promise<void>;
|
|
13
|
+
}
|
|
14
|
+
export interface SseAndStreamableHttpMcpServerOptions {
|
|
15
|
+
host: string;
|
|
16
|
+
port: number;
|
|
17
|
+
createMcpServer: (context: {
|
|
18
|
+
headers: Record<string, string>;
|
|
19
|
+
}) => Promise<any>;
|
|
20
|
+
}
|
|
21
|
+
export interface StdioServerTransport {
|
|
22
|
+
new (): StdioServerTransport;
|
|
23
|
+
}
|
|
24
|
+
export type StartHttpServerFn = (options: SseAndStreamableHttpMcpServerOptions) => Promise<void>;
|
|
25
|
+
//# sourceMappingURL=types.d.ts.map
|
package/dist/types.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "national-stats-mcp",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "MCP server for accessing National Bureau of Statistics data",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"build": "tsc",
|
|
8
|
+
"start": "node dist/index.js",
|
|
9
|
+
"dev": "tsx watch src/index.ts",
|
|
10
|
+
"lint": "eslint src --ext .ts",
|
|
11
|
+
"clean": "rm -rf dist"
|
|
12
|
+
},
|
|
13
|
+
"keywords": [
|
|
14
|
+
"mcp",
|
|
15
|
+
"statistics",
|
|
16
|
+
"api"
|
|
17
|
+
],
|
|
18
|
+
"author": "Notional Data",
|
|
19
|
+
"license": "MIT",
|
|
20
|
+
"dependencies": {
|
|
21
|
+
"@modelcontextprotocol/sdk": "^1.9.0",
|
|
22
|
+
"axios": "^1.8.4",
|
|
23
|
+
"commander": "^14.0.0",
|
|
24
|
+
"mcp-http-server": "^1.1.5",
|
|
25
|
+
"zod": "^3.24.2"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"@types/node": "^20.10.0",
|
|
29
|
+
"@typescript-eslint/eslint-plugin": "^6.13.0",
|
|
30
|
+
"@typescript-eslint/parser": "^6.13.0",
|
|
31
|
+
"eslint": "^8.55.0",
|
|
32
|
+
"tsx": "^4.7.0",
|
|
33
|
+
"typescript": "^5.3.0"
|
|
34
|
+
}
|
|
35
|
+
}
|
package/wbcode.json
ADDED
package/zb.json
ADDED
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
[
|
|
2
|
+
{
|
|
3
|
+
"dbcode": "hgnd",
|
|
4
|
+
"id": "A01",
|
|
5
|
+
"isParent": true,
|
|
6
|
+
"name": "综合",
|
|
7
|
+
"pid": "",
|
|
8
|
+
"wdcode": "zb"
|
|
9
|
+
},
|
|
10
|
+
{
|
|
11
|
+
"dbcode": "hgnd",
|
|
12
|
+
"id": "A02",
|
|
13
|
+
"isParent": true,
|
|
14
|
+
"name": "国民经济核算",
|
|
15
|
+
"pid": "",
|
|
16
|
+
"wdcode": "zb"
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
"dbcode": "hgnd",
|
|
20
|
+
"id": "A03",
|
|
21
|
+
"isParent": true,
|
|
22
|
+
"name": "人口",
|
|
23
|
+
"pid": "",
|
|
24
|
+
"wdcode": "zb"
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
"dbcode": "hgnd",
|
|
28
|
+
"id": "A04",
|
|
29
|
+
"isParent": true,
|
|
30
|
+
"name": "就业人员和工资",
|
|
31
|
+
"pid": "",
|
|
32
|
+
"wdcode": "zb"
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
"dbcode": "hgnd",
|
|
36
|
+
"id": "A05",
|
|
37
|
+
"isParent": true,
|
|
38
|
+
"name": "固定资产投资和房地产",
|
|
39
|
+
"pid": "",
|
|
40
|
+
"wdcode": "zb"
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
"dbcode": "hgnd",
|
|
44
|
+
"id": "A06",
|
|
45
|
+
"isParent": true,
|
|
46
|
+
"name": "对外经济贸易",
|
|
47
|
+
"pid": "",
|
|
48
|
+
"wdcode": "zb"
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
"dbcode": "hgnd",
|
|
52
|
+
"id": "A07",
|
|
53
|
+
"isParent": true,
|
|
54
|
+
"name": "能源",
|
|
55
|
+
"pid": "",
|
|
56
|
+
"wdcode": "zb"
|
|
57
|
+
},
|
|
58
|
+
{
|
|
59
|
+
"dbcode": "hgnd",
|
|
60
|
+
"id": "A08",
|
|
61
|
+
"isParent": true,
|
|
62
|
+
"name": "财政",
|
|
63
|
+
"pid": "",
|
|
64
|
+
"wdcode": "zb"
|
|
65
|
+
},
|
|
66
|
+
{
|
|
67
|
+
"dbcode": "hgnd",
|
|
68
|
+
"id": "A09",
|
|
69
|
+
"isParent": true,
|
|
70
|
+
"name": "价格指数",
|
|
71
|
+
"pid": "",
|
|
72
|
+
"wdcode": "zb"
|
|
73
|
+
},
|
|
74
|
+
{
|
|
75
|
+
"dbcode": "hgnd",
|
|
76
|
+
"id": "A0A",
|
|
77
|
+
"isParent": true,
|
|
78
|
+
"name": "人民生活",
|
|
79
|
+
"pid": "",
|
|
80
|
+
"wdcode": "zb"
|
|
81
|
+
},
|
|
82
|
+
{
|
|
83
|
+
"dbcode": "hgnd",
|
|
84
|
+
"id": "A0B",
|
|
85
|
+
"isParent": true,
|
|
86
|
+
"name": "城市概况",
|
|
87
|
+
"pid": "",
|
|
88
|
+
"wdcode": "zb"
|
|
89
|
+
},
|
|
90
|
+
{
|
|
91
|
+
"dbcode": "hgnd",
|
|
92
|
+
"id": "A0C",
|
|
93
|
+
"isParent": true,
|
|
94
|
+
"name": "资源和环境",
|
|
95
|
+
"pid": "",
|
|
96
|
+
"wdcode": "zb"
|
|
97
|
+
},
|
|
98
|
+
{
|
|
99
|
+
"dbcode": "hgnd",
|
|
100
|
+
"id": "A0D",
|
|
101
|
+
"isParent": true,
|
|
102
|
+
"name": "农业",
|
|
103
|
+
"pid": "",
|
|
104
|
+
"wdcode": "zb"
|
|
105
|
+
},
|
|
106
|
+
{
|
|
107
|
+
"dbcode": "hgnd",
|
|
108
|
+
"id": "A0E",
|
|
109
|
+
"isParent": true,
|
|
110
|
+
"name": "工业",
|
|
111
|
+
"pid": "",
|
|
112
|
+
"wdcode": "zb"
|
|
113
|
+
},
|
|
114
|
+
{
|
|
115
|
+
"dbcode": "hgnd",
|
|
116
|
+
"id": "A0F",
|
|
117
|
+
"isParent": true,
|
|
118
|
+
"name": "建筑业",
|
|
119
|
+
"pid": "",
|
|
120
|
+
"wdcode": "zb"
|
|
121
|
+
},
|
|
122
|
+
{
|
|
123
|
+
"dbcode": "hgnd",
|
|
124
|
+
"id": "A0G",
|
|
125
|
+
"isParent": true,
|
|
126
|
+
"name": "运输和邮电",
|
|
127
|
+
"pid": "",
|
|
128
|
+
"wdcode": "zb"
|
|
129
|
+
},
|
|
130
|
+
{
|
|
131
|
+
"dbcode": "hgnd",
|
|
132
|
+
"id": "A0H",
|
|
133
|
+
"isParent": false,
|
|
134
|
+
"name": "社会消费品零售总额",
|
|
135
|
+
"pid": "",
|
|
136
|
+
"wdcode": "zb"
|
|
137
|
+
},
|
|
138
|
+
{
|
|
139
|
+
"dbcode": "hgnd",
|
|
140
|
+
"id": "A0I",
|
|
141
|
+
"isParent": true,
|
|
142
|
+
"name": "批发和零售业",
|
|
143
|
+
"pid": "",
|
|
144
|
+
"wdcode": "zb"
|
|
145
|
+
},
|
|
146
|
+
{
|
|
147
|
+
"dbcode": "hgnd",
|
|
148
|
+
"id": "A0J",
|
|
149
|
+
"isParent": true,
|
|
150
|
+
"name": "住宿和餐饮业",
|
|
151
|
+
"pid": "",
|
|
152
|
+
"wdcode": "zb"
|
|
153
|
+
},
|
|
154
|
+
{
|
|
155
|
+
"dbcode": "hgnd",
|
|
156
|
+
"id": "A0K",
|
|
157
|
+
"isParent": true,
|
|
158
|
+
"name": "旅游业",
|
|
159
|
+
"pid": "",
|
|
160
|
+
"wdcode": "zb"
|
|
161
|
+
},
|
|
162
|
+
{
|
|
163
|
+
"dbcode": "hgnd",
|
|
164
|
+
"id": "A0L",
|
|
165
|
+
"isParent": true,
|
|
166
|
+
"name": "金融业",
|
|
167
|
+
"pid": "",
|
|
168
|
+
"wdcode": "zb"
|
|
169
|
+
},
|
|
170
|
+
{
|
|
171
|
+
"dbcode": "hgnd",
|
|
172
|
+
"id": "A0M",
|
|
173
|
+
"isParent": true,
|
|
174
|
+
"name": "教育",
|
|
175
|
+
"pid": "",
|
|
176
|
+
"wdcode": "zb"
|
|
177
|
+
},
|
|
178
|
+
{
|
|
179
|
+
"dbcode": "hgnd",
|
|
180
|
+
"id": "A0N",
|
|
181
|
+
"isParent": true,
|
|
182
|
+
"name": "科技",
|
|
183
|
+
"pid": "",
|
|
184
|
+
"wdcode": "zb"
|
|
185
|
+
},
|
|
186
|
+
{
|
|
187
|
+
"dbcode": "hgnd",
|
|
188
|
+
"id": "A0O",
|
|
189
|
+
"isParent": true,
|
|
190
|
+
"name": "卫生",
|
|
191
|
+
"pid": "",
|
|
192
|
+
"wdcode": "zb"
|
|
193
|
+
},
|
|
194
|
+
{
|
|
195
|
+
"dbcode": "hgnd",
|
|
196
|
+
"id": "A0P",
|
|
197
|
+
"isParent": true,
|
|
198
|
+
"name": "社会服务",
|
|
199
|
+
"pid": "",
|
|
200
|
+
"wdcode": "zb"
|
|
201
|
+
},
|
|
202
|
+
{
|
|
203
|
+
"dbcode": "hgnd",
|
|
204
|
+
"id": "A0Q",
|
|
205
|
+
"isParent": true,
|
|
206
|
+
"name": "文化",
|
|
207
|
+
"pid": "",
|
|
208
|
+
"wdcode": "zb"
|
|
209
|
+
},
|
|
210
|
+
{
|
|
211
|
+
"dbcode": "hgnd",
|
|
212
|
+
"id": "A0R",
|
|
213
|
+
"isParent": true,
|
|
214
|
+
"name": "体育",
|
|
215
|
+
"pid": "",
|
|
216
|
+
"wdcode": "zb"
|
|
217
|
+
},
|
|
218
|
+
{
|
|
219
|
+
"dbcode": "hgnd",
|
|
220
|
+
"id": "A0S",
|
|
221
|
+
"isParent": true,
|
|
222
|
+
"name": "公共管理、社会保障及其他",
|
|
223
|
+
"pid": "",
|
|
224
|
+
"wdcode": "zb"
|
|
225
|
+
}
|
|
226
|
+
]
|