sqlserver_mcp_catalog/tools/
call_tool.rs1use crate::auth::auth_manager::AuthManager;
4use crate::core::config_schema::Config;
5use crate::data::store::EndpointRecord;
6use crate::services::api_client::ApiClient;
7use crate::validation::validator::{validate_input, validate_output};
8
9pub async fn call_operation(
30 endpoint: &EndpointRecord,
31 config: &Config,
32 auth_manager: &mut AuthManager,
33 operation_id: &str,
34 args: serde_json::Value,
35) -> anyhow::Result<serde_json::Value> {
36 validate_input(&config.api_version, operation_id, &args)?;
37
38 let client = ApiClient::new(config.clone());
39 let response = client.execute(endpoint, &args, auth_manager).await?;
40
41 if let Err(err) = validate_output(&config.api_version, operation_id, &response) {
42 tracing::warn!(
43 operation_id,
44 error = %err,
45 "response did not match the documented schema; returning it as-is"
46 );
47 }
48 Ok(response)
49}
50
51#[cfg(test)]
52mod tests {
53 use super::*;
54
55 fn test_config() -> Config {
56 Config {
57 url: "localhost".to_string(),
58 auth_method: crate::core::config_schema::AuthMethod::SqlServer,
59 sql_port: 1433,
60 pool_max_size: 10,
61 trust_server_cert: true,
62 api_version: "2025".to_string(),
63 log_level: "info".to_string(),
64 rate_limit: 100,
65 timeout_ms: 30_000,
66 cache_size: 500,
67 retry_attempts: 3,
68 transport: crate::core::config_schema::Transport::Stdio,
69 host: "127.0.0.1".to_string(),
70 port: 3000,
71 cors_allow: None,
72 default_database: None,
73 read_only: true,
74 }
75 }
76
77 fn dummy_endpoint(operation_id: &str) -> EndpointRecord {
78 EndpointRecord {
81 operation_id: operation_id.to_string(),
82 path: "/dbo/sp_MailItemResultSets".to_string(),
83 method: "POST".to_string(),
84 summary: None,
85 description: None,
86 input_schema: serde_json::Value::Null,
87 output_schema: serde_json::Value::Null,
88 auth_scheme_ref: None,
89 }
90 }
91
92 #[tokio::test]
99 async fn call_operation_rejects_invalid_input_before_touching_the_network() {
100 let operation_id = "dbo_sp_MailItemResultSets";
101 let endpoint = dummy_endpoint(operation_id);
102 let config = test_config();
103 let mut auth_manager = AuthManager::new(config.auth_method);
104
105 let result = call_operation(
106 &endpoint,
107 &config,
108 &mut auth_manager,
109 operation_id,
110 serde_json::json!({"body": {}}),
111 )
112 .await;
113
114 assert!(result.is_err());
115 assert!(result.unwrap_err().to_string().contains("invalid input"));
116 }
117}