feat: normalize filter values to array format for Grist API

The Grist API requires all filter values to be arrays. This change adds
automatic normalization of filter values in get_records, wrapping single
values in lists before sending to the API.

This fixes 400 errors when filtering on Ref columns with single integer IDs.

Changes:
- Add filters.py module with normalize_filter function
- Update get_records to normalize filters before API call
- Add Orders table with Ref column to mock Grist server
- Add filter validation to mock server (rejects non-array values)
- Fix shell script shebangs for portability (#!/usr/bin/env bash)
This commit is contained in:
2026-01-14 17:56:18 -05:00
parent c868e8a7fa
commit a97930848b
8 changed files with 244 additions and 5 deletions

View File

@@ -0,0 +1,37 @@
"""Filter normalization for Grist API queries."""
from typing import Any
def normalize_filter_value(value: Any) -> list:
"""Ensure a filter value is a list.
Grist API expects filter values to be arrays.
Args:
value: Single value or list of values.
Returns:
Value wrapped in list, or original list if already a list.
"""
if isinstance(value, list):
return value
return [value]
def normalize_filter(filter: dict | None) -> dict | None:
"""Normalize filter values to array format for Grist API.
Grist expects all filter values to be arrays. This function
wraps single values in lists.
Args:
filter: Filter dict with column names as keys.
Returns:
Normalized filter dict, or None if input was None.
"""
if not filter:
return filter
return {key: normalize_filter_value(value) for key, value in filter.items()}

View File

@@ -2,6 +2,7 @@
from grist_mcp.auth import Agent, Authenticator, Permission
from grist_mcp.grist_client import GristClient
from grist_mcp.tools.filters import normalize_filter
async def list_tables(
@@ -56,7 +57,10 @@ async def get_records(
doc = auth.get_document(document)
client = GristClient(doc)
records = await client.get_records(table, filter=filter, sort=sort, limit=limit)
# Normalize filter values to array format for Grist API
normalized_filter = normalize_filter(filter)
records = await client.get_records(table, filter=normalized_filter, sort=sort, limit=limit)
return {"records": records}