snm-brightdata-client 0.4.0

Bright Data Wrapper Client Highly compacted Data implemented in Rust with Actix Web
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
// src/server.rs - Fixed version with unused variables removed
use actix_web::{web, HttpRequest, HttpResponse, Result};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::env;
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::RwLock;
use chrono::{DateTime, Utc};
use reqwest::Client;
use uuid::Uuid;
use crate::error::BrightDataError;
use crate::types::{McpResponse};
use crate::tool::{handle_mcp_initialize, get_current_mcp_session}; // Import MCP session functions

#[derive(Debug, Clone)]
pub struct Config {
    pub api_token: String,
    pub web_unlocker_zone: String,
    pub browser_zone: String,
    pub serp_zone: String,  // Added SERP zone
    pub rate_limit: Option<String>,
    pub timeout: Duration,
    pub max_retries: u32,
}

impl Config {
    pub fn from_env() -> Result<Self, std::io::Error> {
        Ok(Self {
            api_token: env::var("API_TOKEN").unwrap_or_default(),
            web_unlocker_zone: env::var("WEB_UNLOCKER_ZONE").unwrap_or_else(|_| "default_zone".to_string()),
            browser_zone: env::var("BROWSER_ZONE").unwrap_or_else(|_| "default_browser".to_string()),
            serp_zone: env::var("BRIGHTDATA_SERP_ZONE").unwrap_or_else(|_| "serp_api2".to_string()),
            rate_limit: env::var("RATE_LIMIT").ok(),
            timeout: Duration::from_secs(env::var("REQUEST_TIMEOUT").unwrap_or_else(|_| "300".to_string()).parse().unwrap_or(300)),
            max_retries: env::var("MAX_RETRIES").unwrap_or_else(|_| "3".to_string()).parse().unwrap_or(3),
        })
    }
}

#[derive(Debug)]
pub struct AppState {
    pub config: Config,
    pub session_id: Uuid,
    pub http_client: Client,
    pub rate_limits: Arc<RwLock<HashMap<String, (u32, DateTime<Utc>)>>>,
    pub start_time: DateTime<Utc>,
    pub current_mcp_session: Arc<RwLock<Option<String>>>, // Track current MCP session
}

impl AppState {
    pub fn new(config: Config) -> Self {
        Self {
            session_id: Uuid::new_v4(),
            config: config.clone(),
            http_client: Client::builder().timeout(config.timeout).build().unwrap(),
            rate_limits: Arc::new(RwLock::new(HashMap::new())),
            start_time: Utc::now(),
            current_mcp_session: Arc::new(RwLock::new(None)),
        }
    }
}

pub struct BrightDataUrls;

impl BrightDataUrls {
    pub fn request_api() -> String {
        let base_url = std::env::var("BRIGHTDATA_BASE_URL")
            .unwrap_or_else(|_| "https://api.brightdata.com".to_string());
        format!("{}/request", base_url)
    }
}

