Skip to main content
Glama

aiml-calc

Classic AI and machine-learning algorithms, implemented from scratch in pure Python and worked step by step: every result comes with the full trace, a second method or independent check where one exists, and the conventions the algorithm used. It follows Russell & Norvig's Artificial Intelligence: A Modern Approach (AIMA), with an interactive web UI and an MCP server so AI assistants can call the same algorithms.

Try it live: aiml.dannypenrose.com. No install needed; the public site turns off the ▶ Run button, so run the generated code on your own machine.

A* search on a weighted graph

Alpha-beta pruning on a game tree

Exact inference in a Bayesian network

k-means clustering

What's inside

Area

Algorithms

Search (AIMA ch. 3)

Breadth-first, depth-first, uniform-cost, greedy best-first, A*, depth-limited, iterative deepening, IDA*; graph and tree search; heuristic admissibility, consistency and dominance checks

Propositional logic (ch. 7)

Truth tables and model counting, CNF conversion, entailment by model checking, resolution refutation, forward and backward chaining

Satisfiability (ch. 7)

DPLL (pure symbol and unit clause rules), WalkSAT

First-order logic (ch. 9)

Unification with occur check, forward chaining (FOL-FC-ASK), backward chaining with standardising apart

Constraint satisfaction (ch. 6)

Backtracking with MRV, degree and least-constraining-value heuristics; forward checking; MAC; AC-3

Game search (ch. 5)

Minimax, alpha-beta pruning, expectiminimax

Probability (ch. 13–14, 20)

Bayes' rule, inference from a full joint distribution, Bayesian networks by enumeration, naive Bayes

Machine learning (ch. 18; Witten et al. ch. 4)

k-means, k-nearest neighbour, least-squares regression, gradient descent, logistic regression, perceptron learning, feed-forward networks with back-propagation, CNN layer sizes

Design choices

  • Explain, don't just calculate. Every view opens with a one-line overview and three panels. New to this? Start here assumes no background: an everyday analogy, a list of words to know, the steps in plain words, the maths explained symbol by symbol, a tiny worked example (every number checked against the implementation) and how the work grows as the problem gets bigger. How it works is the technical version: key formulas, properties (completeness, optimality), a Cost section giving time and space complexity with what each variable means, pitfalls and the textbook section to read next. When and where to use this says when the algorithm is the right tool (and when it isn't), with real-world and software-engineering examples. Every input field has a tooltip saying what it means.

  • Explain the answer, not just the method. Under every answer, How it got this answer walks through the steps this particular problem took, using its actual numbers: which node was expanded and why, which rule fired, how each probability was multiplied and normalised. It connects the general method to the specific result.

  • Show the code. Every view generates a standalone, readable Python program for the algorithm with the values on the page built in. Change an input and the code updates; edit the code and press Run to try your own changes, or download it and run it anywhere with no dependencies.

  • Show the working, not just the result. Each algorithm returns a step-by-step trace (frontier and explored set per step, α and β per node, every arc revision in AC-3, each enumeration term in a Bayes net) as well as the result.

  • Check every result two ways. CNF is verified against the truth table; resolution against model checking; backward chaining against forward chaining; Bayes-net enumeration against the full joint distribution built from the chain rule; alpha-beta against plain minimax; regression by two closed forms.

  • Make hidden conventions explicit. Implementations of the same algorithm quietly disagree on when to goal-test, how to break ties, and whether graph search re-adds frontier states. Each of these is a named parameter, and every trace states which convention it used and flags ties.

  • No numerical libraries in the algorithms. Everything from Gaussian elimination to back-propagation is written out, so the code reads like the textbook pseudocode.

Related MCP server: agent-core-mcp

Setup

You need Python 3.10 or later and git. Check your Python version with python3 --version. On macOS, if it shows 3.9 or older, install a newer one with brew install python@3.12 and use python3.12 in place of python3 below.

