Skip to content

feat: add experimental rust validation middleware#4031

Closed
lucarlig wants to merge 58 commits intomainfrom
fix/rust-validation-middleware-sidecar
Closed

feat: add experimental rust validation middleware#4031
lucarlig wants to merge 58 commits intomainfrom
fix/rust-validation-middleware-sidecar

Conversation

@lucarlig
Copy link
Copy Markdown
Collaborator

@lucarlig lucarlig commented Apr 3, 2026

✨ Feature / Enhancement PR

🔗 Epic / Issue

No linked issue.


🚀 Summary (1-2 sentences)

Adds an experimental opt-in Rust implementation for mcpgateway/middleware/validation_middleware.py that moves the validation hot path into a compiled PyO3 extension while preserving Python fallback behavior when the extension is unavailable.


🧪 Checks

  • make lint passes
  • make test passes
  • CHANGELOG updated (if user-facing)

📓 Notes (optional)

This branch was benchmarked in two ways against the Python baseline. Focused validation numbers below were refreshed on 2026-04-24 after the latest review fixes.

  1. Focused validation microbenchmark
    This benchmark isolates the validation middleware hot path and measures validation cost directly on representative payload shapes. It is useful for showing the Rust validator speedup without unrelated framework overhead.
Scenario What is being tested Python median Rust median Speedup
resource_path Allowed-root resource path validation 0.031 ms 0.012 ms 2.51x
parameter_batch Batched path/query parameter validation 0.005 ms 0.002 ms 2.53x
response_sanitization Control-character removal from response body 0.052 ms 0.011 ms 4.84x
response_sanitization_safe Safe ASCII response body sanitizer fast path 0.025 ms 0.006 ms 4.52x
small_safe Small safe JSON payload 0.003 ms 0.001 ms 2.93x
first_field_reject Early rejection of dangerous content 0.002 ms 0.002 ms 1.14x
unicode_safe_long Long safe Unicode-heavy string validation 0.014 ms 0.005 ms 2.63x
nested_safe Large nested safe JSON payload traversal 0.335 ms 0.033 ms 10.09x
deep_nested Deep nested JSON traversal and depth-safe validation 2.424 ms 0.340 ms 7.13x
dangerous_string Rejection of dangerous content in JSON strings 1.025 ms 0.092 ms 11.09x
mixed_params_json Request path combining parameter checks with JSON validation 0.003 ms 0.001 ms 2.55x
  1. End-to-end validation benchmark
    This benchmark uses Locust against /protocol/initialize with validation enabled and a validation-heavy mix of accepted and rejected JSON payloads. It exercises the real FastAPI request path, including request parsing, auth, middleware validation, and route handling.
Scenario What is being tested Python median Rust median Python RPS Rust RPS Latency speedup Throughput gain
safe-large Accepted large initialize payload through the full request path 180 ms 160 ms 244.94 255.95 1.12x 1.04x
rejected-large Rejected large initialize payload returning 422 through the full request path 110 ms 100 ms 82.09 86.99 1.10x 1.06x
aggregated Combined accepted and rejected validation-heavy traffic 160 ms 150 ms 327.03 342.94 1.07x 1.05x

The end-to-end run was executed in production-mode validation semantics so rejected payloads were actually blocked instead of only logged.

No ADR added because this remains an experimental opt-in implementation behind a feature flag and does not change the default architecture.


Refs #1807

