MCPcopy Create free account
hub / github.com/AI45Lab/Code / connect_with_timeout

Method connect_with_timeout

core/src/mcp/transport/streamable_http.rs:55–99  ·  view source on GitHub ↗
(
        url: impl Into<String>,
        headers: HashMap<String, String>,
        timeout_secs: u64,
    )

Source from the content-addressed store, hash-verified

53 }
54
55 pub async fn connect_with_timeout(
56 url: impl Into<String>,
57 headers: HashMap<String, String>,
58 timeout_secs: u64,
59 ) -> Result<Self> {
60 let url = url.into().trim_end_matches('/').to_string();
61
62 let mut header_map = reqwest::header::HeaderMap::new();
63 header_map.insert(
64 reqwest::header::CONTENT_TYPE,
65 "application/json".parse().unwrap(),
66 );
67 for (k, v) in &headers {
68 if let (Ok(name), Ok(val)) = (
69 reqwest::header::HeaderName::from_bytes(k.as_bytes()),
70 reqwest::header::HeaderValue::from_str(v),
71 ) {
72 header_map.insert(name, val);
73 }
74 }
75
76 let client =
77 build_reqwest_client(Some(Duration::from_secs(timeout_secs)), Some(header_map))
78 .context("Failed to build HTTP client")?;
79
80 let (notification_tx, notification_rx) = mpsc::channel::<McpNotification>(256);
81 let connected = Arc::new(AtomicBool::new(true));
82
83 // Spawn SSE listener for server-initiated notifications (GET endpoint)
84 let sse_client = client.clone();
85 let sse_url = url.clone();
86 let sse_connected = Arc::clone(&connected);
87 let sse_handle = tokio::spawn(async move {
88 Self::sse_listener(sse_client, sse_url, notification_tx, sse_connected).await;
89 });
90
91 Ok(Self {
92 url,
93 client,
94 session_id: RwLock::new(None),
95 connected,
96 notification_rx: RwLock::new(Some(notification_rx)),
97 sse_abort: RwLock::new(Some(sse_handle.abort_handle())),
98 })
99 }
100
101 /// Build request headers including session ID if present.
102 async fn request_headers(&self) -> reqwest::header::HeaderMap {

Callers

nothing calls this directly

Calls 5

build_reqwest_clientFunction · 0.85
contextMethod · 0.80
insertMethod · 0.45
parseMethod · 0.45
cloneMethod · 0.45

Tested by

no test coverage detected