Metadata-Version: 2.3
Name: joule-mcp
Version: 0.1.1
Summary: MCP server that lets AI agents inspect and manage Joule use cases through the Guardian REST API
License: MIT
Keywords: joule,mcp,model-context-protocol,streaming,ai-agents
Author: Fractalworks
Requires-Python: >=3.10
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Software Development :: Libraries
Classifier: Typing :: Typed
Requires-Dist: mcp (>=2.3,<3.0)
Requires-Dist: pyyaml (>=6.0,<7.0)
Requires-Dist: requests (>=2.33.0,<3.0.0)
Project-URL: Documentation, https://docs.fractalworks.io/joule
Project-URL: Homepage, https://getjoule.io/
Project-URL: changelog, https://gitlab.com/joule-platform/joule-mcp/-/blob/main/CHANGELOG.md
Project-URL: issues, https://gitlab.com/joule-platform/joule-mcp/-/issues
Project-URL: source, https://gitlab.com/joule-platform/joule-mcp
Description-Content-Type: text/markdown

# joule-mcp

<!-- mcp-name: io.fractalworks/joule-mcp -->

An MCP server that lets an AI agent inspect a Joule installation. It calls the Guardian REST API
(`/joule/management/*`). It is independent of `joulectl` and Guardian.

By default the server is read-only. The change tools exist only when you set `JOULE_MCP_ALLOW_WRITE=1`.

## Tools

| Tool | Guardian route | Same as |
|---|---|---|
| `list_pods` | `pods/list` | `joulectl ls pods` |
| `list_pod_members` | `pods/connected` | `joulectl ls members POD` |
| `list_usecases` | `usecase/list` | `joulectl ls usecases` |
| `list_transports` | `transports/list` | `joulectl ls transports` |
| `list_streams` | `stream/list` | `joulectl ls streams` |
| `usecase_status` | `usecase/status` | `joulectl status usecase NAME` |
| `usecase_events(name, pod, limit)` | `usecase/status` | `joulectl status usecase NAME --events` |
| `whoami` | `apikeys/whoami` | the Joule UI's "who am I" |
| `describe_usecase` | `usecase/list`, `usecase/detail`, `usecase/status` | `joulectl inspect usecase NAME` and `status` |

Every tool carries the `readOnlyHint` annotation.

`usecase_events` returns the latest events a use case emitted, parsed from JSON, for each running process (`limit` 1 to
100, default 10, newest last). Guardian keeps only a short sample in memory (`last_emitted_events`), so it is for
inspection, not a stream, and it is empty while a use case is paused or idle. The result gives `emitted_events` for
each process, so two calls show whether events are flowing.

## Change tools

Off unless `JOULE_MCP_ALLOW_WRITE=1` is set. Guardian needs the `usecases:write` scope on the key.
`pod` is always required, so no change reaches every pod by accident.

| Tool | Guardian route | Same as |
|---|---|---|
| `deploy_usecase(pod, specification)` | `POST usecase/deploy` | `joulectl deploy usecase` |
| `pause_usecase(name, pod)` | `PUT usecase/pause` | `joulectl process pause` |
| `resume_usecase(name, pod)` | `PUT usecase/resume` | `joulectl process resume` |
| `undeploy_usecase(name, pod, confirm)` | `DELETE usecase/undeploy` | `joulectl undeploy usecase` |

- `specification` is an object, or JSON or YAML text. It is parsed with a safe loader and sent as JSON.
  The root key is `use case`, and a use case needs at least one source and one sink. For example:
  `{"use case": {"name": "x", "stream name": "s", "sources": ["a"], "sinks": ["b"]}}`.
- Guardian answers HTTP 200 with an empty body for a deploy the daemons rejected (the reason is only in the Guardian
  log), for a duplicate deploy, and for pause, resume or undeploy of an unknown name. The server raises for a failure
  in the envelope, maps service codes (for example 653 to `UNKNOWN_POD`), and never treats an empty reply as proof of
  success.
