Skip to content

[bug] Generated CLI array parsing is broken if the array items are JSON objects #103

@dljsjr

Description

@dljsjr

This is related to the same tool call and schema as #102 so I won't reproduce it here, but if you look at that schema, you can see that the --cells flag wants an array of items, and those items are JSON objects.

However, the logic that's generated for handling that flag looks like this:

.requiredOption(
		"--cells <cells:value1,value2>",
		"Array of cells to set (max 10,000). Each cell can specify any combination of char, color, bgColor. (example: value1,value2)",
		(value) => value.split(",").map((v) => v.trim()),
	)

That means if we pass JSON objects in this array, we'll split the string on the commas that would separate the JSON properties.

If I inject logging here:

const args = cmdOpts.raw
	? JSON.parse(cmdOpts.raw)
	: ({} as Record<string, unknown>);
if (cmdOpts.cells !== undefined) args.cells = cmdOpts.cells;
// new logging statements
console.log('args.cells: ');
console.table(args.cells);

We can see that it breaks the payload up incorrectly, and we get a rejection of the call with way more elements than we intended:

$ ./ascii-motion.ts set-cells-batch --cells '{"x": 50, "y": 15, "char": "█"},{"x": 51, "y": 15, "char": "█"}'
args.cells:
┌───┬──────────────┐
│   │ Values       │
├───┼──────────────┤
│ 0 │ {"x": 50     │
│ 1 │ "y": 15      │
│ 2 │ "char": "█"} │
│ 3 │ {"x": 51     │
│ 4 │ "y": 15      │
│ 5 │ "char": "█"} │
└───┴──────────────┘
MCP error -32602: Input validation error: Invalid arguments for tool set_cells_batch: [
  {
    "code": "invalid_type",
    "expected": "object",
    "received": "string",
    "path": [
      "cells",
      0
    ],
    "message": "Expected object, received string"
  },
  {
    "code": "invalid_type",
    "expected": "object",
    "received": "string",
    "path": [
      "cells",
      1
    ],
    "message": "Expected object, received string"
  },
  {
    "code": "invalid_type",
    "expected": "object",
    "received": "string",
    "path": [
      "cells",
      2
    ],
    "message": "Expected object, received string"
  },
  {
    "code": "invalid_type",
    "expected": "object",
    "received": "string",
    "path": [
      "cells",
      3
    ],
    "message": "Expected object, received string"
  },
  {
    "code": "invalid_type",
    "expected": "object",
    "received": "string",
    "path": [
      "cells",
      4
    ],
    "message": "Expected object, received string"
  },
  {
    "code": "invalid_type",
    "expected": "object",
    "received": "string",
    "path": [
      "cells",
      5
    ],
    "message": "Expected object, received string"
  }
]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions