Files
plugins/packages/calc
yetone bd6e1ed387 tools-calc 0.1.0: exact arithmetic, unit conversion and date math for every agent
A new kind of package: an OpenCode plugin whose only hook is `tool`,
which magpie serves to every agent on /mcp/magpie/tools. Three tools:
eval (exact rational arithmetic in BigInt fractions), convert (data,
time, length, CSS, mass, angle, temperature) and date (calendar-aware
add, between, weekday/ISO week, IANA zones through Intl, DST included).
No network, no key, no dependency.

Run:
- bun test packages/calc: 8 tests, 73 expects, pass with TZ=UTC and
  TZ=Pacific/Kiritimati.
- A sandbox magpie built from yetone/magpie origin/main b03031738
  (nogui, HOME in a scratch dir, gateway on 127.0.0.1:3591):
  `magpie plugin add <folder>`; `magpie plugin` lists
  "tools calc_convert, calc_date, calc_eval". Over /mcp/magpie/tools:
  initialize, tools/list (3 tools with their schemas), tools/call
  calc_eval 0.1+0.2 -> {"exact":"3/10","decimal":"0.3"}, 1/0 -> isError
  "division by zero", calc_convert 1 GiB->MB -> 16777216/15625
  (1073.741824), calc_date across New York's DST with `to` -> between.
Not tried: an npm install. From npm the tools are named tools-calc_*,
since magpie's ShortName doesn't strip "tools-".
2026-10-16 02:57:29 +08:00
..

@magpie-community/tools-calc

Exact arithmetic, unit conversion and date math, as tools magpie offers every agent it connects. Models get arithmetic wrong (0.1 + 0.2, big powers, days between two dates across a DST change), so this gives them a calculator. It needs no network, no key and no dependency.

Install it from magpie's Plugins › Discover, or with:

magpie plugin add @magpie-community/tools-calc
magpie plugin            # lists its tools

magpie serves the tools on its MCP server, /mcp/magpie/tools, to every agent it has connected. Each tool's name starts with the plugin's: tools-calc_eval when installed from npm, calc_eval from a folder (magpie plugin add ./packages/calc).

Tools

eval {expression, places?} evaluates an expression exactly, in fractions, with no floating point.

  • Operators: + - * / % ^ (or **), parentheses, and unary minus.
  • Numbers: decimals, 1e9, 0x/0b/0o literals, and 1_000.
  • Functions: abs floor ceil round trunc min max gcd lcm fact.
  • It answers {exact, decimal}, plus rounded: true when the decimal was cut at places (20 by default).
0.1+0.2          → {"exact":"3/10","decimal":"0.3"}
2^128/3          → every digit, exact
1/0              → an error: division by zero

convert {value, from, to, places?} converts a quantity exactly. value can be an expression, such as 3*1024.

Kind Units
Data bit B kB MB GB TB (1000s), KiB MiB GiB TiB (1024s), kb Mb Gb
Time ns us ms s min h d wk
Length nm um mm cm m km in ft yd mi nmi
CSS px pt pc
Mass mg g kg t oz lb
Angle deg turn grad
Temperature C F K
1 GiB → MB       → {"exact":"16777216/15625","decimal":"1073.741824","unit":"MB"}

date {date?, add?, to?, timeZone?} does date math on the real calendar.

  • date is ISO 8601, unix seconds or milliseconds, or now (the default). It answers the ISO time, weekday, ISO week, day of year and unix time.
  • add is a duration, such as 3d 4h, -90m, 1y2mo or ISO P1DT2H. Years, months and days count on the calendar, so Jan 31 + 1mo is the last day of February, and a day across a DST change is still a day.
  • to is a second date. The answer then also has between, the time from date (after add) to to, in days, hours, minutes, seconds and calendar years, months and days.
  • timeZone is an IANA zone, such as Asia/Shanghai. Dates with no offset are read as that zone's wall time, and the answer shows the local time. Without it, they are UTC.

Test

bun test packages/calc

中文:给 magpie 连接的每个 agent 提供精确计算工具:eval 用分数精确计算表达式,convert 换算数据、时间、长度、质量、角度和温度单位,date 按真实日历做日期加减和间隔(支持时区与夏令时)。不联网,不需要 key。