mcp: 服务级工具名单 tools_allow/tools_deny(发现阶段按原始名过滤,allow 白名单 + deny 叠加)
This commit is contained in:
@@ -7,6 +7,14 @@ const log = require('./log');
|
||||
// MCP 单次工具调用(tools/call)的缺省最大等待,与 mcp.js 的 CALL_TIMEOUT_MS 保持一致。
|
||||
const MCP_CALL_TIMEOUT_MS = 60000;
|
||||
|
||||
// MCP 工具名单(tools_allow/tools_deny)归一化:只接受数组,项统一转字符串去空白、丢空项;
|
||||
// 空数组/非数组视作未配置(返回 undefined),避免「allow: [] 是不是全禁」这种歧义。
|
||||
function normToolNames(v) {
|
||||
if (!Array.isArray(v)) return undefined;
|
||||
const out = v.map(x => String(x).trim()).filter(Boolean);
|
||||
return out.length ? out : undefined;
|
||||
}
|
||||
|
||||
const DEFAULTS = {
|
||||
port: 8090,
|
||||
// 数据根目录:db、prompt dump 等运行时产物默认放这里(容器内挂 ./data → /workspace/data)。
|
||||
@@ -246,6 +254,9 @@ function parseAndValidate(rawText, configPath) {
|
||||
// 单次工具调用的超时上限:缺省 60s,夹在 [1s, 10min](一次调用长期占着模型槽位,不允许无限等)。
|
||||
// YAML 里写成字符串或写了非法值都回落到缺省。
|
||||
s.timeout_ms = Math.min(Math.max(Number(s.timeout_ms) || MCP_CALL_TIMEOUT_MS, 1000), 600000);
|
||||
// 工具名单(发现阶段生效,见 mcp.init):allow 为白名单,deny 在其后叠加生效(deny 优先)。
|
||||
s.tools_allow = normToolNames(s.tools_allow);
|
||||
s.tools_deny = normToolNames(s.tools_deny);
|
||||
}
|
||||
|
||||
normalizePublicLink(cfg);
|
||||
|
||||
+19
-4
@@ -1,6 +1,7 @@
|
||||
// 极简 MCP 客户端(Streamable HTTP:JSON-RPC over HTTP,无状态,响应可能是 SSE 帧包裹的 JSON)。
|
||||
// MCP 服务在 config.yml 的 mcp_servers 中配置(可多个):{name?, url, headers?, enable?, timeout_ms?}。
|
||||
// 启动时 init() 对每个服务调 tools/list 发现工具(enable: false 的服务跳过加载),转成 OpenAI function 格式,
|
||||
// MCP 服务在 config.yml 的 mcp_servers 中配置(可多个):{name?, url, headers?, enable?, timeout_ms?, tools_allow?, tools_deny?}。
|
||||
// 启动时 init() 对每个服务调 tools/list 发现工具(enable: false 的服务跳过加载;配了 tools_allow/tools_deny
|
||||
// 时按名单过滤),转成 OpenAI function 格式,
|
||||
// 追加在内置工具(definitions.json)之后一起随 system prompt 发给模型;
|
||||
// 工具调用经 tools/call 转发给对应服务。
|
||||
// 与内置工具严格分离:本模块不碰内置定义;MCP 工具默认沿用原始名,
|
||||
@@ -133,7 +134,13 @@ async function init(cfg) {
|
||||
if (!/Method not found/i.test(String(e && e.message || e))) throw e;
|
||||
}
|
||||
const result = await rpc(s, 'tools/list', {}, 15000);
|
||||
const tools = (result && result.tools) || [];
|
||||
const all = (result && result.tools) || [];
|
||||
// 工具名单过滤(发生在重命名之前,按服务端的【原始工具名】匹配):
|
||||
// allow 是白名单(配了就只保留名单内),deny 在其后叠加生效(deny 优先)。
|
||||
// 动机:单服务动辄二三十个工具全塞进每次模型请求(如浏览器类),只放行主循环用得上的。
|
||||
let tools = all;
|
||||
if (Array.isArray(s.tools_allow)) tools = tools.filter(t => s.tools_allow.includes(t && t.name));
|
||||
if (Array.isArray(s.tools_deny)) tools = tools.filter(t => !s.tools_deny.includes(t && t.name));
|
||||
for (const t of tools) {
|
||||
if (!t || !t.name) continue;
|
||||
let name = t.name;
|
||||
@@ -148,7 +155,15 @@ async function init(cfg) {
|
||||
},
|
||||
});
|
||||
}
|
||||
log.info('mcp', s.name + ': 发现 ' + tools.length + ' 个工具' + (s.sessionId ? ' (session)' : ''));
|
||||
let msg = s.name + ': 发现 ' + tools.length + ' 个工具' + (s.sessionId ? ' (session)' : '');
|
||||
if (tools.length !== all.length) msg += '(服务端共 ' + all.length + ' 个,已按 tools_allow/tools_deny 过滤)';
|
||||
log.info('mcp', msg);
|
||||
// 配了名单却一个都没过滤中,十有八九是名单里的名字跟 tools/list 返回不一致;
|
||||
// 不 warn 的话排查时只能对着「发现 0 个工具」干瞪眼。
|
||||
if (all.length && !tools.length && (s.tools_allow || s.tools_deny)) {
|
||||
log.warn('mcp', s.name + ': 名单过滤后剩 0 个工具,请核对 tools_allow/tools_deny 与 ' +
|
||||
'服务端实际工具名(如 ' + all.slice(0, 3).map(t => t.name).join(', ') + ' …)');
|
||||
}
|
||||
} catch (err) {
|
||||
log.warn('mcp', s.name + ' 工具发现失败(本轮运行该服务不可用): ' + (err && err.message || err));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user