@lucarlig lucarlig added experimental Experimental features, test proposed MCP Specification changes rust Rust programming labels Apr 3, 2026
@lucarlig lucarlig force-pushed the fix/rust-validation-middleware-sidecar branch from 1eeb9dc to b623679 Compare April 7, 2026 09:50
@lucarlig lucarlig marked this pull request as ready for review April 7, 2026 21:10
@lucarlig lucarlig force-pushed the fix/rust-validation-middleware-sidecar branch 8 times, most recently from 6101dd0 to c609a25 Compare April 8, 2026 10:10
@lucarlig lucarlig added the release-fix Critical bugfix required for the release label Apr 8, 2026
@lucarlig lucarlig requested a review from dawid-nowak April 9, 2026 08:42
@lucarlig lucarlig changed the title feat: add experimental rust validation middleware sidecar feat: add experimental rust validation middleware Apr 10, 2026
dima-zakharov
dima-zakharov previously approved these changes Apr 13, 2026
@lucarlig
Copy link
Copy Markdown
Collaborator Author

check if anything from #4204 applies to changes here

@lucarlig lucarlig force-pushed the fix/rust-validation-middleware-sidecar branch from 7c7ebe7 to 0c821d6 Compare April 15, 2026 15:19
@lucarlig lucarlig requested a review from brian-hussey as a code owner April 15, 2026 15:19
@lucarlig lucarlig force-pushed the fix/rust-validation-middleware-sidecar branch 2 times, most recently from c40e268 to 75b41f4 Compare April 21, 2026 09:41
Signed-off-by: lucarlig <luca.carlig@ibm.com>
Signed-off-by: lucarlig <luca.carlig@ibm.com>
Signed-off-by: lucarlig <luca.carlig@ibm.com>
Signed-off-by: lucarlig <luca.carlig@ibm.com>
lucarlig and others added 22 commits April 24, 2026 08:56
Signed-off-by: lucarlig <luca.carlig@ibm.com>
Signed-off-by: lucarlig <luca.carlig@ibm.com>
Signed-off-by: lucarlig <luca.carlig@ibm.com>
Signed-off-by: lucarlig <luca.carlig@ibm.com>
Signed-off-by: lucarlig <luca.carlig@ibm.com>
Signed-off-by: lucarlig <luca.carlig@ibm.com>
Signed-off-by: lucarlig <luca.carlig@ibm.com>
Signed-off-by: lucarlig <luca.carlig@ibm.com>
Signed-off-by: lucarlig <luca.carlig@ibm.com>
Signed-off-by: lucarlig <luca.carlig@ibm.com>
Signed-off-by: lucarlig <luca.carlig@ibm.com>
Signed-off-by: lucarlig <luca.carlig@ibm.com>
Signed-off-by: lucarlig <luca.carlig@ibm.com>
Signed-off-by: lucarlig <luca.carlig@ibm.com>
Signed-off-by: lucarlig <luca.carlig@ibm.com>
Signed-off-by: lucarlig <luca.carlig@ibm.com>
Signed-off-by: lucarlig <luca.carlig@ibm.com>
Signed-off-by: lucarlig <luca.carlig@ibm.com>
Signed-off-by: lucarlig <luca.carlig@ibm.com>
Signed-off-by: lucarlig <luca.carlig@ibm.com>
Signed-off-by: lucarlig <luca.carlig@ibm.com>
Signed-off-by: lucarlig <20149527+lucarlig@users.noreply.github.com>
@lucarlig lucarlig force-pushed the fix/rust-validation-middleware-sidecar branch from 7f962ee to 9ef0ab7 Compare April 24, 2026 08:08
lucarlig and others added 5 commits April 24, 2026 09:11
Signed-off-by: lucarlig <20149527+lucarlig@users.noreply.github.com>
Signed-off-by: lucarlig <20149527+lucarlig@users.noreply.github.com>
Signed-off-by: lucarlig <luca.carlig@ibm.com>
Signed-off-by: lucarlig <luca.carlig@ibm.com>
Signed-off-by: lucarlig <luca.carlig@ibm.com>
@lucarlig
Copy link
Copy Markdown
Collaborator Author

lucarlig commented May 5, 2026

new CF-DATAPLANE will sove this

@lucarlig lucarlig closed this May 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

experimental Experimental features, test proposed MCP Specification changes release-fix Critical bugfix required for the release rust Rust programming

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants