Hacktoberfest 2026:維護者為十月標記出來的 issue,仍然開放、適合新手。 瀏覽 Hacktoberfest issue

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

未關閉
#583 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

維護者通常 1 天內回覆

還沒有人認領這個 Issue。

評估

難度
5/5
預估耗時
一週以上
新手友好度
18/100
Issue 類型
功能
描述清晰度
基本清楚
活躍度
活躍
技術堆疊
php

研究方向

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.

由索引模型根據 Issue 內容生成。

描述

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.

主要語言
PHP
星號
1.6k
分支
180
平均合併
2 天 16 小時
30 天內合併 PR
36

環境準備

  • 沒有 Dockerfile 或 Docker Compose 檔案
  • 沒有 Pull Request 範本
  • 閱讀貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

modelcontextprotocol/php-sdk 的其他 Issue

查看 modelcontextprotocol/php-sdk 的全部 Issue

相似的 Issue

更多 PHP Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。