[Server] Decode an HTTP request body once instead of per pipeline stage

Offen
#733 2 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Maintainer antworten meist innerhalb von 1 Tag

Dieses Issue hat noch niemand übernommen.

Bewertung

Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Anfängerfreundlichkeit
48/100
Issue-Typ
Refactoring
Klarheit
Größtenteils klar
Aktivitätsstatus
Aktiv
Tech-Stack
php

Rechercherichtung

Start with InboundClassifier::classify(), Protocol and StatelessProtocol, MessageFactory::create(), and Json::objectsToArrays() to trace how the request body is decoded across the StreamableHttpTransport pipeline. Check the HTTP transport tests and add coverage showing that one decoded message is shared through classification and handling. Update docs/run/http.md to explain that maxBodyBytes limits encoded bytes and that decoded memory use depends on body shape; the issue leaves whether to lower the default unresolved.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

enhancement P3

Currently a POST body is decoded several times before it reaches a handler: InboundClassifier::classify(), then Protocol or StatelessProtocol, then MessageFactory::create() and Json::objectsToArrays(). Not all copies are alive at the same time, but peak memory ends up at about twice a single decode, and the CPU cost is paid on every pass - also for requests that are rejected right after.

Measured on main with a tools/call for an unknown tool through StreamableHttpTransport:

body (600 KB) one json_decode() full pipeline
list of [0] 35.6 MB 74.4 MB
list of {"a":0} 31.9 MB 68.4 MB
list of {} 4.0 MB 33.4 MB
list of ints 8.0 MB 19.1 MB

Two things to do:

  • decode once at the edge and pass the decoded message down, so classification, the protocol and the MessageFactory share it
  • document in docs/run/http.md that maxBodyBytes bounds bytes, not decoded size - adversarial shapes need ~60x the body in memory, so the cap should be sized against memory_limit. Maybe also worth lowering the 4 MiB default.

Reported by @kanokwut.

Vorherrschende Sprache
PHP
Sterne
1.7k
Forks
206
Ø Merge
1 T. 17 Std.
Gemergte PRs (30 T.)
102

Entwicklungsumgebung

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus modelcontextprotocol/php-sdk

Alle Issues in modelcontextprotocol/php-sdk

Ähnliche Issues

Weitere Issues zu PHP

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.