pmcp 2.2.0

High-quality Rust SDK for Model Context Protocol (MCP) with full TypeScript SDK compatibility
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
//! EU Currency MCP Server
//!
//! This example demonstrates a comprehensive currency exchange MCP server that provides:
//! - Current exchange rates
//! - Historical rate analysis
//! - Trend detection with moving averages
//! - Currency rate predictions
//! - ASCII sparkline visualization
//!
//! Tools provided:
//! - get_rates: Get current exchange rates
//! - analyze_trend: Analyze historical trends with predictions
//! - list_currencies: List supported currencies
//! - get_historical: Get historical rates for a period
//!
//! Based on the Frankfurter API for real exchange rate data.

use async_trait::async_trait;
use pmcp::server::{Server, ToolHandler};
use pmcp::types::*;
use serde::Deserialize;
use serde_json::{json, Value};
use std::collections::HashMap;

#[derive(Debug, Clone)]
struct CurrencyServer {
    supported_currencies: Vec<String>,
    cache: HashMap<String, (Value, std::time::SystemTime)>,
}

impl CurrencyServer {
    fn new() -> Self {
        Self {
            supported_currencies: vec![
                "EUR".to_string(),
                "USD".to_string(),
                "GBP".to_string(),
                "CHF".to_string(),
                "JPY".to_string(),
                "CAD".to_string(),
                "AUD".to_string(),
                "SEK".to_string(),
                "NOK".to_string(),
                "DKK".to_string(),
                "PLN".to_string(),
                "CZK".to_string(),
                "HUF".to_string(),
                "BGN".to_string(),
                "RON".to_string(),
            ],
            cache: HashMap::new(),
        }
    }

    fn validate_currency(&self, currency: &str) -> Result<(), String> {
        if self.supported_currencies.contains(&currency.to_uppercase()) {
            Ok(())
        } else {
            Err(format!(
                "Unsupported currency: {}. Supported: {:?}",
                currency, self.supported_currencies
            ))
        }
    }

    async fn fetch_current_rates(
        &mut self,
        base: &str,
        symbols: Option<&str>,
    ) -> Result<Value, String> {
        let cache_key = format!("current_{}_{}", base, symbols.unwrap_or("all"));

        // Check cache (24-hour smart caching)
        if let Some((data, timestamp)) = self.cache.get(&cache_key) {
            if timestamp.elapsed().unwrap_or_default().as_secs() < 86400 {
                return Ok(data.clone());
            }
        }

        // Simulate API call to Frankfurter API
        // In real implementation, you would use reqwest or similar
        let rates = match base {
            "EUR" => json!({
                "USD": 1.0847,
                "GBP": 0.8312,
                "CHF": 0.9521,
                "JPY": 164.32,
                "CAD": 1.5123,
                "AUD": 1.6234,
                "SEK": 11.2345,
                "NOK": 11.8765,
                "DKK": 7.4567,
                "PLN": 4.2789,
                "CZK": 25.1234,
                "HUF": 412.34,
                "BGN": 1.9558,
                "RON": 4.9876
            }),
            "USD" => json!({
                "EUR": 0.9219,
                "GBP": 0.7662,
                "CHF": 0.8778,
                "JPY": 151.45,
                "CAD": 1.3945,
                "AUD": 1.4967,
                "SEK": 10.3567,
                "NOK": 10.9456,
                "DKK": 6.8789,
                "PLN": 3.9445,
                "CZK": 23.1678,
                "HUF": 380.12,
                "BGN": 1.8034,
                "RON": 4.5987
            }),
            _ => return Err("Base currency not supported for demo".to_string()),
        };

        let result = json!({
            "amount": 1.0,
            "base": base,
            "date": "2025-01-26",
            "rates": rates
        });

        // Cache the result
        self.cache
            .insert(cache_key, (result.clone(), std::time::SystemTime::now()));
        Ok(result)
    }

    async fn fetch_historical_rates(
        &mut self,
        base: &str,
        start_date: &str,
        end_date: &str,
        _symbols: Option<&str>,
    ) -> Result<Value, String> {
        let cache_key = format!("historical_{}_{}_{}", base, start_date, end_date);

        // Check cache
        if let Some((data, timestamp)) = self.cache.get(&cache_key) {
            if timestamp.elapsed().unwrap_or_default().as_secs() < 86400 {
                return Ok(data.clone());
            }
        }

        // Simulate historical data (in real implementation, fetch from Frankfurter API)
        let mut historical_data = HashMap::new();

        // Generate sample historical data for the last 30 days
        let base_date = chrono::NaiveDate::parse_from_str("2025-01-26", "%Y-%m-%d")
            .map_err(|e| format!("Date parsing error: {}", e))?;

        for i in 0..30 {
            let date = base_date - chrono::Duration::days(i);
            let date_str = date.format("%Y-%m-%d").to_string();

            // Generate slightly varying rates (simulate market fluctuations)
            let variation = (i as f64 * 0.001) + ((i % 3) as f64) * 0.002;
            let rates = match base {
                "EUR" => json!({
                    "USD": 1.0847 + variation,
                    "GBP": 0.8312 - variation * 0.5,
                    "CHF": 0.9521 + variation * 0.3,
                    "JPY": 164.32 + variation * 10.0
                }),
                "USD" => json!({
                    "EUR": 0.9219 - variation,
                    "GBP": 0.7662 + variation * 0.4,
                    "CHF": 0.8778 - variation * 0.2,
                    "JPY": 151.45 - variation * 8.0
                }),
                _ => return Err("Base currency not supported for demo".to_string()),
            };

            historical_data.insert(date_str, rates);
        }

        let result = json!({
            "amount": 1.0,
            "base": base,
            "start_date": start_date,
            "end_date": end_date,
            "rates": historical_data
        });

        // Cache the result
        self.cache
            .insert(cache_key, (result.clone(), std::time::SystemTime::now()));
        Ok(result)
    }

    fn calculate_moving_average(&self, rates: &[f64], window: usize) -> Vec<f64> {
        if rates.len() < window {
            return vec![];
        }

        let mut moving_averages = Vec::new();
        for i in window..=rates.len() {
            let sum: f64 = rates[i - window..i].iter().sum();
            moving_averages.push(sum / window as f64);
        }
        moving_averages
    }

    fn predict_future_rates(&self, rates: &[f64], days: usize) -> Vec<f64> {
        if rates.len() < 2 {
            return vec![];
        }

        // Simple linear regression for prediction
        let n = rates.len() as f64;
        let x_sum: f64 = (0..rates.len()).map(|i| i as f64).sum();
        let y_sum: f64 = rates.iter().sum();
        let xy_sum: f64 = rates.iter().enumerate().map(|(i, &y)| i as f64 * y).sum();
        let x2_sum: f64 = (0..rates.len()).map(|i| (i as f64).powi(2)).sum();

        let slope = (n * xy_sum - x_sum * y_sum) / (n * x2_sum - x_sum.powi(2));
        let intercept = (y_sum - slope * x_sum) / n;

        let mut predictions = Vec::new();
        for i in 0..days {
            let x = rates.len() as f64 + i as f64;
            predictions.push(slope * x + intercept);
        }
        predictions
    }

    fn generate_sparkline(&self, rates: &[f64]) -> String {
        if rates.is_empty() {
            return String::new();
        }

        let min_rate = rates.iter().fold(f64::INFINITY, |a, &b| a.min(b));
        let max_rate = rates.iter().fold(f64::NEG_INFINITY, |a, &b| a.max(b));
        let range = max_rate - min_rate;

        if range == 0.0 {
            return "".repeat(rates.len());
        }

        let chars = ['', '', '', '', '', '', '', ''];
        rates
            .iter()
            .map(|&rate| {
                let normalized = (rate - min_rate) / range;
                let index =
                    ((normalized * (chars.len() - 1) as f64).round() as usize).min(chars.len() - 1);
                chars[index]
            })
            .collect()
    }
}

// Tool handler implementations

#[derive(Debug, Deserialize)]
struct GetRatesArgs {
    #[serde(default = "default_base")]
    base: String,
    symbols: Option<String>,
}

#[derive(Debug, Deserialize)]
struct AnalyzeTrendArgs {
    #[serde(default = "default_base")]
    base: String,
    #[serde(default = "default_target")]
    target: String,
    #[serde(default = "default_days")]
    days: usize,
    #[serde(default = "default_predict_days")]
    predict_days: usize,
}

#[derive(Debug, Deserialize)]
struct GetHistoricalArgs {
    #[serde(default = "default_base")]
    base: String,
    #[serde(default = "default_days")]
    days: usize,
    symbols: Option<String>,
}

fn default_base() -> String {
    "EUR".to_string()
}
fn default_target() -> String {
    "USD".to_string()
}
fn default_days() -> usize {
    30
}
fn default_predict_days() -> usize {
    7
}

struct GetRatesTool {
    server: CurrencyServer,
}

#[async_trait]
impl ToolHandler for GetRatesTool {
    async fn handle(&self, args: Value, _extra: pmcp::RequestHandlerExtra) -> pmcp::Result<Value> {
        let mut server = self.server.clone();
        let params: GetRatesArgs = serde_json::from_value(args)
            .map_err(|e| pmcp::Error::validation(format!("Invalid arguments: {}", e)))?;

        server
            .validate_currency(&params.base)
            .map_err(pmcp::Error::invalid_params)?;

        let rates = server
            .fetch_current_rates(&params.base, params.symbols.as_deref())
            .await
            .map_err(|e| pmcp::Error::internal(format!("Failed to fetch rates: {}", e)))?;

        let result = CallToolResult::new(vec![Content::text(format!(
            "Current exchange rates for {} on {}:\n\n{}",
            params.base,
            rates["date"].as_str().unwrap_or("unknown"),
            serde_json::to_string_pretty(&rates["rates"])
                .unwrap_or_else(|_| "Error formatting rates".to_string())
        ))]);

        Ok(serde_json::to_value(result)?)
    }
}

struct AnalyzeTrendTool {
    server: CurrencyServer,
}

#[async_trait]
impl ToolHandler for AnalyzeTrendTool {
    async fn handle(&self, args: Value, _extra: pmcp::RequestHandlerExtra) -> pmcp::Result<Value> {
        let mut server = self.server.clone();
        let params: AnalyzeTrendArgs = serde_json::from_value(args)
            .map_err(|e| pmcp::Error::validation(format!("Invalid arguments: {}", e)))?;

        server
            .validate_currency(&params.base)
            .map_err(pmcp::Error::invalid_params)?;
        server
            .validate_currency(&params.target)
            .map_err(pmcp::Error::invalid_params)?;

        let start_date =
            chrono::Utc::now().date_naive() - chrono::Duration::days(params.days as i64);
        let end_date = chrono::Utc::now().date_naive();

        let historical = server
            .fetch_historical_rates(
                &params.base,
                &start_date.format("%Y-%m-%d").to_string(),
                &end_date.format("%Y-%m-%d").to_string(),
                Some(&params.target),
            )
            .await
            .map_err(|e| {
                pmcp::Error::internal(format!("Failed to fetch historical data: {}", e))
            })?;

        // Extract rates for the target currency
        let mut rates = Vec::new();
        if let Some(historical_rates) = historical["rates"].as_object() {
            for (_date, rate_data) in historical_rates {
                if let Some(target_rate) = rate_data.get(&params.target).and_then(|v| v.as_f64()) {
                    rates.push(target_rate);
                }
            }
        }

        let moving_avg_7 = server.calculate_moving_average(&rates, 7);
        let moving_avg_14 = server.calculate_moving_average(&rates, 14);
        let predictions = server.predict_future_rates(&rates, params.predict_days);
        let sparkline = server.generate_sparkline(&rates);

        let current_rate = rates.last().copied().unwrap_or(0.0);
        let trend_direction = if rates.len() >= 2 {
            let previous = rates[rates.len() - 2];
            if current_rate > previous {
                "↗️ Rising"
            } else if current_rate < previous {
                "↘️ Falling"
            } else {
                "→ Stable"
            }
        } else {
            "→ Insufficient data"
        };

        let analysis = format!(
            "Currency Trend Analysis: {}{}\n\
            ==========================================\n\
            \n\
            📊 Current Rate: {:.4}\n\
            📈 Trend: {}\n\
            📅 Analysis Period: {} days\n\
            \n\
            📉 Rate Visualization:\n\
            {}\n\
            \n\
            📋 Moving Averages:\n\
            • 7-day MA: {:.4}\n\
            • 14-day MA: {:.4}\n\
            \n\
            🔮 Predictions (next {} days):\n\
            {}\n\
            \n\
            💡 Analysis:\n\
            • Total data points: {}\n\
            • Rate range: {:.4} - {:.4}\n\
            • Volatility: {:.4}%",
            params.base,
            params.target,
            current_rate,
            trend_direction,
            params.days,
            sparkline,
            moving_avg_7.last().copied().unwrap_or(0.0),
            moving_avg_14.last().copied().unwrap_or(0.0),
            params.predict_days,
            predictions
                .iter()
                .enumerate()
                .map(|(i, &pred)| format!("Day {}: {:.4}", i + 1, pred))
                .collect::<Vec<_>>()
                .join("\n"),
            rates.len(),
            rates.iter().fold(f64::INFINITY, |a, &b| a.min(b)),
            rates.iter().fold(f64::NEG_INFINITY, |a, &b| a.max(b)),
            if rates.len() > 1 {
                let mean = rates.iter().sum::<f64>() / rates.len() as f64;
                let variance =
                    rates.iter().map(|r| (r - mean).powi(2)).sum::<f64>() / rates.len() as f64;
                (variance.sqrt() / mean) * 100.0
            } else {
                0.0
            }
        );

        let result = CallToolResult::new(vec![Content::text(analysis)]);

        Ok(serde_json::to_value(result)?)
    }
}

