Skip to main content

qualia_mcp/tools/
sim_space_weather.rs

1//! MCP tools for Space Weather, Heliophysical Oscillations, and Geomagnetic Coupling.
2//!
3//! Powered by `qualia-sim::space_weather`.
4//! Designed according to `AGENTS.md` §0-B (<500 lines).
5
6use crate::types::{parse_tool_args, McpSystemError};
7use qualia_sim::space_weather::{
8    evaluate_solar_cycle_phase, evaluate_space_weather_coupling, SolarWindState,
9};
10use serde_json::{json, Value};
11
12/// `sim_evaluate_space_weather` tool — models 11-year Schwabe and 22-year Hale magnetic cycles, EUV, TSI, and GCR modulation.
13pub fn evaluate_space_weather_tool(args: &[u8]) -> Result<String, McpSystemError> {
14    let v = parse_tool_args(args)?;
15    let date = v.get("decimal_year").and_then(Value::as_f64).unwrap_or(2026.5) as f32;
16
17    let (schwabe, hale) = evaluate_solar_cycle_phase(date);
18
19    let payload = json!({
20        "decimalYear": date,
21        "solarCycle": schwabe.cycle_number,
22        "phaseRad": schwabe.phase_rad,
23        "sunspotNumber": schwabe.sunspot_number,
24        "f107RadioFluxSfu": schwabe.f10_7_radio_flux_sfu,
25        "tsiAnomalyWm2": schwabe.tsi_anomaly_w_m2,
26        "thermosphereClimateIndex10e10W": schwabe.thermosphere_climate_index_10e10_w,
27        "spotlessDaysRatio": schwabe.spotless_days_ratio,
28        "isSolarNorthPositive": hale.is_solar_north_positive,
29        "cosmicRayModulationFactor": hale.cosmic_ray_modulation_factor,
30        "decadalDroughtPropensity": hale.decadal_drought_propensity,
31    });
32    serde_json::to_string(&payload).map_err(|_| McpSystemError::ParseError)
33}
34
35/// `sim_evaluate_solar_wind_coupling` tool — evaluates solar wind dynamic pressure, IMF Bz reconnection, and Kp/Dst storm indices.
36pub fn evaluate_solar_wind_coupling_tool(args: &[u8]) -> Result<String, McpSystemError> {
37    let v = parse_tool_args(args)?;
38    let date = v.get("decimal_year").and_then(Value::as_f64).unwrap_or(2026.5) as f32;
39    let speed = v.get("speed_km_s").and_then(Value::as_f64).unwrap_or(450.0) as f32;
40    let density = v.get("proton_density_cm3").and_then(Value::as_f64).unwrap_or(5.0) as f32;
41    let bz = v.get("imf_bz_nt").and_then(Value::as_f64).unwrap_or(-2.5) as f32;
42    let b_total = v.get("imf_total_nt").and_then(Value::as_f64).unwrap_or(5.0) as f32;
43
44    let (solar, hale) = evaluate_solar_cycle_phase(date);
45    let p_dyn = 1.6726e-27 * (density * 1e6) * (speed * 1e3) * (speed * 1e3) * 1e9;
46    let wind = SolarWindState {
47        velocity_km_s: speed,
48        proton_density_cm3: density,
49        imf_total_nt: b_total,
50        imf_bz_nt: bz,
51        dynamic_pressure_npa: p_dyn,
52    };
53
54    let (storm, gec) = evaluate_space_weather_coupling(&solar, &hale, &wind);
55
56    let payload = json!({
57        "solarWindVelocityKmS": wind.velocity_km_s,
58        "protonDensityCm3": wind.proton_density_cm3,
59        "imfBzNt": wind.imf_bz_nt,
60        "imfTotalNt": wind.imf_total_nt,
61        "dynamicPressureNPa": wind.dynamic_pressure_npa,
62        "isReconnectionActive": storm.is_reconnection_active,
63        "planetaryKpIndex": storm.kp_index,
64        "dstIndexNt": storm.dst_index_nt,
65        "auroralPowerGw": storm.auroral_power_gw,
66        "ionosphericPotentialKv": gec.ionospheric_potential_kv,
67        "fairWeatherFieldVm": gec.fair_weather_field_v_m,
68        "dryLightningSusceptibility": gec.dry_lightning_susceptibility,
69    });
70    serde_json::to_string(&payload).map_err(|_| McpSystemError::ParseError)
71}
72
73pub use evaluate_space_weather_tool as sim_space_weather_eval;
74pub use evaluate_solar_wind_coupling_tool as sim_solar_wind_coupling;