proxy-api-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/.env.example +57 -0
- package/README.md +443 -0
- package/api-tools.json.example +226 -0
- package/dist/examples/env-in-json-usage.d.ts +43 -0
- package/dist/examples/env-in-json-usage.d.ts.map +1 -0
- package/dist/examples/env-in-json-usage.js +242 -0
- package/dist/examples/env-in-json-usage.js.map +1 -0
- package/dist/examples/env-usage.d.ts +7 -0
- package/dist/examples/env-usage.d.ts.map +1 -0
- package/dist/examples/env-usage.js +108 -0
- package/dist/examples/env-usage.js.map +1 -0
- package/dist/examples/preprocessor-usage.d.ts +14 -0
- package/dist/examples/preprocessor-usage.d.ts.map +1 -0
- package/dist/examples/preprocessor-usage.js +196 -0
- package/dist/examples/preprocessor-usage.js.map +1 -0
- package/dist/src/config/env.d.ts +97 -0
- package/dist/src/config/env.d.ts.map +1 -0
- package/dist/src/config/env.js +228 -0
- package/dist/src/config/env.js.map +1 -0
- package/dist/src/core/registry.d.ts +59 -0
- package/dist/src/core/registry.d.ts.map +1 -0
- package/dist/src/core/registry.js +126 -0
- package/dist/src/core/registry.js.map +1 -0
- package/dist/src/core/router.d.ts +34 -0
- package/dist/src/core/router.d.ts.map +1 -0
- package/dist/src/core/router.js +108 -0
- package/dist/src/core/router.js.map +1 -0
- package/dist/src/core/server.d.ts +55 -0
- package/dist/src/core/server.d.ts.map +1 -0
- package/dist/src/core/server.js +159 -0
- package/dist/src/core/server.js.map +1 -0
- package/dist/src/core/session.d.ts +70 -0
- package/dist/src/core/session.d.ts.map +1 -0
- package/dist/src/core/session.js +171 -0
- package/dist/src/core/session.js.map +1 -0
- package/dist/src/core/transports/http.d.ts +16 -0
- package/dist/src/core/transports/http.d.ts.map +1 -0
- package/dist/src/core/transports/http.js +169 -0
- package/dist/src/core/transports/http.js.map +1 -0
- package/dist/src/core/transports/stdio.d.ts +9 -0
- package/dist/src/core/transports/stdio.d.ts.map +1 -0
- package/dist/src/core/transports/stdio.js +32 -0
- package/dist/src/core/transports/stdio.js.map +1 -0
- package/dist/src/index.d.ts +19 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +22 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/server.d.ts +6 -0
- package/dist/src/server.d.ts.map +1 -0
- package/dist/src/server.js +121 -0
- package/dist/src/server.js.map +1 -0
- package/dist/src/tools/api/builder.d.ts +34 -0
- package/dist/src/tools/api/builder.d.ts.map +1 -0
- package/dist/src/tools/api/builder.js +196 -0
- package/dist/src/tools/api/builder.js.map +1 -0
- package/dist/src/tools/api/executor.d.ts +57 -0
- package/dist/src/tools/api/executor.d.ts.map +1 -0
- package/dist/src/tools/api/executor.js +317 -0
- package/dist/src/tools/api/executor.js.map +1 -0
- package/dist/src/tools/api/index.d.ts +12 -0
- package/dist/src/tools/api/index.d.ts.map +1 -0
- package/dist/src/tools/api/index.js +12 -0
- package/dist/src/tools/api/index.js.map +1 -0
- package/dist/src/tools/api/json/kingdee/kingdee_caiwukuaiji_chunaguanli.json +879 -0
- package/dist/src/tools/api/json/kingdee/kingdee_caiwukuaiji_yingfukuanguanli.json +1673 -0
- package/dist/src/tools/api/json/kingdee/kingdee_caiwukuaiji_yingshoukuanguanli.json +1081 -0
- package/dist/src/tools/api/json/kingdee/kingdee_caiwukuaiji_zongzhang.json +976 -0
- package/dist/src/tools/api/json/kingdee/kingdee_chengbenguanli_biaozhunchengbenfenxi.json +1 -0
- package/dist/src/tools/api/json/kingdee/kingdee_chengbenguanli_chanpinchengbenhesuan.json +1 -0
- package/dist/src/tools/api/json/kingdee/kingdee_chengbenguanli_cunhuohesuan.json +1858 -0
- package/dist/src/tools/api/json/kingdee/kingdee_gongyinglianguanli_caigouguanli.json +1 -0
- package/dist/src/tools/api/json/kingdee/kingdee_gongyinglianguanli_gongyingshangguanli.json +616 -0
- package/dist/src/tools/api/json/kingdee/kingdee_gongyinglianguanli_kucunguanli.json +858 -0
- package/dist/src/tools/api/json/kingdee/kingdee_gongyinglianguanli_xiaoshouguanli.json +858 -0
- package/dist/src/tools/api/json/kingdee/kingdee_gongyinglianguanli_zuzhijianjiesuan.json +858 -0
- package/dist/src/tools/api/json/kingdee/kingdee_jichuguanli_jichuziliao.json +867 -0
- package/dist/src/tools/api/json/kingdee/kingdee_jichuguanli_xiaoxipingtai.json +374 -0
- package/dist/src/tools/api/json/kingdee/kingdee_shengchanzhizao_chejianguanli.json +923 -0
- package/dist/src/tools/api/json/kingdee/kingdee_shengchanzhizao_gongchengshujuguanli.json +923 -0
- package/dist/src/tools/api/json/kingdee/kingdee_shengchanzhizao_shengchanguanli.json +858 -0
- package/dist/src/tools/api/json/kingdee/kingdee_shuiwuguanli_fapiaoguanli.json +1081 -0
- package/dist/src/tools/api/json/kingdee/kingdee_yuangongfuwu_renrenbaoxiao.json +858 -0
- package/dist/src/tools/api/json/kingdee/kingdee_zichanguanli_gudingzichan.json +1 -0
- package/dist/src/tools/api/json-config-loader.d.ts +28 -0
- package/dist/src/tools/api/json-config-loader.d.ts.map +1 -0
- package/dist/src/tools/api/json-config-loader.js +123 -0
- package/dist/src/tools/api/json-config-loader.js.map +1 -0
- package/dist/src/tools/api/json-config-mapping.d.ts +41 -0
- package/dist/src/tools/api/json-config-mapping.d.ts.map +1 -0
- package/dist/src/tools/api/json-config-mapping.js +138 -0
- package/dist/src/tools/api/json-config-mapping.js.map +1 -0
- package/dist/src/tools/api/loader.d.ts +38 -0
- package/dist/src/tools/api/loader.d.ts.map +1 -0
- package/dist/src/tools/api/loader.js +240 -0
- package/dist/src/tools/api/loader.js.map +1 -0
- package/dist/src/tools/api/preprocessor.d.ts +39 -0
- package/dist/src/tools/api/preprocessor.d.ts.map +1 -0
- package/dist/src/tools/api/preprocessor.js +64 -0
- package/dist/src/tools/api/preprocessor.js.map +1 -0
- package/dist/src/tools/api/preprocessors/example.d.ts +59 -0
- package/dist/src/tools/api/preprocessors/example.d.ts.map +1 -0
- package/dist/src/tools/api/preprocessors/example.js +133 -0
- package/dist/src/tools/api/preprocessors/example.js.map +1 -0
- package/dist/src/tools/api/preprocessors/index.d.ts +10 -0
- package/dist/src/tools/api/preprocessors/index.d.ts.map +1 -0
- package/dist/src/tools/api/preprocessors/index.js +29 -0
- package/dist/src/tools/api/preprocessors/index.js.map +1 -0
- package/dist/src/tools/api/preprocessors/kingdee.d.ts +24 -0
- package/dist/src/tools/api/preprocessors/kingdee.d.ts.map +1 -0
- package/dist/src/tools/api/preprocessors/kingdee.js +149 -0
- package/dist/src/tools/api/preprocessors/kingdee.js.map +1 -0
- package/dist/src/tools/api/types.d.ts +174 -0
- package/dist/src/tools/api/types.d.ts.map +1 -0
- package/dist/src/tools/api/types.js +5 -0
- package/dist/src/tools/api/types.js.map +1 -0
- package/dist/src/tools/examples/echo.d.ts +6 -0
- package/dist/src/tools/examples/echo.d.ts.map +1 -0
- package/dist/src/tools/examples/echo.js +61 -0
- package/dist/src/tools/examples/echo.js.map +1 -0
- package/dist/src/tools/types.d.ts +104 -0
- package/dist/src/tools/types.d.ts.map +1 -0
- package/dist/src/tools/types.js +15 -0
- package/dist/src/tools/types.js.map +1 -0
- package/dist/src/tools/validator.d.ts +27 -0
- package/dist/src/tools/validator.d.ts.map +1 -0
- package/dist/src/tools/validator.js +133 -0
- package/dist/src/tools/validator.js.map +1 -0
- package/dist/src/utils/cache.d.ts +14 -0
- package/dist/src/utils/cache.d.ts.map +1 -0
- package/dist/src/utils/cache.js +42 -0
- package/dist/src/utils/cache.js.map +1 -0
- package/dist/src/utils/env-replacer.d.ts +86 -0
- package/dist/src/utils/env-replacer.d.ts.map +1 -0
- package/dist/src/utils/env-replacer.js +196 -0
- package/dist/src/utils/env-replacer.js.map +1 -0
- package/dist/src/utils/errors.d.ts +102 -0
- package/dist/src/utils/errors.d.ts.map +1 -0
- package/dist/src/utils/errors.js +164 -0
- package/dist/src/utils/errors.js.map +1 -0
- package/dist/src/utils/logger.d.ts +77 -0
- package/dist/src/utils/logger.d.ts.map +1 -0
- package/dist/src/utils/logger.js +155 -0
- package/dist/src/utils/logger.js.map +1 -0
- package/package.json +73 -0
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": "1.0",
|
|
3
|
+
"globals": {
|
|
4
|
+
"timeout": "${API_TIMEOUT:30000}",
|
|
5
|
+
"headers": {
|
|
6
|
+
"User-Agent": "${USER_AGENT:DefinesysMCP/1.0}"
|
|
7
|
+
}
|
|
8
|
+
},
|
|
9
|
+
"tools": [
|
|
10
|
+
{
|
|
11
|
+
"name": "get_user_by_id",
|
|
12
|
+
"title": "Get User By ID",
|
|
13
|
+
"description": "Retrieve user information by user ID from JSONPlaceholder API",
|
|
14
|
+
"method": "GET",
|
|
15
|
+
"baseUrl": "${JSONPLACEHOLDER_BASE_URL:https://jsonplaceholder.typicode.com}",
|
|
16
|
+
"path": "/users/{userId}",
|
|
17
|
+
"parameters": [
|
|
18
|
+
{
|
|
19
|
+
"name": "userId",
|
|
20
|
+
"in": "path",
|
|
21
|
+
"description": "The ID of the user to retrieve",
|
|
22
|
+
"required": true,
|
|
23
|
+
"type": "integer"
|
|
24
|
+
}
|
|
25
|
+
],
|
|
26
|
+
"responseTransform": {
|
|
27
|
+
"dataPath": "",
|
|
28
|
+
"fieldMapping": {
|
|
29
|
+
"id": "id",
|
|
30
|
+
"userName": "username",
|
|
31
|
+
"userEmail": "email",
|
|
32
|
+
"userPhone": "phone"
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
"enabled": true
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
"name": "list_posts",
|
|
39
|
+
"title": "List Posts",
|
|
40
|
+
"description": "Get a list of posts, optionally filtered by user ID",
|
|
41
|
+
"method": "GET",
|
|
42
|
+
"baseUrl": "${JSONPLACEHOLDER_BASE_URL:https://jsonplaceholder.typicode.com}",
|
|
43
|
+
"path": "/posts",
|
|
44
|
+
"parameters": [
|
|
45
|
+
{
|
|
46
|
+
"name": "userId",
|
|
47
|
+
"in": "query",
|
|
48
|
+
"description": "Filter posts by user ID",
|
|
49
|
+
"required": false,
|
|
50
|
+
"type": "integer"
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
"name": "_limit",
|
|
54
|
+
"in": "query",
|
|
55
|
+
"description": "Limit the number of results",
|
|
56
|
+
"required": false,
|
|
57
|
+
"type": "integer",
|
|
58
|
+
"default": 10
|
|
59
|
+
}
|
|
60
|
+
],
|
|
61
|
+
"enabled": true
|
|
62
|
+
},
|
|
63
|
+
{
|
|
64
|
+
"name": "create_post",
|
|
65
|
+
"title": "Create Post",
|
|
66
|
+
"description": "Create a new post",
|
|
67
|
+
"method": "POST",
|
|
68
|
+
"baseUrl": "${JSONPLACEHOLDER_BASE_URL:https://jsonplaceholder.typicode.com}",
|
|
69
|
+
"path": "/posts",
|
|
70
|
+
"requestBody": {
|
|
71
|
+
"description": "Post data to create",
|
|
72
|
+
"required": true,
|
|
73
|
+
"contentType": "application/json",
|
|
74
|
+
"schema": {
|
|
75
|
+
"type": "object",
|
|
76
|
+
"properties": {
|
|
77
|
+
"title": {
|
|
78
|
+
"type": "string",
|
|
79
|
+
"description": "Post title"
|
|
80
|
+
},
|
|
81
|
+
"body": {
|
|
82
|
+
"type": "string",
|
|
83
|
+
"description": "Post content"
|
|
84
|
+
},
|
|
85
|
+
"userId": {
|
|
86
|
+
"type": "integer",
|
|
87
|
+
"description": "User ID of the post author"
|
|
88
|
+
}
|
|
89
|
+
},
|
|
90
|
+
"required": ["title", "body", "userId"]
|
|
91
|
+
}
|
|
92
|
+
},
|
|
93
|
+
"enabled": true
|
|
94
|
+
},
|
|
95
|
+
{
|
|
96
|
+
"name": "update_post",
|
|
97
|
+
"title": "Update Post",
|
|
98
|
+
"description": "Update an existing post",
|
|
99
|
+
"method": "PUT",
|
|
100
|
+
"baseUrl": "${JSONPLACEHOLDER_BASE_URL:https://jsonplaceholder.typicode.com}",
|
|
101
|
+
"path": "/posts/{postId}",
|
|
102
|
+
"parameters": [
|
|
103
|
+
{
|
|
104
|
+
"name": "postId",
|
|
105
|
+
"in": "path",
|
|
106
|
+
"description": "The ID of the post to update",
|
|
107
|
+
"required": true,
|
|
108
|
+
"type": "integer"
|
|
109
|
+
}
|
|
110
|
+
],
|
|
111
|
+
"requestBody": {
|
|
112
|
+
"description": "Updated post data",
|
|
113
|
+
"required": true,
|
|
114
|
+
"contentType": "application/json",
|
|
115
|
+
"schema": {
|
|
116
|
+
"type": "object",
|
|
117
|
+
"properties": {
|
|
118
|
+
"title": {
|
|
119
|
+
"type": "string",
|
|
120
|
+
"description": "Updated post title"
|
|
121
|
+
},
|
|
122
|
+
"body": {
|
|
123
|
+
"type": "string",
|
|
124
|
+
"description": "Updated post content"
|
|
125
|
+
},
|
|
126
|
+
"userId": {
|
|
127
|
+
"type": "integer",
|
|
128
|
+
"description": "User ID of the post author"
|
|
129
|
+
}
|
|
130
|
+
},
|
|
131
|
+
"required": ["title", "body", "userId"]
|
|
132
|
+
}
|
|
133
|
+
},
|
|
134
|
+
"enabled": true
|
|
135
|
+
},
|
|
136
|
+
{
|
|
137
|
+
"name": "delete_post",
|
|
138
|
+
"title": "Delete Post",
|
|
139
|
+
"description": "Delete a post by ID",
|
|
140
|
+
"method": "DELETE",
|
|
141
|
+
"baseUrl": "${JSONPLACEHOLDER_BASE_URL:https://jsonplaceholder.typicode.com}",
|
|
142
|
+
"path": "/posts/{postId}",
|
|
143
|
+
"parameters": [
|
|
144
|
+
{
|
|
145
|
+
"name": "postId",
|
|
146
|
+
"in": "path",
|
|
147
|
+
"description": "The ID of the post to delete",
|
|
148
|
+
"required": true,
|
|
149
|
+
"type": "integer"
|
|
150
|
+
}
|
|
151
|
+
],
|
|
152
|
+
"enabled": true
|
|
153
|
+
},
|
|
154
|
+
{
|
|
155
|
+
"name": "search_github_repos",
|
|
156
|
+
"title": "Search GitHub Repositories",
|
|
157
|
+
"description": "Search for GitHub repositories by keyword",
|
|
158
|
+
"method": "GET",
|
|
159
|
+
"baseUrl": "https://api.github.com",
|
|
160
|
+
"path": "/search/repositories",
|
|
161
|
+
"headers": {
|
|
162
|
+
"Authorization": "Bearer ${GITHUB_TOKEN:}",
|
|
163
|
+
"Accept": "application/vnd.github.v3+json"
|
|
164
|
+
},
|
|
165
|
+
"parameters": [
|
|
166
|
+
{
|
|
167
|
+
"name": "q",
|
|
168
|
+
"in": "query",
|
|
169
|
+
"description": "Search keyword",
|
|
170
|
+
"required": true,
|
|
171
|
+
"type": "string"
|
|
172
|
+
},
|
|
173
|
+
{
|
|
174
|
+
"name": "sort",
|
|
175
|
+
"in": "query",
|
|
176
|
+
"description": "Sort field",
|
|
177
|
+
"required": false,
|
|
178
|
+
"type": "string",
|
|
179
|
+
"enum": ["stars", "forks", "updated"],
|
|
180
|
+
"default": "stars"
|
|
181
|
+
},
|
|
182
|
+
{
|
|
183
|
+
"name": "order",
|
|
184
|
+
"in": "query",
|
|
185
|
+
"description": "Sort order",
|
|
186
|
+
"required": false,
|
|
187
|
+
"type": "string",
|
|
188
|
+
"enum": ["asc", "desc"],
|
|
189
|
+
"default": "desc"
|
|
190
|
+
},
|
|
191
|
+
{
|
|
192
|
+
"name": "per_page",
|
|
193
|
+
"in": "query",
|
|
194
|
+
"description": "Results per page",
|
|
195
|
+
"required": false,
|
|
196
|
+
"type": "integer",
|
|
197
|
+
"default": 10
|
|
198
|
+
}
|
|
199
|
+
],
|
|
200
|
+
"responseTransform": {
|
|
201
|
+
"dataPath": "items"
|
|
202
|
+
},
|
|
203
|
+
"enabled": true
|
|
204
|
+
},
|
|
205
|
+
{
|
|
206
|
+
"name": "api_with_preprocessor_example",
|
|
207
|
+
"title": "API with Preprocessor Example",
|
|
208
|
+
"description": "Demonstrates using a preprocessor to add timestamp and custom headers to requests",
|
|
209
|
+
"method": "GET",
|
|
210
|
+
"baseUrl": "${JSONPLACEHOLDER_BASE_URL:https://jsonplaceholder.typicode.com}",
|
|
211
|
+
"path": "/posts",
|
|
212
|
+
"preprocessor": "add_timestamp",
|
|
213
|
+
"parameters": [
|
|
214
|
+
{
|
|
215
|
+
"name": "_limit",
|
|
216
|
+
"in": "query",
|
|
217
|
+
"description": "Limit the number of results",
|
|
218
|
+
"required": false,
|
|
219
|
+
"type": "integer",
|
|
220
|
+
"default": 5
|
|
221
|
+
}
|
|
222
|
+
],
|
|
223
|
+
"enabled": false
|
|
224
|
+
}
|
|
225
|
+
]
|
|
226
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* API 配置中环境变量使用示例
|
|
3
|
+
*
|
|
4
|
+
* 本示例演示如何在 api-tools.json 配置文件中使用环境变量
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* 示例 1: 基本的环境变量替换
|
|
8
|
+
*/
|
|
9
|
+
declare function example1BasicReplacement(): void;
|
|
10
|
+
/**
|
|
11
|
+
* 示例 2: 使用默认值
|
|
12
|
+
*/
|
|
13
|
+
declare function example2WithDefaults(): void;
|
|
14
|
+
/**
|
|
15
|
+
* 示例 3: 验证环境变量
|
|
16
|
+
*/
|
|
17
|
+
declare function example3Validation(): void;
|
|
18
|
+
/**
|
|
19
|
+
* 示例 4: 提取环境变量名称
|
|
20
|
+
*/
|
|
21
|
+
declare function example4ExtractVariables(): void;
|
|
22
|
+
/**
|
|
23
|
+
* 示例 5: 检查是否包含占位符
|
|
24
|
+
*/
|
|
25
|
+
declare function example5HasPlaceholder(): void;
|
|
26
|
+
/**
|
|
27
|
+
* 示例 6: 从配置文件加载 API 工具
|
|
28
|
+
*/
|
|
29
|
+
declare function example6LoadApiTools(): Promise<void>;
|
|
30
|
+
/**
|
|
31
|
+
* 示例 7: 多环境配置
|
|
32
|
+
*/
|
|
33
|
+
declare function example7MultiEnvironment(): void;
|
|
34
|
+
/**
|
|
35
|
+
* 示例 8: 错误处理
|
|
36
|
+
*/
|
|
37
|
+
declare function example8ErrorHandling(): void;
|
|
38
|
+
/**
|
|
39
|
+
* 示例 9: 复杂对象替换
|
|
40
|
+
*/
|
|
41
|
+
declare function example9ComplexObject(): void;
|
|
42
|
+
export { example1BasicReplacement, example2WithDefaults, example3Validation, example4ExtractVariables, example5HasPlaceholder, example6LoadApiTools, example7MultiEnvironment, example8ErrorHandling, example9ComplexObject, };
|
|
43
|
+
//# sourceMappingURL=env-in-json-usage.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"env-in-json-usage.d.ts","sourceRoot":"","sources":["../../examples/env-in-json-usage.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAUH;;GAEG;AACH,iBAAS,wBAAwB,SAoBhC;AAED;;GAEG;AACH,iBAAS,oBAAoB,SAoB5B;AAED;;GAEG;AACH,iBAAS,kBAAkB,SAqB1B;AAED;;GAEG;AACH,iBAAS,wBAAwB,SAehC;AAED;;GAEG;AACH,iBAAS,sBAAsB,SAa9B;AAED;;GAEG;AACH,iBAAe,oBAAoB,kBAqBlC;AAED;;GAEG;AACH,iBAAS,wBAAwB,SAiChC;AAED;;GAEG;AACH,iBAAS,qBAAqB,SAoB7B;AAED;;GAEG;AACH,iBAAS,qBAAqB,SAsC7B;AAoCD,OAAO,EACL,wBAAwB,EACxB,oBAAoB,EACpB,kBAAkB,EAClB,wBAAwB,EACxB,sBAAsB,EACtB,oBAAoB,EACpB,wBAAwB,EACxB,qBAAqB,EACrB,qBAAqB,GACtB,CAAC"}
|
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* API 配置中环境变量使用示例
|
|
3
|
+
*
|
|
4
|
+
* 本示例演示如何在 api-tools.json 配置文件中使用环境变量
|
|
5
|
+
*/
|
|
6
|
+
import { ApiToolLoader } from '../src/tools/api/loader.js';
|
|
7
|
+
import { replaceEnvVariables, validateEnvVariables, extractEnvVariables, hasEnvPlaceholder, } from '../src/utils/env-replacer.js';
|
|
8
|
+
/**
|
|
9
|
+
* 示例 1: 基本的环境变量替换
|
|
10
|
+
*/
|
|
11
|
+
function example1BasicReplacement() {
|
|
12
|
+
console.log('\n=== 示例 1: 基本的环境变量替换 ===\n');
|
|
13
|
+
const config = {
|
|
14
|
+
baseUrl: '${API_BASE_URL}',
|
|
15
|
+
timeout: '${API_TIMEOUT}',
|
|
16
|
+
headers: {
|
|
17
|
+
Authorization: 'Bearer ${API_TOKEN}',
|
|
18
|
+
},
|
|
19
|
+
};
|
|
20
|
+
console.log('原始配置:', JSON.stringify(config, null, 2));
|
|
21
|
+
// 设置环境变量(实际使用时应在 .env 文件中设置)
|
|
22
|
+
process.env.API_BASE_URL = 'https://api.example.com';
|
|
23
|
+
process.env.API_TIMEOUT = '30000';
|
|
24
|
+
process.env.API_TOKEN = 'demo_token_123';
|
|
25
|
+
const result = replaceEnvVariables(config);
|
|
26
|
+
console.log('\n替换后的配置:', JSON.stringify(result, null, 2));
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* 示例 2: 使用默认值
|
|
30
|
+
*/
|
|
31
|
+
function example2WithDefaults() {
|
|
32
|
+
console.log('\n=== 示例 2: 使用默认值 ===\n');
|
|
33
|
+
const config = {
|
|
34
|
+
baseUrl: '${API_BASE_URL:https://api.example.com}',
|
|
35
|
+
timeout: '${API_TIMEOUT:30000}',
|
|
36
|
+
headers: {
|
|
37
|
+
'User-Agent': '${USER_AGENT:DefinesysMCP/1.0}',
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
console.log('原始配置:', JSON.stringify(config, null, 2));
|
|
41
|
+
// 清除可能存在的环境变量
|
|
42
|
+
delete process.env.API_BASE_URL;
|
|
43
|
+
delete process.env.API_TIMEOUT;
|
|
44
|
+
delete process.env.USER_AGENT;
|
|
45
|
+
const result = replaceEnvVariables(config);
|
|
46
|
+
console.log('\n替换后的配置(使用默认值):', JSON.stringify(result, null, 2));
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* 示例 3: 验证环境变量
|
|
50
|
+
*/
|
|
51
|
+
function example3Validation() {
|
|
52
|
+
console.log('\n=== 示例 3: 验证环境变量 ===\n');
|
|
53
|
+
const config = {
|
|
54
|
+
baseUrl: '${API_BASE_URL}',
|
|
55
|
+
token: '${API_TOKEN}',
|
|
56
|
+
optionalValue: '${OPTIONAL_VALUE:default}',
|
|
57
|
+
};
|
|
58
|
+
console.log('配置:', JSON.stringify(config, null, 2));
|
|
59
|
+
// 设置部分环境变量
|
|
60
|
+
process.env.API_BASE_URL = 'https://api.example.com';
|
|
61
|
+
delete process.env.API_TOKEN; // API_TOKEN 未设置
|
|
62
|
+
const validation = validateEnvVariables(config);
|
|
63
|
+
console.log('\n验证结果:', validation);
|
|
64
|
+
if (!validation.valid) {
|
|
65
|
+
console.log('缺失的环境变量:', validation.missing.join(', '));
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* 示例 4: 提取环境变量名称
|
|
70
|
+
*/
|
|
71
|
+
function example4ExtractVariables() {
|
|
72
|
+
console.log('\n=== 示例 4: 提取环境变量名称 ===\n');
|
|
73
|
+
const str1 = '${API_BASE_URL}/api/${API_VERSION}/users';
|
|
74
|
+
const str2 = 'Bearer ${API_TOKEN}';
|
|
75
|
+
const str3 = '${TIMEOUT:30000}';
|
|
76
|
+
console.log(`字符串: "${str1}"`);
|
|
77
|
+
console.log('包含环境变量:', extractEnvVariables(str1));
|
|
78
|
+
console.log(`\n字符串: "${str2}"`);
|
|
79
|
+
console.log('包含环境变量:', extractEnvVariables(str2));
|
|
80
|
+
console.log(`\n字符串: "${str3}"`);
|
|
81
|
+
console.log('包含环境变量:', extractEnvVariables(str3));
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* 示例 5: 检查是否包含占位符
|
|
85
|
+
*/
|
|
86
|
+
function example5HasPlaceholder() {
|
|
87
|
+
console.log('\n=== 示例 5: 检查是否包含占位符 ===\n');
|
|
88
|
+
const strings = [
|
|
89
|
+
'https://api.example.com',
|
|
90
|
+
'${API_BASE_URL}',
|
|
91
|
+
'Bearer ${TOKEN}',
|
|
92
|
+
'${API_URL:https://api.example.com}',
|
|
93
|
+
];
|
|
94
|
+
strings.forEach(str => {
|
|
95
|
+
console.log(`"${str}" 包含占位符: ${hasEnvPlaceholder(str)}`);
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* 示例 6: 从配置文件加载 API 工具
|
|
100
|
+
*/
|
|
101
|
+
async function example6LoadApiTools() {
|
|
102
|
+
console.log('\n=== 示例 6: 从配置文件加载 API 工具 ===\n');
|
|
103
|
+
// 设置必需的环境变量
|
|
104
|
+
process.env.JSONPLACEHOLDER_BASE_URL = 'https://jsonplaceholder.typicode.com';
|
|
105
|
+
process.env.API_TIMEOUT = '30000';
|
|
106
|
+
process.env.USER_AGENT = 'DefinesysMCP/1.0';
|
|
107
|
+
try {
|
|
108
|
+
const configPath = './api-tools.json';
|
|
109
|
+
console.log(`从 ${configPath} 加载 API 工具配置...`);
|
|
110
|
+
const tools = await ApiToolLoader.loadFromFile(configPath);
|
|
111
|
+
console.log(`\n成功加载 ${tools.length} 个 API 工具:`);
|
|
112
|
+
tools.forEach((tool, index) => {
|
|
113
|
+
console.log(` ${index + 1}. ${tool.name} - ${tool.description}`);
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
catch (error) {
|
|
117
|
+
console.error('加载失败:', error instanceof Error ? error.message : error);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* 示例 7: 多环境配置
|
|
122
|
+
*/
|
|
123
|
+
function example7MultiEnvironment() {
|
|
124
|
+
console.log('\n=== 示例 7: 多环境配置 ===\n');
|
|
125
|
+
const config = {
|
|
126
|
+
version: '1.0',
|
|
127
|
+
globals: {
|
|
128
|
+
baseUrl: '${API_BASE_URL}',
|
|
129
|
+
timeout: '${API_TIMEOUT:30000}',
|
|
130
|
+
headers: {
|
|
131
|
+
Authorization: 'Bearer ${API_TOKEN}',
|
|
132
|
+
},
|
|
133
|
+
},
|
|
134
|
+
};
|
|
135
|
+
console.log('配置模板:', JSON.stringify(config, null, 2));
|
|
136
|
+
// 开发环境
|
|
137
|
+
console.log('\n--- 开发环境 ---');
|
|
138
|
+
process.env.API_BASE_URL = 'https://dev-api.example.com';
|
|
139
|
+
process.env.API_TOKEN = 'dev_token_123';
|
|
140
|
+
delete process.env.API_TIMEOUT;
|
|
141
|
+
const devConfig = replaceEnvVariables({ ...config });
|
|
142
|
+
console.log('开发环境配置:', JSON.stringify(devConfig, null, 2));
|
|
143
|
+
// 生产环境
|
|
144
|
+
console.log('\n--- 生产环境 ---');
|
|
145
|
+
process.env.API_BASE_URL = 'https://api.example.com';
|
|
146
|
+
process.env.API_TOKEN = 'prod_token_xyz';
|
|
147
|
+
process.env.API_TIMEOUT = '15000';
|
|
148
|
+
const prodConfig = replaceEnvVariables({ ...config });
|
|
149
|
+
console.log('生产环境配置:', JSON.stringify(prodConfig, null, 2));
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* 示例 8: 错误处理
|
|
153
|
+
*/
|
|
154
|
+
function example8ErrorHandling() {
|
|
155
|
+
console.log('\n=== 示例 8: 错误处理 ===\n');
|
|
156
|
+
const config = {
|
|
157
|
+
baseUrl: '${REQUIRED_API_URL}',
|
|
158
|
+
token: '${REQUIRED_TOKEN}',
|
|
159
|
+
};
|
|
160
|
+
console.log('配置:', JSON.stringify(config, null, 2));
|
|
161
|
+
// 清除环境变量,模拟缺失情况
|
|
162
|
+
delete process.env.REQUIRED_API_URL;
|
|
163
|
+
delete process.env.REQUIRED_TOKEN;
|
|
164
|
+
try {
|
|
165
|
+
replaceEnvVariables(config);
|
|
166
|
+
}
|
|
167
|
+
catch (error) {
|
|
168
|
+
console.log('\n捕获到错误:', error instanceof Error ? error.message : error);
|
|
169
|
+
console.log('错误类型:', error?.constructor.name);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* 示例 9: 复杂对象替换
|
|
174
|
+
*/
|
|
175
|
+
function example9ComplexObject() {
|
|
176
|
+
console.log('\n=== 示例 9: 复杂对象替换 ===\n');
|
|
177
|
+
const config = {
|
|
178
|
+
version: '1.0',
|
|
179
|
+
globals: {
|
|
180
|
+
baseUrl: '${API_BASE_URL:https://api.example.com}',
|
|
181
|
+
timeout: '${API_TIMEOUT:30000}',
|
|
182
|
+
},
|
|
183
|
+
tools: [
|
|
184
|
+
{
|
|
185
|
+
name: 'get_user',
|
|
186
|
+
baseUrl: '${USER_SERVICE_URL}',
|
|
187
|
+
path: '/users/{id}',
|
|
188
|
+
headers: {
|
|
189
|
+
Authorization: 'Bearer ${API_TOKEN}',
|
|
190
|
+
'X-API-Key': '${API_KEY:default_key}',
|
|
191
|
+
},
|
|
192
|
+
},
|
|
193
|
+
{
|
|
194
|
+
name: 'get_posts',
|
|
195
|
+
baseUrl: '${POST_SERVICE_URL:https://posts.example.com}',
|
|
196
|
+
path: '/posts',
|
|
197
|
+
},
|
|
198
|
+
],
|
|
199
|
+
};
|
|
200
|
+
console.log('原始配置:', JSON.stringify(config, null, 2));
|
|
201
|
+
// 设置环境变量
|
|
202
|
+
process.env.API_BASE_URL = 'https://api.example.com';
|
|
203
|
+
process.env.USER_SERVICE_URL = 'https://users.example.com';
|
|
204
|
+
process.env.API_TOKEN = 'secret_token';
|
|
205
|
+
delete process.env.API_KEY;
|
|
206
|
+
delete process.env.POST_SERVICE_URL;
|
|
207
|
+
const result = replaceEnvVariables(config);
|
|
208
|
+
console.log('\n替换后的配置:', JSON.stringify(result, null, 2));
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* 主函数 - 运行所有示例
|
|
212
|
+
*/
|
|
213
|
+
async function main() {
|
|
214
|
+
console.log('='.repeat(70));
|
|
215
|
+
console.log('API 配置中环境变量使用示例');
|
|
216
|
+
console.log('='.repeat(70));
|
|
217
|
+
try {
|
|
218
|
+
// 运行所有示例
|
|
219
|
+
example1BasicReplacement();
|
|
220
|
+
example2WithDefaults();
|
|
221
|
+
example3Validation();
|
|
222
|
+
example4ExtractVariables();
|
|
223
|
+
example5HasPlaceholder();
|
|
224
|
+
await example6LoadApiTools();
|
|
225
|
+
example7MultiEnvironment();
|
|
226
|
+
example8ErrorHandling();
|
|
227
|
+
example9ComplexObject();
|
|
228
|
+
console.log(`\n${'='.repeat(70)}`);
|
|
229
|
+
console.log('所有示例执行完成!');
|
|
230
|
+
console.log('='.repeat(70));
|
|
231
|
+
}
|
|
232
|
+
catch (error) {
|
|
233
|
+
console.error('\n执行示例时出错:', error);
|
|
234
|
+
process.exit(1);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
// 运行示例
|
|
238
|
+
if (import.meta.url === `file://${process.argv[1]}`) {
|
|
239
|
+
main().catch(console.error);
|
|
240
|
+
}
|
|
241
|
+
export { example1BasicReplacement, example2WithDefaults, example3Validation, example4ExtractVariables, example5HasPlaceholder, example6LoadApiTools, example7MultiEnvironment, example8ErrorHandling, example9ComplexObject, };
|
|
242
|
+
//# sourceMappingURL=env-in-json-usage.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"env-in-json-usage.js","sourceRoot":"","sources":["../../examples/env-in-json-usage.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAC;AAC3D,OAAO,EACL,mBAAmB,EACnB,oBAAoB,EACpB,mBAAmB,EACnB,iBAAiB,GAClB,MAAM,8BAA8B,CAAC;AAEtC;;GAEG;AACH,SAAS,wBAAwB;IAC/B,OAAO,CAAC,GAAG,CAAC,6BAA6B,CAAC,CAAC;IAE3C,MAAM,MAAM,GAAG;QACb,OAAO,EAAE,iBAAiB;QAC1B,OAAO,EAAE,gBAAgB;QACzB,OAAO,EAAE;YACP,aAAa,EAAE,qBAAqB;SACrC;KACF,CAAC;IAEF,OAAO,CAAC,GAAG,CAAC,OAAO,EAAE,IAAI,CAAC,SAAS,CAAC,MAAM,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC,CAAC;IAEtD,6BAA6B;I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WAAW,CAAC,CAAC;QACzB,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC,CAAC;IAC9B,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,OAAO,CAAC,KAAK,CAAC,YAAY,EAAE,KAAK,CAAC,CAAC;QACnC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAClB,CAAC;AACH,CAAC;AAED,OAAO;AACP,IAAI,MAAM,CAAC,IAAI,CAAC,GAAG,KAAK,UAAU,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC;IACpD,IAAI,EAAE,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;AAC9B,CAAC;AAED,OAAO,EACL,wBAAwB,EACxB,oBAAoB,EACpB,kBAAkB,EAClB,wBAAwB,EACxB,sBAAsB,EACtB,oBAAoB,EACpB,wBAAwB,EACxB,qBAAqB,EACrB,qBAAqB,GACtB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"env-usage.d.ts","sourceRoot":"","sources":["../../examples/env-usage.ts"],"names":[],"mappings":"AAAA;;;;GAIG"}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 环境变量使用示例
|
|
3
|
+
*
|
|
4
|
+
* 本文件展示如何在项目中使用环境变量管理类
|
|
5
|
+
*/
|
|
6
|
+
import { env } from '../src/config/env.js';
|
|
7
|
+
// ===== 示例 1: 获取单个环境变量 =====
|
|
8
|
+
function example1() {
|
|
9
|
+
console.log('\n===== 示例 1: 获取单个环境变量 =====');
|
|
10
|
+
const port = env.get('MCP_PORT');
|
|
11
|
+
console.log(`服务器端口: ${port}`);
|
|
12
|
+
const transport = env.get('MCP_TRANSPORT');
|
|
13
|
+
console.log(`传输模式: ${transport}`);
|
|
14
|
+
const logLevel = env.get('MCP_LOG_LEVEL');
|
|
15
|
+
console.log(`日志级别: ${logLevel}`);
|
|
16
|
+
}
|
|
17
|
+
// ===== 示例 2: 获取所有环境变量 =====
|
|
18
|
+
function example2() {
|
|
19
|
+
console.log('\n===== 示例 2: 获取所有环境变量 =====');
|
|
20
|
+
const allEnv = env.getAll();
|
|
21
|
+
console.log('所有环境变量:', allEnv);
|
|
22
|
+
}
|
|
23
|
+
// ===== 示例 3: 检查环境变量是否存在 =====
|
|
24
|
+
function example3() {
|
|
25
|
+
console.log('\n===== 示例 3: 检查环境变量是否存在 =====');
|
|
26
|
+
if (env.has('MCP_PORT')) {
|
|
27
|
+
console.log('✓ MCP_PORT 已配置');
|
|
28
|
+
}
|
|
29
|
+
if (env.has('MCP_API_TOOLS_CONFIG')) {
|
|
30
|
+
console.log('✓ MCP_API_TOOLS_CONFIG 已配置');
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
// ===== 示例 4: 验证环境变量 =====
|
|
34
|
+
function example4() {
|
|
35
|
+
console.log('\n===== 示例 4: 验证环境变量 =====');
|
|
36
|
+
try {
|
|
37
|
+
env.validate();
|
|
38
|
+
console.log('✓ 所有环境变量验证通过');
|
|
39
|
+
}
|
|
40
|
+
catch (error) {
|
|
41
|
+
console.error('✗ 环境变量验证失败:', error);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
// ===== 示例 5: 判断运行环境 =====
|
|
45
|
+
function example5() {
|
|
46
|
+
console.log('\n===== 示例 5: 判断运行环境 =====');
|
|
47
|
+
if (env.isProduction()) {
|
|
48
|
+
console.log('当前环境: Production');
|
|
49
|
+
}
|
|
50
|
+
else if (env.isDevelopment()) {
|
|
51
|
+
console.log('当前环境: Development');
|
|
52
|
+
}
|
|
53
|
+
else if (env.isTest()) {
|
|
54
|
+
console.log('当前环境: Test');
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
// ===== 示例 6: 在配置中使用环境变量 =====
|
|
58
|
+
function example6() {
|
|
59
|
+
console.log('\n===== 示例 6: 在配置中使用环境变量 =====');
|
|
60
|
+
const serverConfig = {
|
|
61
|
+
name: 'proxy-api-mcp',
|
|
62
|
+
version: '1.0.0',
|
|
63
|
+
transport: env.get('MCP_TRANSPORT'),
|
|
64
|
+
port: env.get('MCP_PORT'),
|
|
65
|
+
sessionTimeout: env.get('MCP_SESSION_TIMEOUT'),
|
|
66
|
+
logLevel: env.get('MCP_LOG_LEVEL'),
|
|
67
|
+
};
|
|
68
|
+
console.log('服务器配置:', serverConfig);
|
|
69
|
+
}
|
|
70
|
+
// ===== 示例 7: 条件逻辑 =====
|
|
71
|
+
function example7() {
|
|
72
|
+
console.log('\n===== 示例 7: 条件逻辑 =====');
|
|
73
|
+
const port = env.get('MCP_PORT');
|
|
74
|
+
if (port < 1024) {
|
|
75
|
+
console.log('警告: 使用特权端口,可能需要管理员权限');
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
console.log('使用非特权端口:', port);
|
|
79
|
+
}
|
|
80
|
+
const timeout = env.get('MCP_SESSION_TIMEOUT');
|
|
81
|
+
const minutes = timeout / 1000 / 60;
|
|
82
|
+
console.log(`会话超时时间: ${minutes} 分钟`);
|
|
83
|
+
}
|
|
84
|
+
// ===== 示例 8: 打印环境变量配置 =====
|
|
85
|
+
function example8() {
|
|
86
|
+
console.log('\n===== 示例 8: 打印环境变量配置 =====');
|
|
87
|
+
env.print();
|
|
88
|
+
}
|
|
89
|
+
// 运行所有示例
|
|
90
|
+
async function runAllExamples() {
|
|
91
|
+
console.log('========================================');
|
|
92
|
+
console.log('环境变量使用示例');
|
|
93
|
+
console.log('========================================');
|
|
94
|
+
example1();
|
|
95
|
+
example2();
|
|
96
|
+
example3();
|
|
97
|
+
example4();
|
|
98
|
+
example5();
|
|
99
|
+
example6();
|
|
100
|
+
example7();
|
|
101
|
+
example8();
|
|
102
|
+
console.log('\n========================================');
|
|
103
|
+
console.log('所有示例执行完成');
|
|
104
|
+
console.log('========================================\n');
|
|
105
|
+
}
|
|
106
|
+
// 执行示例
|
|
107
|
+
runAllExamples().catch(console.error);
|
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,OAAO,CAAC,KAAK,CAAC,CAAC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 预处理器完整使用示例
|
|
3
|
+
* 演示如何在实际项目中使用预处理器功能
|
|
4
|
+
*/
|
|
5
|
+
import { type RequestPreprocessor } from '../src/tools/api/index.js';
|
|
6
|
+
export declare function setupPreprocessors(): void;
|
|
7
|
+
export declare function exampleUsage(): void;
|
|
8
|
+
/**
|
|
9
|
+
* 根据配置动态创建预处理器
|
|
10
|
+
*/
|
|
11
|
+
export declare function createDynamicPreprocessor(name: string, config: Record<string, unknown>): RequestPreprocessor;
|
|
12
|
+
export declare function setupDynamicPreprocessor(): void;
|
|
13
|
+
export declare function setupCompositePreprocessor(): void;
|
|
14
|
+
//# sourceMappingURL=preprocessor-usage.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"preprocessor-usage.d.ts","sourceRoot":"","sources":["../../examples/preprocessor-usage.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAEL,KAAK,mBAAmB,EAGzB,MAAM,2BAA2B,CAAC;AA4HnC,wBAAgB,kBAAkB,IAAI,IAAI,CAiCzC;AAMD,wBAAgB,YAAY,IAAI,IAAI,CAYnC;AAMD;;GAEG;AACH,wBAAgB,yBAAyB,CACvC,IAAI,EAAE,MAAM,EACZ,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAC9B,mBAAmB,CAoBrB;AAGD,wBAAgB,wBAAwB,IAAI,IAAI,CAY/C;AA8BD,wBAAgB,0BAA0B,IAAI,IAAI,CAQjD"}
|