/** * Credible source metadata for an MCP adapter extension. Only the paths a tool * actually comes from count: descriptions, labels and parameter schemas are * written by the tool author and must not classify a tool as MCP. */ const MCP_SOURCE_INFO_PATTERN = /(?:^|[/\\])(?:pi-)?mcp(?:-adapter)?(?:[/\\]|$)/i; export function isRecord(value: unknown): value is Record { return typeof value === "object" && value !== null && !Array.isArray(value); } export function toRecord(value: unknown): Record { if (!isRecord(value)) { return {}; } return value; } export function getTextField(value: unknown, field: string): string | undefined { const record = toRecord(value); const raw = record[field]; return typeof raw === "string" && raw.trim().length > 0 ? raw.trim() : undefined; } /** Native MCP tools are named `mcp____`; history renders before a server connects. */ export function isNativeMcpToolName(name: unknown): boolean { return typeof name === "string" && name.startsWith("mcp__"); } /** True when a tool's source metadata names an MCP adapter extension. */ function hasMcpSourceInfo(value: unknown): boolean { const sourceInfo = toRecord(value); for (const field of ["path", "source", "baseDir"] as const) { const raw = sourceInfo[field]; if (typeof raw === "string" && MCP_SOURCE_INFO_PATTERN.test(raw)) { return true; } } return false; } export function isMcpToolIdentity(tool: unknown): boolean { const record = toRecord(tool); return isNativeMcpToolName(record.name) || hasMcpSourceInfo(record.sourceInfo); }