[Server] Handler fibers give integrations no hook to carry OpenTelemetry's context
Los mantenedores suelen responder en 1 día
Nadie ha tomado este issue todavía.
Evaluación
- Dificultad
- 5/5
- Tiempo estimado
- Más de una semana
- Aptitud para principiantes
- 18/100
- Tipo de issue
- Nueva funcionalidad
- Claridad
- Bastante claro
- Estado de actividad
- Activo
- Stack tecnológico
- php
- Área
- backend, observability
Línea de trabajo
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.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
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.
- Lenguaje dominante
- PHP
- Estrellas
- 1.6k
- Forks
- 177
- Merge medio
- 2 d 16 h
- PR fusionados (30 d)
- 36
Preparar el entorno
- Sin Dockerfile ni archivo de Docker Compose
- Sin plantilla de pull request
- Leer la guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de modelcontextprotocol/php-sdk
-
[Server] Handler type uses bare Closure, hard to decorate RegistryInterface under strict PHPStanAbiertoServer
Dificultad 1/5 Menos de una hora Aptitud para principiantes 78/100
modelcontextprotocol/php-sdk#468 · 2 comentarios ·
Los mantenedores suelen responder en 1 día
-
Builder::build() silently skips configured file-based discovery when symfony/finder is missing — should fail loudlyPosiblemente ocupada @ousamabenyounes la tomó hace 55 días. Abiertoneeds confirmation needs maintainer action Server
Dificultad 2/5 1-3 horas Aptitud para principiantes 68/100
modelcontextprotocol/php-sdk#398 · 1 reacción ·
Los mantenedores suelen responder en 1 día
-
enhancement
Dificultad 2/5 1-3 horas Aptitud para principiantes 68/100
modelcontextprotocol/php-sdk#370 ·
Los mantenedores suelen responder en 1 día
-
enhancement
Dificultad 4/5 3-5 días Aptitud para principiantes 52/100
modelcontextprotocol/php-sdk#587 ·
Los mantenedores suelen responder en 1 día
-
bug
Dificultad 3/5 Medio día Aptitud para principiantes 60/100
modelcontextprotocol/php-sdk#586 · 2 comentarios ·
Los mantenedores suelen responder en 1 día
Todos los issues de modelcontextprotocol/php-sdk
Issues similares
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 76/100
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 65/100
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 76/100
Los mantenedores suelen responder en 2 días
-
b:1.9 t:enhancement
Dificultad 2/5 1-3 horas Aptitud para principiantes 65/100
Los mantenedores suelen responder en 3 días
-
backend bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 76/100