Add around_request hook for request instrumentation#309
Open
koic wants to merge 1 commit intomodelcontextprotocol:mainfrom
Open
Add around_request hook for request instrumentation#309koic wants to merge 1 commit intomodelcontextprotocol:mainfrom
around_request hook for request instrumentation#309koic wants to merge 1 commit intomodelcontextprotocol:mainfrom
Conversation
## Motivation and Context
`instrumentation_callback` fires after request execution, making it impossible to wrap
requests with Application Performance Monitoring (APM) spans (e.g. Datadog tracing).
This adds `around_request`, which wraps request handling and allows executing code
before and after each request.
For example, to wrap requests with a Datadog trace:
```ruby
MCP.configure do |config|
config.around_request = ->(data, &request_handler) {
Datadog::Tracing.trace("mcp.#{data[:method]}") do
request_handler.call
end
}
end
```
`instrumentation_callback` is soft-deprecated in favor of `around_request`.
## How Has This Been Tested?
Added tests for `around_request` in configuration, instrumentation,
and server test files. All existing tests continue to pass.
## Breaking Changes
`around_request` itself is optional and defaults to a pass-through,
and `instrumentation_callback` continues to work as before.
However, `instrument_call` now accepts a `server_context:` keyword and its block is invoked
without arguments (previously `yield block` passed the block Proc as an argument).
This affects anyone mixing `MCP::Instrumentation` into their own classes,
though the public `Server` API is unchanged.
Resolves modelcontextprotocol#302.
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.
Motivation and Context
instrumentation_callbackfires after request execution, making it impossible to wrap requests with Application Performance Monitoring (APM) spans (e.g. Datadog tracing). This addsaround_request, which wraps request handling and allows executing code before and after each request.For example, to wrap requests with a Datadog trace:
instrumentation_callbackis soft-deprecated in favor ofaround_request.How Has This Been Tested?
Added tests for
around_requestin configuration, instrumentation, and server test files. All existing tests continue to pass.Breaking Changes
around_requestitself is optional and defaults to a pass-through, andinstrumentation_callbackcontinues to work as before.However,
instrument_callnow accepts aserver_context:keyword and its block is invoked without arguments (previouslyyield blockpassed the block Proc as an argument). This affects anyone mixingMCP::Instrumentationinto their own classes, though the publicServerAPI is unchanged.Resolves #302.
Types of changes
Checklist