- Before a change the server reads `usecase/list`. Deploy of a name already on the pod, and pause, resume or undeploy
  of a name that is not on it, fail at once and send nothing.
- After a deploy the result has `verified: true` or `false` and the listed `state`. If the use case is not listed,
  the note says the deployment most likely failed and points at the DSL schema and the daemon logs.
- Pause and resume return `state_before` and `state_after` from `usecase/list` (`RUNNING`, `SUSPENDED`), and the live
  status rows.
- `undeploy_usecase` removes the use case and cannot be undone. `confirm` must equal `name` exactly. If it does not,
  the error tells the agent to ask the user, and no request is sent. After the call the server checks `usecase/list`
  and returns `removed: true` or `false`.
- A change is never retried.
- Every change tool is annotated as not read-only. `undeploy_usecase` is annotated destructive; the others are not. `pause_usecase` and `resume_usecase` are idempotent.

## Resources

An agent should read these before it writes a use case specification.

| Resource | Guardian route | Content |
|---|---|---|
| `joule://dsl/schema` | `dsl/schema` | JSON Schema (2020-12) for the Joule DSL, as `application/schema+json` |
| `joule://dsl/catalogue` | `dsl/catalogue` | Every component with its key, properties and example, plus analytic and aggregate functions |
| `joule://dsl/component/{key}` | `dsl/catalogue` | One component by its DSL key (not case sensitive). Much smaller than the full catalogue |

The key needs the `dsl:read` scope. Guardian caches both routes for 5 minutes.

## Install

Needs Python 3.10 or newer. After the first release on PyPI:

```bash
pipx install joule-mcp        # or: pip install joule-mcp
joule-mcp --help              # starts the stdio server; an MCP client launches it for you
```

Without installing anything, with `uv`:

```bash
claude mcp add joule -- uvx joule-mcp
```

From a source checkout:

```bash
python3 -m venv .venv
.venv/bin/pip install -e ".[test]"      # or: poetry install
.venv/bin/joule-mcp
```

Upgrade with `pipx upgrade joule-mcp` or `pip install --upgrade joule-mcp`.

## API keys

The server follows the same rules as the Joule UI. Guardian refuses any request without a valid `X-API-Key`, so the server
needs a key unless Guardian runs with authentication off.

| Situation | What the server does |
|---|---|
| Key is malformed (not 8 to 200 printable characters, no spaces) | Stops at start-up with a message. The key is not repeated. |
| Guardian wants a key and none is set | Every tool and resource answers "Guardian requires an API key" with how to set one. Nothing else is sent. |
| Guardian rejects the key (`AUTH_INVALID`, `AUTH_EXPIRED`, `AUTH_REVOKED`) | Every call says so and tells you to get a new key and restart. |
| Key is valid but lacks a scope | The call is refused before it is sent, and the message names the scope and the key's owner. |
| Guardian uses no keys (its `apikeys/whoami` does not exist) | Calls go through. |
| Guardian cannot be reached | The call itself reports that. |

Each tool states the scope it needs, the same as its Guardian route: `pods:read`, `usecases:read`, `transports:read`,
`streams:read`, `usecases:write` (deploy, pause, resume, undeploy) and `dsl:read` (the resources). A higher level
covers a lower one, so `usecases:write` includes `usecases:read`. Guardian stays the authority. The server's check only
saves a request that Guardian would refuse, and it never grants anything.

Use the `whoami` tool first when a call is refused. It says whether Guardian wants a key, whether this key is accepted,
who owns it, its scopes and when it ends. It never returns the key.

Get a key from the Joule UI (API keys) or joulectl. A `viewer` key is enough for the read tools, and an `operator` key adds
the change tools. Put the key in a file, set `JOULE_API_KEY_FILE` to its path, and keep the file private (`chmod 600`).
The server reads the key when it starts, so restart it after you change the key.

## Settings

