Metadata-Version: 2.4
Name: geodms-mcp
Version: 20.31.0
Summary: An MCP server that gives AI agents GeoDMS as typed tools: load a configuration, list its tree, add items, compute an item, explain a value
License-Expression: GPL-3.0-or-later
License-File: license.txt
Requires-Python: >=3.12
Requires-Dist: mcp>=2.3,<3
Project-URL: Homepage, https://www.geodms.nl
Project-URL: Documentation, https://github.com/ObjectVision/GeoDMS/wiki/GeoDMS-MCP-server
Project-URL: Source, https://github.com/ObjectVision/GeoDMS
Project-URL: Issues, https://github.com/ObjectVision/GeoDMS/issues
Classifier: Programming Language :: Python :: 3
Classifier: Operating System :: Microsoft :: Windows
Classifier: Operating System :: POSIX :: Linux
Classifier: Topic :: Scientific/Engineering :: GIS
Description-Content-Type: text/markdown

# geodms-mcp: GeoDMS as tools for an AI agent

geodms-mcp is an [MCP](https://modelcontextprotocol.io) server that lets an AI agent, such as
Claude Code or Claude Desktop, work with GeoDMS configurations without a shell. The agent can
load a configuration (`.dms`), list its tree, read what an item is, add items and change
expressions, compute an item and get its statistics or values, hold intermediate results for the
calls that follow, and ask how one value came about. The server talks to its client over stdio.

The server keeps each configuration that the agent loads loaded, in a worker process of its own,
so that the items the agent adds stay there from call to call and nothing is loaded again. The
worker imports the `geodms` module of the installed GeoDMS whose `GeoDmsRun` the server found.
Every worker

- runs in the restricted mode of GeoDMS: it runs no command line, so `exec_ec` fails, and it
  writes only in a LocalData folder of its own and in the export folder that the server was
  started with, also when the write is the storage of an item that the requested item depends on;
- gets a LocalData folder of its own, made for the worker and removed after it;
- is held to a memory limit (8 GB of commit on Windows, of resident memory on Linux), and each
  call to a wall-clock limit (300 s). A call that reaches one returns `LIMIT(commit)`,
  `LIMIT(memory)` or `LIMIT(time)` instead of a result. On Windows the memory limit refuses the
  allocation inside the worker: the calculation fails with "memory allocation failed", and the
  worker goes on with its held results. In rare cases GeoDMS crashes there instead (#1418).
  A worker that went on stays at its limit: GeoDMS refuses every large allocation in it, so a
  calculation that needs memory fails in the same way, until `load_config` loads the
  configuration afresh; every result says so under `allocations_refused`.
  Right after the refusal the server has GeoDMS hand the memory that it keeps for reuse back to the
  operating system.
  GeoDMS cannot be stopped in the middle of a calculation, so at the time limit, and at the memory
  limit on Linux, the server ends the worker. After that, or when GeoDMS crashed, the
  server starts a new one, loads the configuration again, adds the same items, changes the same
  expressions and holds the same results again, so that the agent keeps its items and handles. The
  held results were lost with the old worker: each says why under `lost` until a call calculates it
  again.

GeoDMS is sure to keep a calculated result of more than 128 bytes only while something holds it, unless
the configuration marks the item with KeepData, so without a handle the next call that reads it usually
calculates it again. `hold_result` holds it in the worker
under a name such as `h1`, until `release_handle` releases it or the configuration is closed, and
the calls in between, and the calculation of every item from it, use it. A held result is
`current`, or `stale` once `set_expr` changed something it depends on, and then the next read
calculates it again. The held results count against the memory limit, and every result lists
them with their state, next to the memory of the worker. That memory also counts what GeoDMS freed
and keeps for its next calculations: freed pieces of 8 KB to 1 MB, such as the tiles of a result,
which it hands back in part only on Windows, once the process has allocated 8 GB in pieces of 4 KB
or more and the RAM use of the computer passes its MemoryFlushThreshold.

GeoDMS takes any expression that `set_expr` gives it, also one with a syntax error, an unknown
name, or another value type or domain than the item has. `set_expr` then returns `changed: true`
with `ok: false` and the reason, and the item fails, and so does every item calculated from it,
until another `set_expr` gives it an expression that works. The exception is a cycle through an
item that a handle holds, which GeoDMS does not see: `set_expr` returns `ok: true`, and every read
of the item ends the worker, also the new worker, which makes the change again. After
`release_handle` one more read ends the worker, and from then on a read fails with the recursion.

Only configurations under the folders named with `--config-root` can be loaded, and at most two
at a time unless `--max-sessions` says otherwise.

## Tools

| Tool | What it does |
|---|---|
| `version` | the GeoDMS version that the server runs, its folders, its limits and the loaded configurations |
| `load_config` | loads a configuration, or loads it afresh, and returns the errors and warnings of loading it |
| `close_config` | closes a loaded configuration, with the items that were added to it and its held results |
| `list_items` | per item below a given item: path, kind, value type, domain unit, values unit, expression and failure reason |
| `item_info` | the same for one item, with its properties and the description of the storages and items its value comes from |
| `compute` | calculates an item and returns its statistics: count, minimum, maximum, sum, average, nulls and the like |
| `get_values` | calculates an attribute or parameter and returns its values as text, at most 1000 per call |
| `explain_value` | the Value Info page of one value: its expression and the values it came from |
| `add_item` | adds a container, unit, attribute or parameter, with an expression, to the loaded configuration, and with a storage in the export folder when it should be written there |
| `set_expr` | changes the expression of an item of the loaded configuration, and says whether the item works with it |
| `hold_result` | calculates an attribute, parameter or unit and holds its result under a name such as `h1` |
| `list_handles` | the held results of a configuration, with their state |
| `release_handle` | releases a held result |

A call on a configuration that is not loaded loads it first. Added items and changed expressions
are not written to the `.dms` file.

## Installing

The server needs a GeoDMS of the same version, installed by its setup, and a Python for which
that GeoDMS has its `geodms` module: CPython 3.12, 3.13 or 3.14. Pin the version of that GeoDMS:

    pip install geodms-mcp==20.30.0

PyPI has the full releases only. For a pre-release, install the wheel from the assets of its
GitHub release, `pip install geodms_mcp-<version>-py3-none-any.whl`. pip installs the MCP Python
SDK (`mcp`) from PyPI with it. The server finds `GeoDmsRun` on `PATH`,
or else in the installation folder of the GeoDMS of its own version; `--geodmsrun` names another.
The `geodms` module is taken from the folder of that `GeoDmsRun`.

## Starting it from a client

In Claude Code:

    claude mcp add geodms -- geodms-mcp --config-root C:/prj/mymodel --export-dir C:/LocalData/mymodel_export

In the configuration file of Claude Desktop:

    "mcpServers": {
      "geodms": {
        "command": "geodms-mcp",
        "args": ["--config-root", "C:/prj/mymodel", "--export-dir", "C:/LocalData/mymodel_export"]
      }
    }

Options: `--config-root FOLDER` (required, may be given more than once), `--export-dir FOLDER`,
`--geodmsrun EXE`, `--work-dir FOLDER` (where the folder of each worker is made), `--max-commit-gb N`
(per loaded configuration) and `--timeout-sec N` (per call; 0 switches a limit off),
`--max-sessions N` (configurations loaded at a time, 2 by default), `--keep-work` (keep the folder
of each worker, with its log).

The documentation is on the wiki: https://github.com/ObjectVision/GeoDMS/wiki/GeoDMS-MCP-server
