Tin

Examples · mcp · 324 of 339

units

A Tin program as an MCP server: its tool fns become MCP tools, with schemas from the types.

examples/mcp/units.tintin run examples/mcp/units.tin
// A Tin program as an MCP server: its tool fns become MCP tools, with schemas from the types.
//
//   tin mcp examples/mcp/units.tin
//
// Add it to any MCP client (Claude Desktop, Claude Code, Cursor, ...) as a stdio server:
//   { "command": "tin", "args": ["mcp", "/path/to/units.tin"] }

/// A temperature in one unit, converted to the others.
type Temperatures = {
  celsius: float,
  fahrenheit: float,
  kelvin: float,
}

/// Convert a temperature. `unit` is "C", "F" or "K".
tool fn convert(
  /// the temperature to convert
  value: float,
  /// its unit: "C", "F" or "K"
  unit: string,
): Result<Temperatures, string> {
  let c = match unit {
    "C" => value,
    "F" => (value - 32.0) * 5.0 / 9.0,
    "K" => value - 273.15,
    _ => return Err("unknown unit '{unit}': use C, F or K"),
  }
  if c < -273.15 {
    return Err("{value}{unit} is below absolute zero")
  }
  Ok(Temperatures { celsius: c, fahrenheit: c * 9.0 / 5.0 + 32.0, kelvin: c + 273.15 })
}

/// How many seconds are in a number of minutes, hours or days.
tool fn seconds(amount: int, unit: string): int {
  match unit {
    "min" => amount * 60,
    "h" => amount * 3600,
    "d" => amount * 86400,
    _ => amount,
  }
}