git clone https://github.com/<you>/aiml-calc.git
cd aiml-calc
python3 -m venv .venv                 # a private environment inside the project folder
.venv/bin/pip install -e ".[ui]"      # the algorithms, the MCP server and the web UI

On Windows, use .venv\Scripts\ wherever these instructions say .venv/bin/.

What gets installed:

Install

Includes

pip install -e .

the algorithms and the MCP server (aiml-calc)

pip install -e ".[ui]"

plus the web UI (aiml-calc-ui)

pip install -e ".[dev]"

plus everything needed to run the tests

Run

Web UI

.venv/bin/aiml-calc-ui

This opens http://localhost:8501 in your browser. Keep the terminal open while you use it, and press Ctrl+C to stop. To use another port: .venv/bin/aiml-calc-ui --server.port 8600.

The sidebar groups the 27 views by area (Search, Propositional logic, Satisfiability, …), and every view has its own address, so you can link straight to one, e.g. http://localhost:8501/bayes-net or /search-trace. The sidebar groups open on click, and the search box at the top finds an algorithm by name or keyword (A*, backprop, pruning…). The home page has an overview table of when to use each algorithm, with real-world and software examples.

Each view has:

  • Built-in examples and a tooltip on every field.

  • 🌱 New to this? Start here: the idea in plain words, words to know, the maths, a tiny example and how long it takes.

  • 📖 How it works (technical): formulas, properties, time and space complexity, pitfalls and references.

  • 🌍 When and where to use this: when to reach for it, and real-world and software-engineering uses.

  • 🪜 How it got this answer: a numbered, plain-language walkthrough of how this answer was reached, with the actual numbers, followed by the full working.

  • 💻 Code (top right of each page): opens the algorithm as a standalone program with your inputs built in. Edit and run gives you a syntax-highlighted editor that grows with the code; change it and press ▶ Run, and the dialog switches to the Output tab to show what it printed (runs locally with your Python, 20-second limit); Read shows it with highlighting. ⬇ Download saves it as a .py file that runs with plain python, no installs needed.

Running code is turned off when the app is deployed publicly (see below), because it would let anyone run code on the server. The code is still there to read, copy and download.

As a Python library

from aiml_calc import games, probability

games.alphabeta([[3, 12, 8], [2, 4, 6], [14, 5, 2]])["pruned"]      # ['I', 'J']

burglary = {
    "Burglary": {"cpt": {"": 0.001}}, "Earthquake": {"cpt": {"": 0.002}},
    "Alarm": {"parents": ["Burglary", "Earthquake"], "cpt": {"T,T": 0.95, "T,F": 0.94, "F,T": 0.29, "F,F": 0.001}},
    "JohnCalls": {"parents": ["Alarm"], "cpt": {"T": 0.90, "F": 0.05}},
    "MaryCalls": {"parents": ["Alarm"], "cpt": {"T": 0.70, "F": 0.01}},
}
probability.bayes_net_query(burglary, "Burglary", {"JohnCalls": True, "MaryCalls": True})["distribution"]
# {'true': 0.284..., 'false': 0.715...}

Every function also returns a markdown field with the full worked trace.

Run the tests

.venv/bin/pip install -e ".[dev]"
.venv/bin/pytest

Deploy the web UI

The live site, aiml.dannypenrose.com, runs on a self-hosted Ubuntu server: Streamlit on 127.0.0.1:3090 under a hardened systemd unit (deploy/forge/aiml-calc.service), Nginx in front (deploy/forge/aiml-calc.nginx.conf) and a Cloudflare Tunnel to the internet. deploy/forge-setup.sh installs or updates all of it and is safe to run twice:

deploy/forge-setup.sh            # plan: checks everything and changes nothing
deploy/forge-setup.sh --apply    # install or repair, then smoke test
deploy/forge-setup.sh --update   # pull main, reinstall dependencies, restart

The repo also works on Streamlit Community Cloud, which is free for public repositories: at share.streamlit.io choose Create app, this repository, branch main and main file streamlit_app.py, with Python 3.12 under Advanced settings.

