@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
54 lines • 1.99 kB
JavaScript
import { createHash } from "node:crypto";
export const MCP_TOOL_NAME_MAX_LENGTH = 64;
export function buildMcpToolNames(entries, warn) {
const items = entries.map((entry, index) => ({
base: ellipsizeMiddle(buildBaseName(entry), MCP_TOOL_NAME_MAX_LENGTH),
entry,
index,
}));
const groups = new Map();
for (const item of items) {
const key = matcherKey(item.base);
const group = groups.get(key) ?? [];
group.push(item);
groups.set(key, group);
}
const names = items.map((item) => item.base);
for (const group of groups.values()) {
if (group.length < 2)
continue;
const [first] = group;
if (!first)
continue;
warn?.(`MCP tool name collision after normalization for '${first.base}'; appended deterministic suffixes.`);
for (const item of group) {
const suffix = hashSuffix(item.entry);
names[item.index] = `${ellipsizeMiddle(item.base, MCP_TOOL_NAME_MAX_LENGTH - suffix.length)}${suffix}`;
}
}
return names;
}
function buildBaseName(entry) {
return `mcp_${sanitizeNamePart(entry.serverName)}_${sanitizeNamePart(entry.toolName)}`;
}
function sanitizeNamePart(name) {
return name.replace(/[^a-zA-Z0-9_-]/g, "_");
}
function matcherKey(name) {
return name.replace(/-/g, "_");
}
function hashSuffix(entry) {
return `_${createHash("sha1").update(`${entry.serverName}\0${entry.toolName}`).digest("hex").slice(0, 4)}`;
}
function ellipsizeMiddle(value, maxLength) {
if (value.length <= maxLength)
return value;
if (maxLength <= 3)
return value.slice(0, maxLength);
const marker = "...";
const remaining = maxLength - marker.length;
const prefixLength = Math.ceil(remaining / 2);
const suffixLength = Math.floor(remaining / 2);
return `${value.slice(0, prefixLength)}${marker}${value.slice(value.length - suffixLength)}`;
}
//# sourceMappingURL=naming.js.map