Skip to content

Commit 20a564e

Browse files
committed
DirectDispatcher: resync coverage tracer after each request
A handler-side exception (even one caught and converted to a result, as MCPServer's tool wrapper does) desyncs CPython 3.11's CTracer when the DirectDispatcher request path resumes the awaiting coroutine chain via throw — the same python/cpython#106749 the other transport seams already work around. The resync_tracer() checkpoint at the end of _dispatch_request restores tracing for the caller's subsequent lines.
1 parent 2d12b96 commit 20a564e

1 file changed

Lines changed: 3 additions & 0 deletions

File tree

src/mcp/shared/direct_dispatcher.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import anyio.abc
2727
from pydantic import ValidationError
2828

29+
from mcp.shared._compat import resync_tracer
2930
from mcp.shared.dispatcher import CallOptions, OnNotify, OnRequest, ProgressFnT
3031
from mcp.shared.exceptions import MCPError, NoBackChannelError
3132
from mcp.shared.message import MessageMetadata
@@ -251,6 +252,8 @@ async def _dispatch_request(
251252
code=REQUEST_TIMEOUT,
252253
message=f"Timed out after {opts.get('timeout')}s waiting for {method!r}",
253254
) from None
255+
finally:
256+
await resync_tracer()
254257

255258
async def _dispatch_notify(self, method: str, params: Mapping[str, Any] | None) -> None:
256259
try:

0 commit comments

Comments
 (0)