Purujit Vij
← all work

case study ·

Keeping an AI astrologer honest: a Vedic engine and a 30-tool MCP server

LLMs make up planetary positions, and the real chart is 146 KB of JSON that drowns them. I built a Vedic astrology engine within 39″ of Swiss Ephemeris and an MCP server that hands the model verified numbers, one small block at a time.

  • TypeScript
  • Python
  • MCP
  • Vercel
  • Unkey
Role
Solo project: engine, MCP server, API and hosting
Published
20 Nov 2026
Read
6 min

the results

  • <39″worst planet-position error vs Swiss Ephemeris, 21 charts, 1900–2050
  • 146 → 7 KBdefault chart response after field selection
  • 30MCP tools, each returning one block of the chart
  • 526engine tests, plus 77 for the MCP server

The problem

Ask an LLM for a birth chart and it will happily give you one: planets, signs, nakshatras, dasha periods, all in a confident tone and mostly made up. Astrology numbers are pure computation, ephemeris maths on a birth date, time and place. A language model can’t do that maths, but it never says so.

So the rule I started from: the model explains, it never calculates. Every number has to come from a real engine. That moved the problem instead of solving it, because the real data has its own failure mode. A full chart with every block is 146 KB of JSON, roughly 37,000 tokens by the usual four-characters-a-token estimate. Hand that to a model and it crowds out the conversation, gets slow and expensive, and starts reading the wrong field.

Two things to control, then: made-up answers, and tool outputs too long to use.

The engine: numbers the model can’t argue with

I wrote the calculation engine in TypeScript: planets, houses in four systems, 15 divisional charts, five dasha systems, 300+ yogas, Shadbala, Ashtakavarga, KP, Jaimini, transits, annual charts and compatibility matching. It runs as a library and as a REST API.

Accuracy is measured, not assumed. A benchmark test compares the engine against Swiss Ephemeris, the standard reference, on 21 charts from 1900 to 2050 and from 69.6°N to 33.9°S:

Body Max error Mean error
Sun 17.4″ 11.7″
Moon 24.6″ 11.8″
Venus (worst) 38.6″ 16.9″
Ascendant 32.9″ 5.6″

All 168 retrograde flags match. The test asserts a 60″ ceiling, so an accuracy regression fails the build instead of reaching a user. Shadbala components were cross-checked against JHora and AstroSage. 526 tests cover the engine.

The reference positions come from a Python script I wrote with pyswisseph, the Python binding for Swiss Ephemeris. The TypeScript tests then check every chart against them. The benchmark caught real bugs: the lunar node was up to 2.8° off, the Ascendant came out 180° wrong at 69.6°N, the Lahiri ayanamsa wobbled by ±17″ over an 18.6-year cycle, and retrograde flags were wrong near a planet’s stations.

Controlling hallucinations

The model never calculates

The MCP server has no astrology logic of its own. Every chart tool is one HTTPS call to the engine API and returns the engine’s JSON as-is; the only other tool, place search, calls a geocoding API. The model’s job is to pick the tool and explain the result.

Every answer says how it was computed

Astrology has competing conventions: which ayanamsa, which house system, true or mean nodes, which dosha rules. A model that doesn’t know which one was used will guess. So every engine response carries a meta block that states the conventions in force, and meta.included lists exactly which blocks were returned. The model can quote the method instead of inventing one.

“Not available” beats a plausible guess

Kalachakra dasha needs per-pada tables whose conventions disagree across sources. An early version shipped a simplified model marked experimental. I replaced it: the engine now returns status: "unavailable", an empty period list and a note saying why, and the tool description says “NOT IMPLEMENTED yet”. A confident wrong sequence is worse than none, because a caller can’t tell it apart from a real one.

Put the caveats where the model reads them

Tool descriptions are written for the model, not for humans. The dosha tool says that a Mangal dosha seen only from the Moon or Venus is a partial indication, and that a full Manglik verdict needs it from the Lagna (the fromLagna field). That one sentence stops the most common overclaim in this domain.

Don’t let it guess the birth place

Every tool needs latitude, longitude and an IANA timezone, and models will happily guess coordinates. search_birth_places comes first (“Always use this first when the user gives a place name”) and returns real coordinates and timezones from a geocoding API. Two details mattered. The API ranks by population, so “Solan” came back after “Solana”; exact name matches now sort first. And if the geocoder is down, the error tells the model to ask the user for coordinates, not to make them up.

Reject impossible input, by field

The input schema checks shape and reality: 30 February, 23:60 and Asia/Nowhere are rejected before they reach the engine, with the field named (“2026-02-30 is not a real calendar date”). Errors come back as tool content marked isError, not as exceptions, so the model reads the message and fixes its next call instead of retrying blind or giving up.

Controlling long tool outputs

One block per tool

Instead of one get_chart tool returning everything, each tool asks the engine for one block through field selection. The engine only computes what was requested, and the API defaults to a small core set:

Request Size
Everything, dashas 3 levels deep (the old default) 146 KB
generate_kundli (core chart) 7 KB
get_vimshottari_dasha, 2 levels 8 KB
get_planets 4 KB
get_yogas 3 KB

The knobs that make blocks big are opt-in: dasha depth defaults to 2 (3 adds every sub-sub-period), one dasha system per tool, and divisional charts can be limited to the ones asked about.

Many small calls, one computation

Splitting a chart into many tool calls could have meant computing it many times. The engine caches charts by birth data only, never by selection, so every block for the same person is projected from one computation.

Picking from 30 tools

Thirty tools is a lot to choose from, so the descriptions say when to use each one, not just what it returns: generate_kundli says “Start here for any birth-chart question”, Vimshottari says “use this unless another system is asked for”, and divisional charts name what D9 and D10 are for.

Shipping it

The server runs two ways: over stdio for Claude Desktop and Claude Code, and as a hosted Streamable HTTP endpoint on Vercel, stateless, so each request is a fresh serverless call.

The hosted endpoint is a paid product, so the gate is careful:

  • Keys are verified with Unkey and must carry a kmcp_ prefix, so an engine API key can never open the MCP endpoint.
  • Only tools/call costs a credit. Protocol chatter like initialize and tools/list is free, so quotas count real usage.
  • Any Unkey error fails closed, and with no keys configured the endpoint is open for local development but closed in production.
  • Static keys are compared in constant time, and browser Origin headers are checked against an allowlist.

77 tests run offline against a stubbed engine, checking each tool’s request wiring, input validation, auth and error handling, and 3 live smoke tests run against the real API.

What I learned

  • Let the LLM explain, not compute. Deterministic work belongs in code with a benchmark behind it.
  • An honest “unavailable” is a feature. It’s the only output a model can’t turn into a confident wrong answer.
  • Output size is a design decision. Default small, make big opt-in, and cache so splitting is free.
  • Put caveats in the data and in the tool description. The model reads both, and that’s where overclaims get stopped.
  • Name the field in every error. The model fixes its own call on the next try.

let's talk!

Hiring for AI or platform engineering?

I like ambiguous 0→1 problems: turning them into clear system designs, and shipping software that behaves safely in production.