// Enhanced MCP handler with initialize support for metrics
pub async fn handle_mcp_request(
    _req: HttpRequest,
    payload: web::Json<crate::types::McpRequest>,
    state: web::Data<AppState>,
) -> Result<HttpResponse> {
    let req = payload.into_inner();
    let id = req.id.clone();

    let mcp_result: Result<McpResponse, String> = match req.method.as_str() {
        // Handle MCP initialize - this resets metrics for new session
        "initialize" => {
            log::info!("🎯 MCP Initialize received - starting new metrics session");
            
            // Start new MCP session and reset metrics
            let session_id = handle_mcp_initialize();
            
            // Update app state with new session
            {
                let mut current_session = state.current_mcp_session.write().await;
                *current_session = Some(session_id.clone());
            }
            
            log::info!("📊 New MCP session started: {}", session_id);
            
            Ok(McpResponse {
                jsonrpc: "2.0".to_string(),
                id,
                result: Some(serde_json::json!({
                    "protocolVersion": "2024-11-05",
                    "capabilities": {
                        "tools": {},
                        "logging": {},
                        "prompts": {}
                    },
                    "serverInfo": {
                        "name": "snm-brightdata-client",
                        "version": env!("CARGO_PKG_VERSION")
                    },
                    "instructions": "BrightData MCP Server ready with metrics tracking",
                    "session_id": session_id
                })),
                error: None,
            })
        }

        "tools/list" => {
            let current_session = get_current_mcp_session();
            log::info!("📋 Tools list requested [Session: {:?}]", current_session);
            
            Ok(McpResponse {
                jsonrpc: "2.0".to_string(),
                id,
                result: Some(serde_json::json!({
                    "tools": [
                        { "name": "scrape_website", "description": "Scrape a web page" },
                        { "name": "search_web", "description": "Perform a web search" },
                        { "name": "extract_data", "description": "Extract structured data from a webpage" },
                        { "name": "take_screenshot", "description": "Take a screenshot of a webpage" },
                        { "name": "get_stock_data", "description": "Get stock market data" },
                        { "name": "get_crypto_data", "description": "Get cryptocurrency data" },
                        { "name": "get_etf_data", "description": "Get ETF data" },
                        { "name": "get_bond_data", "description": "Get bond market data" },
                        { "name": "get_mutual_fund_data", "description": "Get mutual fund data" },
                        { "name": "get_commodity_data", "description": "Get commodity market data" },
                        { "name": "multi_zone_search", "description": "Search across multiple zones" }
                    ],
                    "session_id": current_session
                })),
                error: None,
            })
        }

        "tools/call" => {
            let current_session = get_current_mcp_session();
            
            if let Some(params) = req.params {
                let name = params.get("name").and_then(|v| v.as_str()).unwrap_or("");
                let args = params.get("arguments").cloned().unwrap_or_default();

                log::info!("🔧 Tool call: {} [Session: {:?}]", name, current_session);

                if !check_rate_limit(name, &state).await {
                    return Ok(HttpResponse::TooManyRequests().json(McpResponse {
                        jsonrpc: "2.0".to_string(),
                        id,
                        result: None,
                        error: Some(crate::types::McpError {
                            code: -32000,
                            message: "Rate limit exceeded".to_string(),
                            data: None,
                        }),
                    }));
                }

                let result = match name {
                    "scrape_website" => handle_scrape_website(&args, &state).await,
                    "search_web" => handle_search_web(&args, &state).await,
                    "extract_data" => handle_extract_placeholder(&args).await,
                    "take_screenshot" => handle_take_screenshot(&args, &state).await,
                    "get_stock_data" => handle_financial_tool("get_stock_data", &args).await,
                    "get_crypto_data" => handle_financial_tool("get_crypto_data", &args).await,
                    "get_etf_data" => handle_financial_tool("get_etf_data", &args).await,
                    "get_bond_data" => handle_financial_tool("get_bond_data", &args).await,
                    "get_mutual_fund_data" => handle_financial_tool("get_mutual_fund_data", &args).await,
                    "get_commodity_data" => handle_financial_tool("get_commodity_data", &args).await,
                    "multi_zone_search" => handle_financial_tool("multi_zone_search", &args).await,
                    _ => Err("Unknown tool".to_string()),
                };

                Ok(match result {
                    Ok(content) => McpResponse {
                        jsonrpc: "2.0".to_string(),
                        id,
                        result: Some(serde_json::json!({ 
                            "content": content,
                            "session_id": current_session
                        })),
                        error: None,
                    },
                    Err(msg) => McpResponse {
                        jsonrpc: "2.0".to_string(),
                        id,
                        result: None,
                        error: Some(crate::types::McpError {
                            code: -32603,
                            message: msg,
                            data: Some(serde_json::json!({
                                "session_id": current_session
                            })),
                        }),
                    },
                })
            } else {
                Ok(McpResponse {
                    jsonrpc: "2.0".to_string(),
                    id,
                    result: None,
                    error: Some(crate::types::McpError {
                        code: -32602,
                        message: "Missing parameters".into(),
                        data: None,
                    }),
                })
            }
        }

        _ => Ok(McpResponse {
            jsonrpc: "2.0".to_string(),
            id,
            result: None,
            error: Some(crate::types::McpError {
                code: -32601,
                message: "Method not found".to_string(),
                data: None,
            }),
        }),
    };

    match mcp_result {
        Ok(resp) => Ok(HttpResponse::Ok().json(resp)),
        Err(e) => Ok(HttpResponse::InternalServerError().json(McpResponse {
            jsonrpc: "2.0".to_string(),
            id: req.id,
            result: None,
            error: Some(crate::types::McpError {
                code: -32603,
                message: e,
                data: None,
            }),
        })),
    }
}

pub async fn health_check(state: web::Data<AppState>) -> Result<HttpResponse> {
    let current_session = get_current_mcp_session();
    
    Ok(HttpResponse::Ok().json(serde_json::json!({
        "status": "healthy",
        "session_id": state.session_id,
        "uptime_seconds": (Utc::now() - state.start_time).num_seconds(),
        "zones": {
            "web_unlocker": state.config.web_unlocker_zone,
            "browser": state.config.browser_zone,
            "serp": state.config.serp_zone
        },
        "mcp_session": current_session,
        "metrics_tracking": current_session.is_some()
    })))
}

pub async fn cors_handler() -> HttpResponse {
    HttpResponse::Ok()
        .insert_header(("Access-Control-Allow-Origin", "*"))
        .insert_header(("Access-Control-Allow-Methods", "POST, GET, OPTIONS"))
        .insert_header(("Access-Control-Allow-Headers", "Content-Type, Authorization"))
        .finish()
}

