mcpField
C
chunk-invariance

chunk-invariance

Local
github.comSecurity13Updated 3d ago

About chunk-invariance

chunk-invariance tests Python streaming filters for output that differs from whole-input filtering across chunk boundaries. It helps developers validate redactors, guardrails, and rewriters, and can provide an optional MCP server for assistant-guided checks and pytest generation.

Key features

  1. 01Checks output across chunk splits
  2. 02Reports the first failing split
  3. 03Supports sync and async filters
  4. 04Generates pytest regression tests
  5. 05Optional Hypothesis strategies

Use cases

  1. 01Test whether redaction misses split secrets
  2. 02Check stream output against whole-input output
  3. 03Add chunk-boundary regression tests

MCP support

The optional MCP server exposes tools to check a streaming function, generate a pytest regression test, and explain per-chunk leaks. It imports and runs the function locally in a separate process.

Configuration

Local server config is the same for every client. Copy it into your client’s MCP config file.

mcp.json · Universal config
{
"mcpServers": {
"ninadphalak-chunk-invariance": {
"command": "uvx",
"args": [
"chunk-invariance"
]
}
}
}
Run commanduvx chunk-invariance

Local servers need a runtime on your machine (e.g. Node.js 18+). Config file locations differ by client; see your client’s docs.

Packages
pypichunk-invariance0.2.0

Tools

3

Saved tool list · updated 1w ago

