[Server] Handler fibers give integrations no hook to carry OpenTelemetry's context
維護者通常 1 天內回覆
還沒有人認領這個 Issue。
評估
- 難度
- 5/5
- 預估耗時
- 一週以上
- 新手友好度
- 18/100
- Issue 類型
- 功能
- 描述清晰度
- 基本清楚
- 活躍度
- 活躍
- 技術堆疊
- php
- 領域
- backend, observability
研究方向
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 範本
- 閱讀貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
modelcontextprotocol/php-sdk 的其他 Issue
-
[Server] Handler type uses bare Closure, hard to decorate RegistryInterface under strict PHPStan未關閉Server
難度 1/5 1 小時以內 新手友好度 78/100
modelcontextprotocol/php-sdk#468 · 2 則留言 ·
維護者通常 1 天內回覆
-
Builder::build() silently skips configured file-based discovery when symfony/finder is missing — should fail loudly可能已有人在做 @ousamabenyounes 於 55 天前認領。 未關閉needs confirmation needs maintainer action Server
難度 2/5 1-3 小時 新手友好度 68/100
modelcontextprotocol/php-sdk#398 · 1 個 reaction ·
維護者通常 1 天內回覆
-
enhancement
難度 2/5 1-3 小時 新手友好度 68/100
modelcontextprotocol/php-sdk#370 ·
維護者通常 1 天內回覆
-
enhancement
難度 4/5 3-5 天 新手友好度 52/100
modelcontextprotocol/php-sdk#587 ·
維護者通常 1 天內回覆
-
bug
難度 3/5 半天 新手友好度 60/100
modelcontextprotocol/php-sdk#586 · 2 則留言 ·
維護者通常 1 天內回覆
查看 modelcontextprotocol/php-sdk 的全部 Issue
相似的 Issue
-
needs-triage Platform(Default)
難度 2/5 1-3 小時 新手友好度 76/100
維護者通常 1 天內回覆
-
Feature-Request
難度 2/5 1-3 小時 新手友好度 65/100
RSS-Bridge/rss-bridge#5123 ·
維護者通常 1 天內回覆
-
[Store] RstLoader detects headings inside indented code blocks可能已有人在做 @lazerg 今天認領。 未關閉Bug Status: Needs Review Store
難度 2/5 1-3 小時 新手友好度 82/100
維護者通常 1 天內回覆
-
bug repo:ckeditor
難度 2/5 1-3 小時 新手友好度 78/100
-
No Code Attached Yet
難度 2/5 1-3 小時 新手友好度 72/100
joomla/joomla-cms#48611 ·
維護者通常 1 天內回覆