async fn check_rate_limit(tool: &str, state: &web::Data<AppState>) -> bool {
    let mut limits = state.rate_limits.write().await;
    let now = Utc::now();
    let entry = limits.entry(tool.to_string()).or_insert((0, now));

    let limit = 10;
    let window = chrono::Duration::seconds(60);

    if now - entry.1 > window {
        entry.0 = 0;
        entry.1 = now;
    }

    if entry.0 >= limit {
        false
    } else {
        entry.0 += 1;
        true
    }
}

pub async fn handle_scrape_website(args: &serde_json::Value, state: &web::Data<AppState>) -> Result<String, String> {
    let url = args.get("url").and_then(|v| v.as_str()).ok_or("Missing 'url'")?;
    let format = args.get("format").and_then(|v| v.as_str()).unwrap_or("markdown");

    let mut payload = serde_json::json!({
        "url": url,
        "zone": state.config.web_unlocker_zone,
        "format": "raw",
    });

    if format == "markdown" {
        payload["data_format"] = serde_json::json!("markdown");
    }

    let api_url = BrightDataUrls::request_api();

    let res = state.http_client
        .post(&api_url)
        .header("Authorization", format!("Bearer {}", state.config.api_token))
        .json(&payload)
        .send()
        .await
        .map_err(|e| e.to_string())?;

    let body = res.text().await.map_err(|e| e.to_string())?;
    Ok(body)
}

pub async fn handle_search_web(args: &serde_json::Value, state: &web::Data<AppState>) -> Result<String, String> {
    let query = args.get("query").and_then(|v| v.as_str()).ok_or("Missing 'query'")?;
    let engine = args.get("engine").and_then(|v| v.as_str()).unwrap_or("google");
    let cursor = args.get("cursor").and_then(|v| v.as_str()).unwrap_or("0");

    let search_url = build_search_url(engine, query, cursor);

    // Use SERP zone for search operations
    let payload = serde_json::json!({
        "url": search_url,
        "zone": state.config.serp_zone,  // Use SERP zone instead of web_unlocker_zone
        "format": "raw",
        "data_format": "markdown"
    });

    let api_url = BrightDataUrls::request_api();
    let res = state.http_client
        .post(&api_url)
        .header("Authorization", format!("Bearer {}", state.config.api_token))
        .json(&payload)
        .send()
        .await
        .map_err(|e| e.to_string())?;

    let body = res.text().await.map_err(|e| e.to_string())?;
    Ok(body)
}

pub async fn handle_take_screenshot(args: &serde_json::Value, state: &web::Data<AppState>) -> Result<String, String> {
    let url = args.get("url").and_then(|v| v.as_str()).ok_or("Missing 'url'")?;
    let width = args.get("width").and_then(|v| v.as_i64()).unwrap_or(1280);
    let height = args.get("height").and_then(|v| v.as_i64()).unwrap_or(720);
    let full_page = args.get("full_page").and_then(|v| v.as_bool()).unwrap_or(false);

    let payload = serde_json::json!({
        "url": url,
        "zone": state.config.browser_zone,
        "format": "raw",
        "data_format": "screenshot",
        "viewport": {
            "width": width,
            "height": height
        },
        "full_page": full_page
    });

    let api_url = BrightDataUrls::request_api();
    let res = state.http_client
        .post(&api_url)
        .header("Authorization", format!("Bearer {}", state.config.api_token))
        .json(&payload)
        .send()
        .await
        .map_err(|e| e.to_string())?;

    // FIXED: Remove unused variable warning
    let _body = res.text().await.map_err(|e| e.to_string())?;
    Ok(format!("Screenshot captured for {} ({}x{})", url, width, height))
}

// Handler for financial tools using the tool resolver
async fn handle_financial_tool(tool_name: &str, args: &serde_json::Value) -> Result<String, String> {
    use crate::tool::ToolResolver; // FIXED: Remove unused Tool import
    
    let resolver = ToolResolver::default();
    match resolver.resolve(tool_name) {
        Some(tool) => {
            match tool.execute(args.clone()).await {
                Ok(result) => {
                    if !result.content.is_empty() {
                        Ok(result.content[0].text.clone())
                    } else {
                        Ok("No content returned".to_string())
                    }
                },
                Err(e) => Err(e.to_string()),
            }
        },
        None => Err(format!("Tool '{}' not found", tool_name)),
    }
}

pub async fn handle_extract_placeholder(_args: &serde_json::Value) -> Result<String, String> {
    Ok("🧠 Extract tool placeholder: AI-based structured data extraction coming soon.".to_string())
}

fn build_search_url(engine: &str, query: &str, cursor: &str) -> String {
    let q = urlencoding::encode(query);
    let page: usize = cursor.parse().unwrap_or(0);
    let start = page * 10;

    match engine {
        "yandex" => format!("https://yandex.com/search/?text={q}&p={page}"),
        "bing" => format!("https://www.bing.com/search?q={q}&first={}", start + 1),
        "duckduckgo" => format!("https://duckduckgo.com/?q={q}&s={start}"),
        _ => format!("https://www.google.com/search?q={q}&start={start}"),
    }
}