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

Release files for cpex-output-length-guard 0.1.0

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.0
File Size Uploaded
cpex_output_length_guard-0.1.0.tar.gz 143.2 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for cpex-output-length-guard 0.1.0
File
cpex_output_length_guard-0.1.0-cp311-abi3-win_amd64.whl CPython 3.11 abi3 Windows x86-64 Details
cpex_output_length_guard-0.1.0-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.0-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.0-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.0-cp311-abi3-manylinux_2_34_aarch64.whl CPython 3.11 abi3 Linux glibc 2.34+ ARM64 Details
cpex_output_length_guard-0.1.0-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.0.tar.gz

Download URL cpex_output_length_guard-0.1.0.tar.gz
Size 143.2 kB
Tags Source
SHA-256 checksum
How to use checksums
a942f38642a8d55a0c41aaefb860191b6a4110adfc7eb480eafcb2e0a64d61bc
BLAKE2b-256 checksum
How to use checksums
4ec83793e1c0901003ade650e9f17783ae657dfc6134486ecb002e1e70566987
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.15

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

Download URL cpex_output_length_guard-0.1.0-cp311-abi3-win_amd64.whl
Size 209.5 kB
Tags CPython 3.11 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
48bd132b6da6180ee5640bd37cf551565efbdc694632f4d60d7d075147e60efc
BLAKE2b-256 checksum
How to use checksums
d6d3413c132792d1b4e3e0a59b301b52de66cc2be5b7d4f4df88474b543da0d1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.15

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

Download URL cpex_output_length_guard-0.1.0-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
266de17e8e627d7e9b12bd028ba55ec0930ab271033970af32945f33aef87781
BLAKE2b-256 checksum
How to use checksums
e1308af8b0619a7d57ea66f62a78c548aa6e99601728b734c44cef602693bdf5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.15

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

Download URL cpex_output_length_guard-0.1.0-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
a51e7b2e912bf59c112457188c5dea8a4ecb5895e4d6a40869263d9565f69be8
BLAKE2b-256 checksum
How to use checksums
44096f6cc851dc2b4672abf1800a5448b723f583de6619ad51eaca0b20347882
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.15

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

Download URL cpex_output_length_guard-0.1.0-cp311-abi3-manylinux_2_34_ppc64le.whl
Size 322.2 kB
Tags CPython 3.11 Linux glibc 2.34+ PowerPC 64-le abi3
SHA-256 checksum
How to use checksums
af79af656cf43f39f707c8292185724ca76092c0cf6145437dbfcf7871694e29
BLAKE2b-256 checksum
How to use checksums
1bf49517fbcd81be1009070dde9c399e871bfcada9326d4ff48acb61df897bce
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.15

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

Download URL cpex_output_length_guard-0.1.0-cp311-abi3-manylinux_2_34_aarch64.whl
Size 279.3 kB
Tags CPython 3.11 Linux glibc 2.34+ ARM64 abi3
SHA-256 checksum
How to use checksums
18b736e7e43e7ee693cf9b6bc97158ed548c8d2dab4bfe5ca38a7b53d3e02e8c
BLAKE2b-256 checksum
How to use checksums
5426d92b2b1977843d0501935d3b3545ae2a70806da9f7db7dabf452639964f9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.15

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

Download URL cpex_output_length_guard-0.1.0-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
3d7c67930e628d7d895804b7f6c193b7fd4556bc2af31af01beb2cd97341868c
BLAKE2b-256 checksum
How to use checksums
cadf1757e8cf0b783c607ecdf42bbc09d0b0be5d7f7c7901472b43810d215c29
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.15

Release history Release notifications | RSS feed

This release

0.1.0 This release

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