diff --git a/crates/rmcp/tests/test_with_js.rs b/crates/rmcp/tests/test_with_js.rs index 77e5a996..fdc54d98 100644 --- a/crates/rmcp/tests/test_with_js.rs +++ b/crates/rmcp/tests/test_with_js.rs @@ -1,4 +1,7 @@ -use rmcp::transport::sse_server::SseServer; +use rmcp::{ + ServiceExt, + transport::{SseServer, TokioChildProcess}, +}; use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; mod common; use common::calculator::Calculator; @@ -34,3 +37,32 @@ async fn test_with_js_client() -> anyhow::Result<()> { ct.cancel(); Ok(()) } + +#[tokio::test] +async fn test_with_js_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 = TokioChildProcess::new( + tokio::process::Command::new("node").arg("tests/test_with_js/server.js"), + )?; + + 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); + + client.cancel().await?; + Ok(()) +} diff --git a/crates/rmcp/tests/test_with_js/package.json b/crates/rmcp/tests/test_with_js/package.json index 19ee4749..6dee815c 100644 --- a/crates/rmcp/tests/test_with_js/package.json +++ b/crates/rmcp/tests/test_with_js/package.json @@ -6,7 +6,6 @@ "name": "test_with_ts", "version": "1.0.0", "main": "index.js", - "devDependencies": {}, "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, diff --git a/crates/rmcp/tests/test_with_js/server.js b/crates/rmcp/tests/test_with_js/server.js new file mode 100644 index 00000000..c128340f --- /dev/null +++ b/crates/rmcp/tests/test_with_js/server.js @@ -0,0 +1,35 @@ +import { McpServer, ResourceTemplate } from "@modelcontextprotocol/sdk/server/mcp.js"; +import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"; +import { z } from "zod"; + +const server = new McpServer({ + name: "Demo", + version: "1.0.0" +}); + +server.resource( + "greeting", + new ResourceTemplate("greeting://{name}", { list: undefined }), + async (uri, { name }) => ({ + contents: [{ + uri: uri.href, + text: `Hello, ${name}` + }] + }) +); + +server.tool( + "add", + { a: z.number(), b: z.number() }, + async ({ a, b }) => ({ + "content": [ + { + "type": "text", + "text": `${a + b}` + } + ] + }) +); + +const transport = new StdioServerTransport(); +await server.connect(transport);