Either way the public site sets AIML_CALC_PUBLIC=1 (streamlit_app.py and the systemd unit), which turns off the ▶ Run button in the code panels, because running visitors' code on a shared server is not safe. Set AIML_CALC_PUBLIC=0 to turn it back on, but only on a server that only you use.

Use it from Claude (MCP)

The aiml-calc command is an MCP server that gives an AI assistant all 33 algorithms as tools, so it can compute traces and numbers exactly instead of estimating them. It runs locally on your machine.

First, find the full path to the command (apps launched from the dock don't share your terminal's PATH, so they need the full path):

echo "$(pwd)/.venv/bin/aiml-calc"
# e.g. /Users/you/dev/aiml-calc/.venv/bin/aiml-calc

Claude Desktop

  1. Open Settings → Developer → Edit Config. This opens claude_desktop_config.json (on macOS: ~/Library/Application Support/Claude/claude_desktop_config.json).

  2. Add the server, using your full path. If the file already has an mcpServers section, add the aiml-calc entry inside it.

    {
      "mcpServers": {
        "aiml-calc": {
          "command": "/Users/you/dev/aiml-calc/.venv/bin/aiml-calc"
        }
      }
    }
  3. Quit Claude Desktop completely (Cmd+Q on macOS) and reopen it.

  4. Start a new chat: the aiml-calc tools appear in the tools menu, and Settings → Developer shows the server as running.

Local servers only work in the Claude Desktop app, not in Claude in the browser.

Claude Code

claude mcp add --transport stdio --scope user aiml-calc -- /Users/you/dev/aiml-calc/.venv/bin/aiml-calc

--scope user makes the tools available in every project; leave it out to add them to the current project only. Check it worked with claude mcp list (it should show ✔ Connected) or /mcp inside a session.

Try it

Ask something like "Use aiml-calc to trace A* from Arad to Bucharest on the Romania map" or "Run DPLL on (A ∨ B), (¬A ∨ B), (¬B ∨ C) with aiml-calc". Every result starts with Answer: and ends with the conventions the algorithm used.

Troubleshooting

Problem

Fix

Server doesn't appear in Claude Desktop

Check the JSON is valid and the path is the full absolute path, then fully quit and reopen the app

spawn … ENOENT or "command not found"

The path is wrong. Run ls <your path> to check the file exists

Server fails to start

Run the path directly in a terminal: it should wait silently for input (press Ctrl+C). Any error shown there is the problem

Still stuck (macOS)

Read the logs: tail -n 50 ~/Library/Logs/Claude/mcp-server-aiml-calc.log

claude mcp list shows ✘ Failed to connect

Run claude mcp get aiml-calc to check the command, and test the path as above

Input syntax

  • Propositional formulas: ¬ ~ ! (not), ∧ & (and), ∨ | (or), ⇒ => (implies), ⇔ <=> (iff), with brackets. Clauses can also be written compactly: Av-BvC.

  • First-order terms: variables start lower-case (x), constants, functions and predicates upper-case (Knows(John, Mother(x))). Rules: King(x) & Greedy(x) => Evil(x).

  • Graphs: [[from, to, cost], ...] (in the UI, one from to cost per line).

  • Weights: bias first, [w0, w1, ..., wn], with a dummy input x₀ = 1.

  • Game trees: nested lists of leaf values (levels alternate MAX and MIN), or a dict with children, values, types (max / min / chance) and probabilities.

Conventions

Rule

Default

Parameter

Goal test

BFS: when a node is generated. Others: when selected

goal_test

Graph-search duplicates

Not re-added if already in the frontier or explored set; UCS and A* replace a frontier entry when a cheaper path appears

mode

Child order

Alphabetical

child_order

Priority-queue ties

Alphabetical, always flagged

tie_break

DPLL

Pure symbol rule, then unit clauses in clause order, then branch on the next symbol, true first

use_pure_symbols, true_first, symbol_order

CSP variable / value order

