Skip to main content

cpex-output-length-guard

Rust-backed output length guard plugin for MCP Gateway. Guards tool outputs by enforcing configurable minimum/maximum character or token limits, with either truncation or blocking strategies.

Features

  • Character mode (limit_mode: "character"): enforce min/max character counts
  • Token mode (limit_mode: "token"): enforce min/max estimated token counts (using configurable chars_per_token ratio)
  • Truncate strategy: shorten over-limit output, optionally at word boundaries, with configurable ellipsis
  • Block strategy: return a PluginViolation to halt processing when limits are exceeded
  • Supported input shapes:
    • Plain str
    • dict with a text field
    • list[str]
    • MCP content array: [{"type": "text", "text": "..."}]
    • MCP CallToolResult dict with content list (and optional structuredContent)
  • Numeric string preservation: numeric values (integers, floats, scientific notation) pass through without modification
  • Security limits: max_text_length, max_structure_size, max_recursion_depth prevent DoS from oversized inputs

Configuration

kind: "cpex_output_length_guard.output_length_guard.OutputLengthGuardPlugin"
available_hooks:
  - "tool_post_invoke"
config:
  min_chars: 0           # Minimum characters (0 = disabled)
  max_chars: 15000       # Maximum characters (null = disabled)
  min_tokens: 0          # Minimum estimated tokens (0 = disabled)
  max_tokens: null       # Maximum estimated tokens (null = disabled)
  chars_per_token: 4     # Characters per token estimate (1–10)
  limit_mode: "character"  # "character" or "token"
  strategy: "truncate"    # "truncate" or "block"
  ellipsis: "…"          # Appended on truncation (empty = none)
  word_boundary: false   # Truncate at word boundary
  max_text_length: 1000000    # Security: max bytes to process (1KB–10MB)
  max_structure_size: 10000   # Security: max items in list/dict (1–100K; Rust intentionally supports tighter limits than Python)
  max_recursion_depth: 100    # Security: max nesting depth (10–1000)

Observability

When an OpenTelemetry trace is active (via extensions.request.trace_id), the plugin emits metrics to result.metadata["output_length_guard"]:

result.metadata["output_length_guard"] = {
    "chars_seen": 42000,       # characters in the oversized content
    "truncated_count": 1,      # number of items truncated
    "blocked": False,          # True if blocked, False if truncated
    "limit_mode": "character", # enforcement mode used
    "strategy": "truncate",    # strategy applied
    "stage": "tool_post_invoke",
}

Metrics never contain raw output content — only counts, labels, and status indicators.

Violation Codes

Code Description
OUTPUT_LENGTH_VIOLATION String length outside configured bounds
OUTPUT_TOKEN_VIOLATION Estimated token count outside configured bounds
STRUCTURE_SIZE_VIOLATION List/dict too large (security limit)
STRUCTURE_DEPTH_VIOLATION Nesting too deep (security limit)

Development

uv sync --dev
make install          # Build Rust extension and install
make test-all         # Run Rust + Python tests
make test-integration # Run plugin-framework integration tests
make check-all        # fmt-check + clippy + Rust tests

License

Apache-2.0

Metadata

Release files for cpex-output-length-guard 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cpex-output-length-guard 0.1.2
File Size Uploaded
cpex_output_length_guard-0.1.2.tar.gz 61.0 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for cpex-output-length-guard 0.1.2
File
cpex_output_length_guard-0.1.2-cp311-abi3-win_amd64.whl CPython 3.11 abi3 Windows x86-64 Details
cpex_output_length_guard-0.1.2-cp311-abi3-manylinux_2_34_x86_64.whl CPython 3.11 abi3 Linux glibc 2.34+ x86-64 Details
cpex_output_length_guard-0.1.2-cp311-abi3-manylinux_2_34_s390x.whl CPython 3.11 abi3 Linux glibc 2.34+ IBM System/390x Details
cpex_output_length_guard-0.1.2-cp311-abi3-manylinux_2_34_ppc64le.whl CPython 3.11 abi3 Linux glibc 2.34+ PowerPC 64-le Details
cpex_output_length_guard-0.1.2-cp311-abi3-manylinux_2_34_aarch64.whl CPython 3.11 abi3 Linux glibc 2.34+ ARM64 Details
cpex_output_length_guard-0.1.2-cp311-abi3-macosx_11_0_arm64.whl CPython 3.11 abi3 macOS 11.0+ ARM64 Details

Total release size: 1.8 MB

