Refactor to Pure Native JSON-RPC (Ditch SDKs)
This commit is contained in:
1 parent
5f0eba8a80
commit
f9dee11f32
9 files changed
+575
-1169
No files matched your search
+20
-6
@@ -1,22 +1,32 @@
|
||||
use rust_mcp_sdk::error::SdkResult;
|
||||
use tokio_util::io::StreamReader;
|
||||
use tokio::io::AsyncBufReadExt;
|
||||
use futures_util::StreamExt;
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::RwLock;
|
||||
|
||||
pub fn run_proxy(target_url: &str) -> SdkResult<bool> {
|
||||
pub fn run_proxy(target_url: &str) -> Result<bool, Box<dyn std::error::Error + Send + Sync>> {
|
||||
let rt = tokio::runtime::Runtime::new().unwrap();
|
||||
rt.block_on(async {
|
||||
let client = reqwest::Client::builder().build().unwrap();
|
||||
let sse_url = format!("{}/sse", target_url);
|
||||
|
||||
let resp = match client.get(&sse_url).send().await {
|
||||
eprintln!("[PROXY] Connecting to Streamable HTTP: {}", sse_url);
|
||||
let resp = match client.get(&sse_url)
|
||||
.header("Accept", "text/event-stream")
|
||||
.send().await {
|
||||
Ok(r) => r,
|
||||
Err(_) => return Ok(true), // Connection failed (Leader is dead)
|
||||
Err(e) => {
|
||||
eprintln!("[PROXY] Connection failed: {}", e);
|
||||
return Ok(true);
|
||||
}
|
||||
};
|
||||
|
||||
let post_url = Arc::new(RwLock::new(format!("{}/messages", target_url)));
|
||||
if resp.status() == reqwest::StatusCode::GONE {
|
||||
eprintln!("[PROXY] Streamable HTTP endpoint is gone.");
|
||||
return Ok(true);
|
||||
}
|
||||
|
||||
let post_url = Arc::new(RwLock::new(format!("{}/messages", target_url))); // Will be updated by endpoint event
|
||||
let post_url_clone = Arc::clone(&post_url);
|
||||
|
||||
let (tx, mut rx) = tokio::sync::mpsc::channel(1);
|
||||
@@ -47,7 +57,11 @@ pub fn run_proxy(target_url: &str) -> SdkResult<bool> {
|
||||
if url.is_empty() {
|
||||
url = url_arc.read().await.clone();
|
||||
}
|
||||
let _ = client.post(&url).header("Content-Type", "application/json").body(body).send().await;
|
||||
let _ = client.post(&url)
|
||||
.header("Accept", "application/json, text/event-stream")
|
||||
.header("Content-Type", "application/json")
|
||||
.body(body)
|
||||
.send().await;
|
||||
});
|
||||
}
|
||||
let _ = tx_clone.blocking_send(false); // Stdin EOF
|
||||
|
||||
Reference in new issue
Block a user