@cloudbase/cloudbase-mcp 1.8.3 → 1.8.4
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 +72 -44
- package/dist/cli.cjs +6080 -0
- package/dist/cli.cjs.map +1 -0
- package/dist/cli.d.cts +1 -0
- package/dist/cli.d.ts +0 -2
- package/dist/cli.js +6043 -60
- package/dist/cli.js.map +1 -1
- package/dist/index.cjs +6093 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +213 -0
- package/dist/index.d.ts +213 -5
- package/dist/index.js +6050 -7
- package/dist/index.js.map +1 -1
- package/package.json +14 -5
- package/dist/auth.d.ts +0 -3
- package/dist/auth.d.ts.map +0 -1
- package/dist/auth.js +0 -26
- package/dist/auth.js.map +0 -1
- package/dist/cli.d.ts.map +0 -1
- package/dist/cloudbase-manager.d.ts +0 -30
- package/dist/cloudbase-manager.d.ts.map +0 -1
- package/dist/cloudbase-manager.js +0 -156
- package/dist/cloudbase-manager.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/interactive-server.d.ts +0 -34
- package/dist/interactive-server.d.ts.map +0 -1
- package/dist/interactive-server.js +0 -2545
- package/dist/interactive-server.js.map +0 -1
- package/dist/server.d.ts +0 -25
- package/dist/server.d.ts.map +0 -1
- package/dist/server.js +0 -60
- package/dist/server.js.map +0 -1
- package/dist/tools/database.d.ts +0 -3
- package/dist/tools/database.d.ts.map +0 -1
- package/dist/tools/database.js +0 -1329
- package/dist/tools/database.js.map +0 -1
- package/dist/tools/download.d.ts +0 -3
- package/dist/tools/download.d.ts.map +0 -1
- package/dist/tools/download.js +0 -259
- package/dist/tools/download.js.map +0 -1
- package/dist/tools/env.d.ts +0 -3
- package/dist/tools/env.d.ts.map +0 -1
- package/dist/tools/env.js +0 -213
- package/dist/tools/env.js.map +0 -1
- package/dist/tools/file.d.ts +0 -3
- package/dist/tools/file.d.ts.map +0 -1
- package/dist/tools/file.js +0 -211
- package/dist/tools/file.js.map +0 -1
- package/dist/tools/functions.d.ts +0 -5
- package/dist/tools/functions.d.ts.map +0 -1
- package/dist/tools/functions.js +0 -540
- package/dist/tools/functions.js.map +0 -1
- package/dist/tools/gateway.d.ts +0 -3
- package/dist/tools/gateway.d.ts.map +0 -1
- package/dist/tools/gateway.js +0 -39
- package/dist/tools/gateway.js.map +0 -1
- package/dist/tools/hosting.d.ts +0 -3
- package/dist/tools/hosting.d.ts.map +0 -1
- package/dist/tools/hosting.js +0 -231
- package/dist/tools/hosting.js.map +0 -1
- package/dist/tools/interactive.d.ts +0 -13
- package/dist/tools/interactive.d.ts.map +0 -1
- package/dist/tools/interactive.js +0 -195
- package/dist/tools/interactive.js.map +0 -1
- package/dist/tools/rag.d.ts +0 -3
- package/dist/tools/rag.d.ts.map +0 -1
- package/dist/tools/rag.js +0 -119
- package/dist/tools/rag.js.map +0 -1
- package/dist/tools/setup.d.ts +0 -3
- package/dist/tools/setup.d.ts.map +0 -1
- package/dist/tools/setup.js +0 -248
- package/dist/tools/setup.js.map +0 -1
- package/dist/tools/storage.d.ts +0 -3
- package/dist/tools/storage.d.ts.map +0 -1
- package/dist/tools/storage.js +0 -53
- package/dist/tools/storage.js.map +0 -1
- package/dist/types.d.ts +0 -62
- package/dist/types.d.ts.map +0 -1
- package/dist/types.js +0 -2
- package/dist/types.js.map +0 -1
- package/dist/utils/logger.d.ts +0 -50
- package/dist/utils/logger.d.ts.map +0 -1
- package/dist/utils/logger.js +0 -120
- package/dist/utils/logger.js.map +0 -1
- package/dist/utils/telemetry.d.ts +0 -73
- package/dist/utils/telemetry.d.ts.map +0 -1
- package/dist/utils/telemetry.js +0 -288
- package/dist/utils/telemetry.js.map +0 -1
- package/dist/utils/tool-wrapper.d.ts +0 -12
- package/dist/utils/tool-wrapper.d.ts.map +0 -1
- package/dist/utils/tool-wrapper.js +0 -100
- package/dist/utils/tool-wrapper.js.map +0 -1
package/dist/tools/database.js
DELETED
|
@@ -1,1329 +0,0 @@
|
|
|
1
|
-
import { z } from "zod";
|
|
2
|
-
import { getCloudBaseManager, getEnvId } from '../cloudbase-manager.js';
|
|
3
|
-
// 获取数据库实例ID
|
|
4
|
-
async function getDatabaseInstanceId(getManager) {
|
|
5
|
-
const cloudbase = await getManager();
|
|
6
|
-
const { EnvInfo } = await cloudbase.env.getEnvInfo();
|
|
7
|
-
if (!EnvInfo?.Databases?.[0]?.InstanceId) {
|
|
8
|
-
throw new Error("无法获取数据库实例ID");
|
|
9
|
-
}
|
|
10
|
-
return EnvInfo.Databases[0].InstanceId;
|
|
11
|
-
}
|
|
12
|
-
// 生成SDK使用文档的函数
|
|
13
|
-
function generateSDKDocs(modelName, modelTitle, userFields, relations) {
|
|
14
|
-
// 获取主要字段(前几个非关联字段)
|
|
15
|
-
const mainFields = userFields.filter(f => !f.linkage);
|
|
16
|
-
const requiredFields = userFields.filter(f => f.required);
|
|
17
|
-
const stringFields = userFields.filter(f => f.type === 'string' && !f.linkage);
|
|
18
|
-
const numberFields = userFields.filter(f => f.type === 'number');
|
|
19
|
-
// 生成字段示例值
|
|
20
|
-
const generateFieldValue = (field) => {
|
|
21
|
-
if (field.enum && field.enum.length > 0) {
|
|
22
|
-
return `"${field.enum[0]}"`;
|
|
23
|
-
}
|
|
24
|
-
switch (field.type) {
|
|
25
|
-
case 'string':
|
|
26
|
-
return field.format === 'email' ? '"user@example.com"' :
|
|
27
|
-
field.format === 'url' ? '"https://example.com"' :
|
|
28
|
-
`"示例${field.title || field.name}"`;
|
|
29
|
-
case 'number':
|
|
30
|
-
return field.format === 'currency' ? '99.99' : '1';
|
|
31
|
-
case 'boolean':
|
|
32
|
-
return field.default !== undefined ? field.default : 'true';
|
|
33
|
-
case 'array':
|
|
34
|
-
return '[]';
|
|
35
|
-
case 'object':
|
|
36
|
-
return '{}';
|
|
37
|
-
default:
|
|
38
|
-
return `"${field.title || field.name}值"`;
|
|
39
|
-
}
|
|
40
|
-
};
|
|
41
|
-
// 生成创建数据示例
|
|
42
|
-
const createDataExample = mainFields.map(field => ` ${field.name}: ${generateFieldValue(field)}, // ${field.description || field.title || field.name}`).join('\n');
|
|
43
|
-
// 生成更新数据示例
|
|
44
|
-
const updateDataExample = mainFields.slice(0, 2).map(field => ` ${field.name}: ${generateFieldValue(field)}, // ${field.description || field.title || field.name}`).join('\n');
|
|
45
|
-
// 生成查询条件示例
|
|
46
|
-
const queryField = stringFields[0] || mainFields[0];
|
|
47
|
-
const queryExample = queryField ?
|
|
48
|
-
` ${queryField.name}: {\n $eq: ${generateFieldValue(queryField)}, // 根据${queryField.description || queryField.title || queryField.name}查询\n },` :
|
|
49
|
-
' _id: {\n $eq: "记录ID", // 根据ID查询\n },';
|
|
50
|
-
return `# 数据模型 ${modelTitle} (${modelName}) SDK 使用文档
|
|
51
|
-
|
|
52
|
-
## 数据模型字段说明
|
|
53
|
-
|
|
54
|
-
${userFields.map(field => {
|
|
55
|
-
let fieldDoc = `- **${field.name}** (${field.type})`;
|
|
56
|
-
if (field.required)
|
|
57
|
-
fieldDoc += ' *必填*';
|
|
58
|
-
if (field.description)
|
|
59
|
-
fieldDoc += `: ${field.description}`;
|
|
60
|
-
if (field.format)
|
|
61
|
-
fieldDoc += ` [格式: ${field.format}]`;
|
|
62
|
-
if (field.enum)
|
|
63
|
-
fieldDoc += ` [可选值: ${field.enum.join(', ')}]`;
|
|
64
|
-
if (field.default !== undefined)
|
|
65
|
-
fieldDoc += ` [默认值: ${field.default}]`;
|
|
66
|
-
return fieldDoc;
|
|
67
|
-
}).join('\n')}
|
|
68
|
-
|
|
69
|
-
${relations.length > 0 ? `
|
|
70
|
-
## 关联关系
|
|
71
|
-
|
|
72
|
-
${relations.map(rel => `- **${rel.field}**: 关联到 ${rel.targetModel} 模型的 ${rel.foreignKey} 字段`).join('\n')}
|
|
73
|
-
` : ''}
|
|
74
|
-
|
|
75
|
-
## 增删改查操作
|
|
76
|
-
|
|
77
|
-
### 创建数据
|
|
78
|
-
|
|
79
|
-
#### 创建单条数据 \`create\`
|
|
80
|
-
|
|
81
|
-
\`\`\`javascript
|
|
82
|
-
const { data } = await models.${modelName}.create({
|
|
83
|
-
data: {
|
|
84
|
-
${createDataExample}
|
|
85
|
-
},
|
|
86
|
-
});
|
|
87
|
-
|
|
88
|
-
// 返回创建的记录 id
|
|
89
|
-
console.log(data);
|
|
90
|
-
// { id: "7d8ff72c665eb6c30243b6313aa8539e"}
|
|
91
|
-
\`\`\`
|
|
92
|
-
|
|
93
|
-
#### 创建多条数据 \`createMany\`
|
|
94
|
-
|
|
95
|
-
\`\`\`javascript
|
|
96
|
-
const { data } = await models.${modelName}.createMany({
|
|
97
|
-
data: [
|
|
98
|
-
{
|
|
99
|
-
${createDataExample}
|
|
100
|
-
},
|
|
101
|
-
{
|
|
102
|
-
${createDataExample}
|
|
103
|
-
},
|
|
104
|
-
],
|
|
105
|
-
});
|
|
106
|
-
|
|
107
|
-
// 返回创建的记录 idList
|
|
108
|
-
console.log(data);
|
|
109
|
-
// {
|
|
110
|
-
// "idList": [
|
|
111
|
-
// "7d8ff72c665ebe5c02442a1a7b29685e",
|
|
112
|
-
// "7d8ff72c665ebe5c02442a1b77feba4b"
|
|
113
|
-
// ]
|
|
114
|
-
// }
|
|
115
|
-
\`\`\`
|
|
116
|
-
|
|
117
|
-
### 更新数据
|
|
118
|
-
|
|
119
|
-
#### 更新单条数据 \`update\`
|
|
120
|
-
|
|
121
|
-
\`\`\`javascript
|
|
122
|
-
const { data } = await models.${modelName}.update({
|
|
123
|
-
data: {
|
|
124
|
-
${updateDataExample}
|
|
125
|
-
},
|
|
126
|
-
filter: {
|
|
127
|
-
where: {
|
|
128
|
-
_id: {
|
|
129
|
-
$eq: "记录ID", // 推荐传入_id数据标识进行操作
|
|
130
|
-
},
|
|
131
|
-
},
|
|
132
|
-
},
|
|
133
|
-
});
|
|
134
|
-
|
|
135
|
-
// 返回更新成功的条数
|
|
136
|
-
console.log(data);
|
|
137
|
-
// { count: 1}
|
|
138
|
-
\`\`\`
|
|
139
|
-
|
|
140
|
-
#### 创建或更新数据 \`upsert\`
|
|
141
|
-
|
|
142
|
-
\`\`\`javascript
|
|
143
|
-
const recordData = {
|
|
144
|
-
${createDataExample}
|
|
145
|
-
_id: "指定ID",
|
|
146
|
-
};
|
|
147
|
-
|
|
148
|
-
const { data } = await models.${modelName}.upsert({
|
|
149
|
-
create: recordData,
|
|
150
|
-
update: recordData,
|
|
151
|
-
filter: {
|
|
152
|
-
where: {
|
|
153
|
-
_id: {
|
|
154
|
-
$eq: recordData._id,
|
|
155
|
-
},
|
|
156
|
-
},
|
|
157
|
-
},
|
|
158
|
-
});
|
|
159
|
-
|
|
160
|
-
console.log(data);
|
|
161
|
-
// 新增时返回: { "count": 0, "id": "指定ID" }
|
|
162
|
-
// 更新时返回: { "count": 1, "id": "" }
|
|
163
|
-
\`\`\`
|
|
164
|
-
|
|
165
|
-
#### 更新多条数据 \`updateMany\`
|
|
166
|
-
|
|
167
|
-
\`\`\`javascript
|
|
168
|
-
const { data } = await models.${modelName}.updateMany({
|
|
169
|
-
data: {
|
|
170
|
-
${updateDataExample}
|
|
171
|
-
},
|
|
172
|
-
filter: {
|
|
173
|
-
where: {
|
|
174
|
-
${queryExample}
|
|
175
|
-
},
|
|
176
|
-
},
|
|
177
|
-
});
|
|
178
|
-
|
|
179
|
-
// 返回更新成功的条数
|
|
180
|
-
console.log(data);
|
|
181
|
-
// { "count": 5 }
|
|
182
|
-
\`\`\`
|
|
183
|
-
|
|
184
|
-
### 删除数据
|
|
185
|
-
|
|
186
|
-
#### 删除单条 \`delete\`
|
|
187
|
-
|
|
188
|
-
\`\`\`javascript
|
|
189
|
-
const { data } = await models.${modelName}.delete({
|
|
190
|
-
filter: {
|
|
191
|
-
where: {
|
|
192
|
-
_id: {
|
|
193
|
-
$eq: "记录ID", // 推荐传入_id数据标识进行操作
|
|
194
|
-
},
|
|
195
|
-
},
|
|
196
|
-
},
|
|
197
|
-
});
|
|
198
|
-
|
|
199
|
-
// 返回删除成功的条数
|
|
200
|
-
console.log(data);
|
|
201
|
-
// { "count": 1 }
|
|
202
|
-
\`\`\`
|
|
203
|
-
|
|
204
|
-
#### 删除多条 \`deleteMany\`
|
|
205
|
-
|
|
206
|
-
\`\`\`javascript
|
|
207
|
-
const { data } = await models.${modelName}.deleteMany({
|
|
208
|
-
filter: {
|
|
209
|
-
where: {
|
|
210
|
-
${queryExample}
|
|
211
|
-
},
|
|
212
|
-
},
|
|
213
|
-
});
|
|
214
|
-
|
|
215
|
-
// 返回删除成功的条数
|
|
216
|
-
console.log(data);
|
|
217
|
-
// { "count": 3 }
|
|
218
|
-
\`\`\`
|
|
219
|
-
|
|
220
|
-
### 读取数据
|
|
221
|
-
|
|
222
|
-
#### 读取单条数据 \`get\`
|
|
223
|
-
|
|
224
|
-
\`\`\`javascript
|
|
225
|
-
const { data } = await models.${modelName}.get({
|
|
226
|
-
filter: {
|
|
227
|
-
where: {
|
|
228
|
-
_id: {
|
|
229
|
-
$eq: "记录ID", // 推荐传入_id数据标识进行操作
|
|
230
|
-
},
|
|
231
|
-
},
|
|
232
|
-
},
|
|
233
|
-
});
|
|
234
|
-
|
|
235
|
-
// 返回查询到的数据
|
|
236
|
-
console.log(data);
|
|
237
|
-
// {
|
|
238
|
-
// "_id": "记录ID",
|
|
239
|
-
${userFields.slice(0, 5).map(field => `// "${field.name}": ${generateFieldValue(field)}, // ${field.description || field.title || field.name}`).join('\n')}
|
|
240
|
-
// "createdAt": 1717488585078,
|
|
241
|
-
// "updatedAt": 1717490751944
|
|
242
|
-
// }
|
|
243
|
-
\`\`\`
|
|
244
|
-
|
|
245
|
-
#### 读取多条数据 \`list\`
|
|
246
|
-
|
|
247
|
-
\`\`\`javascript
|
|
248
|
-
const { data } = await models.${modelName}.list({
|
|
249
|
-
filter: {
|
|
250
|
-
where: {
|
|
251
|
-
${queryExample}
|
|
252
|
-
},
|
|
253
|
-
},
|
|
254
|
-
getCount: true, // 开启用来获取总数
|
|
255
|
-
});
|
|
256
|
-
|
|
257
|
-
// 返回查询到的数据列表 records 和 总数 total
|
|
258
|
-
console.log(data);
|
|
259
|
-
// {
|
|
260
|
-
// "records": [
|
|
261
|
-
// {
|
|
262
|
-
// "_id": "记录ID1",
|
|
263
|
-
${userFields.slice(0, 3).map(field => `// "${field.name}": ${generateFieldValue(field)}, // ${field.description || field.title || field.name}`).join('\n')}
|
|
264
|
-
// "createdAt": 1717488585078,
|
|
265
|
-
// "updatedAt": 1717490751944
|
|
266
|
-
// },
|
|
267
|
-
// // ... 更多记录
|
|
268
|
-
// ],
|
|
269
|
-
// "total": 10
|
|
270
|
-
// }
|
|
271
|
-
\`\`\`
|
|
272
|
-
|
|
273
|
-
## 查询条件和排序
|
|
274
|
-
|
|
275
|
-
### 常用查询条件
|
|
276
|
-
|
|
277
|
-
\`\`\`javascript
|
|
278
|
-
// 等于查询
|
|
279
|
-
const { data } = await models.${modelName}.list({
|
|
280
|
-
filter: {
|
|
281
|
-
where: {
|
|
282
|
-
${queryField ? ` ${queryField.name}: {
|
|
283
|
-
$eq: ${generateFieldValue(queryField)}, // ${queryField.description || queryField.title || queryField.name}等于指定值
|
|
284
|
-
},` : ' _id: { $eq: "记录ID" },'}
|
|
285
|
-
},
|
|
286
|
-
},
|
|
287
|
-
});
|
|
288
|
-
|
|
289
|
-
${stringFields.length > 0 ? `// 模糊查询
|
|
290
|
-
const { data: searchData } = await models.${modelName}.list({
|
|
291
|
-
filter: {
|
|
292
|
-
where: {
|
|
293
|
-
${stringFields[0].name}: {
|
|
294
|
-
$regex: "关键词", // ${stringFields[0].description || stringFields[0].title || stringFields[0].name}包含关键词
|
|
295
|
-
},
|
|
296
|
-
},
|
|
297
|
-
},
|
|
298
|
-
});` : ''}
|
|
299
|
-
|
|
300
|
-
${numberFields.length > 0 ? `// 范围查询
|
|
301
|
-
const { data: rangeData } = await models.${modelName}.list({
|
|
302
|
-
filter: {
|
|
303
|
-
where: {
|
|
304
|
-
${numberFields[0].name}: {
|
|
305
|
-
$gte: 10, // ${numberFields[0].description || numberFields[0].title || numberFields[0].name}大于等于10
|
|
306
|
-
$lte: 100, // ${numberFields[0].description || numberFields[0].title || numberFields[0].name}小于等于100
|
|
307
|
-
},
|
|
308
|
-
},
|
|
309
|
-
},
|
|
310
|
-
});` : ''}
|
|
311
|
-
\`\`\`
|
|
312
|
-
|
|
313
|
-
### 排序
|
|
314
|
-
|
|
315
|
-
\`\`\`javascript
|
|
316
|
-
const { data } = await models.${modelName}.list({
|
|
317
|
-
filter: {
|
|
318
|
-
where: {},
|
|
319
|
-
orderBy: [
|
|
320
|
-
{
|
|
321
|
-
${mainFields[0] ? `${mainFields[0].name}: "asc", // 按${mainFields[0].description || mainFields[0].title || mainFields[0].name}升序` : '_id: "desc", // 按ID降序'}
|
|
322
|
-
},
|
|
323
|
-
],
|
|
324
|
-
},
|
|
325
|
-
});
|
|
326
|
-
\`\`\`
|
|
327
|
-
|
|
328
|
-
${relations.length > 0 ? `
|
|
329
|
-
## 关联查询
|
|
330
|
-
|
|
331
|
-
${relations.map(rel => `
|
|
332
|
-
### 查询关联的 ${rel.targetModel} 数据
|
|
333
|
-
|
|
334
|
-
\`\`\`javascript
|
|
335
|
-
const { data } = await models.${modelName}.list({
|
|
336
|
-
filter: {
|
|
337
|
-
where: {},
|
|
338
|
-
include: {
|
|
339
|
-
${rel.field}: true, // 包含关联的${rel.targetModel}数据
|
|
340
|
-
},
|
|
341
|
-
},
|
|
342
|
-
});
|
|
343
|
-
|
|
344
|
-
// 返回的数据中会包含关联信息
|
|
345
|
-
console.log(data.records[0].${rel.field});
|
|
346
|
-
\`\`\`
|
|
347
|
-
`).join('')}
|
|
348
|
-
` : ''}
|
|
349
|
-
|
|
350
|
-
## 更多操作
|
|
351
|
-
|
|
352
|
-
更多高级查询、分页、聚合等操作,请参考:
|
|
353
|
-
- [查询和筛选](https://docs.cloudbase.net/model/select)
|
|
354
|
-
- [过滤和排序](https://docs.cloudbase.net/model/filter-and-sort)
|
|
355
|
-
${relations.length > 0 ? '- [关联关系](https://docs.cloudbase.net/model/relation)' : ''}
|
|
356
|
-
`;
|
|
357
|
-
}
|
|
358
|
-
export function registerDatabaseTools(server) {
|
|
359
|
-
// 获取 cloudBaseOptions,如果没有则为 undefined
|
|
360
|
-
const cloudBaseOptions = server.cloudBaseOptions;
|
|
361
|
-
// 创建闭包函数来获取 CloudBase Manager
|
|
362
|
-
const getManager = () => getCloudBaseManager({ cloudBaseOptions });
|
|
363
|
-
// 创建云开发数据库集合
|
|
364
|
-
server.registerTool?.("createCollection", {
|
|
365
|
-
title: "创建数据库集合",
|
|
366
|
-
description: "创建一个新的云开发数据库集合",
|
|
367
|
-
inputSchema: {
|
|
368
|
-
collectionName: z.string().describe("云开发数据库集合名称")
|
|
369
|
-
},
|
|
370
|
-
annotations: {
|
|
371
|
-
readOnlyHint: false,
|
|
372
|
-
destructiveHint: false,
|
|
373
|
-
idempotentHint: false,
|
|
374
|
-
openWorldHint: true,
|
|
375
|
-
category: "database"
|
|
376
|
-
}
|
|
377
|
-
}, async ({ collectionName }) => {
|
|
378
|
-
try {
|
|
379
|
-
const cloudbase = await getManager();
|
|
380
|
-
const result = await cloudbase.database.createCollection(collectionName);
|
|
381
|
-
return {
|
|
382
|
-
content: [
|
|
383
|
-
{
|
|
384
|
-
type: "text",
|
|
385
|
-
text: JSON.stringify({
|
|
386
|
-
success: true,
|
|
387
|
-
requestId: result.RequestId,
|
|
388
|
-
message: "云开发数据库集合创建成功"
|
|
389
|
-
}, null, 2)
|
|
390
|
-
}
|
|
391
|
-
]
|
|
392
|
-
};
|
|
393
|
-
}
|
|
394
|
-
catch (error) {
|
|
395
|
-
return {
|
|
396
|
-
content: [
|
|
397
|
-
{
|
|
398
|
-
type: "text",
|
|
399
|
-
text: JSON.stringify({
|
|
400
|
-
success: false,
|
|
401
|
-
error: error.message,
|
|
402
|
-
message: "云开发数据库集合创建失败"
|
|
403
|
-
}, null, 2)
|
|
404
|
-
}
|
|
405
|
-
]
|
|
406
|
-
};
|
|
407
|
-
}
|
|
408
|
-
});
|
|
409
|
-
// collectionQuery - 集合查询(合并 checkCollectionExists + describeCollection + listCollections)
|
|
410
|
-
server.registerTool?.("collectionQuery", {
|
|
411
|
-
title: "集合查询",
|
|
412
|
-
description: "数据库集合的查询操作,支持检查存在性、查看详情和列表查询",
|
|
413
|
-
inputSchema: {
|
|
414
|
-
action: z.enum(["check", "describe", "list"]).describe("操作类型:check=检查是否存在,describe=查看详情,list=列表查询"),
|
|
415
|
-
collectionName: z.string().optional().describe("集合名称(check、describe操作时必填)"),
|
|
416
|
-
limit: z.number().optional().describe("返回数量限制(list操作时可选)"),
|
|
417
|
-
offset: z.number().optional().describe("偏移量(list操作时可选)")
|
|
418
|
-
},
|
|
419
|
-
annotations: {
|
|
420
|
-
readOnlyHint: true,
|
|
421
|
-
openWorldHint: true,
|
|
422
|
-
category: "database"
|
|
423
|
-
}
|
|
424
|
-
}, async ({ action, collectionName, limit, offset }) => {
|
|
425
|
-
try {
|
|
426
|
-
const cloudbase = await getManager();
|
|
427
|
-
let result;
|
|
428
|
-
switch (action) {
|
|
429
|
-
case "check":
|
|
430
|
-
if (!collectionName) {
|
|
431
|
-
throw new Error("检查集合时必须提供 collectionName");
|
|
432
|
-
}
|
|
433
|
-
result = await cloudbase.database.checkCollectionExists(collectionName);
|
|
434
|
-
return {
|
|
435
|
-
content: [{
|
|
436
|
-
type: "text",
|
|
437
|
-
text: JSON.stringify({
|
|
438
|
-
success: true,
|
|
439
|
-
exists: result.Exists,
|
|
440
|
-
requestId: result.RequestId,
|
|
441
|
-
message: result.Exists ? "云开发数据库集合已存在" : "云开发数据库集合不存在"
|
|
442
|
-
}, null, 2)
|
|
443
|
-
}]
|
|
444
|
-
};
|
|
445
|
-
case "describe":
|
|
446
|
-
if (!collectionName) {
|
|
447
|
-
throw new Error("查看集合详情时必须提供 collectionName");
|
|
448
|
-
}
|
|
449
|
-
result = await cloudbase.database.describeCollection(collectionName);
|
|
450
|
-
return {
|
|
451
|
-
content: [{
|
|
452
|
-
type: "text",
|
|
453
|
-
text: JSON.stringify({
|
|
454
|
-
success: true,
|
|
455
|
-
requestId: result.RequestId,
|
|
456
|
-
indexNum: result.IndexNum,
|
|
457
|
-
indexes: result.Indexes,
|
|
458
|
-
message: "获取云开发数据库集合信息成功"
|
|
459
|
-
}, null, 2)
|
|
460
|
-
}]
|
|
461
|
-
};
|
|
462
|
-
case "list":
|
|
463
|
-
result = await cloudbase.database.listCollections({
|
|
464
|
-
MgoOffset: offset,
|
|
465
|
-
MgoLimit: limit
|
|
466
|
-
});
|
|
467
|
-
return {
|
|
468
|
-
content: [{
|
|
469
|
-
type: "text",
|
|
470
|
-
text: JSON.stringify({
|
|
471
|
-
success: true,
|
|
472
|
-
requestId: result.RequestId,
|
|
473
|
-
collections: result.Collections,
|
|
474
|
-
pager: result.Pager,
|
|
475
|
-
message: "获取云开发数据库集合列表成功"
|
|
476
|
-
}, null, 2)
|
|
477
|
-
}]
|
|
478
|
-
};
|
|
479
|
-
default:
|
|
480
|
-
throw new Error(`不支持的操作类型: ${action}`);
|
|
481
|
-
}
|
|
482
|
-
}
|
|
483
|
-
catch (error) {
|
|
484
|
-
return {
|
|
485
|
-
content: [{
|
|
486
|
-
type: "text",
|
|
487
|
-
text: JSON.stringify({
|
|
488
|
-
success: false,
|
|
489
|
-
error: error.message,
|
|
490
|
-
message: `集合查询失败: ${action}`
|
|
491
|
-
}, null, 2)
|
|
492
|
-
}]
|
|
493
|
-
};
|
|
494
|
-
}
|
|
495
|
-
});
|
|
496
|
-
// // 删除云开发数据库集合
|
|
497
|
-
// server.tool(
|
|
498
|
-
// "deleteCollection",
|
|
499
|
-
// "删除一个云开发数据库集合",
|
|
500
|
-
// {
|
|
501
|
-
// collectionName: z.string().describe("云开发数据库集合名称")
|
|
502
|
-
// },
|
|
503
|
-
// async ({ collectionName }) => {
|
|
504
|
-
// try {
|
|
505
|
-
// const result = await cloudbase.database.deleteCollection(collectionName);
|
|
506
|
-
// return {
|
|
507
|
-
// content: [
|
|
508
|
-
// {
|
|
509
|
-
// type: "text",
|
|
510
|
-
// text: JSON.stringify({
|
|
511
|
-
// success: true,
|
|
512
|
-
// requestId: result.RequestId,
|
|
513
|
-
// exists: result.Exists,
|
|
514
|
-
// message: "云开发数据库集合删除成功"
|
|
515
|
-
// }, null, 2)
|
|
516
|
-
// }
|
|
517
|
-
// ]
|
|
518
|
-
// };
|
|
519
|
-
// } catch (error: any) {
|
|
520
|
-
// return {
|
|
521
|
-
// content: [
|
|
522
|
-
// {
|
|
523
|
-
// type: "text",
|
|
524
|
-
// text: JSON.stringify({
|
|
525
|
-
// success: false,
|
|
526
|
-
// error: error.message,
|
|
527
|
-
// message: "云开发数据库集合删除失败"
|
|
528
|
-
// }, null, 2)
|
|
529
|
-
// }
|
|
530
|
-
// ]
|
|
531
|
-
// };
|
|
532
|
-
// }
|
|
533
|
-
// }
|
|
534
|
-
// );
|
|
535
|
-
// 更新云开发数据库集合(创建/删除索引)
|
|
536
|
-
server.registerTool?.("updateCollection", {
|
|
537
|
-
title: "更新数据库集合",
|
|
538
|
-
description: "更新云开发数据库集合配置(创建或删除索引)",
|
|
539
|
-
inputSchema: {
|
|
540
|
-
collectionName: z.string().describe("云开发数据库集合名称"),
|
|
541
|
-
options: z.object({
|
|
542
|
-
CreateIndexes: z.array(z.object({
|
|
543
|
-
IndexName: z.string(),
|
|
544
|
-
MgoKeySchema: z.object({
|
|
545
|
-
MgoIsUnique: z.boolean(),
|
|
546
|
-
MgoIndexKeys: z.array(z.object({
|
|
547
|
-
Name: z.string(),
|
|
548
|
-
Direction: z.string()
|
|
549
|
-
}))
|
|
550
|
-
})
|
|
551
|
-
})).optional(),
|
|
552
|
-
DropIndexes: z.array(z.object({
|
|
553
|
-
IndexName: z.string()
|
|
554
|
-
})).optional()
|
|
555
|
-
}).describe("更新选项,支持创建和删除索引")
|
|
556
|
-
},
|
|
557
|
-
annotations: {
|
|
558
|
-
readOnlyHint: false,
|
|
559
|
-
destructiveHint: false,
|
|
560
|
-
idempotentHint: false,
|
|
561
|
-
openWorldHint: true,
|
|
562
|
-
category: "database"
|
|
563
|
-
}
|
|
564
|
-
}, async ({ collectionName, options }) => {
|
|
565
|
-
try {
|
|
566
|
-
const cloudbase = await getManager();
|
|
567
|
-
const result = await cloudbase.database.updateCollection(collectionName, options);
|
|
568
|
-
return {
|
|
569
|
-
content: [
|
|
570
|
-
{
|
|
571
|
-
type: "text",
|
|
572
|
-
text: JSON.stringify({
|
|
573
|
-
success: true,
|
|
574
|
-
requestId: result.RequestId,
|
|
575
|
-
message: "云开发数据库集合更新成功"
|
|
576
|
-
}, null, 2)
|
|
577
|
-
}
|
|
578
|
-
]
|
|
579
|
-
};
|
|
580
|
-
}
|
|
581
|
-
catch (error) {
|
|
582
|
-
return {
|
|
583
|
-
content: [
|
|
584
|
-
{
|
|
585
|
-
type: "text",
|
|
586
|
-
text: JSON.stringify({
|
|
587
|
-
success: false,
|
|
588
|
-
error: error.message,
|
|
589
|
-
message: "云开发数据库集合更新失败"
|
|
590
|
-
}, null, 2)
|
|
591
|
-
}
|
|
592
|
-
]
|
|
593
|
-
};
|
|
594
|
-
}
|
|
595
|
-
});
|
|
596
|
-
// 检查索引是否存在
|
|
597
|
-
server.registerTool?.("checkIndexExists", {
|
|
598
|
-
title: "检查索引是否存在",
|
|
599
|
-
description: "检查索引是否存在",
|
|
600
|
-
inputSchema: {
|
|
601
|
-
collectionName: z.string().describe("云开发数据库集合名称"),
|
|
602
|
-
indexName: z.string().describe("索引名称")
|
|
603
|
-
},
|
|
604
|
-
annotations: {
|
|
605
|
-
readOnlyHint: true,
|
|
606
|
-
openWorldHint: true,
|
|
607
|
-
category: "database"
|
|
608
|
-
}
|
|
609
|
-
}, async ({ collectionName, indexName }) => {
|
|
610
|
-
try {
|
|
611
|
-
const cloudbase = await getManager();
|
|
612
|
-
const result = await cloudbase.database.checkIndexExists(collectionName, indexName);
|
|
613
|
-
return {
|
|
614
|
-
content: [
|
|
615
|
-
{
|
|
616
|
-
type: "text",
|
|
617
|
-
text: JSON.stringify({
|
|
618
|
-
success: true,
|
|
619
|
-
exists: result.Exists,
|
|
620
|
-
requestId: result.RequestId,
|
|
621
|
-
message: result.Exists ? "索引已存在" : "索引不存在"
|
|
622
|
-
}, null, 2)
|
|
623
|
-
}
|
|
624
|
-
]
|
|
625
|
-
};
|
|
626
|
-
}
|
|
627
|
-
catch (error) {
|
|
628
|
-
return {
|
|
629
|
-
content: [
|
|
630
|
-
{
|
|
631
|
-
type: "text",
|
|
632
|
-
text: JSON.stringify({
|
|
633
|
-
success: false,
|
|
634
|
-
error: error.message,
|
|
635
|
-
message: "检查索引失败"
|
|
636
|
-
}, null, 2)
|
|
637
|
-
}
|
|
638
|
-
]
|
|
639
|
-
};
|
|
640
|
-
}
|
|
641
|
-
});
|
|
642
|
-
// // 导入数据
|
|
643
|
-
// server.tool(
|
|
644
|
-
// "importData",
|
|
645
|
-
// "导入数据到云开发数据库集合中",
|
|
646
|
-
// {
|
|
647
|
-
// collectionName: z.string().describe("云开发数据库集合名称"),
|
|
648
|
-
// file: z.object({
|
|
649
|
-
// ObjectKey: z.string().optional().describe("对象存储中的文件路径"),
|
|
650
|
-
// FilePath: z.string().optional().describe("本地文件路径")
|
|
651
|
-
// }).describe("导入文件信息"),
|
|
652
|
-
// options: z.object({
|
|
653
|
-
// FileType: z.enum(["csv", "json"]).optional().describe("文件类型"),
|
|
654
|
-
// StopOnError: z.boolean().optional().describe("遇到错误时是否停止导入"),
|
|
655
|
-
// ConflictMode: z.enum(["insert", "upsert"]).optional().describe("冲突处理方式")
|
|
656
|
-
// }).optional().describe("导入选项")
|
|
657
|
-
// },
|
|
658
|
-
// async ({ collectionName, file, options }) => {
|
|
659
|
-
// try {
|
|
660
|
-
// const result = await cloudbase.database.import(collectionName, file, options);
|
|
661
|
-
// return {
|
|
662
|
-
// content: [
|
|
663
|
-
// {
|
|
664
|
-
// type: "text",
|
|
665
|
-
// text: JSON.stringify({
|
|
666
|
-
// success: true,
|
|
667
|
-
// requestId: result.RequestId,
|
|
668
|
-
// jobId: result.JobId,
|
|
669
|
-
// message: "数据导入任务创建成功"
|
|
670
|
-
// }, null, 2)
|
|
671
|
-
// }
|
|
672
|
-
// ]
|
|
673
|
-
// };
|
|
674
|
-
// } catch (error: any) {
|
|
675
|
-
// return {
|
|
676
|
-
// content: [
|
|
677
|
-
// {
|
|
678
|
-
// type: "text",
|
|
679
|
-
// text: JSON.stringify({
|
|
680
|
-
// success: false,
|
|
681
|
-
// error: error.message,
|
|
682
|
-
// message: "数据导入任务创建失败"
|
|
683
|
-
// }, null, 2)
|
|
684
|
-
// }
|
|
685
|
-
// ]
|
|
686
|
-
// };
|
|
687
|
-
// }
|
|
688
|
-
// }
|
|
689
|
-
// );
|
|
690
|
-
// // 导出数据
|
|
691
|
-
// server.tool(
|
|
692
|
-
// "exportData",
|
|
693
|
-
// "从云开发数据库集合中导出数据",
|
|
694
|
-
// {
|
|
695
|
-
// collectionName: z.string().describe("云开发数据库集合名称"),
|
|
696
|
-
// file: z.object({
|
|
697
|
-
// ObjectKey: z.string().describe("导出到对象存储的文件路径")
|
|
698
|
-
// }).describe("导出文件信息"),
|
|
699
|
-
// options: z.object({
|
|
700
|
-
// FileType: z.enum(["csv", "json"]).optional().describe("文件类型"),
|
|
701
|
-
// Query: z.string().optional().describe("查询条件(JSON字符串)"),
|
|
702
|
-
// Sort: z.string().optional().describe("排序条件(JSON字符串)"),
|
|
703
|
-
// Skip: z.number().optional().describe("跳过的记录数"),
|
|
704
|
-
// Limit: z.number().optional().describe("限制返回的记录数"),
|
|
705
|
-
// Fields: z.string().optional().describe("导出字段列表,以逗号分隔")
|
|
706
|
-
// }).optional().describe("导出选项")
|
|
707
|
-
// },
|
|
708
|
-
// async ({ collectionName, file, options }) => {
|
|
709
|
-
// try {
|
|
710
|
-
// const result = await cloudbase.database.export(collectionName, file, options);
|
|
711
|
-
// return {
|
|
712
|
-
// content: [
|
|
713
|
-
// {
|
|
714
|
-
// type: "text",
|
|
715
|
-
// text: JSON.stringify({
|
|
716
|
-
// success: true,
|
|
717
|
-
// requestId: result.RequestId,
|
|
718
|
-
// jobId: result.JobId,
|
|
719
|
-
// message: "数据导出任务创建成功"
|
|
720
|
-
// }, null, 2)
|
|
721
|
-
// }
|
|
722
|
-
// ]
|
|
723
|
-
// };
|
|
724
|
-
// } catch (error: any) {
|
|
725
|
-
// return {
|
|
726
|
-
// content: [
|
|
727
|
-
// {
|
|
728
|
-
// type: "text",
|
|
729
|
-
// text: JSON.stringify({
|
|
730
|
-
// success: false,
|
|
731
|
-
// error: error.message,
|
|
732
|
-
// message: "数据导出任务创建失败"
|
|
733
|
-
// }, null, 2)
|
|
734
|
-
// }
|
|
735
|
-
// ]
|
|
736
|
-
// };
|
|
737
|
-
// }
|
|
738
|
-
// }
|
|
739
|
-
// );
|
|
740
|
-
// // 查询迁移状态
|
|
741
|
-
// server.tool(
|
|
742
|
-
// "migrateStatus",
|
|
743
|
-
// "查询数据迁移(导入/导出)任务的状态",
|
|
744
|
-
// {
|
|
745
|
-
// jobId: z.number().describe("任务ID")
|
|
746
|
-
// },
|
|
747
|
-
// async ({ jobId }) => {
|
|
748
|
-
// try {
|
|
749
|
-
// const result = await cloudbase.database.migrateStatus(jobId);
|
|
750
|
-
// return {
|
|
751
|
-
// content: [
|
|
752
|
-
// {
|
|
753
|
-
// type: "text",
|
|
754
|
-
// text: JSON.stringify({
|
|
755
|
-
// success: true,
|
|
756
|
-
// requestId: result.RequestId,
|
|
757
|
-
// status: result.Status,
|
|
758
|
-
// recordSuccess: result.RecordSuccess,
|
|
759
|
-
// recordFail: result.RecordFail,
|
|
760
|
-
// errorMsg: result.ErrorMsg,
|
|
761
|
-
// fileUrl: result.FileUrl,
|
|
762
|
-
// message: "获取迁移状态成功"
|
|
763
|
-
// }, null, 2)
|
|
764
|
-
// }
|
|
765
|
-
// ]
|
|
766
|
-
// };
|
|
767
|
-
// } catch (error: any) {
|
|
768
|
-
// return {
|
|
769
|
-
// content: [
|
|
770
|
-
// {
|
|
771
|
-
// type: "text",
|
|
772
|
-
// text: JSON.stringify({
|
|
773
|
-
// success: false,
|
|
774
|
-
// error: error.message,
|
|
775
|
-
// message: "获取迁移状态失败"
|
|
776
|
-
// }, null, 2)
|
|
777
|
-
// }
|
|
778
|
-
// ]
|
|
779
|
-
// };
|
|
780
|
-
// }
|
|
781
|
-
// }
|
|
782
|
-
// );
|
|
783
|
-
// 查询数据分布
|
|
784
|
-
server.registerTool?.("distribution", {
|
|
785
|
-
title: "查询数据分布",
|
|
786
|
-
description: "查询数据库中云开发数据库集合的数据分布情况",
|
|
787
|
-
inputSchema: {},
|
|
788
|
-
annotations: {
|
|
789
|
-
readOnlyHint: true,
|
|
790
|
-
openWorldHint: true,
|
|
791
|
-
category: "database"
|
|
792
|
-
}
|
|
793
|
-
}, async () => {
|
|
794
|
-
try {
|
|
795
|
-
const cloudbase = await getManager();
|
|
796
|
-
const result = await cloudbase.database.distribution();
|
|
797
|
-
return {
|
|
798
|
-
content: [
|
|
799
|
-
{
|
|
800
|
-
type: "text",
|
|
801
|
-
text: JSON.stringify({
|
|
802
|
-
success: true,
|
|
803
|
-
requestId: result.RequestId,
|
|
804
|
-
collections: result.Collections,
|
|
805
|
-
message: "获取数据分布成功"
|
|
806
|
-
}, null, 2)
|
|
807
|
-
}
|
|
808
|
-
]
|
|
809
|
-
};
|
|
810
|
-
}
|
|
811
|
-
catch (error) {
|
|
812
|
-
return {
|
|
813
|
-
content: [
|
|
814
|
-
{
|
|
815
|
-
type: "text",
|
|
816
|
-
text: JSON.stringify({
|
|
817
|
-
success: false,
|
|
818
|
-
error: error.message,
|
|
819
|
-
message: "获取数据分布失败"
|
|
820
|
-
}, null, 2)
|
|
821
|
-
}
|
|
822
|
-
]
|
|
823
|
-
};
|
|
824
|
-
}
|
|
825
|
-
});
|
|
826
|
-
// 插入文档
|
|
827
|
-
server.registerTool?.("insertDocuments", {
|
|
828
|
-
title: "插入文档",
|
|
829
|
-
description: "向云开发数据库集合中插入一个或多个文档",
|
|
830
|
-
inputSchema: {
|
|
831
|
-
collectionName: z.string().describe("云开发数据库集合名称"),
|
|
832
|
-
documents: z.array(z.string()).describe("要插入的文档JSON 字符串数组,每个文档都是 JSON字符串,注意不是JSON对象")
|
|
833
|
-
},
|
|
834
|
-
annotations: {
|
|
835
|
-
readOnlyHint: false,
|
|
836
|
-
destructiveHint: false,
|
|
837
|
-
idempotentHint: false,
|
|
838
|
-
openWorldHint: true,
|
|
839
|
-
category: "database"
|
|
840
|
-
}
|
|
841
|
-
}, async ({ collectionName, documents }) => {
|
|
842
|
-
try {
|
|
843
|
-
const cloudbase = await getManager();
|
|
844
|
-
const instanceId = await getDatabaseInstanceId(getManager);
|
|
845
|
-
const result = await cloudbase.commonService('flexdb').call({
|
|
846
|
-
Action: 'PutItem',
|
|
847
|
-
Param: {
|
|
848
|
-
TableName: collectionName,
|
|
849
|
-
MgoDocs: documents,
|
|
850
|
-
Tag: instanceId
|
|
851
|
-
}
|
|
852
|
-
});
|
|
853
|
-
return {
|
|
854
|
-
content: [
|
|
855
|
-
{
|
|
856
|
-
type: "text",
|
|
857
|
-
text: JSON.stringify({
|
|
858
|
-
success: true,
|
|
859
|
-
requestId: result.RequestId,
|
|
860
|
-
insertedIds: result.InsertedIds,
|
|
861
|
-
message: "文档插入成功"
|
|
862
|
-
}, null, 2)
|
|
863
|
-
}
|
|
864
|
-
]
|
|
865
|
-
};
|
|
866
|
-
}
|
|
867
|
-
catch (error) {
|
|
868
|
-
return {
|
|
869
|
-
content: [
|
|
870
|
-
{
|
|
871
|
-
type: "text",
|
|
872
|
-
text: JSON.stringify({
|
|
873
|
-
success: false,
|
|
874
|
-
error: error.message,
|
|
875
|
-
message: "文档插入失败"
|
|
876
|
-
}, null, 2)
|
|
877
|
-
}
|
|
878
|
-
]
|
|
879
|
-
};
|
|
880
|
-
}
|
|
881
|
-
});
|
|
882
|
-
// 查询文档
|
|
883
|
-
server.registerTool?.("queryDocuments", {
|
|
884
|
-
title: "查询文档",
|
|
885
|
-
description: "查询云开发数据库集合中的文档",
|
|
886
|
-
inputSchema: {
|
|
887
|
-
collectionName: z.string().describe("云开发数据库集合名称"),
|
|
888
|
-
query: z.string().optional().describe("查询条件(JSON字符串)"),
|
|
889
|
-
projection: z.string().optional().describe("返回字段投影(JSON字符串)"),
|
|
890
|
-
sort: z.string().optional().describe("排序条件(JSON字符串)"),
|
|
891
|
-
limit: z.number().optional().describe("返回数量限制"),
|
|
892
|
-
offset: z.number().optional().describe("跳过的记录数")
|
|
893
|
-
},
|
|
894
|
-
annotations: {
|
|
895
|
-
readOnlyHint: true,
|
|
896
|
-
openWorldHint: true,
|
|
897
|
-
category: "database"
|
|
898
|
-
}
|
|
899
|
-
}, async ({ collectionName, query, projection, sort, limit, offset }) => {
|
|
900
|
-
try {
|
|
901
|
-
const cloudbase = await getManager();
|
|
902
|
-
const instanceId = await getDatabaseInstanceId(getManager);
|
|
903
|
-
const result = await cloudbase.commonService('flexdb').call({
|
|
904
|
-
Action: 'Query',
|
|
905
|
-
Param: {
|
|
906
|
-
TableName: collectionName,
|
|
907
|
-
MgoQuery: query,
|
|
908
|
-
MgoProjection: projection,
|
|
909
|
-
MgoSort: sort,
|
|
910
|
-
MgoLimit: limit,
|
|
911
|
-
MgoOffset: offset,
|
|
912
|
-
Tag: instanceId
|
|
913
|
-
}
|
|
914
|
-
});
|
|
915
|
-
return {
|
|
916
|
-
content: [
|
|
917
|
-
{
|
|
918
|
-
type: "text",
|
|
919
|
-
text: JSON.stringify({
|
|
920
|
-
success: true,
|
|
921
|
-
requestId: result.RequestId,
|
|
922
|
-
data: result.Data,
|
|
923
|
-
pager: result.Pager,
|
|
924
|
-
message: "文档查询成功"
|
|
925
|
-
}, null, 2)
|
|
926
|
-
}
|
|
927
|
-
]
|
|
928
|
-
};
|
|
929
|
-
}
|
|
930
|
-
catch (error) {
|
|
931
|
-
return {
|
|
932
|
-
content: [
|
|
933
|
-
{
|
|
934
|
-
type: "text",
|
|
935
|
-
text: JSON.stringify({
|
|
936
|
-
success: false,
|
|
937
|
-
error: error.message,
|
|
938
|
-
message: "文档查询失败"
|
|
939
|
-
}, null, 2)
|
|
940
|
-
}
|
|
941
|
-
]
|
|
942
|
-
};
|
|
943
|
-
}
|
|
944
|
-
});
|
|
945
|
-
// 更新文档
|
|
946
|
-
server.registerTool?.("updateDocuments", {
|
|
947
|
-
title: "更新文档",
|
|
948
|
-
description: "更新云开发数据库集合中的文档",
|
|
949
|
-
inputSchema: {
|
|
950
|
-
collectionName: z.string().describe("云开发数据库集合名称"),
|
|
951
|
-
query: z.string().describe("查询条件(JSON字符串)"),
|
|
952
|
-
update: z.string().describe("更新内容(JSON字符串)"),
|
|
953
|
-
isMulti: z.boolean().optional().describe("是否更新多条记录"),
|
|
954
|
-
upsert: z.boolean().optional().describe("是否在不存在时插入")
|
|
955
|
-
},
|
|
956
|
-
annotations: {
|
|
957
|
-
readOnlyHint: false,
|
|
958
|
-
destructiveHint: false,
|
|
959
|
-
idempotentHint: false,
|
|
960
|
-
openWorldHint: true,
|
|
961
|
-
category: "database"
|
|
962
|
-
}
|
|
963
|
-
}, async ({ collectionName, query, update, isMulti, upsert }) => {
|
|
964
|
-
try {
|
|
965
|
-
const cloudbase = await getManager();
|
|
966
|
-
const instanceId = await getDatabaseInstanceId(getManager);
|
|
967
|
-
const result = await cloudbase.commonService('flexdb').call({
|
|
968
|
-
Action: 'UpdateItem',
|
|
969
|
-
Param: {
|
|
970
|
-
TableName: collectionName,
|
|
971
|
-
MgoQuery: query,
|
|
972
|
-
MgoUpdate: update,
|
|
973
|
-
MgoIsMulti: isMulti,
|
|
974
|
-
MgoUpsert: upsert,
|
|
975
|
-
Tag: instanceId
|
|
976
|
-
}
|
|
977
|
-
});
|
|
978
|
-
return {
|
|
979
|
-
content: [
|
|
980
|
-
{
|
|
981
|
-
type: "text",
|
|
982
|
-
text: JSON.stringify({
|
|
983
|
-
success: true,
|
|
984
|
-
requestId: result.RequestId,
|
|
985
|
-
modifiedCount: result.ModifiedNum,
|
|
986
|
-
matchedCount: result.MatchedNum,
|
|
987
|
-
upsertedId: result.UpsertedId,
|
|
988
|
-
message: "文档更新成功"
|
|
989
|
-
}, null, 2)
|
|
990
|
-
}
|
|
991
|
-
]
|
|
992
|
-
};
|
|
993
|
-
}
|
|
994
|
-
catch (error) {
|
|
995
|
-
return {
|
|
996
|
-
content: [
|
|
997
|
-
{
|
|
998
|
-
type: "text",
|
|
999
|
-
text: JSON.stringify({
|
|
1000
|
-
success: false,
|
|
1001
|
-
error: error.message,
|
|
1002
|
-
message: "文档更新失败"
|
|
1003
|
-
}, null, 2)
|
|
1004
|
-
}
|
|
1005
|
-
]
|
|
1006
|
-
};
|
|
1007
|
-
}
|
|
1008
|
-
});
|
|
1009
|
-
// 删除文档
|
|
1010
|
-
server.registerTool?.("deleteDocuments", {
|
|
1011
|
-
title: "删除文档",
|
|
1012
|
-
description: "删除云开发数据库集合中的文档",
|
|
1013
|
-
inputSchema: {
|
|
1014
|
-
collectionName: z.string().describe("云开发数据库集合名称"),
|
|
1015
|
-
query: z.string().describe("查询条件(JSON字符串)"),
|
|
1016
|
-
isMulti: z.boolean().optional().describe("是否删除多条记录")
|
|
1017
|
-
},
|
|
1018
|
-
annotations: {
|
|
1019
|
-
readOnlyHint: false,
|
|
1020
|
-
destructiveHint: true,
|
|
1021
|
-
idempotentHint: true,
|
|
1022
|
-
openWorldHint: true,
|
|
1023
|
-
category: "database"
|
|
1024
|
-
}
|
|
1025
|
-
}, async ({ collectionName, query, isMulti }) => {
|
|
1026
|
-
try {
|
|
1027
|
-
const cloudbase = await getManager();
|
|
1028
|
-
const instanceId = await getDatabaseInstanceId(getManager);
|
|
1029
|
-
const result = await cloudbase.commonService('flexdb').call({
|
|
1030
|
-
Action: 'DeleteItem',
|
|
1031
|
-
Param: {
|
|
1032
|
-
TableName: collectionName,
|
|
1033
|
-
MgoQuery: query,
|
|
1034
|
-
MgoIsMulti: isMulti,
|
|
1035
|
-
Tag: instanceId
|
|
1036
|
-
}
|
|
1037
|
-
});
|
|
1038
|
-
return {
|
|
1039
|
-
content: [
|
|
1040
|
-
{
|
|
1041
|
-
type: "text",
|
|
1042
|
-
text: JSON.stringify({
|
|
1043
|
-
success: true,
|
|
1044
|
-
requestId: result.RequestId,
|
|
1045
|
-
deleted: result.Deleted,
|
|
1046
|
-
message: "文档删除成功"
|
|
1047
|
-
}, null, 2)
|
|
1048
|
-
}
|
|
1049
|
-
]
|
|
1050
|
-
};
|
|
1051
|
-
}
|
|
1052
|
-
catch (error) {
|
|
1053
|
-
return {
|
|
1054
|
-
content: [
|
|
1055
|
-
{
|
|
1056
|
-
type: "text",
|
|
1057
|
-
text: JSON.stringify({
|
|
1058
|
-
success: false,
|
|
1059
|
-
error: error.message,
|
|
1060
|
-
message: "文档删除失败"
|
|
1061
|
-
}, null, 2)
|
|
1062
|
-
}
|
|
1063
|
-
]
|
|
1064
|
-
};
|
|
1065
|
-
}
|
|
1066
|
-
});
|
|
1067
|
-
// 数据模型查询工具
|
|
1068
|
-
server.registerTool?.("manageDataModel", {
|
|
1069
|
-
title: "数据模型管理",
|
|
1070
|
-
description: "数据模型查询工具,支持查询和列表数据模型(只读操作)。list操作返回基础信息(不含Schema),get操作返回详细信息(含简化的Schema,包括字段列表、格式、关联关系等),docs操作生成SDK使用文档",
|
|
1071
|
-
inputSchema: {
|
|
1072
|
-
action: z.enum(["get", "list", "docs"]).describe("操作类型:get=查询单个模型(含Schema字段列表、格式、关联关系),list=获取模型列表(不含Schema),docs=生成SDK使用文档"),
|
|
1073
|
-
name: z.string().optional().describe("模型名称(get操作时必填)"),
|
|
1074
|
-
names: z.array(z.string()).optional().describe("模型名称数组(list操作时可选,用于过滤)")
|
|
1075
|
-
},
|
|
1076
|
-
annotations: {
|
|
1077
|
-
readOnlyHint: true,
|
|
1078
|
-
openWorldHint: true,
|
|
1079
|
-
category: "database"
|
|
1080
|
-
}
|
|
1081
|
-
}, async ({ action, name, names }) => {
|
|
1082
|
-
try {
|
|
1083
|
-
const cloudbase = await getManager();
|
|
1084
|
-
let currentEnvId = await getEnvId(cloudBaseOptions);
|
|
1085
|
-
let result;
|
|
1086
|
-
switch (action) {
|
|
1087
|
-
case 'get':
|
|
1088
|
-
if (!name) {
|
|
1089
|
-
throw new Error('获取数据模型需要提供模型名称');
|
|
1090
|
-
}
|
|
1091
|
-
try {
|
|
1092
|
-
result = await cloudbase.commonService('lowcode').call({
|
|
1093
|
-
Action: 'DescribeBasicDataSource',
|
|
1094
|
-
Param: {
|
|
1095
|
-
EnvId: currentEnvId,
|
|
1096
|
-
Name: name
|
|
1097
|
-
}
|
|
1098
|
-
});
|
|
1099
|
-
// 只保留基础字段,过滤掉冗余信息,并简化Schema
|
|
1100
|
-
let simplifiedSchema = null;
|
|
1101
|
-
// 解析并简化Schema
|
|
1102
|
-
if (result.Data.Schema) {
|
|
1103
|
-
try {
|
|
1104
|
-
const schema = JSON.parse(result.Data.Schema);
|
|
1105
|
-
const properties = schema.properties || {};
|
|
1106
|
-
// 提取用户定义的字段(排除系统字段)
|
|
1107
|
-
const userFields = Object.keys(properties)
|
|
1108
|
-
.filter(key => !properties[key]['x-system']) // 排除系统字段
|
|
1109
|
-
.map(key => {
|
|
1110
|
-
const field = properties[key];
|
|
1111
|
-
const fieldInfo = {
|
|
1112
|
-
name: key,
|
|
1113
|
-
type: field.type,
|
|
1114
|
-
format: field.format,
|
|
1115
|
-
title: field.title || key,
|
|
1116
|
-
required: schema.required?.includes(key) || false,
|
|
1117
|
-
description: field.description || ''
|
|
1118
|
-
};
|
|
1119
|
-
if (field['x-parent']) {
|
|
1120
|
-
fieldInfo.linkage = field['x-parent'];
|
|
1121
|
-
}
|
|
1122
|
-
return fieldInfo;
|
|
1123
|
-
});
|
|
1124
|
-
// 提取关联关系
|
|
1125
|
-
const relations = userFields
|
|
1126
|
-
.filter(field => field.linkage)
|
|
1127
|
-
.map(field => ({
|
|
1128
|
-
field: field.name,
|
|
1129
|
-
type: field.format,
|
|
1130
|
-
title: field.title,
|
|
1131
|
-
targetModel: field.linkage.parentDataSourceName,
|
|
1132
|
-
foreignKey: field.linkage.parentFieldKey,
|
|
1133
|
-
displayField: field.linkage.parentFieldTitle
|
|
1134
|
-
}));
|
|
1135
|
-
simplifiedSchema = {
|
|
1136
|
-
userFields,
|
|
1137
|
-
relations,
|
|
1138
|
-
totalFields: Object.keys(properties).length,
|
|
1139
|
-
userFieldsCount: userFields.length
|
|
1140
|
-
};
|
|
1141
|
-
}
|
|
1142
|
-
catch (e) {
|
|
1143
|
-
simplifiedSchema = { error: 'Schema解析失败' };
|
|
1144
|
-
}
|
|
1145
|
-
}
|
|
1146
|
-
const simplifiedModel = {
|
|
1147
|
-
DbInstanceType: result.Data.DbInstanceType,
|
|
1148
|
-
Title: result.Data.Title,
|
|
1149
|
-
Description: result.Data.Description,
|
|
1150
|
-
Name: result.Data.Name,
|
|
1151
|
-
UpdatedAt: result.Data.UpdatedAt,
|
|
1152
|
-
Schema: simplifiedSchema
|
|
1153
|
-
};
|
|
1154
|
-
return {
|
|
1155
|
-
content: [{
|
|
1156
|
-
type: "text",
|
|
1157
|
-
text: JSON.stringify({
|
|
1158
|
-
success: true,
|
|
1159
|
-
action: 'get',
|
|
1160
|
-
data: simplifiedModel,
|
|
1161
|
-
message: "获取数据模型成功"
|
|
1162
|
-
}, null, 2)
|
|
1163
|
-
}]
|
|
1164
|
-
};
|
|
1165
|
-
}
|
|
1166
|
-
catch (error) {
|
|
1167
|
-
if (error.original?.Code === 'ResourceNotFound') {
|
|
1168
|
-
return {
|
|
1169
|
-
content: [{
|
|
1170
|
-
type: "text",
|
|
1171
|
-
text: JSON.stringify({
|
|
1172
|
-
success: false,
|
|
1173
|
-
action: 'get',
|
|
1174
|
-
error: 'ResourceNotFound',
|
|
1175
|
-
message: `数据模型 ${name} 不存在`
|
|
1176
|
-
}, null, 2)
|
|
1177
|
-
}]
|
|
1178
|
-
};
|
|
1179
|
-
}
|
|
1180
|
-
throw error;
|
|
1181
|
-
}
|
|
1182
|
-
case 'list':
|
|
1183
|
-
// 构建请求参数
|
|
1184
|
-
const listParams = {
|
|
1185
|
-
EnvId: currentEnvId,
|
|
1186
|
-
PageIndex: 1,
|
|
1187
|
-
PageSize: 1000,
|
|
1188
|
-
QuerySystemModel: true, // 查询系统模型
|
|
1189
|
-
QueryConnector: 0 // 0 表示数据模型
|
|
1190
|
-
};
|
|
1191
|
-
// 只有当 names 参数存在且不为空时才添加过滤条件
|
|
1192
|
-
if (names && names.length > 0) {
|
|
1193
|
-
listParams.DataSourceNames = names;
|
|
1194
|
-
}
|
|
1195
|
-
result = await cloudbase.commonService('lowcode').call({
|
|
1196
|
-
Action: 'DescribeDataSourceList',
|
|
1197
|
-
Param: listParams
|
|
1198
|
-
});
|
|
1199
|
-
const models = result.Data?.Rows || [];
|
|
1200
|
-
// 只保留基础字段,list操作不返回Schema
|
|
1201
|
-
const simplifiedModels = models.map((model) => ({
|
|
1202
|
-
DbInstanceType: model.DbInstanceType,
|
|
1203
|
-
Title: model.Title,
|
|
1204
|
-
Description: model.Description,
|
|
1205
|
-
Name: model.Name,
|
|
1206
|
-
UpdatedAt: model.UpdatedAt
|
|
1207
|
-
}));
|
|
1208
|
-
return {
|
|
1209
|
-
content: [{
|
|
1210
|
-
type: "text",
|
|
1211
|
-
text: JSON.stringify({
|
|
1212
|
-
success: true,
|
|
1213
|
-
action: 'list',
|
|
1214
|
-
data: simplifiedModels,
|
|
1215
|
-
count: simplifiedModels.length,
|
|
1216
|
-
message: "获取数据模型列表成功"
|
|
1217
|
-
}, null, 2)
|
|
1218
|
-
}]
|
|
1219
|
-
};
|
|
1220
|
-
case 'docs':
|
|
1221
|
-
if (!name) {
|
|
1222
|
-
throw new Error('生成SDK文档需要提供模型名称');
|
|
1223
|
-
}
|
|
1224
|
-
try {
|
|
1225
|
-
// 先获取模型信息
|
|
1226
|
-
result = await cloudbase.commonService('lowcode').call({
|
|
1227
|
-
Action: 'DescribeBasicDataSource',
|
|
1228
|
-
Param: {
|
|
1229
|
-
EnvId: currentEnvId,
|
|
1230
|
-
Name: name
|
|
1231
|
-
}
|
|
1232
|
-
});
|
|
1233
|
-
if (!result.Data) {
|
|
1234
|
-
throw new Error(`数据模型 ${name} 不存在`);
|
|
1235
|
-
}
|
|
1236
|
-
// 解析Schema获取字段信息
|
|
1237
|
-
let userFields = [];
|
|
1238
|
-
let relations = [];
|
|
1239
|
-
if (result.Data.Schema) {
|
|
1240
|
-
try {
|
|
1241
|
-
const schema = JSON.parse(result.Data.Schema);
|
|
1242
|
-
const properties = schema.properties || {};
|
|
1243
|
-
// 提取用户定义的字段
|
|
1244
|
-
userFields = Object.keys(properties)
|
|
1245
|
-
.filter(key => !properties[key]['x-system'])
|
|
1246
|
-
.map(key => {
|
|
1247
|
-
const field = properties[key];
|
|
1248
|
-
return {
|
|
1249
|
-
name: key,
|
|
1250
|
-
type: field.type,
|
|
1251
|
-
title: field.title || key,
|
|
1252
|
-
required: schema.required?.includes(key) || false,
|
|
1253
|
-
description: field.description || '',
|
|
1254
|
-
format: field.format,
|
|
1255
|
-
enum: field.enum,
|
|
1256
|
-
default: field.default,
|
|
1257
|
-
linkage: field['x-parent']
|
|
1258
|
-
};
|
|
1259
|
-
});
|
|
1260
|
-
// 提取关联关系
|
|
1261
|
-
relations = userFields
|
|
1262
|
-
.filter(field => field.linkage)
|
|
1263
|
-
.map(field => ({
|
|
1264
|
-
field: field.name,
|
|
1265
|
-
type: field.format,
|
|
1266
|
-
title: field.title,
|
|
1267
|
-
targetModel: field.linkage.parentDataSourceName,
|
|
1268
|
-
foreignKey: field.linkage.parentFieldKey,
|
|
1269
|
-
displayField: field.linkage.parentFieldTitle
|
|
1270
|
-
}));
|
|
1271
|
-
}
|
|
1272
|
-
catch (e) {
|
|
1273
|
-
// Schema解析失败,使用空数组
|
|
1274
|
-
console.error('Schema解析失败', e);
|
|
1275
|
-
}
|
|
1276
|
-
}
|
|
1277
|
-
// 生成SDK使用文档
|
|
1278
|
-
const docs = generateSDKDocs(result.Data.Name, result.Data.Title, userFields, relations);
|
|
1279
|
-
return {
|
|
1280
|
-
content: [{
|
|
1281
|
-
type: "text",
|
|
1282
|
-
text: JSON.stringify({
|
|
1283
|
-
success: true,
|
|
1284
|
-
action: 'docs',
|
|
1285
|
-
modelName: name,
|
|
1286
|
-
modelTitle: result.Data.Title,
|
|
1287
|
-
docs,
|
|
1288
|
-
message: "SDK使用文档生成成功"
|
|
1289
|
-
}, null, 2)
|
|
1290
|
-
}]
|
|
1291
|
-
};
|
|
1292
|
-
}
|
|
1293
|
-
catch (error) {
|
|
1294
|
-
if (error.original?.Code === 'ResourceNotFound') {
|
|
1295
|
-
return {
|
|
1296
|
-
content: [{
|
|
1297
|
-
type: "text",
|
|
1298
|
-
text: JSON.stringify({
|
|
1299
|
-
success: false,
|
|
1300
|
-
action: 'docs',
|
|
1301
|
-
error: 'ResourceNotFound',
|
|
1302
|
-
message: `数据模型 ${name} 不存在`
|
|
1303
|
-
}, null, 2)
|
|
1304
|
-
}]
|
|
1305
|
-
};
|
|
1306
|
-
}
|
|
1307
|
-
throw error;
|
|
1308
|
-
}
|
|
1309
|
-
default:
|
|
1310
|
-
throw new Error(`不支持的操作类型: ${action}`);
|
|
1311
|
-
}
|
|
1312
|
-
}
|
|
1313
|
-
catch (error) {
|
|
1314
|
-
return {
|
|
1315
|
-
content: [{
|
|
1316
|
-
type: "text",
|
|
1317
|
-
text: JSON.stringify({
|
|
1318
|
-
success: false,
|
|
1319
|
-
action,
|
|
1320
|
-
error: error.message || error.original?.Message || '未知错误',
|
|
1321
|
-
code: error.original?.Code,
|
|
1322
|
-
message: "数据模型操作失败"
|
|
1323
|
-
}, null, 2)
|
|
1324
|
-
}]
|
|
1325
|
-
};
|
|
1326
|
-
}
|
|
1327
|
-
});
|
|
1328
|
-
}
|
|
1329
|
-
//# sourceMappingURL=database.js.map
|