Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

test(mcp): cover retry after a lost callback reply

Steven Enamakel0c82a3c27484
diff --git a/crates/openhuman-core/src/mcp/server/http.rs b/crates/openhuman-core/src/mcp/server/http.rs
index 02970b5000..b96eaa8e5c 100644
--- a/crates/openhuman-core/src/mcp/server/http.rs
+++ b/crates/openhuman-core/src/mcp/server/http.rs
@@ -289,3 +289,7 @@ fn text_error(status: StatusCode, body: &'static str) -> Response {
fn header_value<'a>(headers: &'a HeaderMap, name: &str) -> Option<&'a str> {
headers.get(name).and_then(|value| value.to_str().ok())
}
+
+#[cfg(test)]
+#[path = "http_tests.rs"]
+mod tests;
diff --git a/crates/openhuman-core/src/mcp/server/http_tests.rs b/crates/openhuman-core/src/mcp/server/http_tests.rs
new file mode 100644
index 0000000000..78ca8b75a8
--- /dev/null
+++ b/crates/openhuman-core/src/mcp/server/http_tests.rs
@@ -0,0 +1,70 @@
+use super::*;
+use axum::body::to_bytes;
+use std::collections::BTreeMap;
+
+#[tokio::test]
+async fn module_http_metadata_controls_status_headers_and_response_body() {
+ let response = response_from_metadata(
+ tinymcp_bus::ServerHttpResponseMetadata {
+ status_code: 202,
+ headers: BTreeMap::from([
+ ("mcp-session-id".to_owned(), "session-7".to_owned()),
+ ("mcp-protocol-version".to_owned(), "2025-11-25".to_owned()),
+ ]),
+ content_type: Some("application/json".to_owned()),
+ error_body: None,
+ },
+ r#"{"jsonrpc":"2.0","id":4,"result":{}}"#.to_owned(),
+ );
+
+ assert_eq!(response.status(), StatusCode::ACCEPTED);
+ assert_eq!(response.headers()[HEADER_SESSION_ID], "session-7");
+ assert_eq!(response.headers()["mcp-protocol-version"], "2025-11-25");
+ assert_eq!(response.headers()["content-type"], "application/json");
+ assert_eq!(
+ to_bytes(response.into_body(), usize::MAX).await.unwrap(),
+ r#"{"jsonrpc":"2.0","id":4,"result":{}}"#
+ );
+}
+
+#[tokio::test]
+async fn module_http_error_metadata_preserves_plain_text_status_and_body() {
+ let response = response_from_metadata(
+ tinymcp_bus::ServerHttpResponseMetadata {
+ status_code: 400,
+ headers: BTreeMap::new(),
+ content_type: Some("text/plain; charset=utf-8".to_owned()),
+ error_body: Some("missing or invalid Mcp-Session-Id header".to_owned()),
+ },
+ String::new(),
+ );
+
+ assert_eq!(response.status(), StatusCode::BAD_REQUEST);
+ assert_eq!(
+ response.headers()["content-type"],
+ "text/plain; charset=utf-8"
+ );
+ assert_eq!(
+ to_bytes(response.into_body(), usize::MAX).await.unwrap(),
+ "missing or invalid Mcp-Session-Id header"
+ );
+}
+
+#[tokio::test]
+async fn invalid_module_http_status_fails_closed_as_internal_server_error() {
+ let response = response_from_metadata(
+ tinymcp_bus::ServerHttpResponseMetadata {
+ status_code: 99,
+ headers: BTreeMap::new(),
+ content_type: None,
+ error_body: Some("bad status".to_owned()),
+ },
+ String::new(),
+ );
+
+ assert_eq!(response.status(), StatusCode::INTERNAL_SERVER_ERROR);
+ assert_eq!(
+ to_bytes(response.into_body(), usize::MAX).await.unwrap(),
+ "bad status"
+ );
+}
diff --git a/crates/openhuman-core/src/mcp/server/module_tests.rs b/crates/openhuman-core/src/mcp/server/module_tests.rs
index f8a393d03a..b5004e65fb 100644
--- a/crates/openhuman-core/src/mcp/server/module_tests.rs
+++ b/crates/openhuman-core/src/mcp/server/module_tests.rs
@@ -5,7 +5,10 @@ use std::sync::Mutex;
struct PortState {
opened: Option<ServerSessionConfig>,
submitted: Option<ServerInput>,
+ submit_count: usize,
completed: Option<ServerHostReply>,
+ complete_count: usize,
+ lose_first_complete_reply: bool,
cancelled: bool,
unavailable: bool,
polls: usize,
@@ -34,7 +37,16 @@ impl ServerPort for FixturePort {
_headers: Map<String, Value>,
input: ServerInput,
) -> Result<(), ModuleCallError> {
- self.0.lock().unwrap().submitted = Some(input);
+ let mut state = self.0.lock().unwrap();
+ state.submit_count += 1;
+ if let Some(previous) = &state.submitted {
+ assert_eq!(
+ previous, &input,
+ "retries must reuse the operation identity"
+ );
+ } else {
+ state.submitted = Some(input);
+ }
Ok(())
}
@@ -42,7 +54,15 @@ impl ServerPort for FixturePort {
let mut state = self.0.lock().unwrap();
state.polls += 1;
let input = state.submitted.clone().unwrap();
- if state.polls == 1 {
+ if state.completed.is_some() {
+ Ok(ServerOperationSnapshot {
+ operation_id: input.operation_id,
+ state: ServerOperationState::Complete {
+ response: Some("{\"jsonrpc\":\"2.0\",\"id\":1,\"result\":{}}".to_owned()),
+ },
+ http_response: None,
+ })
+ } else {
Ok(ServerOperationSnapshot {
operation_id: input.operation_id,
state: ServerOperationState::Callback {
@@ -57,14 +77,6 @@ impl ServerPort for FixturePort {
},
http_response: None,
})
- } else {
- Ok(ServerOperationSnapshot {
- operation_id: input.operation_id,
- state: ServerOperationState::Complete {
- response: Some("{\"jsonrpc\":\"2.0\",\"id\":1,\"result\":{}}".to_owned()),
- },
- http_response: None,
- })
}
}
@@ -74,7 +86,16 @@ impl ServerPort for FixturePort {
_callback: &str,
reply: ServerHostReply,
) -> Result<(), ModuleCallError> {
- self.0.lock().unwrap().completed = Some(reply);
+ let mut state = self.0.lock().unwrap();
+ state.complete_count += 1;
+ if state.lose_first_complete_reply && state.complete_count == 1 {
+ return Err(ModuleCallError::TransportFailed);
+ }
+ if let Some(previous) = &state.completed {
+ assert_eq!(previous, &reply, "callback replay must reuse its reply");
+ } else {
+ state.completed = Some(reply);
+ }
Ok(())
}
@@ -124,6 +145,43 @@ async fn server_session_completes_host_resource_callback() {
assert_eq!(opened.info["name"], "openhuman-core");
}
+#[tokio::test]
+async fn retry_after_lost_callback_reply_reuses_operation_and_host_reply() {
+ let port = Arc::new(FixturePort(Mutex::new(PortState {
+ lose_first_complete_reply: true,
+ ..PortState::default()
+ })));
+ let session = ServerSession::open_with_port(port.clone()).await.unwrap();
+ let line = r#"{"jsonrpc":"2.0","id":1,"method":"resources/read","params":{"uri":"openhuman://prompts/identity"}}"#;
+
+ let first = session
+ .submit(Map::new(), line.to_owned(), &CancellationToken::new())
+ .await;
+ assert!(matches!(
+ first,
+ Err(ServerError::Module(ModuleCallError::TransportFailed))
+ ));
+
+ let retried = session
+ .submit(Map::new(), line.to_owned(), &CancellationToken::new())
+ .await
+ .unwrap();
+ assert!(matches!(
+ retried.state,
+ ServerOperationState::Complete { .. }
+ ));
+
+ let state = port.0.lock().unwrap();
+ assert_eq!(state.submit_count, 2);
+ assert_eq!(state.complete_count, 2);
+ assert_eq!(state.submitted.as_ref().unwrap().line, line);
+ assert!(matches!(
+ state.completed,
+ Some(ServerHostReply::Success { ref value })
+ if value["contents"][0]["uri"] == "openhuman://prompts/identity"
+ ));
+}
+
#[tokio::test]
async fn server_session_cancellation_reaches_module_owner() {
let port = Arc::new(FixturePort(Mutex::new(PortState::default())));