-
Notifications
You must be signed in to change notification settings - Fork 115
fix: propagate context to MCP server clean #1677
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
fix: propagate context to MCP server clean #1677
Conversation
Hi @mikeldking heres the new MCP propagation PR in case i lost you in my mess |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems to be failing tests. Can you check the failures?
@@ -1 +1 @@ | |||
__version__ = "1.3.0" | |||
__version__ = "1.3.1" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
__version__ = "1.3.1" | |
__version__ = "1.3.0" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does CD handle it?
@@ -79,6 +87,7 @@ def _instrument(self, **kwargs: Any) -> None: | |||
def _uninstrument(self, **kwargs: Any) -> None: | |||
unwrap("mcp.client.stdio", "stdio_client") | |||
unwrap("mcp.server.stdio", "stdio_server") | |||
unwrap("mcp.client.session", "ClientSession.call_tool") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't match the method instrumented above?
85b33d4
to
cd5cf15
Compare
cd5cf15
to
d1d3c40
Compare
@mikeldking
|
token = None | ||
try: | ||
# Message has been deserialized, we need to extract the traceparent | ||
_meta = {"traceparent": args[1].params.meta.traceparent} | ||
ctx = propagate.extract(_meta) | ||
token = context.attach(ctx) | ||
finally: | ||
res = await wrapped(*args, **kwargs) | ||
if token: | ||
context.detach(token) | ||
return res |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current try/finally structure could lead to unexpected behavior. If an exception occurs during context extraction, the wrapped function would still execute in the finally block, which is likely not the intended behavior.
Consider restructuring like this:
token = None
try:
# Message has been deserialized, we need to extract the traceparent
_meta = {"traceparent": args[1].params.meta.traceparent}
ctx = propagate.extract(_meta)
token = context.attach(ctx)
# Execute the wrapped function inside the try block
res = await wrapped(*args, **kwargs)
return res
finally:
# Only detach if we successfully attached
if token:
context.detach(token)
This ensures the wrapped function only executes if context extraction succeeds, while still guaranteeing context detachment in all cases where it was attached.
token = None | |
try: | |
# Message has been deserialized, we need to extract the traceparent | |
_meta = {"traceparent": args[1].params.meta.traceparent} | |
ctx = propagate.extract(_meta) | |
token = context.attach(ctx) | |
finally: | |
res = await wrapped(*args, **kwargs) | |
if token: | |
context.detach(token) | |
return res | |
token = None | |
try: | |
# Message has been deserialized, we need to extract the traceparent | |
_meta = {"traceparent": args[1].params.meta.traceparent} | |
ctx = propagate.extract(_meta) | |
token = context.attach(ctx) | |
# Execute the wrapped function inside the try block | |
res = await wrapped(*args, **kwargs) | |
return res | |
finally: | |
# Only detach if we successfully attached | |
if token: | |
context.detach(token) | |
Spotted by Diamond
Is this helpful? React 👍 or 👎 to let us know.
TLDR:


The context passed by the
InstrumentedStreamWriter.send
was being mutated by processes preceedingInstrumentedStreamReader
resulting in disjointed MCP traces.By adding instrumentation at
_handle_request
(an earlier state in the program flow) the original message composed byInstrumentedStreamWriter.send
is preserved. This context is then propagated at the server level.the context is detached by the same method it was being detached before, through
InstrumentedStreamReader
.