unknownOptions

tcmts.cli.mcp.unknownOptions · #7ridmyu7 · term · runs anywhere

The arguments a call gives that its input schema doesn't have, where the schema allows nothing else (additionalProperties: false), in objects and arrays of them too: each by where it is (patches[0].imports), with the options known there.

export function unknownOptions(
  inputSchema: Record<string, unknown>,
  value: unknown,
  at = "",
): { at: string; known: string[] }[] {
  if (Array.isArray(value)) {
    const items = inputSchema.items as Record<string, unknown> | undefined;
    return items ? value.flatMap((v, i) => unknownOptions(items, v, `${at}[${i}]`)) : [];
  }
  if (!value || typeof value !== "object" || inputSchema.type !== "object") return [];
  const props = (inputSchema.properties ?? {}) as Record<string, Record<string, unknown>>;
  return Object.entries(value as Record<string, unknown>).flatMap(([k, v]) => {
    const where = at ? `${at}.${k}` : k;
    if (Object.hasOwn(props, k)) return unknownOptions(props[k]!, v, where);
    return inputSchema.additionalProperties === false
      ? [{ at: where, known: Object.keys(props).sort() }]
      : [];
  });
}

used by