Metadata-Version: 2.5
Name: precis-static
Version: 0.1.0
Summary: Stateless tools for an assistant, as plugins of mcp-dispatch: a calculator (sympy, pint), a clock, a slug minter, names of words for ids and chemical identities (rdkit). Distilled from precis-mcp.
Author-email: Reto Stamm <reto@retostamm.com>
License-Expression: GPL-3.0-or-later
License-File: LICENSE
Keywords: calculator,mcp,mcp-dispatch,plugins,rdkit,time,units
Classifier: Development Status :: 2 - Pre-Alpha
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Scientific/Engineering
Requires-Python: >=3.12
Requires-Dist: mcp-dispatch<0.4,>=0.3.0
Requires-Dist: pint>=0.23
Requires-Dist: sympy>=1.14.0
Requires-Dist: tzdata>=2024.1
Provides-Extra: chem
Requires-Dist: rdkit>=2023.9; extra == 'chem'
Description-Content-Type: text/markdown

# precis-static

Stateless tools for an assistant, as plugins of [mcp-dispatch](https://github.com/retospect/mcp-dispatch). Each wraps a Python library. There is no database, no network and nothing kept between calls, so any host can install them: the answer depends on the call alone.

They were distilled from the kinds of the same names in precis-mcp.

| Package | Actions | Library | For |
|---|---|---|---|
| `calc` | `calc/eval` | sympy, pint | exact arithmetic, calculus, solving, matrices; unit conversion with a `to` clause |
| `time` | `time/at`, `time/window` | standard library | now; a timestamp read, converted to a zone, formatted; a phrase such as "last week" as UTC bounds |
| `random` | `random/slug` | `secrets` | a short random name that says nothing |
| `chem` | `chem/mol`, `chem/rxn` | rdkit (extra `chem`) | canonical SMILES, formula, mass and InChIKey of a molecule; identity keys of a reaction |
| `words` | `words/of`, `words/read` | none | a name to say aloud for an id: its last bits as a few common words, and back |

```
get {"path": "calc/eval", "q": "integrate(exp(-x**2), (x, -oo, oo))"}     ->  ... = sqrt(pi)
get {"path": "calc/eval", "q": "3 ft + 2 in to cm"}                       ->  3 ft + 2 in = 96.52 cm
get {"path": "time/at", "q": "tomorrow 9am", "zone": "Europe/Zurich", "to": "Asia/Tokyo"}
get {"path": "time/window", "q": "last week"}
get {"path": "random/slug", "len": 8}
get {"path": "chem/mol", "smiles": "OC(=O)c1ccccc1OC(C)=O"}
get {"path": "words/of", "hex": "adfa0923"}                               ->  pine giant eagle
```

Three how-to guides ship with it and are found by the host's search for skills: `calculate-and-convert`, `read-a-time`, `compare-molecules`.

## Use

```
pip install precis-static            # calc, time, random, words
pip install "precis-static[chem]"    # and chem, with rdkit
```

A host of mcp-dispatch finds the five plugins by itself (`Dispatch().find_plugins()`); they are listed under the entry point group `mcp_dispatch.plugins`, and under `reticula.plugins` for Reticula. Without rdkit the `chem` plugin still loads and answers each call with "not installed here".

From a shell, with the same calls:

```
uv run mcp-dispatch call get calc/eval "q=3 ft to m"
uv run mcp-dispatch help time/at
```

## calc evaluates mathematics, not Python

sympy reads a string with Python's `eval`, with every builtin in reach; most of its functions parse a string they are handed; and for some numeric work it prints an expression as Python source and runs that. An expression passed straight to `sympy.sympify` can therefore run anything: in the handler this was distilled from, `__import__('os').system('...')` as the expression ran the command.

Here an expression is never given to `eval`. sympy's tokenizer makes a syntax tree of it, and a small interpreter walks the tree:

- a name is one of sympy's mathematical classes, constants, or functions from its mathematical modules; nothing that prints, plots, reads files, builds code or changes global state;
- a method is one of a fixed list (`.subs`, `.diff`, `.inv`, `.evalf`, ...);
- every value on the way is checked to be mathematics: a sympy object, a matrix, a number, or a list of those. A string, a class or any other Python object ends the evaluation where it appears;
- a quoted string stands only as a symbol's name, a number's digits, or one of five option words; a symbol cannot be named after a builtin or keyword of Python, and a function of your own is one letter;
- as a second line, sympy's own parser refuses text that names anything of Python for the length of a call.

A first version checked the source and then evaluated it; a red-team pass of 865 expressions found ways to run code through it (functions named `exec` that sympy compiled, strings sympy hands out). Those expressions are in the test suite and are refused by the interpreter. The interpreter itself has had no red-team pass of its own yet.

Not bounded: time and memory. `9**9**9` is mathematics and keeps a process busy. A host that takes expressions from strangers runs calls under a deadline of its own.

## words names the end of a key

A word stands for seven bits. The last word is a noun and names the key's last seven bits; before it come an adjective, a colour, an adjective and so on, each naming the seven bits before. The front of the key is what is cut off, so a shorter name is the end of the longer one:

```
get {"path": "words/of", "hex": "adfa0923", "bits": 14}    ->  giant eagle
get {"path": "words/of", "hex": "adfa0923"}                ->  pine giant eagle
get {"path": "words/of", "hex": "adfa0923", "bits": 32}    ->  cyan bouncy pine giant eagle
get {"path": "words/read", "q": "giant eagle"}             ->  2339 (hex 923): the last 14 bits of a key
```

The caller says how many bits must be told apart and gets the fewest words that carry them (21 by default, three words). Keys that end in the same bits share a name. Which keys exist is the host's knowledge, so the host chooses the bits (few while few things are in play, more as they grow) and, when a name fits several keys, asks the person which is meant. A host uses the functions directly:

```python
from precis_static.words import to_number, to_words

to_words(0xADFA0923, bits=14)      # "giant eagle"
to_number("pine giant eagle")      # (1706275, 21): every key with key % 2**21 == 1706275
```

The three lists of 128 words are those of precis-mcp, and a number below 2,097,152 has the three words it has there. Editing a list renames keys.

## What stayed in precis-mcp

A kind was moved when it needs nothing but a library. These need more and stayed:

| Kind | Why |
|---|---|
| `math` | asks Wolfram Alpha: a key, the network, a cache |
| `chem` (name or CAS number to structure) | asks OPSIN and PubChem and caches the answer; only the rdkit part came here |
| `random` (a random block of the corpus) | reads the database; only the slug came here |
| `rxn`, `structure`, `material`, `measure`, `estimate`, `cad`, `pcb`, `se` | store what they compute, or work on stored things |
| the numeric kernels `precis_chain`, `precis_flow`, `precis_geom`, `precis_surface` | libraries with no kind of their own; they are called by the kinds above |

Candidates that would fit here and are not built: the reaction classifier (rdkit templates to an RXNO class), which needs its template table moved with it; the composition tier of `estimate` (element properties and alloying heuristics from `ase`), which is a few hundred lines once its cache is taken off; unit-aware quantities beyond conversion.

## Development

```
uv sync
uv run pytest
uv run ruff check . && uv run ruff format --check . && uv run mypy
uv run mcp-dispatch check        # what an assistant reads is in order
```

mcp-dispatch comes from PyPI (`mcp-dispatch>=0.3.0,<0.4`).

## Licence

GPL-3.0-or-later, as precis-mcp, from which the code was taken. See `LICENSE`.
