Gold Inventory
Don't let the model convert the calendar: Jalali dates in an MCP server for a gold inventory
Why a gold inventory's MCP server converts Jalali dates itself, rejects days that don't exist, and returns every date in both calendars to AI agents.
In short
The panel shows Jalali dates while the database stores Gregorian ones, and AI agents are unreliable at converting between them, so the read-only MCP server does it. It accepts Jalali or Gregorian dates in Persian, Arabic or Latin digits, reads years 1200 to 1699 as Jalali, rejects days that don't exist such as 1404/12/30, and returns a Jalali field beside every date. Tests convert every day from 1990 through 2059 both ways.
The Gold Inventory Panel runs the stock of a gold business: coins, bars and plaques by weight and purity, the invoices and transfers that move them between branches, and the deals and prices around them. It's a Django REST back end with a React panel, built by a small team, and the panel shows dates the way its users read them: Jalali (Solar Hijri) for Persian, Islamic Hijri for Arabic.
The database stores Gregorian dates, which the panel hides by converting every date it draws. That stopped being invisible when I added a read-only MCP server so AI agents such as Claude Code and Codex could query the inventory: an agent reads the database's dates, and the people asking it questions speak in Jalali ones.
For a user, a wrong conversion is quiet and expensive: ask an assistant what came into the safe last Esfand and get a right-looking answer for the wrong days.
Think of a bureau de change that prints every price in both currencies. The tourist never does the arithmetic, so they can't get it wrong.
A boundary that moves
The Jalali year starts at Nowruz, around 20 or 21 March. The first six months have 31 days, the next five 30, and Esfand, the last, has 29 or 30 depending on a leap rule built on 33-year cycles. 1403 was a leap year, so 1403/12/30 is 20 March 2025. 1404 isn't, so 1404/12/30 doesn't exist.
That's the kind of arithmetic a language model does confidently and unreliably, and the module that handles it says as much: every date in the output carries its Jalali form so that no model has to do calendar arithmetic itself.
What a query does
An agent calls query_records on metal_items with {"entry_date__range": ["1404/10/01", "1404/12/29"]}. The server folds Persian and Arabic digits to Latin ones, so ۱۴۰۵/۰۸/۲۴ and 1405/08/24 are the same input, then reads the year: anything from 1200 to 1699 is Jalali, because no Gregorian date this system holds is that old.
A Jalali date is checked before it's converted: a month from 1 to 12, and a day that exists in that month of that year. 1404/12/30 is rejected with an error naming the missing day instead of rolling over to the next one. A valid date becomes a Gregorian date and the ORM filters on it as usual; on a timestamp column such as created_at, a bare date means midnight in Tehran, the server's time zone.
On the way out, each date and datetime gets a sibling field in the panel's format, with datetimes in local time:
{ "entry_date": "2026-11-15",
"entry_date_jalali": "1405/08/24",
"created_at_jalali": "1405/08/24 14:05" }
Aggregates do the same, so grouping by entry_date or taking its max returns the Jalali form too, and the tool description tells the agent to quote that field to Persian speakers rather than convert it.
Why not let the agent convert
The alternative is to return ISO dates and trust the model. Agents handle ISO 8601 well and 33-year leap cycles badly, and the failure would be silent: an off-by-one at a month boundary would move stock into the wrong month's answer. Converting on the server costs a few more bytes per record and one short module, and makes the answer the same for every agent.
The conversion is the Borkowski/jalaali algorithm, a short pure-Python port exact for Jalali years −61 to 3177. Weights and prices follow the same thinking: they come back as strings, to keep their exact precision.
The year rule is a deliberate trade-off. A Gregorian year between 1200 and 1699 can't be written, and for an inventory with no records that old, that's a fair price for not needing a calendar flag on every filter.
How I checked it
JalaliCalendarTests converts known dates both ways: Nowruz in several years, the last day of leap year 1403, the last day of 1404, the boundary between the sixth and seventh months and older anchors such as 1357/11/22. Another test walks every day from 1990 through 2059, more than 25,000 of them, converts each to Jalali and back, and checks the Jalali dates strictly increase, so there are no gaps or repeats. A third checks that 1404/12/30, 1405/07/31 and month 13 all raise.
PersianToolTests goes through the real MCP endpoint with fixture data. A Jalali filter, in Latin or Persian digits, finds the item entered on 2026-11-15; a Dey-to-Esfand 1404 range finds the five fixture items in it; 1404/12/30 comes back as an error. Records and aggregates carry the _jalali fields.
What tests can't show is that an agent will always quote the field instead of converting anyway: the description asks it to, and nothing forces it. And the MCP output is Jalali only; the panel's Hijri view has no counterpart there.
A date taken from the wrong record
The same class of bug turned up in the panel itself. Coins of the same type fold into one pool, and each new entry records an incoming transaction for what it added. That transaction was dated by the pool's first entry, so one coin added in October looked like it arrived in July with the first ten. The fix dates each transaction by its own entry, and test_folded_coin_entry_is_dated_by_its_own_entry_date checks the ten land on 2026-07-03 and the one on 2026-10-27, while the pool keeps its first date.
What carries over
- Convert calendars where the data lives, and return both forms.
- Validate a date in the calendar it was written in before converting it.
- Normalize digits on input; return text exactly as stored.
- Test a calendar converter by walking every day of a long range.
- When a value could come from two records, test which one it comes from.
For the business, someone can ask an assistant about last Esfand's stock and get the same days the panel would show. The database stays Gregorian, conversion happens once in tested server code, and every date leaves the server in both calendars, so a model that can't do the arithmetic never has to.
Further reading
- Solar Hijri calendar, Wikipediaen.wikipedia.org
- Model Context Protocol specificationmodelcontextprotocol.io
- Django, time zonesdocs.djangoproject.com
- jalaali-js, the reference implementation of this algorithmgithub.com
More writing
-
A cold start that draws last launch: persisting the TanStack Query cache in a mobile word game
How ClueShot restores last launch's TanStack Query cache as stale, checks whose it is, and revalidates with ETags so a cold start never opens empty.

-
Two failure handlers, one refund: idempotent cleanup in an async AI image pipeline
How a BullMQ image worker, its failed event and a Telegram bot all reach one refund in MrPicasso, and how two Redis claims keep it to one credit.

-
Two tabs, one student: computing class attendance as a union of intervals in Django
How a Django backend turns Jitsi join and leave events into class attendance, and why it merges overlapping intervals instead of summing them.
