Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

[Server] Handler fibers give integrations no hook to carry OpenTelemetry's context

Aperta
#583 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
18/100
Tipo di issue
Funzionalità
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
php

Direzione di ricerca

The handler fiber is started in src/Server/Protocol.php (around lines 347-358) and resumed in src/Server/Transport/StreamableHttpTransport.php (around 305-328) and StdioTransport.php (around 363-385), so any hook must reach all three. Read how each resume path is structured before choosing an extension point. Done means maintainers agree on the observer's shape and a test shows a handler fiber can be wrapped on start, resume and termination.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Version(s) affected

0.8.1 (via symfony/mcp-bundle 0.14.1); the handler fiber is unchanged on main at b35b52d.

Description

Protocol runs every request handler in a \Fiber (Protocol.php#L347-L358), and the transports resume it (StreamableHttpTransport.php#L305-L328, StdioTransport.php#L363-L385).

OpenTelemetry PHP keeps its context in a fiber-bound storage by default (open-telemetry/context 1.5). The first context access inside a fiber it was not told about raises an E_USER_WARNING:

Access to not initialized OpenTelemetry context in fiber (id: 1712), automatic forking not supported, must attach initial fiber context manually

With OpenTelemetry auto-instrumentation installed, almost any handler touches the context (a PDO query, an HTTP call, a log record). Under Symfony's debug error handler the warning becomes an ErrorException, so every tools/call answers -32603 Internal server error. In production the warning is logged on each call, and the handler's spans only find a parent through OpenTelemetry's fallback to the main context.

OpenTelemetry's built-in answer, automatic fiber forking (OTEL_PHP_FIBERS_ENABLED with ext-ffi), requires an NTS build. It is not available on ZTS builds, and FrankenPHP is always ZTS. So the application has no way to give the handler fiber its context.

How to reproduce

Without the SDK, with only open-telemetry/context installed:

$fiber = new \Fiber(static fn () => \OpenTelemetry\Context\Context::getCurrent());
$fiber->start(); // E_USER_WARNING: Access to not initialized OpenTelemetry context in fiber

With the SDK: an MCP server whose tool runs any OpenTelemetry-instrumented code (e.g. a PDO query with open-telemetry/opentelemetry-auto-pdo), served over Streamable HTTP in a Symfony app in debug mode. tools/call answers -32603, and the log shows the warning above.

Possible Solution

OpenTelemetry supports code that drives its own fibers through ExecutionContextAwareInterface on Context::storage(): fork($id) before a fiber first starts, switch($id) around each start()/resume(), destroy($id) when it terminates. The SDK should not depend on OpenTelemetry, but a small extension point around the handler fiber would let an integration (the Symfony bundle, or an OpenTelemetry contrib package) do this switching. For example, an observer the protocol and the transports call:

interface FiberObserverInterface // name and shape open for discussion
{
    public function beforeStart(\Fiber $fiber): void;
    public function beforeResume(\Fiber $fiber): void;
    public function afterSuspendOrReturn(\Fiber $fiber): void;
    public function terminated(\Fiber $fiber): void;
}

Because the fiber is started in Protocol and resumed in each transport, the hook has to reach both places. Alternatively, the resume logic could first move to a single place.

Additional Context

Our workaround: for the duration of an MCP request we replace the storage with OpenTelemetry's plain ContextStorage, seeded with the current context, and restore it afterwards (Context::setStorage() on kernel.request / kernel.finish_request). This works because our handlers never suspend, but it relies on a class OpenTelemetry marks @internal, and every application has to rediscover it.


This issue was investigated and written by Claude Opus 5.5 (Anthropic) while building an MCP server on symfony/mcp-bundle in our application, and filed on our behalf.

Lingua principale
PHP
Stelle
1.6k
Fork
177
Merge medio
2g 16h
PR unite (30g)
36

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di modelcontextprotocol/php-sdk

Tutte le issue di modelcontextprotocol/php-sdk

Issue simili

Altre issue su PHP

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.