/**
* Utilities for namespacing tool names, resource URIs, and prompt names
* to avoid collisions between multiple downstream MCP servers.
*/
export const SEPARATOR = "__";
export function addToolPrefix(serverId: string, toolName: string): string {
return `${serverId}${SEPARATOR}${toolName}`;
}
export function removeToolPrefix(namespacedName: string): { serverId: string; toolName: string } | null {
const idx = namespacedName.indexOf(SEPARATOR);
if (idx === -1) return null;
return {
serverId: namespacedName.substring(0, idx),
toolName: namespacedName.substring(idx + SEPARATOR.length),
};
}
export function addResourcePrefix(serverId: string, uri: string): string {
return `${serverId}://${uri}`;
}
export function removeResourcePrefix(namespacedUri: string): { serverId: string; uri: string } | null {
const match = namespacedUri.match(/^([^:]+):\/\/(.+)$/);
if (!match) return null;
return {
serverId: match[1],
uri: match[2],
};
}
export function addPromptPrefix(serverId: string, promptName: string): string {
return `${serverId}${SEPARATOR}${promptName}`;
}
export function removePromptPrefix(namespacedName: string): { serverId: string; promptName: string } | null {
const idx = namespacedName.indexOf(SEPARATOR);
if (idx === -1) return null;
return {
serverId: namespacedName.substring(0, idx),
promptName: namespacedName.substring(idx + SEPARATOR.length),
};
}