Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

fix(mcp-host): read a record row's index keys, and one column per header spelling

marcoteb015389f0971f
diff --git a/mcp-host/src/workflow/__tests__/tableRows.test.ts b/mcp-host/src/workflow/__tests__/tableRows.test.ts
index 422231039..19fe818b4 100644
--- a/mcp-host/src/workflow/__tests__/tableRows.test.ts
+++ b/mcp-host/src/workflow/__tests__/tableRows.test.ts
@@ -1,5 +1,6 @@
import { describe, expect, it } from 'vitest'
import { headerText, normalizeTableRows } from '../tableRows'
+import { sheetTable } from '../xlsxSheet'
describe('normalizeTableRows', () => {
it('passes array rows through untouched', () => {
@@ -31,6 +32,29 @@ describe('normalizeTableRows', () => {
])
})
+ it('reads a key that names no header but a 0-based column, as the schema example does', () => {
+ const warnings: string[] = []
+ const rows = normalizeTableRows(
+ [{ '0': 'Ana', '1': 1200 }, { Name: 'Luis', '0': 'ignored', ' 1 ': 900 }, { '2': 'x' }],
+ ['Name', 'Amount'],
+ 'tables[0]',
+ warnings
+ )
+ expect(rows).toEqual([
+ ['Ana', 1200],
+ ['Luis', 900],
+ [undefined, undefined],
+ ])
+ expect(warnings[1]).toBe(
+ "tables[0]: the key(s) '2' match no header, so those values were left out."
+ )
+ })
+
+ it('reads a key that is a header before reading it as a column index', () => {
+ const rows = normalizeTableRows([{ '1': 'by name', '0': 'first' }], ['Q', '1'], 't', [])
+ expect(rows).toEqual([['first', 'by name']])
+ })
+
it('takes record values in order when there are no headers', () => {
expect(normalizeTableRows([{ a: 1, b: 2 }], undefined, 's', [])).toEqual([[1, 2]])
})
@@ -56,3 +80,24 @@ describe('headerText', () => {
expect(headerText('Q1')).toBe('Q1')
})
})
+
+describe('sheetTable', () => {
+ it('makes one column of record keys that differ only in case or surrounding space', () => {
+ const warnings: string[] = []
+ const table = sheetTable(
+ {
+ rows: [
+ { Name: 'Ana', Amount: 1 },
+ { 'name ': 'Luis', amount: 2 },
+ ],
+ },
+ 'sheets[0]',
+ warnings
+ )
+ expect(table.header).toEqual(['Name', 'Amount'])
+ expect(table.rows).toEqual([
+ ['Ana', 1],
+ ['Luis', 2],
+ ])
+ })
+})
diff --git a/mcp-host/src/workflow/internalTools.ts b/mcp-host/src/workflow/internalTools.ts
index 41f01352b..a035a6d8e 100644
--- a/mcp-host/src/workflow/internalTools.ts
+++ b/mcp-host/src/workflow/internalTools.ts
@@ -117,11 +117,12 @@ const CELL_TYPES = ['string', 'number', 'boolean', 'null']
/**
* A row sent as a {header: cell} record, which every runtime reads by header
* name. Gemini rejects an object schema with no properties, so it declares one
- * example key; additionalProperties keeps the others valid.
+ * example key; additionalProperties keeps the others valid. The example is a
+ * column index, which the runtime reads as that column.
*/
const RECORD_ROW_SCHEMA = {
type: 'object',
- properties: { '0': { type: CELL_TYPES, description: 'Cell.' } },
+ properties: { '0': { type: CELL_TYPES, description: 'Column 0, or a header.' } },
additionalProperties: { type: CELL_TYPES },
}
diff --git a/mcp-host/src/workflow/tableRows.ts b/mcp-host/src/workflow/tableRows.ts
index 0912451c4..0f31a9d6e 100644
--- a/mcp-host/src/workflow/tableRows.ts
+++ b/mcp-host/src/workflow/tableRows.ts
@@ -20,11 +20,17 @@ export function columnNames(headers: string[], columns: number[]): string {
return `${names.slice(0, 3).join(', ')} and ${names.length - 3} more column(s)`
}
+/** A record key as it is matched against a header: case and surrounding space aside. */
+export function headerKey(key: unknown): string {
+ return headerText(key).trim().toLowerCase()
+}
+
/**
* `rows` as arrays of cells. A record is read by header name, exactly or
- * ignoring case and surrounding space; without headers its values are taken in
- * key order. Anything else is dropped. Each kind of repair is reported once in
- * `warnings`, prefixed with `label`.
+ * ignoring case and surrounding space; a key that names no header but is a
+ * 0-based column index, such as "0", fills that column. Without headers its
+ * values are taken in key order. Anything else is dropped. Each kind of repair
+ * is reported once in `warnings`, prefixed with `label`.
*/
export function normalizeTableRows(
rows: unknown,
@@ -37,7 +43,11 @@ export function normalizeTableRows(
warnings.push(`${label}: rows must be an array of rows; they were left out.`)
return []
}
- const names = (headers ?? []).map(h => headerText(h).trim().toLowerCase())
+ const names = (headers ?? []).map(headerKey)
+ const columnAt = (key: string): number | undefined => {
+ const t = key.trim()
+ return /^\d{1,9}$/.test(t) && Number(t) < names.length ? Number(t) : undefined
+ }
let fromRecords = 0
let dropped = 0
const unmatched = new Set<string>()
@@ -51,15 +61,21 @@ export function normalizeTableRows(
out.push(Object.values(row))
continue
}
- const byName = new Map(Object.entries(row).map(([k, v]) => [k.trim().toLowerCase(), v]))
- for (const key of Object.keys(row)) {
- if (!names.includes(key.trim().toLowerCase())) unmatched.add(key)
+ const byName = new Map(Object.entries(row).map(([k, v]) => [headerKey(k), v]))
+ const byIndex = new Map<number, unknown>()
+ for (const [key, value] of Object.entries(row)) {
+ if (names.includes(headerKey(key))) continue
+ const column = columnAt(key)
+ if (column === undefined) unmatched.add(key)
+ else byIndex.set(column, value)
}
out.push(
(headers ?? []).map((h, i) =>
Object.prototype.hasOwnProperty.call(row, headerText(h))
? row[headerText(h)]
- : byName.get(names[i])
+ : byName.has(names[i])
+ ? byName.get(names[i])
+ : byIndex.get(i)
)
)
} else {
@@ -68,7 +84,8 @@ export function normalizeTableRows(
}
if (fromRecords > 0) {
warnings.push(
- `${label}: ${fromRecords} row(s) were objects and were read by header name; send each row as an array of cells in header order.`
+ `${label}: ${fromRecords} row(s) were objects and were read by header name; send each ` +
+ 'row as an array of cells in header order.'
)
}
if (unmatched.size > 0) {
diff --git a/mcp-host/src/workflow/xlsxSheet.ts b/mcp-host/src/workflow/xlsxSheet.ts
index c6f6e63c5..4d98736e9 100644
--- a/mcp-host/src/workflow/xlsxSheet.ts
+++ b/mcp-host/src/workflow/xlsxSheet.ts
@@ -3,7 +3,7 @@
* arguments point at, and the colors it names.
*/
import { own } from './ownEntry'
-import { headerText, normalizeTableRows } from './tableRows'
+import { headerKey, headerText, normalizeTableRows } from './tableRows'
import { cellProblem } from './xlsxCells'
function isRecord(value: unknown): value is Record<string, unknown> {
@@ -68,9 +68,14 @@ export function sheetTable(
const rest = rows.filter((_, i) => i !== first)
return { header, rows: normalizeTableRows(rest, header, label, warnings) }
}
- const keys = new Set<string>()
- for (const row of rows) if (isRecord(row)) for (const k of Object.keys(row)) keys.add(k)
- const header = [...keys]
+ // Keys that differ only in case or surrounding space name one column, which
+ // takes the first spelling; rows read every spelling into it.
+ const keys = new Map<string, string>()
+ for (const row of rows) {
+ if (!isRecord(row)) continue
+ for (const k of Object.keys(row)) if (!keys.has(headerKey(k))) keys.set(headerKey(k), k)
+ }
+ const header = [...keys.values()]
return { header, rows: normalizeTableRows(rows, header, label, warnings) }
}