struct ListCurrenciesTool {
    server: CurrencyServer,
}

#[async_trait]
impl ToolHandler for ListCurrenciesTool {
    async fn handle(&self, _args: Value, _extra: pmcp::RequestHandlerExtra) -> pmcp::Result<Value> {
        let server = self.server.clone();

        let result = CallToolResult::new(vec![Content::text(format!(
            "Supported Currencies ({} total):\n\n{}",
            server.supported_currencies.len(),
            server.supported_currencies.join(", ")
        ))]);

        Ok(serde_json::to_value(result)?)
    }
}

struct GetHistoricalTool {
    server: CurrencyServer,
}

#[async_trait]
impl ToolHandler for GetHistoricalTool {
    async fn handle(&self, args: Value, _extra: pmcp::RequestHandlerExtra) -> pmcp::Result<Value> {
        let mut server = self.server.clone();
        let params: GetHistoricalArgs = serde_json::from_value(args)
            .map_err(|e| pmcp::Error::validation(format!("Invalid arguments: {}", e)))?;

        server
            .validate_currency(&params.base)
            .map_err(pmcp::Error::invalid_params)?;

        let start_date =
            chrono::Utc::now().date_naive() - chrono::Duration::days(params.days as i64);
        let end_date = chrono::Utc::now().date_naive();

        let historical = server
            .fetch_historical_rates(
                &params.base,
                &start_date.format("%Y-%m-%d").to_string(),
                &end_date.format("%Y-%m-%d").to_string(),
                params.symbols.as_deref(),
            )
            .await
            .map_err(|e| {
                pmcp::Error::internal(format!("Failed to fetch historical data: {}", e))
            })?;

        let result = CallToolResult::new(vec![Content::text(format!(
            "Historical exchange rates for {} (last {} days):\n\n{}",
            params.base,
            params.days,
            serde_json::to_string_pretty(&historical)
                .unwrap_or_else(|_| "Error formatting historical data".to_string())
        ))]);

        Ok(serde_json::to_value(result)?)
    }
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let currency_server = CurrencyServer::new();

    let server = Server::builder()
        .name("EU Currency Server")
        .version("1.0.0")
        .capabilities(ServerCapabilities::tools_only())
        .tool(
            "get_rates",
            GetRatesTool {
                server: currency_server.clone(),
            },
        )
        .tool(
            "analyze_trend",
            AnalyzeTrendTool {
                server: currency_server.clone(),
            },
        )
        .tool(
            "list_currencies",
            ListCurrenciesTool {
                server: currency_server.clone(),
            },
        )
        .tool(
            "get_historical",
            GetHistoricalTool {
                server: currency_server,
            },
        )
        .build()?;

    println!("🏦 EU Currency MCP Server starting...");
    println!("💱 Providing real-time currency analysis and predictions");
    println!("📊 Tools available: get_rates, analyze_trend, list_currencies, get_historical");
    println!("💾 Smart caching enabled (24-hour cache)");
    println!();

    server.run_stdio().await?;
    Ok(())
}