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

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.4
File Size Uploaded Provenance Actions
cpex_output_length_guard-0.1.4.tar.gz 61.4 kB With provenance Details for cpex_output_length_guard-0.1.4.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for cpex-output-length-guard 0.1.4
File Actions
cpex_output_length_guard-0.1.4-cp311-abi3-win_amd64.whl CPython 3.11 abi3 Windows x86-64 Details for cpex_output_length_guard-0.1.4-cp311-abi3-win_amd64.whl
cpex_output_length_guard-0.1.4-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.4-cp311-abi3-manylinux_2_34_x86_64.whl
cpex_output_length_guard-0.1.4-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.4-cp311-abi3-manylinux_2_34_s390x.whl
cpex_output_length_guard-0.1.4-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.4-cp311-abi3-manylinux_2_34_ppc64le.whl
cpex_output_length_guard-0.1.4-cp311-abi3-manylinux_2_34_aarch64.whl CPython 3.11 abi3 Linux glibc 2.34+ ARM64 Details for cpex_output_length_guard-0.1.4-cp311-abi3-manylinux_2_34_aarch64.whl
cpex_output_length_guard-0.1.4-cp311-abi3-macosx_11_0_arm64.whl CPython 3.11 abi3 macOS 11.0+ ARM64 Details for cpex_output_length_guard-0.1.4-cp311-abi3-macosx_11_0_arm64.whl

Total release size: 1.7 MB

Release files / cpex_output_length_guard-0.1.4.tar.gz

Download URL cpex_output_length_guard-0.1.4.tar.gz
Size 61.4 kB
Tags Source
SHA-256 checksum
How to use checksums
e884d7a794cedd8bdbbff1b1313a68e41d4b82519765cde67a35d6bfba8c323a
BLAKE2b-256 checksum
How to use checksums
62a8edf6277acccc9d4a5944b15343bf5459c40e2269b6347b706edd0231a19e
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 Nov 25, 2026.

Transparency log

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

Download URL cpex_output_length_guard-0.1.4-cp311-abi3-win_amd64.whl
Size 211.4 kB
Tags CPython 3.11 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
6258510f4b8a3fd45d1a2dba022f8758d110305066bf1acb26db17c9dc3b8c14
BLAKE2b-256 checksum
How to use checksums
138a66d4c40c82f9ae64e635b3c317bd85013878fc3b88bb8b212516c939211e
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 Nov 25, 2026.

Transparency log

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

Download URL cpex_output_length_guard-0.1.4-cp311-abi3-manylinux_2_34_x86_64.whl
Size 287.0 kB
Tags CPython 3.11 Linux glibc 2.34+ x86-64 abi3
SHA-256 checksum
How to use checksums
444901bdc5aa5962cc5d429178f465e46524b5120580e83c410b560db9b2da55
BLAKE2b-256 checksum
How to use checksums
4cd9c8a8ee4eb7c1c86ffaf85e89f0bd147f4ea1c88aa0e47f7a8a54fe0dfd7d
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 Nov 25, 2026.

Transparency log

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

Download URL cpex_output_length_guard-0.1.4-cp311-abi3-manylinux_2_34_s390x.whl
Size 316.4 kB
Tags CPython 3.11 Linux glibc 2.34+ IBM System/390x abi3
SHA-256 checksum
How to use checksums
612cf98a816fd7e69075adfe873cff716bad903fd2c6db23d3d5260fec4b47c3
BLAKE2b-256 checksum
How to use checksums
d6fa58b650f1059681e1f77aab1f64fe4b58c4245afca4d7ec92a21e7454fa31
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 Nov 25, 2026.

Transparency log

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

Download URL cpex_output_length_guard-0.1.4-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
863fc737c4c458622f8dd88a984028aa3ff9fea9309ab561cdbff480a7551157
BLAKE2b-256 checksum
How to use checksums
ca53a48937626d4f0ab6e3a0ad1b9c3e4262df77fd6d9ed02f49612de2f3e52b
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 Nov 25, 2026.

Transparency log

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

Download URL cpex_output_length_guard-0.1.4-cp311-abi3-manylinux_2_34_aarch64.whl
Size 273.4 kB
Tags CPython 3.11 Linux glibc 2.34+ ARM64 abi3
SHA-256 checksum
How to use checksums
9ec21d1ec98851222fa07325b3a7391acb032c1b18be2391f4651e41a182afe9
BLAKE2b-256 checksum
How to use checksums
a3dacf3ef19a94bf501b5d2b7ad49376a4620094af1b96841dedd4469776c858
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 Nov 25, 2026.

Transparency log

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

Download URL cpex_output_length_guard-0.1.4-cp311-abi3-macosx_11_0_arm64.whl
Size 265.1 kB
Tags CPython 3.11 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
a6a977515f3a81cec5b4bad99cdd28cec93f63398413ec775e0664873207305a
BLAKE2b-256 checksum
How to use checksums
c7052f3a2833249f2c2f3764faea1314f7d89353ebf185c57ce651317499a44c
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 Nov 25, 2026.

Transparency log

Security for cpex-output-length-guard 0.1.4

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.4.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 Nov 25, 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