fix: expose MRTR state to tool handlers (#1104)

* fix: expose MRTR state to tool handlers

* docs: document tool call context fields
This commit is contained in:
Dale Seo 2026-08-05 09:55:09 -04:00 • committed by GitHub
commit c4def55422
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -34,10 +34,18 @@ pub fn parse_json_object<T: DeserializeOwned>(input: JsonObject) -> Result<T, cr
}
#[non_exhaustive]
pub struct ToolCallContext<'s, S> {
/// The request-specific context for this tool call.
pub request_context: RequestContext<RoleServer>,
/// The server handling this tool call.
pub service: &'s S,
/// The name of the tool being called.
pub name: Cow<'static, str>,
/// The arguments supplied for the tool call.
pub arguments: Option<JsonObject>,
/// Client responses to input requests from the previous MRTR round.
pub input_responses: Option<crate::model::InputResponses>,
/// Opaque state returned by the server during the previous MRTR round.
pub request_state: Option<String>,
}
impl<'s, S> ToolCallContext<'s, S> {
@ -47,6 +55,8 @@ impl<'s, S> ToolCallContext<'s, S> {
meta: _,
name,
arguments,
input_responses,
request_state,
..
}: CallToolRequestParams,
request_context: RequestContext<RoleServer>,
@ -56,6 +66,8 @@ impl<'s, S> ToolCallContext<'s, S> {
service,
name,
arguments,
input_responses,
request_state,
}
}
pub fn name(&self) -> &str {
@ -98,6 +110,12 @@ impl IntoCallToolResult for InputRequiredResult {
}
}
impl IntoCallToolResult for CallToolResponse {
fn into_call_tool_result(self) -> Result<CallToolResponse, crate::ErrorData> {
Ok(self)
}
}
impl IntoCallToolResult for crate::ErrorData {
fn into_call_tool_result(self) -> Result<CallToolResponse, crate::ErrorData> {
Err(self)
@ -193,6 +211,26 @@ impl<S> FromContextPart<ToolCallContext<'_, S>> for ToolName {
}
}
/// Extracts the opaque state returned by the server during the previous MRTR round.
#[expect(clippy::exhaustive_structs, reason = "intentionally exhaustive")]
pub struct RequestState(pub Option<String>);
impl<S> FromContextPart<ToolCallContext<'_, S>> for RequestState {
fn from_context_part(context: &mut ToolCallContext<S>) -> Result<Self, crate::ErrorData> {
Ok(Self(context.request_state.take()))
}
}
/// Extracts client responses to input requests from the previous MRTR round.
#[expect(clippy::exhaustive_structs, reason = "intentionally exhaustive")]
pub struct InputResponses(pub Option<crate::model::InputResponses>);
impl<S> FromContextPart<ToolCallContext<'_, S>> for InputResponses {
fn from_context_part(context: &mut ToolCallContext<S>) -> Result<Self, crate::ErrorData> {
Ok(Self(context.input_responses.take()))
}
}
// Special implementation for Parameters that handles tool arguments
impl<S, P> FromContextPart<ToolCallContext<'_, S>> for Parameters<P>
where