Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion crates/contextforge-gateway-rs-lib/src/gateway/backend_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use super::mcp_gateway::prefixed_name;
#[derive(Clone)]
pub(crate) struct GatewayBackendClient {
backend_name: String,
namespace_identifiers: bool,
initialize_request: InitializeRequestParams,
plugin_runtime: Option<GatewayPluginRuntimeHandle>,
in_flight_calls: Arc<Mutex<HashMap<ProgressToken, Arc<InFlightToolCall>>>>,
Expand All @@ -36,11 +37,13 @@ struct InFlightToolCall {
impl GatewayBackendClient {
pub(crate) fn new(
backend_name: String,
namespace_identifiers: bool,
initialize_request: InitializeRequestParams,
plugin_runtime: Option<GatewayPluginRuntimeHandle>,
) -> Self {
Self {
backend_name,
namespace_identifiers,
initialize_request,
plugin_runtime,
in_flight_calls: Arc::default(),
Expand Down Expand Up @@ -155,13 +158,17 @@ impl ClientHandler for GatewayBackendClient {
return;
};

params.uri = prefixed_name(&self.backend_name, &params.uri);
params.uri = resource_uri_for_downstream(&self.backend_name, params.uri, self.namespace_identifiers);
if let Err(error) = downstream.notify_resource_updated(params).await {
warn!("resource_updated: unable to forward backend notification downstream: {error:?}");
}
}
}

fn resource_uri_for_downstream(backend_name: &str, uri: String, namespace_identifiers: bool) -> String {
if namespace_identifiers { prefixed_name(backend_name, &uri) } else { uri }
}

/// Calls the tool on the backend, keeping the downstream progress token on
/// the request (`Peer::call_tool` would stamp an auto-generated token over it
/// at serialization) and relaying a downstream cancellation to the backend.
Expand Down Expand Up @@ -196,3 +203,21 @@ pub(crate) async fn call_backend_tool(
_ => Err(ServiceError::UnexpectedResponse),
}
}

#[cfg(test)]
mod tests {
use super::resource_uri_for_downstream;

#[test]
fn single_backend_resource_update_preserves_uri() {
assert_eq!("test://resource", resource_uri_for_downstream("backend-id", "test://resource".to_owned(), false));
}

#[test]
fn multi_backend_resource_update_prefixes_uri() {
assert_eq!(
"backend-id-test://resource",
resource_uri_for_downstream("backend-id", "test://resource".to_owned(), true)
);
}
}
Loading
Loading