MRV with degree tie-break / least constraining value

variable_order, value_order

Alpha-beta

Children searched left to right

–

Hard threshold

1 when the linear input is ≥ 0

–

Gradient descent

Factor 2 folded into α, as in AIMA

fold_two

How it is tested

pip install -e ".[dev]"
pytest

233 tests:

  • Reference cases reproduce textbook results: the crime knowledge base proved in two rounds of forward chaining, the Australia map colouring starting from SA, two leaves pruned in AIMA Fig 5.5, P(Burglary | both neighbours call) ≈ 0.284, P(Cavity | toothache) = 0.6, and more.

  • Property tests compare each algorithm with an independent method on hundreds of random inputs: DPLL and three backtracking configurations against brute force, resolution against truth tables, A* and IDA* against uniform-cost search, alpha-beta against minimax, back-propagation against a numerical gradient.

  • Generated code tests run every view's Python program, with several different inputs each, and check it prints the same answer as the library.

  • Walkthrough tests check that the numbers in the step-by-step explanations agree with the algorithms (for example, the replayed perceptron updates end at the library's weights, and the naive Bayes scores normalise to its posterior).

  • UI tests render every page and every built-in example headlessly, open the code dialog and press Run, and check that every view has its three explanation panels, its walkthrough and its code button, and that every input field has a tooltip.

Limitations

  • Sized for small, hand-checkable problems: truth tables, resolution and Bayes-net enumeration are exponential by design.

  • Bayesian networks are Boolean-only; CSP constraints are binary.

License

MIT © Danny Penrose

Available Tools

33 tools
backward_chainingC

Backward chaining over definite clauses, shown as an indented goal tree (checked against forward chaining).

Inputs: facts (required), query (required), rules (required). Returns: result

bayes_net_queryB

Exact inference in a Bayesian network by enumeration, checked against the full joint distribution. nodes: {"Alarm": {"parents": ["Burglary", "Earthquake"], "cpt": {"T,T": 0.95, "T,F": 0.94, "F,T": 0.29, "F,F": 0.001}}, "Burglary": {"cpt": {"": 0.001}}, ...}; CPT values are P(node = true | parents).

Inputs: nodes (required), query (required), evidence. Returns: result

bayes_ruleB

P(H | E) from P(H), P(E | H) and P(E | ¬H), by formula and by natural frequencies.

Inputs: prior (required), likelihood (required), false_positive_rate (required), evidence, +1 more. Returns: result

cnf_convertB

Convert a formula to CNF with each rewrite step, plus the compact form (e.g. -BvSvW) and a truth-table check.

Inputs: formula (required). Returns: result

cnn_layer_sizeB

Convolutional layer output volume, number of neurons and number of (shared) weights. input_shape: [A, B, C]; filter_size: [P, Q] or [P, Q, C].

Inputs: n_filters (required), filter_size (required), input_shape (required), stride, +1 more. Returns: result

csp_ac3C

AC-3 arc consistency: every arc revision, the values removed and the arcs re-queued.

Inputs: domains (required), variables (required), neighbours, constraints. Returns: result

csp_backtrackingB

Backtracking search for a CSP. constraints: [[X, op, Y]] with op in != == < > <= >=; or neighbours {X: [Y, ...]} meaning X != Y (map colouring). variable_order: static | mrv. value_order: static | lcv. inference: none | forward_checking | mac.

Inputs: domains (required), variables (required), inference, neighbours, +3 more. Returns: result

dpll_traceC

DPLL as a step summary ("Step 1: A=true, B=true, C=true, fail") plus a detailed trace. clauses: ["A | ~B | C", ...] or compact ["Av-BvC", ...]. Unit clauses are taken in the order given.

Inputs: clauses (required), true_first, symbol_order, use_pure_symbols. Returns: result

entailment_checkC

Does KB ⊨ query? Method 1: model checking (truth table). Method 2: resolution refutation.

Inputs: kb (required), query (required). Returns: result

fc_layer_weightsC

Weights in a fully connected layer: (inputs + 1) × neurons.

Inputs: n_neurons (required), input_shape (required). Returns: result

fol_backward_chainingC

First-order backward chaining: depth-first proof tree with variables renamed apart for each rule use.

Inputs: kb (required), query (required). Returns: result

fol_forward_chainingC

First-order forward chaining over definite clauses ("A(x) & B(x, y) => C(y)" or ground facts), round by round.

Inputs: kb (required), query (required). Returns: result

fol_unifyA

Most general unifier of two first-order terms or atoms, e.g. "Knows(John, x)" and "Knows(y, Mother(y))". Variables start lower-case; constants, functions and predicates start upper-case.

Inputs: x (required), y (required). Returns: result

forward_chainingC

Forward chaining over definite clauses (e.g. "L & M => P") with the agenda and count table at every step.

Inputs: facts (required), query (required), rules (required). Returns: result

game_alphabetaC

Alpha-beta pruning, left to right: α and β at every node, which subtrees are pruned, checked against minimax.

Inputs: tree (required), root_type. Returns: result

game_minimaxB

Minimax / expectiminimax values for a game tree. tree: nested lists of leaf utilities (levels alternate MAX, MIN) or {"children": {...}, "values": {...}, "types": {"B": "chance"}, "probabilities": {"B": [0.5, 0.5]}}.

Inputs: tree (required), root_type. Returns: result

gradient_descent_stepsC

Gradient descent on squared error for a linear model. mode: batch | stochastic. weights = [w0, w1, ...].

Inputs: X (required), y (required), weights (required), mode, +3 more. Returns: result

heuristic_checkA

Is a heuristic admissible (h ≤ true cost) and consistent (h(n) ≤ c(n,n') + h(n'))? Optionally compare two for dominance.

Inputs: goal (required), edges (required), heuristic (required), directed, +1 more. Returns: result

hypothesis_checkC

Which linear classifiers get every example right? examples: [[x1, ..., y]]; hypotheses: [[w0, w1, ...]].

Inputs: examples (required), hypotheses (required), names, activation. Returns: result

joint_distribution_queryB

P(query | evidence) from a full joint table of Boolean variables. Rows: [v1, v2, ..., probability].

Inputs: query (required), table (required), variables (required), evidence. Returns: result

kmeans_stepC

k-means: distance table, assignments, new centres (and quality measures). Ties go to the first centre.

Inputs: points (required), centres (required), names, metric, +1 more. Returns: result

knn_classifyC

k-nearest-neighbour classification. train rows: [a1, ..., an, label]. Optional min-max normalisation.

Inputs: query (required), train (required), k, metric, +1 more. Returns: result

linear_regression_fitB

Least-squares linear regression. Univariate: two closed-form methods. Multivariate: normal equations.

Inputs: X (required), y (required). Returns: result

logistic_regression_calcB

Logistic regression probability, and (with y and alpha) one update step under both common rules.

Inputs: x (required), weights (required), y, alpha. Returns: result

mlp_forward_backpropB

Forward pass through a feed-forward network and, with targets, one back-propagation step (AIMA Fig 18.24). layers[l][j] = weights [w0, w1, ...] of unit j in layer l.

Inputs: inputs (required), layers (required), alpha, targets, +1 more. Returns: result

naive_bayes_classifyC

Naive Bayes classification from categorical data rows [attr values..., class], with optional Laplace smoothing.

Inputs: data (required), query (required), attributes (required), laplace. Returns: result

neuron_outputC

Output of one neuron. weights = [w0 (bias), w1, ...]. activation: all | threshold | logistic | relu | tanh | linear.

Inputs: inputs (required), weights (required), activation. Returns: result

resolution_proofB

Resolution: convert KB ∧ ¬query to clauses and derive the empty clause, listing each resolvent used.

Inputs: kb (required), query. Returns: result

search_traceB

Trace a search algorithm step by step (selected node, children added/skipped, frontier, explored set).

algorithm: bfs | dfs | ucs | greedy | astar | dls | ids | idastar edges: [[from, to, cost], ...]; cost defaults to 1. Undirected unless directed=true. heuristic: {"state": h, ...} for greedy / astar / idastar. mode: "graph" (explored set; don't re-add states in frontier/explored) or "tree" (no explored set). child_order: "alphabetical" or "given" (order edges were listed). tie_break: alphabetical | fifo | lifo. goal_test: "default" (BFS on generation, everything else on selection), "generation" or "selection".

Inputs: goal (required), edges (required), start (required), algorithm (required), +9 more. Returns: result

truth_tableB

Truth table and model count for one or more formulas. Syntax: ¬ ~ ! -, ∧ &, ∨ |, ⇒ =>, ⇔ <=>. extra_symbols: symbols in the problem that don't appear in the formula (they still double the model count).

Inputs: formulas (required), extra_symbols. Returns: result

walksat_runC

A full randomised WalkSAT run for practice (repeatable with a seed).

Inputs: clauses (required), p, seed, model, +1 more. Returns: result

walksat_stepB

One WalkSAT step: which clauses are false, the satisfied-clause count after each possible flip, the greedy choice, and every assignment reachable in one step. clause_index (1-based) fixes which false clause is picked.

Inputs: model (required), clauses (required), p, clause_index. Returns: result

weight_updateC

Perceptron (threshold) or logistic single-example weight updates, one row per example. examples: [[x1, ..., y]].

Inputs: weights (required), examples (required), alpha, epochs, +1 more. Returns: result

Tool Schema Changelog

Recent tool additions, removals, and schema changes observed during successful MCP inspections.

Loading tool schema changelog…

    TDQS

    C2.9/5.0

    Scored across 33 tools

    Disambiguation4/5

    Most tools target distinct algorithms or algorithm families, with clear topic separation across search, logic, probability, ML, and neural networks. A few clusters overlap, especially truth_table/entailment_check/resolution_proof and propositional vs first-order chaining, but the descriptions generally expose the boundaries.

    Naming Consistency3/5

    All tool names use readable snake_case, which is a consistent surface convention. However, the underlying pattern is mixed: some are noun_verb (e.g., walksat_run, truth_table), some are algorithm_action (e.g., fol_unify, csp_ac3), and abbreviations are used unevenly.

    Tool Count2/5

    At 33 tools, the surface is substantially larger than the typical well-scoped 3-15 range and is heavy for an agent to navigate. The breadth of AI/ML topics justifies many tools, but several educational variants could potentially be consolidated.

    Completeness4/5

    The set covers a broad range of classic AI/ML topics, including search, games, CSPs, propositional and first-order logic, probabilistic inference, basic ML, and neural network calculations. Some major areas such as reinforcement learning, decision trees, HMMs, and SVM-specific tools are absent, but the covered domains are mostly self-contained.

    Maintenance

    ActivityMaintained
    ResponsivenessNo recent community issues

    Related MCP Connectors

    Related MCP Servers

    • A
      license
      B
      quality
      B
      maintenance
      Enables AI agents to perform accurate local computations including arbitrary-precision math, date handling, unit conversion, subnet calculations, encoding, hashing, and text analysis, all without network calls or API keys.
      27
      MIT
    • A
      license
      A
      quality
      C
      maintenance
      Enables AI agents to perform exact, reproducible mechanical computation — text diffing, hashing, JSON querying, calendar arithmetic, deadline-bounded regex extraction, CSV parsing, similarity scoring, unit and base conversion — instead of relying on probabilistic guessing. Runs dependency-free, read-only and offline over stdio, with schema validation and hard caps on every operation so no call can hang the client.
      15
      MIT
    • A
      license
      Not graded
      quality
      B
      maintenance
      Enables AI assistants to search and run over 500 tools for PDF, text, data, crypto, network, and conversion tasks, with file processing done locally or at the edge so inputs are not uploaded.
      MIT