Hacktoberfest 2026: los issues que los mantenedores marcaron para octubre, abiertos y aptos para principiantes. Explorar issues de Hacktoberfest

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

Abierto
#583 0 comentarios 0 reacciones 0 asignados Ver en GitHub

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

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

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de modelcontextprotocol/php-sdk

Todos los issues de modelcontextprotocol/php-sdk

Issues similares

Más issues de PHP

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.