fix(mcp): use model_dump(by_alias=True) to preserve _meta field in instrumentation#2062
Open
Zelys-DFKH wants to merge 1 commit intostrands-agents:mainfrom
Open
fix(mcp): use model_dump(by_alias=True) to preserve _meta field in instrumentation#2062Zelys-DFKH wants to merge 1 commit intostrands-agents:mainfrom
Zelys-DFKH wants to merge 1 commit intostrands-agents:mainfrom
Conversation
…e _meta field
CallToolRequestParams defines its tracing field as `meta` with alias `_meta`.
model_dump() serialises it under the Python field name `meta`, so the subsequent
setdefault("_meta", ...) always created a second, disconnected key and the
OpenTelemetry context was never forwarded to the MCP server.
Switching to model_dump(by_alias=True) ensures the key is `_meta` throughout,
matching the MCP wire format.
Adds a regression test using the real CallToolRequestParams to verify the alias
is preserved end-to-end.
Fixes strands-agents#1916
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
CallToolRequestParams defines its tracing field with the Python name `meta`
and the wire alias `_meta`. Calling model_dump() without by_alias=True
serialises the field as `meta`, so the subsequent setdefault("_meta", ...)
call always creates a second, unrelated key. The OpenTelemetry context is
injected into that orphaned key and never reaches the MCP server.
There is a second part to the bug: `_meta` is None by default, and setdefault
does not replace an existing key even when its value is None. So even after
switching to by_alias=True the inject() call would receive None as its carrier.
The fix explicitly replaces None with an empty dict before injecting.
Changes:
is `_meta` throughout, matching the MCP wire format
initialises `_meta` to `{}` when the value is absent or None
Related Issues
Fixes #1916
Documentation PR
N/A
Type of Change
Bug fix
Testing
the actual `CallToolRequestParams` type to verify `inject()` receives a dict
carrier (not None) and that `_meta` is a dict after `model_validate`
compatible with the `by_alias=True` call
Checklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.