ERROR: _.reduce is not a function in jSq token counting function

Resolved 💬 2 comments Opened Mar 24, 2026 by morluto Closed Apr 22, 2026

Bug Description

The jSq function (token counting logic) throws TypeError: _.reduce is not a function when processing MCP tool output that is neither a string nor an array.

Error Stack

ERROR  _.reduce is not a function. (In '_.reduce((T,q)=>{if(oY7(q))return T+CK(q.text);else if(sY7(q))return T+RZT;return T},0)', '_.reduce' is undefined)

/$bunfs/root/src/entrypoints/cli.js:2348:1772

function jSq(_){if(!_)return 0;if(typeof _==="string")return CK(_);return _.reduce((T,q)=>{if(oY7(q))return T+CK(q.text);else if(sY7(q))return T+RZT;return T},0)}

Root Cause

The jSq function assumes that if the input is not falsy and not a string, it must be an array:

function jSq(_) {
  if (!_) return 0;
  if (typeof _ === "string") return CK(_);
  return _.reduce((T, q) => { ... }, 0);  // Crashes here if _ is not an array
}

However, MCP tools can return content in various formats (objects, numbers, etc.) that don't have a .reduce() method.

Suggested Fix

Add an Array.isArray() check before calling .reduce():

function jSq(_) {
  if (!_) return 0;
  if (typeof _ === "string") return CK(_);
  if (!Array.isArray(_)) return 0;  // or handle non-array types appropriately
  return _.reduce((T, q) => { ... }, 0);
}

Environment

  • Claude Code version: Latest (bundled)
  • OS: macOS
  • Trigger: MCP tool output processing

Workaround

Set MAX_MCP_OUTPUT_TOKENS=0 to disable token limiting, or avoid MCP tool patterns that return non-array/non-string content.

View original on GitHub ↗

This issue has 2 comments on GitHub. Read the full discussion on GitHub ↗