use mcp_protocol_sdk::{
protocol::{
messages::InitializeParams,
methods,
types::{ClientCapabilities, Implementation, JsonRpcRequest, LATEST_PROTOCOL_VERSION},
},
server::McpServer,
transport::stdio::StdioServerTransport,
};
use serde_json::json;
use std::sync::Arc;
use tokio::sync::Mutex;
#[tokio::test]
async fn test_initialization_fix() {
let server = McpServer::new("test-server".to_string(), "1.0.0".to_string());
let init_params = InitializeParams {
protocol_version: LATEST_PROTOCOL_VERSION.to_string(),
capabilities: ClientCapabilities::default(),
client_info: Implementation {
name: "test-client".to_string(),
version: "1.0.0".to_string(),
title: Some("Test Client".to_string()),
},
meta: None,
};
let init_request = JsonRpcRequest {
jsonrpc: "2.0".to_string(),
id: json!(1),
method: methods::INITIALIZE.to_string(),
params: Some(serde_json::to_value(init_params).unwrap()),
};
let response = server.handle_request(init_request).await;
assert!(
response.is_ok(),
"Initialize request should succeed: {:?}",
response.err()
);
let response = response.unwrap();
assert_eq!(response.jsonrpc, "2.0");
assert_eq!(response.id, json!(1));
assert!(response.result.is_some(), "Response should have a result");
if let Some(result) = response.result {
assert!(
result.get("protocolVersion").is_some(),
"Response should contain protocolVersion"
);
assert!(
result.get("capabilities").is_some(),
"Response should contain capabilities"
);
assert!(
result.get("serverInfo").is_some(),
"Response should contain serverInfo"
);
assert_eq!(
result["protocolVersion"].as_str().unwrap(),
LATEST_PROTOCOL_VERSION,
"Protocol version should match"
);
let server_info = &result["serverInfo"];
assert_eq!(server_info["name"].as_str().unwrap(), "test-server");
assert_eq!(server_info["version"].as_str().unwrap(), "1.0.0");
}
println!("✅ Initialization test passed! Server correctly handles initialize method.");
}
#[tokio::test]
async fn test_method_not_found_for_unknown_method() {
let server = McpServer::new("test-server".to_string(), "1.0.0".to_string());
let unknown_request = JsonRpcRequest {
jsonrpc: "2.0".to_string(),
id: json!(2),
method: "unknown_method".to_string(),
params: None,
};
let response = server.handle_request(unknown_request).await;
match response {
Ok(response) => {
assert_eq!(response.jsonrpc, "2.0");
assert_eq!(response.id, json!(2));
println!(
"Unknown method handled correctly with response: {:?}",
response
);
}
Err(error) => {
println!("Unknown method correctly returned error: {:?}", error);
assert!(
error.to_string().contains("unknown_method"),
"Error should mention the unknown method"
);
}
}
println!("✅ Unknown method test passed! Server handles unknown methods appropriately.");
}
#[tokio::test]
async fn test_transport_integration() {
use mcp_protocol_sdk::transport::traits::ServerTransport;
let server = McpServer::new("integration-test".to_string(), "1.0.0".to_string());
let mut transport = StdioServerTransport::new();
let server_arc = Arc::new(Mutex::new(server));
let request_handler: mcp_protocol_sdk::transport::traits::ServerRequestHandler = {
let server_ref = server_arc.clone();
Arc::new(move |request: JsonRpcRequest| {
let server = server_ref.clone();
Box::pin(async move {
let server = server.lock().await;
server.handle_request(request).await
})
})
};
transport.set_request_handler(request_handler);
println!(
"✅ Transport integration test passed! Transport can now properly delegate requests to server."
);
}
#[tokio::test]
async fn test_complete_server_startup() {
let mut server = McpServer::new("startup-test".to_string(), "1.0.0".to_string());
use async_trait::async_trait;
use mcp_protocol_sdk::{
core::{error::McpResult, tool::ToolHandler},
protocol::types::{Content, ToolResult},
};
use std::collections::HashMap;
struct TestTool;
#[async_trait]
impl ToolHandler for TestTool {
async fn call(&self, _args: HashMap<String, serde_json::Value>) -> McpResult<ToolResult> {
Ok(ToolResult {
content: vec![Content::text("Hello from test tool!")],
is_error: None,
structured_content: None,
meta: None,
})
}
}
server
.add_tool(
"test_tool".to_string(),
Some("A test tool".to_string()),
json!({
"type": "object",
"properties": {
"message": {"type": "string"}
}
}),
TestTool,
)
.await
.unwrap();
let transport = StdioServerTransport::new();
println!("✅ Complete server startup test passed! Server is configured correctly for startup.");
}