Release files / cpex_output_length_guard-0.1.2.tar.gz

Download URL cpex_output_length_guard-0.1.2.tar.gz
Size 61.0 kB
Tags Source
SHA-256 checksum
How to use checksums
a9ae8956b0d6cde40057244e170fbd6cae3deb5c918878c78eff98f1204cdd41
BLAKE2b-256 checksum
How to use checksums
28a9bbede74a445eb4f478d6c1d6ca35e20010ffe516c922037d724d26f9adbf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 22, 2026.

Transparency log

Release files / cpex_output_length_guard-0.1.2-cp311-abi3-win_amd64.whl

Download URL cpex_output_length_guard-0.1.2-cp311-abi3-win_amd64.whl
Size 209.5 kB
Tags CPython 3.11 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
6c06778ddc1d435f75775cfdb271d3a91a43946d683ad6a294a6458897f51c52
BLAKE2b-256 checksum
How to use checksums
a84b25af82483c2cdca4e794c4e0df0e5774eaabb796f6fbcedea0c42c1d7c21
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 22, 2026.

Transparency log

Release files / cpex_output_length_guard-0.1.2-cp311-abi3-manylinux_2_34_x86_64.whl

Download URL cpex_output_length_guard-0.1.2-cp311-abi3-manylinux_2_34_x86_64.whl
Size 294.5 kB
Tags CPython 3.11 Linux glibc 2.34+ x86-64 abi3
SHA-256 checksum
How to use checksums
356b0b98b28b97e2750b3f26aecff539db38268e0e69c2aa1f6bc89f4fa571cd
BLAKE2b-256 checksum
How to use checksums
2378b0c307d01a5796b0b7d16098d3d04df4b36efb0e604a4f754c9e23826c0a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 22, 2026.

Transparency log

Release files / cpex_output_length_guard-0.1.2-cp311-abi3-manylinux_2_34_s390x.whl

Download URL cpex_output_length_guard-0.1.2-cp311-abi3-manylinux_2_34_s390x.whl
Size 323.9 kB
Tags CPython 3.11 Linux glibc 2.34+ IBM System/390x abi3
SHA-256 checksum
How to use checksums
ae0f45fd6d70f7b8ec9ba014823b03caecba9f15e082f3e33119c5f8c9eedcc8
BLAKE2b-256 checksum
How to use checksums
aa55ea80eb6554b941259e1a0ac0c127958832657f340c1beae78a98bd54e9e5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 22, 2026.

Transparency log

Release files / cpex_output_length_guard-0.1.2-cp311-abi3-manylinux_2_34_ppc64le.whl

Download URL cpex_output_length_guard-0.1.2-cp311-abi3-manylinux_2_34_ppc64le.whl
Size 322.3 kB
Tags CPython 3.11 Linux glibc 2.34+ PowerPC 64-le abi3
SHA-256 checksum
How to use checksums
38ff0fa1bf4444a44c919555ce006abd50055c7ab285a01862798ac284632ca4
BLAKE2b-256 checksum
How to use checksums
374e5712922a21b70c50e4f9dc1a215ac5228cfdb1f62416399988d6005752b7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 22, 2026.

Transparency log

Release files / cpex_output_length_guard-0.1.2-cp311-abi3-manylinux_2_34_aarch64.whl

Download URL cpex_output_length_guard-0.1.2-cp311-abi3-manylinux_2_34_aarch64.whl
Size 279.1 kB
Tags CPython 3.11 Linux glibc 2.34+ ARM64 abi3
SHA-256 checksum
How to use checksums
dd2bd15600ddbe230ce401b218dbac5d27d28df49fbe99811f3ea1d2415379b3
BLAKE2b-256 checksum
How to use checksums
56af147b874f30da315e1d9d0c0075f711b5fc53053c2cf5da7e3b024c9e18e0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 22, 2026.

Transparency log

Release files / cpex_output_length_guard-0.1.2-cp311-abi3-macosx_11_0_arm64.whl

Download URL cpex_output_length_guard-0.1.2-cp311-abi3-macosx_11_0_arm64.whl
Size 267.6 kB
Tags CPython 3.11 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
324a4b2c96e1ae4c0cecb1887b7e3d72511c01edbaf9a1732d03e96d4938cc38
BLAKE2b-256 checksum
How to use checksums
27eaebeb36be2375760470f0cc9fa60bb807aa5f2372c0b8a0001acba2eeb9c4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 22, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.1.2 This release

7 release files

0.1.0

7 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page