@yeaft/webchat-agent 0.0.81 → 0.0.83
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/index.js +44 -7
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import 'dotenv/config';
|
|
2
|
-
import { platform } from 'os';
|
|
2
|
+
import { platform, homedir } from 'os';
|
|
3
3
|
import { existsSync, readFileSync, writeFileSync } from 'fs';
|
|
4
4
|
import { join, dirname } from 'path';
|
|
5
5
|
import { exec } from 'child_process';
|
|
@@ -62,14 +62,51 @@ const CONFIG = {
|
|
|
62
62
|
workDir: process.env.WORK_DIR || fileConfig.workDir,
|
|
63
63
|
reconnectInterval: fileConfig.reconnectInterval,
|
|
64
64
|
agentSecret: process.env.AGENT_SECRET || fileConfig.agentSecret,
|
|
65
|
-
//
|
|
66
|
-
//
|
|
67
|
-
//
|
|
65
|
+
// MCP 白名单:只允许这些 MCP 服务器的工具,其余自动禁用
|
|
66
|
+
// 通过 ALLOWED_MCP_SERVERS 环境变量(逗号分隔)或配置文件 allowedMcpServers 指定
|
|
67
|
+
// 默认只允许 playwright
|
|
68
68
|
disallowedTools: (() => {
|
|
69
|
+
// 解析显式禁用列表
|
|
69
70
|
const raw = process.env.DISALLOWED_TOOLS || fileConfig.disallowedTools || '';
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
71
|
+
const explicit = raw === 'none' ? [] : raw.split(',').map(s => s.trim()).filter(Boolean);
|
|
72
|
+
|
|
73
|
+
// 解析 MCP 白名单
|
|
74
|
+
const allowedRaw = process.env.ALLOWED_MCP_SERVERS || fileConfig.allowedMcpServers || 'playwright';
|
|
75
|
+
const allowedMcpServers = allowedRaw.split(',').map(s => s.trim()).filter(Boolean);
|
|
76
|
+
|
|
77
|
+
// 读取 ~/.claude.json 中所有配置的 MCP 服务器名
|
|
78
|
+
const claudeConfigPath = join(homedir(), '.claude.json');
|
|
79
|
+
const mcpDisallowed = [];
|
|
80
|
+
try {
|
|
81
|
+
if (existsSync(claudeConfigPath)) {
|
|
82
|
+
const claudeConfig = JSON.parse(readFileSync(claudeConfigPath, 'utf-8'));
|
|
83
|
+
const allMcpNames = new Set();
|
|
84
|
+
// 收集所有项目中配置的 MCP 服务器名
|
|
85
|
+
for (const [, projCfg] of Object.entries(claudeConfig.projects || {})) {
|
|
86
|
+
for (const name of Object.keys(projCfg.mcpServers || {})) {
|
|
87
|
+
allMcpNames.add(name);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
// 顶层 mcpServers
|
|
91
|
+
for (const name of Object.keys(claudeConfig.mcpServers || {})) {
|
|
92
|
+
allMcpNames.add(name);
|
|
93
|
+
}
|
|
94
|
+
// 不在白名单中的 MCP 服务器 → 禁用
|
|
95
|
+
for (const name of allMcpNames) {
|
|
96
|
+
if (!allowedMcpServers.includes(name)) {
|
|
97
|
+
mcpDisallowed.push(`mcp__${name}`);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
if (mcpDisallowed.length > 0) {
|
|
101
|
+
console.log(`[MCP] Allowed: ${allowedMcpServers.join(', ')}`);
|
|
102
|
+
console.log(`[MCP] Disallowed: ${mcpDisallowed.join(', ')}`);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
} catch (e) {
|
|
106
|
+
console.warn('[MCP] Failed to read ~/.claude.json:', e.message);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
return [...explicit, ...mcpDisallowed];
|
|
73
110
|
})()
|
|
74
111
|
};
|
|
75
112
|
|