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.3

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.3
File Size Uploaded Provenance Actions
cpex_output_length_guard-0.1.3.tar.gz 61.3 kB With provenance Details for cpex_output_length_guard-0.1.3.tar.gz

Built distributions (wheels)

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

Total release size: 1.7 MB

Release files / cpex_output_length_guard-0.1.3.tar.gz

Download URL cpex_output_length_guard-0.1.3.tar.gz
Size 61.3 kB
Tags Source
SHA-256 checksum
How to use checksums
d6c8851303f260b7f8af64bc5ad47eac018651184e012bc24c08fb3fc1930da7
BLAKE2b-256 checksum
How to use checksums
ce4198fc8a955ab0cf8c0c48a0d921bd315c65eeef8b0706c06fe8f62c9fd698
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Oct 28, 2026.

Transparency log

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

Download URL cpex_output_length_guard-0.1.3-cp311-abi3-win_amd64.whl
Size 209.5 kB
Tags CPython 3.11 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
31da4e382b79aeb2aad564fd1d60baf59b654cf899b94d8bb21569e5c0a1badd
BLAKE2b-256 checksum
How to use checksums
025b51a999a03956f8b5aa0970cf62e8e578f3cd9e7d29f2bda667b0ee73464d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Oct 28, 2026.

Transparency log

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

Download URL cpex_output_length_guard-0.1.3-cp311-abi3-manylinux_2_34_x86_64.whl
Size 285.4 kB
Tags CPython 3.11 Linux glibc 2.34+ x86-64 abi3
SHA-256 checksum
How to use checksums
97194566d1b870a5021b080233bf1c6bba0dd64759f01b0cf4b9aa2dbdf1d759
BLAKE2b-256 checksum
How to use checksums
deb29a99b10d592a94bf00eca121338928f3d00d49778b2c7d43e37170e3a7ae
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Oct 28, 2026.

Transparency log

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

Download URL cpex_output_length_guard-0.1.3-cp311-abi3-manylinux_2_34_s390x.whl
Size 319.4 kB
Tags CPython 3.11 Linux glibc 2.34+ IBM System/390x abi3
SHA-256 checksum
How to use checksums
76645e21146fa1a3223655823bd48dc9243ceef6b099def86b5a5a5f1b898bd5
BLAKE2b-256 checksum
How to use checksums
26b9ba1fba6a997128face32eec318e1f71fbb47c5566024e62996aeff4460ea
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Oct 28, 2026.

Transparency log

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

Download URL cpex_output_length_guard-0.1.3-cp311-abi3-manylinux_2_34_ppc64le.whl
Size 314.1 kB
Tags CPython 3.11 Linux glibc 2.34+ PowerPC 64-le abi3
SHA-256 checksum
How to use checksums
77d258d3ee7fff23d339f533277fe17dcba615afb9bdc9cac3a22dfa1ac7181a
BLAKE2b-256 checksum
How to use checksums
7d040f094529a331ef423babc6a8ede8db4dd35c1d6c3c267762da203f387480
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Oct 28, 2026.

Transparency log

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

Download URL cpex_output_length_guard-0.1.3-cp311-abi3-manylinux_2_34_aarch64.whl
Size 271.9 kB
Tags CPython 3.11 Linux glibc 2.34+ ARM64 abi3
SHA-256 checksum
How to use checksums
4232e7c7cc5cd472fcb43c657526f8ee254586a6298c2da5bf7cf5b133c3953b
BLAKE2b-256 checksum
How to use checksums
df2a10570ab953ea42d641feab002bda046bd1298df2ba7df184e8867ab0a9b0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Oct 28, 2026.

Transparency log

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

Download URL cpex_output_length_guard-0.1.3-cp311-abi3-macosx_11_0_arm64.whl
Size 263.7 kB
Tags CPython 3.11 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
02ffa66d4c8da7db1056500208a53ce3290a820d7198dae0a85f18acae1ed652
BLAKE2b-256 checksum
How to use checksums
d68c78bb7f583ef928934e46e3755718d75bc8e21d18df79fdc35cb48f7c4c17
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Oct 28, 2026.

Transparency log

Security for cpex-output-length-guard 0.1.3

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.

Full provenance

All 7 files in this release have been uploaded with provenance metadata.

Select from 7 files

Attestation for cpex_output_length_guard-0.1.3.tar.gz

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 Oct 28, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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