Skip to content

feat: add experimental rust validation middleware#4031

Open
lucarlig wants to merge 41 commits intomainfrom
fix/rust-validation-middleware-sidecar
Open

feat: add experimental rust validation middleware#4031
lucarlig wants to merge 41 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.

  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
small_safe Small safe JSON payload 0.002 ms 0.001 ms 1.56x
unicode_safe_long Long safe Unicode-heavy string validation 0.010 ms 0.005 ms 1.87x
nested_safe Large nested safe JSON payload traversal 0.173 ms 0.036 ms 4.79x
deep_nested Deep nested JSON traversal and depth-safe validation 1.355 ms 0.366 ms 3.71x
dangerous_string Rejection of dangerous content in JSON strings 0.509 ms 0.100 ms 5.07x
mixed_params_json Request path combining parameter checks with JSON validation 0.002 ms 0.002 ms 1.41x
  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

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>
lucarlig added 25 commits April 15, 2026 15:51
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 <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 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
Signed-off-by: lucarlig <luca.carlig@ibm.com>
Signed-off-by: lucarlig <luca.carlig@ibm.com>
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