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

Ouverte
#733 2 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Les mainteneurs répondent en général sous 1 jour

Personne n'a encore pris cette issue.

Évaluation

Difficulté
4/5
Temps estimé
3-5 jours
Accessibilité débutants
48/100
Type d'issue
Refactorisation
Clarté
Plutôt claire
Activité
Active
Stack technique
php

Piste de recherche

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.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Description

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.

Langage dominant
PHP
Étoiles
1.7k
Forks
206
Merge moyen
1 j 17 h
PR mergées (30 j)
102

Préparer son environnement

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Autres issues de modelcontextprotocol/php-sdk

Toutes les issues de modelcontextprotocol/php-sdk

Issues similaires

Plus d'issues PHP

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.