Skip to content

[Server] Allow context propagation into handler fibers - #670

Merged
chr-hertel merged 3 commits into
modelcontextprotocol:mainfrom
bbdu3:codex/fiber-context-factory
Nov 8, 2026
Merged

chr-hertel merged 3 commits into
modelcontextprotocol:mainfrom
bbdu3:codex/fiber-context-factory

Conversation

@bbdu3

@bbdu3 bbdu3 commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #583 - adds Builder::setFiberContext() and FiberContextInterface, so an integration can carry execution context, e.g. the active OpenTelemetry context, into handler fibers in both protocol eras.

  • wrap() runs in the context of the caller, the returned closure runs in the fiber - the SDK keeps creating the fiber itself
  • No OpenTelemetry dependency, and nothing changes without a fiber context
  • Under an event loop the application needs to activate a context in the request fiber first, and fibers a handler starts itself don't get it - both documented
  • Also frees a discarded handler fiber right away - since [Server] Let an event loop drive the fibers request handlers run in #616 its finally only ran on the next GC run

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are recorded for the optional handler-fiber context-propagation hook.

No open issues 🎉

What changed in this PR

Adds an optional context-propagation hook for SDK-managed handler fibers, addressing #583 without introducing an OpenTelemetry dependency or changing fiber scheduling.

Changes:

  • Introduces FiberFactoryInterface and Builder::setFiberFactory() for both protocol eras, including buildStateless().
  • Documents explicit OpenTelemetry context propagation while preserving default behavior.
  • Adds regression coverage for context isolation, suspension/resumption, errors, and cleanup.
File Description
tests/​Unit/​Server/​FiberFactoryTest.php Tests context propagation and cleanup across HTTP and stdio.
tests/​Unit/​Fixtures/​ContextFiberFactory.php Provides a context-tracking factory for tests.
src/​Server/​Stateless/​StatelessProtocol.php Uses the optional factory for modern-era handler fibers.
src/​Server/​Protocol.php Uses the optional factory for handshake-era handler fibers.
src/​Server/​FiberFactoryInterface.php Defines the factory contract and callback-preservation requirements.
src/​Server/​Builder.php Adds factory configuration and passes it to both protocols.
docs/​run/​server-builder.md Explains factory usage with an OpenTelemetry example.
CHANGELOG.md Records the new server integration hook.

Review effort: Balanced

@chr-hertel chr-hertel added the Server Issues & PRs related to the Server component label Nov 8, 2026
@chr-hertel
chr-hertel force-pushed the codex/fiber-context-factory branch from 536b2b1 to 7d9bc33 Compare November 8, 2026 13:12
@chr-hertel

Copy link
Copy Markdown
Member

Thanks @bbdu3 - approach works nicely, tested it with real OTel & Revolt 👍 I took over to land it on top of #616: rebased your commit and narrowed the factory to a wrap() of the handler closure, so the SDK keeps creating the fiber. Hope that is fine for you!

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues block approval of the optional fiber context-propagation change.

No open issues 🎉

Review effort: Balanced

@chr-hertel chr-hertel left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @bbdu3!

@chr-hertel
chr-hertel merged commit 0c184ad into modelcontextprotocol:main Nov 8, 2026
31 checks passed
@bbdu3
bbdu3 deleted the codex/fiber-context-factory branch November 9, 2026 01:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Server Issues & PRs related to the Server component

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

3 participants