| Setting | Source, highest first |
|---|---|
| Guardian host | `JOULE_HOST`, `joule_host` in `~/.config/joule/config.json`, `127.0.0.1:9080` |
| API key | `JOULE_API_KEY_FILE`, `JOULE_API_KEY`, the key `joulectl` stored for that host |
| Change tools | `JOULE_MCP_ALLOW_WRITE=1` (default off) |
| Timeout | `JOULE_TIMEOUT`, default 30 seconds |
| Private CA | `JOULE_CA_BUNDLE` |

The key is sent as `X-API-Key`. It is never a tool argument, and it is not logged. Redirects are
refused so the key cannot be forwarded. A key with only `*:read` scopes keeps the agent read-only
at Guardian too.

## Use with Claude Code

```bash
claude mcp add joule -- joule-mcp
claude mcp add joule -e JOULE_MCP_ALLOW_WRITE=1 -- joule-mcp   # with change tools
```

## Checked against a real Guardian

Run on 2026-12-02 against Joule 1.3 on minikube, through `kubectl port-forward`, with the key from joulectl's config:
all read tools and resources; pause and resume of a running use case (`RUNNING` to `SUSPENDED` and back, on both
daemons); deploy, refused duplicate and undeploy of a throwaway use case; and the refusal paths.

## Release

Same process as joulectl (`.gitlab-ci.yml` here follows `fractalworks-joule-cli/.gitlab-ci.yml`).

1. Set `version` in `pyproject.toml` and add the entry to `CHANGELOG.md`.
2. Check: `poetry install && poetry run pytest --cov --cov-fail-under=95`, then `poetry build && twine check dist/*`.
3. Tag with `release/release.sh <version> joule-mcp`, then push the tag yourself. The tag must be `vX.Y.Z` and equal
   `version`, or the pipeline stops.
4. The pipeline runs tests on Python 3.10 and 3.13, the security gates (pip-audit, bandit, secret detection), builds,
   signs with Sigstore, then waits. Publish to TestPyPI first, then PyPI, each with a manual click.

### Releasing from your own machine

`scripts/release_local.py` does the same checks and the same build as the pipeline, then uploads if you ask. Use one route
for each version: an index accepts a version once.

```bash
git tag -a v0.1.0 -m "Release v0.1.0"                  # on a clean tree; the script never tags or pushes
python scripts/release_local.py                        # dry run: tests, build, twine check. Uploads nothing.
python scripts/release_local.py --upload testpypi      # then try it: pip install -i https://test.pypi.org/simple/ joule-mcp
python scripts/release_local.py --upload pypi          # needs the version on TestPyPI first; you type the tag to confirm
```

Trusted Publishing works only from CI, so a local upload uses an API token. Put it in `TESTPYPI_TOKEN` or `PYPI_TOKEN`
(never on the command line), or leave it unset and twine reads `~/.pypirc`, your keyring, or asks. The first upload of a new
project needs a token for the whole account; replace it with one scoped to `joule-mcp` afterwards. A local upload has no
Sigstore signature and no SBOM: those come from the pipeline.

One-time setup, by the maintainers: create the GitLab project and add it to `repos.txt`; register a Trusted Publisher
(or a pending publisher for the first upload) for `joule-mcp` on pypi.org and test.pypi.org; protect `v*` tags and the
`pypi` environment. Details are at the top of `.gitlab-ci.yml`.

## Test

```bash
.venv/bin/python -m pytest
```

## Next

1. Streamable HTTP transport.

## Known risk: shared streams

On the Joule 1.3 test install (2026-12-02), deploying a second use case on the same stream as a running one worked.
Undeploying it then shut down the shared stream processors (`cleanMarketData` and the select step). The first use
case stayed `RUNNING` and kept receiving events, but processed and emitted none, and Guardian reported no error.
`undeploy_usecase` now refuses when another use case on the pod uses the same stream, and `deploy_usecase` returns a
`warning` in that case. Treat this as a Joule platform issue to confirm, not as a rule.