ParameterTypeRequiredDescription
shapeenumNogenerator: target(chunks) takes an iterable of text chunks and yields or returns output chunks. per_chunk: target(chunk, state) returns the output for one chunk; state is a dict kept across the stream; give the end-of-stream step as flush_target. async_generator: target takes an async iterable of chunks and yields output.generator | per_chunk | async_generator
pythonanyNoInterpreter that imports the user's code. Default: project_dir/.venv or project_dir/venv if present, else the server's own Python.
splitsanyNo'all-two-part' (default): the whole text plus every single cut. An int up to 3: every split into at most that many parts (slower).
targetstringYesThe streaming filter as package.module:function, importable from project_dir the way the user's tests import it.
samplesanyNoInput texts that contain values the filter should redact or rewrite. Default: synthetic samples (an email, an SSN, a card number, API-key shapes; all fake): ['Contact [email protected] about the invoice.', 'SSN on file: 987-65-4321 (fake).', 'Card 4111 1111 1111 1111 exp 12/30 (test card).', 'Use key sk-FAKEab12cd34ef56gh78 for the demo.', 'aws_access_key_id = AKIAIOSFODNN7EXAMPLE', 'token ghp_EXAMPLENOTAREALGITHUBTOKEN0000000000 ok']. Pass made-up values in the formats this filter matches (for example a key typed in the provider's format), or the check proves nothing. Never real secrets or personal data: samples are echoed in the result and in the generated test file.
project_dirstringYesAbsolute path to the user's project root. The target is imported from here (and from its src/ folder).
flush_targetanyNoper_chunk only: package.module:function for flush(state), called once after the last chunk.
whole_targetanyNoThe non-streaming function, package.module:function, taking the whole text and returning the filtered text. Default: the stream filter run on the whole text as one chunk, which cannot catch a missing end-of-stream flush (one chunk drops the same tail), so give it when one exists.
timeout_secondsnumberNoSeconds before the check process is stopped.

Details

From the project README

chunk-invariance

A streaming filter (a redactor, a guardrail, a rewriter) is correct only if, for every way of splitting an input into chunks, the concatenated streamed output equals the output of filtering the whole input at once. This package turns that rule into one assertion.

from chunk_invariance import assert_chunk_invariant

def redact(text):                 # your filter on the whole input
    return text.replace("sk-AbCd1234", "[KEY]")

def redact_stream(chunks):        # your filter on a stream: takes chunks, yields chunks
    for chunk in chunks:
        yield redact(chunk)       # redacts each chunk alone

def test_streaming_redaction():
    assert_chunk_invariant(redact_stream, redact, "use sk-AbCd1234 now")

That test fails, and names the smallest split that breaks it:

chunk_invariance.core.ChunkInvarianceError: stream filter is not chunk-invariant
  input:    'use sk-AbCd1234 now'
  split:    ['use s', 'k-AbCd1234 now'] (cut at 5)
  whole:    'use [KEY] now'
  streamed: 'use sk-AbCd1234 now'
  split #6 in the order tried; generated splits run fewest parts first
pip install chunk-invariance
pip install "chunk-invariance[hypothesis]"   # optional Hypothesis strategies
pip install "chunk-invariance[mcp]"          # optional MCP server for AI assistants

What it catches

  • A filter that evaluates each chunk alone, so a value cut across two chunks matches neither half.
  • A filter that prepends a fixed carryover from the previous chunk. The match fires, but the start of the value was already emitted with the previous chunk.
  • A hold-back that is shorter than the pattern, a missing end-of-stream flush, and output that is corrupted rather than leaked (any difference from the whole-input output fails).

chunk_invariance.examples has one correct filter (hold_back) and the two wrong designs (design_a, design_b) for the same pattern, as a worked comparison.

What it does not catch

It tests a filter you can call in-process. It does not test a running gateway, where the chunks are decided by an upstream model, a network and an SSE parser. For that, use pii-leak-benchmark, which sends fragmented responses through the gateway and checks what reaches the client.

It checks the splits you ask for. The default is the whole input plus every single cut, which catches a value cut in two. splits="all" tries every split of a short input (up to 16 characters); splits=3 tries every split into at most three parts; an explicit list of chunk lists runs exactly those. It cannot prove a filter correct for inputs you did not give it.

Use it from your AI assistant

Do you use Claude Code, Cursor or Claude Desktop? The package has an optional MCP server. With it you can ask the assistant to run the every-split check on your own streaming function, and then to add the test to your repo.

pip install "chunk-invariance[mcp]"      # Python 3.10 or later
claude mcp add chunk-invariance -- chunk-invariance-mcp

For Claude Desktop, add this to claude_desktop_config.json (give the full path to the command if Claude Desktop cannot find it on your PATH):

{
  "mcpServers": {
    "chunk-invariance": { "command": "chunk-invariance-mcp" }
  }
}

Then ask, for example: "Check whether redact_stream in myapp/filters.py leaks values split across chunks, and add a regression test for it."

There are three tools. check_stream_filter imports your function (myapp.filters:redact_stream), streams each sample through it in every two-part split and tells you which split broke it, for example that 'sk-AbCd1234' came out unredacted when the input was cut into 'use s' and 'k-AbCd1234 now'. write_regression_test returns a pytest file built on assert_chunk_invariant, so the check stays in your CI. explain is one paragraph on why per-chunk filtering leaks.

The default samples (an email, an SSN, a card number and a few API-key shapes) are all fake. Pass your own made-up samples in the formats your filter redacts, such as a key you typed yourself in your provider's format. A sample the filter does not change tests nothing, and the tool says so.

Never pass a real secret or real personal data as a sample. The samples, and any value your filter leaks, come back in the tool's result, and your assistant sends that result to its model provider like the rest of the conversation. So does the message of any exception your code raises, but nothing your code prints. The same samples also end up in the generated test file.

Checking a function runs your code. The server imports your module on your own machine, in a separate process that is stopped after a timeout (60 seconds by default). If your project has a .venv, that is the Python it uses. The server itself makes no network calls, sends no telemetry and writes no files. Saving the test file is up to you and your assistant.

API

NameDoes
assert_chunk_invariant(stream_filter, whole_filter, text, *, splits="all-two-part")Raises ChunkInvarianceError (an AssertionError) on the first failing split, fewest parts first. Returns the number of splits checked. Works on str and bytes.
assert_chunk_invariant_async(...)The same, for an async generator filter, inside an async test.
two_part_splits(text)Every split into two non-empty parts.
all_splits(text, max_parts=None)Every split into at most max_parts non-empty parts, the whole input first.
per_chunk(process, *, state=dict, flush=None)Adapts process(chunk, state) -> output and flush(state) -> output, with fresh state per stream.
from_async(async_stream_filter)Adapts an async generator filter for the synchronous assertion.
strategies.splits_of(text, max_parts=None)Hypothesis strategy: random splits of text, shrinking toward fewer cuts.
strategies.text_and_splits(texts, max_parts=None)Hypothesis strategy: (text, chunks) pairs. texts is itself a strategy, such as st.sampled_from([...]), not a list.

stream_filter is called once per split, so it must start from fresh state each time.

Background

The invariant and the two wrong designs are described in Phalak, N. (2026), Split-Boundary Leaks in Streaming Guardrails, Zenodo, 10.5281/zenodo.22909585.

Standard library only (the mcp extra adds the MCP SDK, for the server alone). Apache-2.0. A TypeScript version with the same API is published on npm as chunk-invariance.

Comments

Markdown supported

New to MCP?

From what the protocol is to installing your first server, these answers cover most first questions.

An open protocol that defines how AI applications connect to external tools and data in a uniform way. Clients talk to the model; MCP servers provide tools, resources and prompt templates. They exchange standard messages, so one server can be reused by many clients.