refactor: Transport trait and worker transport, and streamable http client with those new features. (#167)
* refactor: a transport trait and streamable http client * test: test with js streamable http server * refactor: separate sse stream connection * fix(test): streamable http client test with js * fix(test): wait longer time for js server startup
This commit is contained in:
parent
01eedb7770
commit
9a771fb157
1 changed files with 2761 additions and 1315 deletions
|
|
@ -1,6 +1,10 @@
|
|||
use rmcp::{
|
||||
ServiceExt,
|
||||
transport::{SseServer, TokioChildProcess, streamable_http_server::axum::StreamableHttpServer},
|
||||
service::QuitReason,
|
||||
transport::{
|
||||
SseServer, StreamableHttpClientTransport, TokioChildProcess,
|
||||
streamable_http_server::axum::StreamableHttpServer,
|
||||
},
|
||||
};
|
||||
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
|
||||
mod common;
|
||||
|
|
@ -8,6 +12,7 @@ use common::calculator::Calculator;
|
|||
|
||||
const SSE_BIND_ADDRESS: &str = "127.0.0.1:8000";
|
||||
const STREAMABLE_HTTP_BIND_ADDRESS: &str = "127.0.0.1:8001";
|
||||
const STREAMABLE_HTTP_JS_BIND_ADDRESS: &str = "127.0.0.1:8002";
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_with_js_client() -> anyhow::Result<()> {
|
||||
|
|
@ -97,3 +102,41 @@ async fn test_with_js_streamable_http_client() -> anyhow::Result<()> {
|
|||
ct.cancel();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_with_js_streamable_http_server() -> anyhow::Result<()> {
|
||||
let _ = tracing_subscriber::registry()
|
||||
.with(
|
||||
tracing_subscriber::EnvFilter::try_from_default_env()
|
||||
.unwrap_or_else(|_| "debug".to_string().into()),
|
||||
)
|
||||
.with(tracing_subscriber::fmt::layer())
|
||||
.try_init();
|
||||
tokio::process::Command::new("npm")
|
||||
.arg("install")
|
||||
.current_dir("tests/test_with_js")
|
||||
.spawn()?
|
||||
.wait()
|
||||
.await?;
|
||||
|
||||
let transport = StreamableHttpClientTransport::from_uri(format!(
|
||||
"http://{STREAMABLE_HTTP_JS_BIND_ADDRESS}/mcp"
|
||||
));
|
||||
|
||||
let mut server = tokio::process::Command::new("node")
|
||||
.arg("tests/test_with_js/streamable_server.js")
|
||||
.spawn()?;
|
||||
|
||||
// waiting for server up
|
||||
tokio::time::sleep(tokio::time::Duration::from_millis(1000)).await;
|
||||
|
||||
let client = ().serve(transport).await?;
|
||||
let resources = client.list_all_resources().await?;
|
||||
tracing::info!("{:#?}", resources);
|
||||
let tools = client.list_all_tools().await?;
|
||||
tracing::info!("{:#?}", tools);
|
||||
let quit_reason = client.cancel().await?;
|
||||
server.kill().await?;
|
||||
assert!(matches!(quit_reason, QuitReason::Cancelled));
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue