feat(core): Associate resource/tool/prompt invocations with request span instead of response span #16126
+416
−47
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.
This PR adds a best effor mechanism to associate handler spans for
resource
,tool
andprompt
with the incoming message requests instead of the outgoing SSE response.The work is based on the upstream PR which exposes the request id on the handler metadata: modelcontextprotocol/typescript-sdk#358
This PR actually also fixes a bug where we were instrumenting the registering of the handler instead of the actual handler 🤦♂️.
One thing to note in this PR is that it has a risk of being slightly memory leaky. We are storing away spans by session and request id and cleaning them up when they are used. In the case of long lived sessions the span will not be garbage collected for a long time. There is unfortunately no way of doing a weak reference because session id and request ids are primitives and the framework doesn't give us any identity objects to cross-reference stuff with.