You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add around_request hook for request instrumentation
## 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.
Resolvesmodelcontextprotocol#302.
The `server_context` is a user-defined hash that is passed into the server instance and made available to tools, prompts, and exception/instrumentation callbacks. It can be used to provide contextual information such as authentication state, user IDs, or request-specific data.
200
+
The `server_context` is a user-defined hash that is passed into the server instance and made available to tool and prompt calls.
201
+
It can be used to provide contextual information such as authentication state, user IDs, or request-specific data.
197
202
198
203
**Type:**
199
204
@@ -210,7 +215,9 @@ server = MCP::Server.new(
210
215
)
211
216
```
212
217
213
-
This hash is then passed as the `server_context` argument to tool and prompt calls, and is included in exception and instrumentation callbacks.
218
+
This hash is then passed as the `server_context` keyword argument to tool and prompt calls.
219
+
Note that exception and instrumentation callbacks do not receive this user-defined hash.
220
+
See the relevant sections below for the arguments they receive.
214
221
215
222
#### Request-specific `_meta` Parameter
216
223
@@ -263,17 +270,77 @@ end
263
270
The exception reporter receives:
264
271
265
272
-`exception`: The Ruby exception object that was raised
266
-
-`server_context`: The context hash provided to the server
273
+
-`server_context`: A hash describing where the failure occurred (e.g., `{ request: <raw JSON-RPC request> }`
274
+
for request handling, `{ notification: "tools_list_changed" }` for notification delivery).
275
+
This is not the user-defined `server_context` passed to `Server.new`.
0 commit comments