| 283 | #[async_trait] |
| 284 | impl McpTransport for StreamableHttpTransport { |
| 285 | async fn request(&self, request: JsonRpcRequest) -> Result<JsonRpcResponse> { |
| 286 | if !self.connected.load(Ordering::SeqCst) { |
| 287 | return Err(anyhow!("Transport not connected")); |
| 288 | } |
| 289 | |
| 290 | let extra_headers = self.request_headers().await; |
| 291 | let body = serde_json::to_string(&request)?; |
| 292 | |
| 293 | let response = self |
| 294 | .client |
| 295 | .post(&self.url) |
| 296 | .headers(extra_headers) |
| 297 | .body(body) |
| 298 | .send() |
| 299 | .await |
| 300 | .with_context(|| format!("Streamable HTTP POST to {} failed", self.url))?; |
| 301 | |
| 302 | if !response.status().is_success() { |
| 303 | let status = response.status(); |
| 304 | let body = response.text().await.unwrap_or_default(); |
| 305 | return Err(anyhow!("MCP server returned HTTP {}: {}", status, body)); |
| 306 | } |
| 307 | |
| 308 | self.capture_session_id(response.headers()).await; |
| 309 | Self::parse_response(response).await |
| 310 | } |
| 311 | |
| 312 | async fn notify(&self, notification: JsonRpcNotification) -> Result<()> { |
| 313 | if !self.connected.load(Ordering::SeqCst) { |