@cloudbase/cloudbase-mcp 1.0.2 → 1.0.3
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/dist/index.js +53 -6
- package/dist/tools/database.js +558 -405
- package/dist/tools/env.js +149 -67
- package/dist/tools/file.js +208 -147
- package/dist/tools/functions.js +218 -109
- package/dist/tools/hosting.js +223 -116
- package/package.json +1 -1
package/dist/tools/env.js
CHANGED
|
@@ -1,91 +1,173 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
11
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
12
|
+
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
13
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
14
|
+
function step(op) {
|
|
15
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
16
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
17
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
18
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
19
|
+
switch (op[0]) {
|
|
20
|
+
case 0: case 1: t = op; break;
|
|
21
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
22
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
23
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
24
|
+
default:
|
|
25
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
26
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
27
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
28
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
29
|
+
if (t[2]) _.ops.pop();
|
|
30
|
+
_.trys.pop(); continue;
|
|
31
|
+
}
|
|
32
|
+
op = body.call(thisArg, _);
|
|
33
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
34
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
35
|
+
}
|
|
36
|
+
};
|
|
1
37
|
import { z } from "zod";
|
|
2
38
|
import CloudBase from "@cloudbase/manager-node";
|
|
3
39
|
// 初始化CloudBase
|
|
4
|
-
|
|
40
|
+
var cloudbase = new CloudBase({
|
|
5
41
|
secretId: process.env.TENCENTCLOUD_SECRETID,
|
|
6
42
|
secretKey: process.env.TENCENTCLOUD_SECRETKEY,
|
|
7
43
|
envId: process.env.CLOUDBASE_ENV_ID,
|
|
8
44
|
token: process.env.TENCENTCLOUD_SESSIONTOKEN
|
|
9
45
|
});
|
|
10
46
|
export function registerEnvTools(server) {
|
|
47
|
+
var _this = this;
|
|
11
48
|
// listEnvs
|
|
12
|
-
server.tool("listEnvs", "获取所有云开发环境信息", {},
|
|
13
|
-
|
|
14
|
-
return {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
49
|
+
server.tool("listEnvs", "获取所有云开发环境信息", {}, function () { return __awaiter(_this, void 0, void 0, function () {
|
|
50
|
+
var result;
|
|
51
|
+
return __generator(this, function (_a) {
|
|
52
|
+
switch (_a.label) {
|
|
53
|
+
case 0: return [4 /*yield*/, cloudbase.env.listEnvs()];
|
|
54
|
+
case 1:
|
|
55
|
+
result = _a.sent();
|
|
56
|
+
return [2 /*return*/, {
|
|
57
|
+
content: [
|
|
58
|
+
{
|
|
59
|
+
type: "text",
|
|
60
|
+
text: JSON.stringify(result, null, 2)
|
|
61
|
+
}
|
|
62
|
+
]
|
|
63
|
+
}];
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
}); });
|
|
23
67
|
// getEnvAuthDomains
|
|
24
|
-
server.tool("getEnvAuthDomains", "获取云开发环境的合法域名列表", {},
|
|
25
|
-
|
|
26
|
-
return {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
68
|
+
server.tool("getEnvAuthDomains", "获取云开发环境的合法域名列表", {}, function () { return __awaiter(_this, void 0, void 0, function () {
|
|
69
|
+
var result;
|
|
70
|
+
return __generator(this, function (_a) {
|
|
71
|
+
switch (_a.label) {
|
|
72
|
+
case 0: return [4 /*yield*/, cloudbase.env.getEnvAuthDomains()];
|
|
73
|
+
case 1:
|
|
74
|
+
result = _a.sent();
|
|
75
|
+
return [2 /*return*/, {
|
|
76
|
+
content: [
|
|
77
|
+
{
|
|
78
|
+
type: "text",
|
|
79
|
+
text: JSON.stringify(result, null, 2)
|
|
80
|
+
}
|
|
81
|
+
]
|
|
82
|
+
}];
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
}); });
|
|
35
86
|
// createEnvDomain
|
|
36
87
|
server.tool("createEnvDomain", "为云开发环境添加安全域名", {
|
|
37
88
|
domains: z.array(z.string()).describe("安全域名数组")
|
|
38
|
-
},
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
89
|
+
}, function (_a) { return __awaiter(_this, [_a], void 0, function (_b) {
|
|
90
|
+
var result;
|
|
91
|
+
var domains = _b.domains;
|
|
92
|
+
return __generator(this, function (_c) {
|
|
93
|
+
switch (_c.label) {
|
|
94
|
+
case 0: return [4 /*yield*/, cloudbase.env.createEnvDomain(domains)];
|
|
95
|
+
case 1:
|
|
96
|
+
result = _c.sent();
|
|
97
|
+
return [2 /*return*/, {
|
|
98
|
+
content: [
|
|
99
|
+
{
|
|
100
|
+
type: "text",
|
|
101
|
+
text: JSON.stringify(result, null, 2)
|
|
102
|
+
}
|
|
103
|
+
]
|
|
104
|
+
}];
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
}); });
|
|
49
108
|
// deleteEnvDomain
|
|
50
109
|
server.tool("deleteEnvDomain", "删除云开发环境的指定安全域名", {
|
|
51
110
|
domains: z.array(z.string()).describe("安全域名数组")
|
|
52
|
-
},
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
111
|
+
}, function (_a) { return __awaiter(_this, [_a], void 0, function (_b) {
|
|
112
|
+
var result;
|
|
113
|
+
var domains = _b.domains;
|
|
114
|
+
return __generator(this, function (_c) {
|
|
115
|
+
switch (_c.label) {
|
|
116
|
+
case 0: return [4 /*yield*/, cloudbase.env.deleteEnvDomain(domains)];
|
|
117
|
+
case 1:
|
|
118
|
+
result = _c.sent();
|
|
119
|
+
return [2 /*return*/, {
|
|
120
|
+
content: [
|
|
121
|
+
{
|
|
122
|
+
type: "text",
|
|
123
|
+
text: JSON.stringify(result, null, 2)
|
|
124
|
+
}
|
|
125
|
+
]
|
|
126
|
+
}];
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
}); });
|
|
63
130
|
// getEnvInfo
|
|
64
|
-
server.tool("getEnvInfo", "获取当前云开发环境信息", {},
|
|
65
|
-
|
|
66
|
-
return {
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
131
|
+
server.tool("getEnvInfo", "获取当前云开发环境信息", {}, function () { return __awaiter(_this, void 0, void 0, function () {
|
|
132
|
+
var result;
|
|
133
|
+
return __generator(this, function (_a) {
|
|
134
|
+
switch (_a.label) {
|
|
135
|
+
case 0: return [4 /*yield*/, cloudbase.env.getEnvInfo()];
|
|
136
|
+
case 1:
|
|
137
|
+
result = _a.sent();
|
|
138
|
+
return [2 /*return*/, {
|
|
139
|
+
content: [
|
|
140
|
+
{
|
|
141
|
+
type: "text",
|
|
142
|
+
text: JSON.stringify(result, null, 2)
|
|
143
|
+
}
|
|
144
|
+
]
|
|
145
|
+
}];
|
|
146
|
+
}
|
|
147
|
+
});
|
|
148
|
+
}); });
|
|
75
149
|
// updateEnvInfo
|
|
76
150
|
server.tool("updateEnvInfo", "修改云开发环境别名", {
|
|
77
151
|
alias: z.string().describe("环境别名")
|
|
78
|
-
},
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
152
|
+
}, function (_a) { return __awaiter(_this, [_a], void 0, function (_b) {
|
|
153
|
+
var result;
|
|
154
|
+
var alias = _b.alias;
|
|
155
|
+
return __generator(this, function (_c) {
|
|
156
|
+
switch (_c.label) {
|
|
157
|
+
case 0: return [4 /*yield*/, cloudbase.env.updateEnvInfo(alias)];
|
|
158
|
+
case 1:
|
|
159
|
+
result = _c.sent();
|
|
160
|
+
return [2 /*return*/, {
|
|
161
|
+
content: [
|
|
162
|
+
{
|
|
163
|
+
type: "text",
|
|
164
|
+
text: JSON.stringify(result, null, 2)
|
|
165
|
+
}
|
|
166
|
+
]
|
|
167
|
+
}];
|
|
168
|
+
}
|
|
169
|
+
});
|
|
170
|
+
}); });
|
|
89
171
|
// // getLoginConfigList
|
|
90
172
|
// server.tool(
|
|
91
173
|
// "getLoginConfigList",
|
package/dist/tools/file.js
CHANGED
|
@@ -1,15 +1,52 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
11
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
12
|
+
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
13
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
14
|
+
function step(op) {
|
|
15
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
16
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
17
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
18
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
19
|
+
switch (op[0]) {
|
|
20
|
+
case 0: case 1: t = op; break;
|
|
21
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
22
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
23
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
24
|
+
default:
|
|
25
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
26
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
27
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
28
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
29
|
+
if (t[2]) _.ops.pop();
|
|
30
|
+
_.trys.pop(); continue;
|
|
31
|
+
}
|
|
32
|
+
op = body.call(thisArg, _);
|
|
33
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
34
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
35
|
+
}
|
|
36
|
+
};
|
|
1
37
|
import { z } from "zod";
|
|
2
38
|
import * as fs from "fs/promises";
|
|
3
39
|
import * as path from "path";
|
|
4
40
|
import * as os from "os";
|
|
5
41
|
import * as crypto from "crypto";
|
|
6
42
|
// 常量定义
|
|
7
|
-
|
|
43
|
+
var MAX_FILE_SIZE = 100 * 1024; // 100KB in bytes
|
|
8
44
|
// 生成随机文件名
|
|
9
|
-
function generateRandomFileName(extension
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
45
|
+
function generateRandomFileName(extension) {
|
|
46
|
+
if (extension === void 0) { extension = ''; }
|
|
47
|
+
var randomBytes = crypto.randomBytes(16);
|
|
48
|
+
var fileName = randomBytes.toString('hex');
|
|
49
|
+
return "".concat(fileName).concat(extension);
|
|
13
50
|
}
|
|
14
51
|
// 获取安全的临时文件路径
|
|
15
52
|
function getSafeTempFilePath(fileName) {
|
|
@@ -19,172 +56,196 @@ function getSafeTempFilePath(fileName) {
|
|
|
19
56
|
function checkBase64Size(base64String) {
|
|
20
57
|
// 计算 base64 解码后的实际大小
|
|
21
58
|
// base64 字符串长度 * 0.75 约等于实际二进制数据大小
|
|
22
|
-
|
|
59
|
+
var actualSize = Math.ceil(base64String.length * 0.75);
|
|
23
60
|
return actualSize <= MAX_FILE_SIZE;
|
|
24
61
|
}
|
|
25
62
|
// 检查文件路径是否在临时目录中
|
|
26
63
|
function isInTempDir(filePath) {
|
|
27
|
-
|
|
28
|
-
|
|
64
|
+
var normalizedPath = path.normalize(filePath);
|
|
65
|
+
var normalizedTempDir = path.normalize(os.tmpdir());
|
|
29
66
|
return normalizedPath.startsWith(normalizedTempDir);
|
|
30
67
|
}
|
|
31
68
|
export function registerFileTools(server) {
|
|
69
|
+
var _this = this;
|
|
32
70
|
// 创建文件
|
|
33
71
|
server.tool("createTempFile", "在云开发 MCP 服务的临时目录创建文件,支持文本内容或 base64 编码的二进制内容(最大 100KB)", {
|
|
34
72
|
content: z.string().describe("文件内容,可以是普通文本或 base64 编码的二进制内容"),
|
|
35
73
|
isBase64: z.boolean().default(false).describe("是否为 base64 编码的内容"),
|
|
36
74
|
extension: z.string().optional().describe("文件扩展名,例如 .txt, .png 等")
|
|
37
|
-
},
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
75
|
+
}, function (_a) { return __awaiter(_this, [_a], void 0, function (_b) {
|
|
76
|
+
var fileName, filePath, buffer, error_1;
|
|
77
|
+
var content = _b.content, _c = _b.isBase64, isBase64 = _c === void 0 ? false : _c, _d = _b.extension, extension = _d === void 0 ? '' : _d;
|
|
78
|
+
return __generator(this, function (_e) {
|
|
79
|
+
switch (_e.label) {
|
|
80
|
+
case 0:
|
|
81
|
+
_e.trys.push([0, 5, , 6]);
|
|
82
|
+
// 如果是 base64 内容,先检查大小
|
|
83
|
+
if (isBase64) {
|
|
84
|
+
if (!checkBase64Size(content)) {
|
|
85
|
+
return [2 /*return*/, {
|
|
86
|
+
content: [
|
|
87
|
+
{
|
|
88
|
+
type: "text",
|
|
89
|
+
text: JSON.stringify({
|
|
90
|
+
success: false,
|
|
91
|
+
error: "文件大小超过限制",
|
|
92
|
+
message: "\u6587\u4EF6\u5927\u5C0F\u8D85\u8FC7 ".concat(MAX_FILE_SIZE / 1024, "KB \u9650\u5236")
|
|
93
|
+
}, null, 2)
|
|
94
|
+
}
|
|
95
|
+
]
|
|
96
|
+
}];
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
fileName = generateRandomFileName(extension);
|
|
100
|
+
filePath = getSafeTempFilePath(fileName);
|
|
101
|
+
if (!isBase64) return [3 /*break*/, 2];
|
|
102
|
+
buffer = Buffer.from(content, 'base64');
|
|
103
|
+
return [4 /*yield*/, fs.writeFile(filePath, buffer)];
|
|
104
|
+
case 1:
|
|
105
|
+
_e.sent();
|
|
106
|
+
return [3 /*break*/, 4];
|
|
107
|
+
case 2:
|
|
108
|
+
// 写入文本文件
|
|
109
|
+
return [4 /*yield*/, fs.writeFile(filePath, content, 'utf-8')];
|
|
110
|
+
case 3:
|
|
111
|
+
// 写入文本文件
|
|
112
|
+
_e.sent();
|
|
113
|
+
_e.label = 4;
|
|
114
|
+
case 4: return [2 /*return*/, {
|
|
43
115
|
content: [
|
|
44
116
|
{
|
|
45
117
|
type: "text",
|
|
46
118
|
text: JSON.stringify({
|
|
47
|
-
success:
|
|
48
|
-
|
|
49
|
-
message:
|
|
119
|
+
success: true,
|
|
120
|
+
filePath: filePath,
|
|
121
|
+
message: "文件创建成功",
|
|
122
|
+
fileSize: isBase64 ? Math.ceil(content.length * 0.75) : Buffer.from(content).length
|
|
50
123
|
}, null, 2)
|
|
51
124
|
}
|
|
52
125
|
]
|
|
53
|
-
};
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
126
|
+
}];
|
|
127
|
+
case 5:
|
|
128
|
+
error_1 = _e.sent();
|
|
129
|
+
return [2 /*return*/, {
|
|
130
|
+
content: [
|
|
131
|
+
{
|
|
132
|
+
type: "text",
|
|
133
|
+
text: JSON.stringify({
|
|
134
|
+
success: false,
|
|
135
|
+
error: error_1.message,
|
|
136
|
+
message: "文件创建失败"
|
|
137
|
+
}, null, 2)
|
|
138
|
+
}
|
|
139
|
+
]
|
|
140
|
+
}];
|
|
141
|
+
case 6: return [2 /*return*/];
|
|
62
142
|
}
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
await fs.writeFile(filePath, content, 'utf-8');
|
|
66
|
-
}
|
|
67
|
-
return {
|
|
68
|
-
content: [
|
|
69
|
-
{
|
|
70
|
-
type: "text",
|
|
71
|
-
text: JSON.stringify({
|
|
72
|
-
success: true,
|
|
73
|
-
filePath,
|
|
74
|
-
message: "文件创建成功",
|
|
75
|
-
fileSize: isBase64 ? Math.ceil(content.length * 0.75) : Buffer.from(content).length
|
|
76
|
-
}, null, 2)
|
|
77
|
-
}
|
|
78
|
-
]
|
|
79
|
-
};
|
|
80
|
-
}
|
|
81
|
-
catch (error) {
|
|
82
|
-
return {
|
|
83
|
-
content: [
|
|
84
|
-
{
|
|
85
|
-
type: "text",
|
|
86
|
-
text: JSON.stringify({
|
|
87
|
-
success: false,
|
|
88
|
-
error: error.message,
|
|
89
|
-
message: "文件创建失败"
|
|
90
|
-
}, null, 2)
|
|
91
|
-
}
|
|
92
|
-
]
|
|
93
|
-
};
|
|
94
|
-
}
|
|
95
|
-
});
|
|
143
|
+
});
|
|
144
|
+
}); });
|
|
96
145
|
// 读取文件
|
|
97
146
|
server.tool("readTempFile", "读取临时目录中的文件,支持文本和二进制文件(二进制文件将以 base64 格式返回)", {
|
|
98
147
|
filePath: z.string().describe("要读取的文件路径"),
|
|
99
148
|
asBase64: z.boolean().default(false).describe("是否以 base64 格式返回内容(用于二进制文件)")
|
|
100
|
-
},
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
catch {
|
|
122
|
-
return {
|
|
123
|
-
content: [
|
|
124
|
-
{
|
|
125
|
-
type: "text",
|
|
126
|
-
text: JSON.stringify({
|
|
127
|
-
success: false,
|
|
128
|
-
error: "文件不存在",
|
|
129
|
-
message: "指定的文件不存在"
|
|
130
|
-
}, null, 2)
|
|
131
|
-
}
|
|
132
|
-
]
|
|
133
|
-
};
|
|
134
|
-
}
|
|
135
|
-
// 检查文件大小
|
|
136
|
-
const stats = await fs.stat(filePath);
|
|
137
|
-
if (stats.size > MAX_FILE_SIZE) {
|
|
138
|
-
return {
|
|
139
|
-
content: [
|
|
140
|
-
{
|
|
141
|
-
type: "text",
|
|
142
|
-
text: JSON.stringify({
|
|
143
|
-
success: false,
|
|
144
|
-
error: "文件大小超过限制",
|
|
145
|
-
message: `文件大小 ${stats.size} 字节超过 ${MAX_FILE_SIZE} 字节限制`
|
|
146
|
-
}, null, 2)
|
|
147
|
-
}
|
|
148
|
-
]
|
|
149
|
-
};
|
|
150
|
-
}
|
|
151
|
-
// 读取文件
|
|
152
|
-
const buffer = await fs.readFile(filePath);
|
|
153
|
-
let content;
|
|
154
|
-
if (asBase64) {
|
|
155
|
-
content = buffer.toString('base64');
|
|
156
|
-
}
|
|
157
|
-
else {
|
|
158
|
-
content = buffer.toString('utf-8');
|
|
159
|
-
}
|
|
160
|
-
return {
|
|
161
|
-
content: [
|
|
162
|
-
{
|
|
163
|
-
type: "text",
|
|
164
|
-
text: JSON.stringify({
|
|
165
|
-
success: true,
|
|
166
|
-
content,
|
|
167
|
-
fileSize: buffer.length,
|
|
168
|
-
encoding: asBase64 ? 'base64' : 'utf-8',
|
|
169
|
-
message: "文件读取成功"
|
|
170
|
-
}, null, 2)
|
|
149
|
+
}, function (_a) { return __awaiter(_this, [_a], void 0, function (_b) {
|
|
150
|
+
var _c, stats, buffer, content, error_2;
|
|
151
|
+
var filePath = _b.filePath, _d = _b.asBase64, asBase64 = _d === void 0 ? false : _d;
|
|
152
|
+
return __generator(this, function (_e) {
|
|
153
|
+
switch (_e.label) {
|
|
154
|
+
case 0:
|
|
155
|
+
_e.trys.push([0, 7, , 8]);
|
|
156
|
+
// 安全检查:确保文件路径在临时目录中
|
|
157
|
+
if (!isInTempDir(filePath)) {
|
|
158
|
+
return [2 /*return*/, {
|
|
159
|
+
content: [
|
|
160
|
+
{
|
|
161
|
+
type: "text",
|
|
162
|
+
text: JSON.stringify({
|
|
163
|
+
success: false,
|
|
164
|
+
error: "安全限制",
|
|
165
|
+
message: "只能读取临时目录中的文件"
|
|
166
|
+
}, null, 2)
|
|
167
|
+
}
|
|
168
|
+
]
|
|
169
|
+
}];
|
|
171
170
|
}
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
171
|
+
_e.label = 1;
|
|
172
|
+
case 1:
|
|
173
|
+
_e.trys.push([1, 3, , 4]);
|
|
174
|
+
return [4 /*yield*/, fs.access(filePath)];
|
|
175
|
+
case 2:
|
|
176
|
+
_e.sent();
|
|
177
|
+
return [3 /*break*/, 4];
|
|
178
|
+
case 3:
|
|
179
|
+
_c = _e.sent();
|
|
180
|
+
return [2 /*return*/, {
|
|
181
|
+
content: [
|
|
182
|
+
{
|
|
183
|
+
type: "text",
|
|
184
|
+
text: JSON.stringify({
|
|
185
|
+
success: false,
|
|
186
|
+
error: "文件不存在",
|
|
187
|
+
message: "指定的文件不存在"
|
|
188
|
+
}, null, 2)
|
|
189
|
+
}
|
|
190
|
+
]
|
|
191
|
+
}];
|
|
192
|
+
case 4: return [4 /*yield*/, fs.stat(filePath)];
|
|
193
|
+
case 5:
|
|
194
|
+
stats = _e.sent();
|
|
195
|
+
if (stats.size > MAX_FILE_SIZE) {
|
|
196
|
+
return [2 /*return*/, {
|
|
197
|
+
content: [
|
|
198
|
+
{
|
|
199
|
+
type: "text",
|
|
200
|
+
text: JSON.stringify({
|
|
201
|
+
success: false,
|
|
202
|
+
error: "文件大小超过限制",
|
|
203
|
+
message: "\u6587\u4EF6\u5927\u5C0F ".concat(stats.size, " \u5B57\u8282\u8D85\u8FC7 ").concat(MAX_FILE_SIZE, " \u5B57\u8282\u9650\u5236")
|
|
204
|
+
}, null, 2)
|
|
205
|
+
}
|
|
206
|
+
]
|
|
207
|
+
}];
|
|
185
208
|
}
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
209
|
+
return [4 /*yield*/, fs.readFile(filePath)];
|
|
210
|
+
case 6:
|
|
211
|
+
buffer = _e.sent();
|
|
212
|
+
content = void 0;
|
|
213
|
+
if (asBase64) {
|
|
214
|
+
content = buffer.toString('base64');
|
|
215
|
+
}
|
|
216
|
+
else {
|
|
217
|
+
content = buffer.toString('utf-8');
|
|
218
|
+
}
|
|
219
|
+
return [2 /*return*/, {
|
|
220
|
+
content: [
|
|
221
|
+
{
|
|
222
|
+
type: "text",
|
|
223
|
+
text: JSON.stringify({
|
|
224
|
+
success: true,
|
|
225
|
+
content: content,
|
|
226
|
+
fileSize: buffer.length,
|
|
227
|
+
encoding: asBase64 ? 'base64' : 'utf-8',
|
|
228
|
+
message: "文件读取成功"
|
|
229
|
+
}, null, 2)
|
|
230
|
+
}
|
|
231
|
+
]
|
|
232
|
+
}];
|
|
233
|
+
case 7:
|
|
234
|
+
error_2 = _e.sent();
|
|
235
|
+
return [2 /*return*/, {
|
|
236
|
+
content: [
|
|
237
|
+
{
|
|
238
|
+
type: "text",
|
|
239
|
+
text: JSON.stringify({
|
|
240
|
+
success: false,
|
|
241
|
+
error: error_2.message,
|
|
242
|
+
message: "文件读取失败"
|
|
243
|
+
}, null, 2)
|
|
244
|
+
}
|
|
245
|
+
]
|
|
246
|
+
}];
|
|
247
|
+
case 8: return [2 /*return*/];
|
|
248
|
+
}
|
|
249
|
+
});
|
|
250
|
+
}); });
|
|
190
251
|
}
|