@cloudbase/cloudbase-mcp 1.0.0 → 1.0.2
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 +133 -0
- package/dist/index.js +32 -0
- package/dist/tools/database.js +657 -0
- package/dist/tools/env.js +166 -0
- package/dist/tools/file.js +190 -0
- package/dist/tools/functions.js +382 -0
- package/dist/tools/hosting.js +221 -0
- package/dist/types.js +1 -0
- package/package.json +3 -3
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import CloudBase from "@cloudbase/manager-node";
|
|
3
|
+
// 初始化CloudBase
|
|
4
|
+
const cloudbase = new CloudBase({
|
|
5
|
+
secretId: process.env.TENCENTCLOUD_SECRETID,
|
|
6
|
+
secretKey: process.env.TENCENTCLOUD_SECRETKEY,
|
|
7
|
+
envId: process.env.CLOUDBASE_ENV_ID,
|
|
8
|
+
token: process.env.TENCENTCLOUD_SESSIONTOKEN
|
|
9
|
+
});
|
|
10
|
+
export function registerEnvTools(server) {
|
|
11
|
+
// listEnvs
|
|
12
|
+
server.tool("listEnvs", "获取所有云开发环境信息", {}, async () => {
|
|
13
|
+
const result = await cloudbase.env.listEnvs();
|
|
14
|
+
return {
|
|
15
|
+
content: [
|
|
16
|
+
{
|
|
17
|
+
type: "text",
|
|
18
|
+
text: JSON.stringify(result, null, 2)
|
|
19
|
+
}
|
|
20
|
+
]
|
|
21
|
+
};
|
|
22
|
+
});
|
|
23
|
+
// getEnvAuthDomains
|
|
24
|
+
server.tool("getEnvAuthDomains", "获取云开发环境的合法域名列表", {}, async () => {
|
|
25
|
+
const result = await cloudbase.env.getEnvAuthDomains();
|
|
26
|
+
return {
|
|
27
|
+
content: [
|
|
28
|
+
{
|
|
29
|
+
type: "text",
|
|
30
|
+
text: JSON.stringify(result, null, 2)
|
|
31
|
+
}
|
|
32
|
+
]
|
|
33
|
+
};
|
|
34
|
+
});
|
|
35
|
+
// createEnvDomain
|
|
36
|
+
server.tool("createEnvDomain", "为云开发环境添加安全域名", {
|
|
37
|
+
domains: z.array(z.string()).describe("安全域名数组")
|
|
38
|
+
}, async ({ domains }) => {
|
|
39
|
+
const result = await cloudbase.env.createEnvDomain(domains);
|
|
40
|
+
return {
|
|
41
|
+
content: [
|
|
42
|
+
{
|
|
43
|
+
type: "text",
|
|
44
|
+
text: JSON.stringify(result, null, 2)
|
|
45
|
+
}
|
|
46
|
+
]
|
|
47
|
+
};
|
|
48
|
+
});
|
|
49
|
+
// deleteEnvDomain
|
|
50
|
+
server.tool("deleteEnvDomain", "删除云开发环境的指定安全域名", {
|
|
51
|
+
domains: z.array(z.string()).describe("安全域名数组")
|
|
52
|
+
}, async ({ domains }) => {
|
|
53
|
+
const result = await cloudbase.env.deleteEnvDomain(domains);
|
|
54
|
+
return {
|
|
55
|
+
content: [
|
|
56
|
+
{
|
|
57
|
+
type: "text",
|
|
58
|
+
text: JSON.stringify(result, null, 2)
|
|
59
|
+
}
|
|
60
|
+
]
|
|
61
|
+
};
|
|
62
|
+
});
|
|
63
|
+
// getEnvInfo
|
|
64
|
+
server.tool("getEnvInfo", "获取当前云开发环境信息", {}, async () => {
|
|
65
|
+
const result = await cloudbase.env.getEnvInfo();
|
|
66
|
+
return {
|
|
67
|
+
content: [
|
|
68
|
+
{
|
|
69
|
+
type: "text",
|
|
70
|
+
text: JSON.stringify(result, null, 2)
|
|
71
|
+
}
|
|
72
|
+
]
|
|
73
|
+
};
|
|
74
|
+
});
|
|
75
|
+
// updateEnvInfo
|
|
76
|
+
server.tool("updateEnvInfo", "修改云开发环境别名", {
|
|
77
|
+
alias: z.string().describe("环境别名")
|
|
78
|
+
}, async ({ alias }) => {
|
|
79
|
+
const result = await cloudbase.env.updateEnvInfo(alias);
|
|
80
|
+
return {
|
|
81
|
+
content: [
|
|
82
|
+
{
|
|
83
|
+
type: "text",
|
|
84
|
+
text: JSON.stringify(result, null, 2)
|
|
85
|
+
}
|
|
86
|
+
]
|
|
87
|
+
};
|
|
88
|
+
});
|
|
89
|
+
// // getLoginConfigList
|
|
90
|
+
// server.tool(
|
|
91
|
+
// "getLoginConfigList",
|
|
92
|
+
// "拉取登录配置列表",
|
|
93
|
+
// {},
|
|
94
|
+
// async () => {
|
|
95
|
+
// const result = await cloudbase.env.getLoginConfigList();
|
|
96
|
+
// return {
|
|
97
|
+
// content: [
|
|
98
|
+
// {
|
|
99
|
+
// type: "text",
|
|
100
|
+
// text: JSON.stringify(result, null, 2)
|
|
101
|
+
// }
|
|
102
|
+
// ]
|
|
103
|
+
// };
|
|
104
|
+
// }
|
|
105
|
+
// );
|
|
106
|
+
// // createLoginConfig
|
|
107
|
+
// server.tool(
|
|
108
|
+
// "createLoginConfig",
|
|
109
|
+
// "创建登录方式",
|
|
110
|
+
// {
|
|
111
|
+
// platform: z.enum(["WECHAT-OPEN", "WECHAT-PUBLIC", "QQ", "ANONYMOUS"]).describe("平台类型"),
|
|
112
|
+
// appId: z.string().describe("第三方平台的AppID"),
|
|
113
|
+
// appSecret: z.string().optional().describe("第三方平台的AppSecret")
|
|
114
|
+
// },
|
|
115
|
+
// async ({ platform, appId, appSecret }) => {
|
|
116
|
+
// const result = await cloudbase.env.createLoginConfig(platform, appId, appSecret);
|
|
117
|
+
// return {
|
|
118
|
+
// content: [
|
|
119
|
+
// {
|
|
120
|
+
// type: "text",
|
|
121
|
+
// text: JSON.stringify(result, null, 2)
|
|
122
|
+
// }
|
|
123
|
+
// ]
|
|
124
|
+
// };
|
|
125
|
+
// }
|
|
126
|
+
// );
|
|
127
|
+
// // updateLoginConfig
|
|
128
|
+
// server.tool(
|
|
129
|
+
// "updateLoginConfig",
|
|
130
|
+
// "更新登录方式配置",
|
|
131
|
+
// {
|
|
132
|
+
// configId: z.string().describe("配置的记录ID"),
|
|
133
|
+
// status: z.enum(["ENABLE", "DISABLE"]).describe("配置状态"),
|
|
134
|
+
// appId: z.string().describe("第三方平台的AppId"),
|
|
135
|
+
// appSecret: z.string().optional().describe("第三方平台的AppSecret")
|
|
136
|
+
// },
|
|
137
|
+
// async ({ configId, status, appId, appSecret }) => {
|
|
138
|
+
// const result = await cloudbase.env.updateLoginConfig(configId, status, appId, appSecret);
|
|
139
|
+
// return {
|
|
140
|
+
// content: [
|
|
141
|
+
// {
|
|
142
|
+
// type: "text",
|
|
143
|
+
// text: JSON.stringify(result, null, 2)
|
|
144
|
+
// }
|
|
145
|
+
// ]
|
|
146
|
+
// };
|
|
147
|
+
// }
|
|
148
|
+
// );
|
|
149
|
+
// // createCustomLoginKeys
|
|
150
|
+
// server.tool(
|
|
151
|
+
// "createCustomLoginKeys",
|
|
152
|
+
// "创建自定义登录密钥",
|
|
153
|
+
// {},
|
|
154
|
+
// async () => {
|
|
155
|
+
// const result = await cloudbase.env.createCustomLoginKeys();
|
|
156
|
+
// return {
|
|
157
|
+
// content: [
|
|
158
|
+
// {
|
|
159
|
+
// type: "text",
|
|
160
|
+
// text: JSON.stringify(result, null, 2)
|
|
161
|
+
// }
|
|
162
|
+
// ]
|
|
163
|
+
// };
|
|
164
|
+
// }
|
|
165
|
+
// );
|
|
166
|
+
}
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import * as fs from "fs/promises";
|
|
3
|
+
import * as path from "path";
|
|
4
|
+
import * as os from "os";
|
|
5
|
+
import * as crypto from "crypto";
|
|
6
|
+
// 常量定义
|
|
7
|
+
const MAX_FILE_SIZE = 100 * 1024; // 100KB in bytes
|
|
8
|
+
// 生成随机文件名
|
|
9
|
+
function generateRandomFileName(extension = '') {
|
|
10
|
+
const randomBytes = crypto.randomBytes(16);
|
|
11
|
+
const fileName = randomBytes.toString('hex');
|
|
12
|
+
return `${fileName}${extension}`;
|
|
13
|
+
}
|
|
14
|
+
// 获取安全的临时文件路径
|
|
15
|
+
function getSafeTempFilePath(fileName) {
|
|
16
|
+
return path.join(os.tmpdir(), fileName);
|
|
17
|
+
}
|
|
18
|
+
// 检查 base64 字符串大小
|
|
19
|
+
function checkBase64Size(base64String) {
|
|
20
|
+
// 计算 base64 解码后的实际大小
|
|
21
|
+
// base64 字符串长度 * 0.75 约等于实际二进制数据大小
|
|
22
|
+
const actualSize = Math.ceil(base64String.length * 0.75);
|
|
23
|
+
return actualSize <= MAX_FILE_SIZE;
|
|
24
|
+
}
|
|
25
|
+
// 检查文件路径是否在临时目录中
|
|
26
|
+
function isInTempDir(filePath) {
|
|
27
|
+
const normalizedPath = path.normalize(filePath);
|
|
28
|
+
const normalizedTempDir = path.normalize(os.tmpdir());
|
|
29
|
+
return normalizedPath.startsWith(normalizedTempDir);
|
|
30
|
+
}
|
|
31
|
+
export function registerFileTools(server) {
|
|
32
|
+
// 创建文件
|
|
33
|
+
server.tool("createTempFile", "在云开发 MCP 服务的临时目录创建文件,支持文本内容或 base64 编码的二进制内容(最大 100KB)", {
|
|
34
|
+
content: z.string().describe("文件内容,可以是普通文本或 base64 编码的二进制内容"),
|
|
35
|
+
isBase64: z.boolean().default(false).describe("是否为 base64 编码的内容"),
|
|
36
|
+
extension: z.string().optional().describe("文件扩展名,例如 .txt, .png 等")
|
|
37
|
+
}, async ({ content, isBase64 = false, extension = '' }) => {
|
|
38
|
+
try {
|
|
39
|
+
// 如果是 base64 内容,先检查大小
|
|
40
|
+
if (isBase64) {
|
|
41
|
+
if (!checkBase64Size(content)) {
|
|
42
|
+
return {
|
|
43
|
+
content: [
|
|
44
|
+
{
|
|
45
|
+
type: "text",
|
|
46
|
+
text: JSON.stringify({
|
|
47
|
+
success: false,
|
|
48
|
+
error: "文件大小超过限制",
|
|
49
|
+
message: `文件大小超过 ${MAX_FILE_SIZE / 1024}KB 限制`
|
|
50
|
+
}, null, 2)
|
|
51
|
+
}
|
|
52
|
+
]
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
const fileName = generateRandomFileName(extension);
|
|
57
|
+
const filePath = getSafeTempFilePath(fileName);
|
|
58
|
+
if (isBase64) {
|
|
59
|
+
// 解码 base64 并写入二进制文件
|
|
60
|
+
const buffer = Buffer.from(content, 'base64');
|
|
61
|
+
await fs.writeFile(filePath, buffer);
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
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
|
+
});
|
|
96
|
+
// 读取文件
|
|
97
|
+
server.tool("readTempFile", "读取临时目录中的文件,支持文本和二进制文件(二进制文件将以 base64 格式返回)", {
|
|
98
|
+
filePath: z.string().describe("要读取的文件路径"),
|
|
99
|
+
asBase64: z.boolean().default(false).describe("是否以 base64 格式返回内容(用于二进制文件)")
|
|
100
|
+
}, async ({ filePath, asBase64 = false }) => {
|
|
101
|
+
try {
|
|
102
|
+
// 安全检查:确保文件路径在临时目录中
|
|
103
|
+
if (!isInTempDir(filePath)) {
|
|
104
|
+
return {
|
|
105
|
+
content: [
|
|
106
|
+
{
|
|
107
|
+
type: "text",
|
|
108
|
+
text: JSON.stringify({
|
|
109
|
+
success: false,
|
|
110
|
+
error: "安全限制",
|
|
111
|
+
message: "只能读取临时目录中的文件"
|
|
112
|
+
}, null, 2)
|
|
113
|
+
}
|
|
114
|
+
]
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
// 检查文件是否存在
|
|
118
|
+
try {
|
|
119
|
+
await fs.access(filePath);
|
|
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)
|
|
171
|
+
}
|
|
172
|
+
]
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
catch (error) {
|
|
176
|
+
return {
|
|
177
|
+
content: [
|
|
178
|
+
{
|
|
179
|
+
type: "text",
|
|
180
|
+
text: JSON.stringify({
|
|
181
|
+
success: false,
|
|
182
|
+
error: error.message,
|
|
183
|
+
message: "文件读取失败"
|
|
184
|
+
}, null, 2)
|
|
185
|
+
}
|
|
186
|
+
]
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
});
|
|
190
|
+
}
|