Skip to content

Reuse decoded MCP request params in the parser - #7475

Open
ningsiyuan1024 wants to merge 1 commit into
stacklok:mainfrom
ningsiyuan1024:perf/7224-reuse-parsed-params
Open

ningsiyuan1024 wants to merge 1 commit into
stacklok:mainfrom
ningsiyuan1024:perf/7224-reuse-parsed-params

Conversation

@ningsiyuan1024

Copy link
Copy Markdown

Summary

The request parser used to build the same params map twice. Admission decoded the params to check that authorization could consume their numeric values, then discarded the map. Parsing built it again to extract the resource and arguments. The middleware and replacement-body path now each reuse the decoded map from their own admission step. DecodeMessage keeps its public signature, and ParsedMCPRequest.Params keeps its original JSON bytes.

Fixes #7224.

Type of change

  • Refactoring (no behavior change)

Test plan

  • Targeted unit tests for the MCP package and its subpackages (go test -count=1 ./pkg/mcp/...). All four packages passed on 4b9f678 and again after integrating the patch onto 8ecced5.
  • Manually exercised the middleware over loopback HTTP. The original request carried ID 1, old-tool and tag A. After replacing the body and republishing, the downstream handler observed ID 2, new-tool and tag B, verified the exact replacement body and raw params, and returned HTTP 200.
  • Compared the same middleware benchmark before and after the change. It parses 1,024 nested document records and checks that the downstream handler receives all of them.

Seven alternating before/after runs on Windows amd64, Intel Core i9-13900H, one benchmark CPU. Medians on 4b9f678:

Metric Before After
ns/op 48,778,612 32,713,866
B/op 3,701,391 2,600,161
allocs/op 89,266 60,563

The behavior tests cover replacement bodies, omitted and empty initialization params, response handling, and admission errors. The replacement-body path derives its arguments from the replacement itself.

AI assistance

Codex handled execution.

@ChrisJBurns

Copy link
Copy Markdown
Collaborator

@ningsiyuan1024 Got some conflicts, but this should be good to approve and merge once fixed

Signed-off-by: ningsiyuan1024 <255516746+ningsiyuan1024@users.noreply.github.com>
@ningsiyuan1024
ningsiyuan1024 force-pushed the perf/7224-reuse-parsed-params branch from a5fe997 to 8f9605f Compare December 4, 2026 01:59

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MCP parser: decode request params once

2 participants