Skip to content

Commit 05a3976

Browse files
authored
fix(mcp): attach session manager for non-OAuth HTTP clients (nearai#793) (nearai#986)
* fix(mcp): attach session manager for non-OAuth HTTP clients (nearai#793) * style(mcp): format factory regression test (nearai#986)
1 parent d9794f4 commit 05a3976

2 files changed

Lines changed: 53 additions & 2 deletions

File tree

src/tools/mcp/client.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,12 @@ impl McpClient {
209209
}
210210
}
211211

212+
/// Attach a session manager for Streamable HTTP session tracking.
213+
pub fn with_session_manager(mut self, session_manager: Arc<McpSessionManager>) -> Self {
214+
self.session_manager = Some(session_manager);
215+
self
216+
}
217+
212218
/// Get the server name.
213219
pub fn server_name(&self) -> &str {
214220
&self.server_name
@@ -219,6 +225,11 @@ impl McpClient {
219225
&self.server_url
220226
}
221227

228+
/// Whether this client has a session manager attached.
229+
pub fn has_session_manager(&self) -> bool {
230+
self.session_manager.is_some()
231+
}
232+
222233
/// Get the next request ID.
223234
fn next_request_id(&self) -> u64 {
224235
self.next_id.fetch_add(1, Ordering::SeqCst)
@@ -716,6 +727,17 @@ mod tests {
716727
assert!(client.session_manager.is_none());
717728
}
718729

730+
#[test]
731+
fn test_with_session_manager() {
732+
let client = McpClient::new("http://localhost:8080");
733+
assert!(!client.has_session_manager());
734+
735+
let session_manager = Arc::new(McpSessionManager::new());
736+
let client = client.with_session_manager(session_manager);
737+
738+
assert!(client.has_session_manager());
739+
}
740+
719741
#[test]
720742
fn test_next_request_id_monotonically_increasing() {
721743
let client = McpClient::new("http://localhost:1234");

src/tools/mcp/factory.rs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,40 @@ pub async fn create_client_from_config(
8888
user_id,
8989
))
9090
} else {
91-
Ok(McpClient::new_with_config(server))
91+
Ok(McpClient::new_with_config(server)
92+
.with_session_manager(Arc::clone(session_manager)))
9293
}
9394
} else {
94-
Ok(McpClient::new_with_config(server))
95+
Ok(McpClient::new_with_config(server)
96+
.with_session_manager(Arc::clone(session_manager)))
9597
}
9698
}
9799
}
98100
}
101+
102+
#[cfg(test)]
103+
mod tests {
104+
use super::*;
105+
106+
#[tokio::test]
107+
async fn test_factory_non_oauth_http_has_session_manager() {
108+
let server = McpServerConfig::new("test-server", "http://localhost:9999");
109+
let session_manager = Arc::new(McpSessionManager::new());
110+
let process_manager = Arc::new(McpProcessManager::new());
111+
112+
let client = create_client_from_config(
113+
server,
114+
&session_manager,
115+
&process_manager,
116+
None,
117+
"test-user",
118+
)
119+
.await
120+
.expect("factory should succeed for HTTP config");
121+
122+
assert!(
123+
client.has_session_manager(),
124+
"non-OAuth HTTP clients must carry a session manager"
125+
);
126+
}
127+
}

0 commit comments

Comments
 (0)