mcpr 0.1.0

Rust implementation of Anthropic's Model Context Protocol
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
//! Direct Web Scraper Example
//!
//! This example demonstrates a direct web scraper that uses Claude to extract
//! information from web pages based on user queries.

use dotenv::dotenv;
use reqwest::Client as HttpClient;
use scraper::{Html, Selector};
use serde_json::{json, Value};
use std::env;
use std::error::Error;
use std::fmt;
use std::fs;
use std::io::{self, BufRead, Write};
use std::path::Path;
use tokio::runtime::Runtime;

// ANSI color codes for better terminal output
const GREEN: &str = "\x1b[0;32m";
const YELLOW: &str = "\x1b[1;33m";
const BLUE: &str = "\x1b[0;34m";
const RED: &str = "\x1b[0;31m";
const CYAN: &str = "\x1b[0;36m";
const BOLD: &str = "\x1b[1m";
const NC: &str = "\x1b[0m"; // No Color

// Anthropic API constants
const ANTHROPIC_API_URL: &str = "https://api.anthropic.com/v1/messages";
const ANTHROPIC_MODEL: &str = "claude-3-opus-20240229";
const MAX_TOKENS: u32 = 4096;

// Custom error type
#[derive(Debug)]
struct WebScraperError(String);

impl fmt::Display for WebScraperError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl Error for WebScraperError {}

impl From<String> for WebScraperError {
    fn from(error: String) -> Self {
        WebScraperError(error)
    }
}

impl From<&str> for WebScraperError {
    fn from(error: &str) -> Self {
        WebScraperError(error.to_string())
    }
}

impl From<reqwest::Error> for WebScraperError {
    fn from(error: reqwest::Error) -> Self {
        WebScraperError(error.to_string())
    }
}

impl From<io::Error> for WebScraperError {
    fn from(error: io::Error) -> Self {
        WebScraperError(error.to_string())
    }
}

impl From<env::VarError> for WebScraperError {
    fn from(error: env::VarError) -> Self {
        WebScraperError(error.to_string())
    }
}

struct WebScraper {
    http_client: HttpClient,
    anthropic_key: String,
}

impl WebScraper {
    fn new(anthropic_key: String) -> Self {
        Self {
            http_client: HttpClient::new(),
            anthropic_key,
        }
    }

    async fn scrape_url(&self, url: &str) -> Result<String, WebScraperError> {
        println!("{}Scraping URL: {}{}", YELLOW, url, NC);

        // Fetch the HTML content
        let response = self.http_client.get(url)
            .header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36")
            .send()
            .await?;

        if !response.status().is_success() {
            return Err(format!("Failed to fetch URL: HTTP {}", response.status()).into());
        }

        let html = response.text().await?;
        println!(
            "{}Successfully fetched HTML content ({} bytes){}",
            GREEN,
            html.len(),
            NC
        );

        // Save HTML to the specified directory or /tmp as fallback
        let html_dir = env::var("HTML_DIR").unwrap_or_else(|_| "/tmp".to_string());
        let file_name = format!("scraped_{}.html", chrono::Utc::now().timestamp());
        let file_path = Path::new(&html_dir).join(&file_name);

        fs::write(&file_path, &html)?;
        println!("{}HTML saved to {}{}", GREEN, file_path.display(), NC);

        Ok(html)
    }

    async fn extract_content(
        &self,
        html: &str,
        query: &str,
        css_selector: Option<&str>,
    ) -> Result<String, WebScraperError> {
        println!(
            "{}Parsing HTML and extracting content for query: {}{}",
            YELLOW, query, NC
        );

        // Parse the HTML
        let document = Html::parse_document(html);

        // Extract the title
        let title_selector = Selector::parse("title").unwrap();
        let title = document
            .select(&title_selector)
            .next()
            .map(|element| element.inner_html())
            .unwrap_or_else(|| "No title found".to_string());

        // Extract specific content if CSS selector is provided
        let specific_content = if let Some(selector_str) = css_selector {
            match Selector::parse(selector_str) {
                Ok(selector) => {
                    println!("{}Using CSS selector: {}{}", GREEN, selector_str, NC);
                    let selected_content: Vec<String> = document
                        .select(&selector)
                        .map(|element| element.html())
                        .collect();

                    if selected_content.is_empty() {
                        println!(
                            "{}No elements found with selector: {}{}",
                            YELLOW, selector_str, NC
                        );
                        None
                    } else {
                        println!(
                            "{}Found {} elements with selector{}",
                            GREEN,
                            selected_content.len(),
                            NC
                        );
                        Some(selected_content.join("\n"))
                    }
                }
                Err(e) => {
                    println!(
                        "{}Invalid CSS selector: {} - {}{}",
                        RED, selector_str, e, NC
                    );
                    None
                }
            }
        } else {
            None
        };

        // Extract the main content if no specific content was selected
        let body_text = if specific_content.is_none() {
            let body_selector = Selector::parse("body").unwrap();
            document
                .select(&body_selector)
                .next()
                .map(|element| element.text().collect::<Vec<_>>().join(" "))
                .unwrap_or_else(|| "No body content found".to_string())
        } else {
            "".to_string()
        };

        // Determine which content to use for extraction
        let content_to_analyze = if let Some(content) = specific_content {
            println!("{}Using selected content for analysis{}", GREEN, NC);
            content
        } else {
            println!("{}Using full body content for analysis{}", GREEN, NC);
            body_text
        };

        // Truncate the content if it's too long
        let truncated_content = if content_to_analyze.len() > 100000 {
            format!("{}... (truncated)", &content_to_analyze[0..100000])
        } else {
            content_to_analyze
        };

        // Use Anthropic's Claude to extract relevant information
        let extracted_content = self
            .query_anthropic(&title, &truncated_content, query)
            .await?;

        Ok(extracted_content)
    }

    async fn query_anthropic(
        &self,
        title: &str,
        content: &str,
        query: &str,
    ) -> Result<String, WebScraperError> {
        println!(
            "{}Querying Anthropic API to extract relevant information{}",
            YELLOW, NC
        );

        let prompt = format!(
            "I need you to extract specific information from this web page based on the user's query.\n\n\
            Page Title: {}\n\n\
            User Query: {}\n\n\
            Web Page Content:\n{}\n\n\
            Please extract and summarize only the information that is relevant to the user's query. \
            Focus on providing accurate, concise, and helpful information. \
            If the requested information is not found in the content, please state that clearly.",
            title, query, content
        );

        let request_body = json!({
            "model": ANTHROPIC_MODEL,
            "max_tokens": MAX_TOKENS,
            "messages": [
                {
                    "role": "user",
                    "content": prompt
                }
            ]
        });

        // Send the request to Anthropic API
        let response = self
            .http_client
            .post(ANTHROPIC_API_URL)
            .header("x-api-key", &self.anthropic_key)
            .header("anthropic-version", "2023-06-01")
            .header("Content-Type", "application/json")
            .json(&request_body)
            .send()
            .await?;

        // Check the status code first
        let status = response.status();
        if !status.is_success() {
            // Get the error text from the response
            let error_text = response.text().await?;
            return Err(format!("Anthropic API error: {} - {}", status, error_text).into());
        }

        // Parse the successful response
        let response_json: Value = response.json().await?;
        let extracted_content = response_json["content"][0]["text"]
            .as_str()
            .ok_or_else(|| WebScraperError("Failed to parse Anthropic API response".to_string()))?
            .to_string();

        println!(
            "{}Successfully extracted relevant content using Anthropic API{}",
            GREEN, NC
        );

        Ok(extracted_content)
    }
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    // Load environment variables from .env file
    dotenv().ok();

    println!("{}{}Direct Web Scraper{}{}", BOLD, BLUE, NC, NC);
    println!(
        "{}This tool allows you to scrape web content and extract information using Claude.{}",
        CYAN, NC
    );

    // Get Anthropic API key from environment
    let anthropic_key = match env::var("ANTHROPIC_API_KEY") {
        Ok(key) => key,
        Err(_) => {
            eprintln!(
                "{}Error: ANTHROPIC_API_KEY environment variable not set{}",
                RED, NC
            );
            eprintln!(
                "{}Please set it using: export ANTHROPIC_API_KEY=your_api_key{}",
                YELLOW, NC
            );
            return Err(Box::<dyn Error>::from(WebScraperError(
                "ANTHROPIC_API_KEY environment variable not set".into(),
            )));
        }
    };

    // Create the web scraper
    let scraper = WebScraper::new(anthropic_key);

    // Parse command-line arguments
    let args: Vec<String> = env::args().collect();
    let mut url = String::new();
    let mut query = String::new();
    let mut css_selector = String::new();

    // Check for command-line arguments
    let mut i = 1;
    while i < args.len() {
        match args[i].as_str() {
            "--url" => {
                if i + 1 < args.len() {
                    url = args[i + 1].clone();
                    i += 2;
                } else {
                    eprintln!("{}Error: Missing value for --url{}", RED, NC);
                    return Err(Box::<dyn Error>::from(WebScraperError(
                        "Missing value for --url".into(),
                    )));
                }
            }
            "--query" => {
                if i + 1 < args.len() {
                    query = args[i + 1].clone();
                    i += 2;
                } else {
                    eprintln!("{}Error: Missing value for --query{}", RED, NC);
                    return Err(Box::<dyn Error>::from(WebScraperError(
                        "Missing value for --query".into(),
                    )));
                }
            }
            "--css-selector" => {
                if i + 1 < args.len() {
                    css_selector = args[i + 1].clone();
                    i += 2;
                } else {
                    eprintln!("{}Error: Missing value for --css-selector{}", RED, NC);
                    return Err(Box::<dyn Error>::from(WebScraperError(
                        "Missing value for --css-selector".into(),
                    )));
                }
            }
            _ => {
                i += 1;
            }
        }
    }

    // If no URL or query is provided via command line, prompt the user
    if url.is_empty() || query.is_empty() {
        let stdin = io::stdin();
        let mut stdin_lock = stdin.lock();
        let mut input = String::new();

        if url.is_empty() {
            print!("{}Enter the URL to scrape: {}", YELLOW, NC);
            io::stdout().flush().unwrap();
            input.clear();
            stdin_lock.read_line(&mut input).unwrap();
            url = input.trim().to_string();

            if url.is_empty() {
                println!("{}URL cannot be empty. Exiting.{}", RED, NC);
                return Err(Box::<dyn Error>::from(WebScraperError(
                    "URL cannot be empty".into(),
                )));
            }
        }

        if css_selector.is_empty() {
            print!(
                "{}Enter CSS selector (optional, e.g., 'div.content', 'article', '#main'): {}",
                YELLOW, NC
            );
            io::stdout().flush().unwrap();
            input.clear();
            stdin_lock.read_line(&mut input).unwrap();
            css_selector = input.trim().to_string();
        }

        if query.is_empty() {
            print!(
                "{}Enter your query (what information are you looking for?): {}",
                YELLOW, NC
            );
            io::stdout().flush().unwrap();
            input.clear();
            stdin_lock.read_line(&mut input).unwrap();
            query = input.trim().to_string();

            if query.is_empty() {
                println!("{}Query cannot be empty. Exiting.{}", RED, NC);
                return Err(Box::<dyn Error>::from(WebScraperError(
                    "Query cannot be empty".into(),
                )));
            }
        }
    }

    // Scrape the URL
    let html = match scraper.scrape_url(&url).await {
        Ok(html) => html,
        Err(e) => {
            eprintln!("{}Error scraping URL: {}{}", RED, e, NC);
            return Err(Box::<dyn Error>::from(e));
        }
    };

    // Extract content based on query and optional CSS selector
    let css_selector_option = if css_selector.is_empty() {
        None
    } else {
        Some(css_selector.as_str())
    };
    let extracted_content = match scraper
        .extract_content(&html, &query, css_selector_option)
        .await
    {
        Ok(content) => content,
        Err(e) => {
            eprintln!("{}Error extracting content: {}{}", RED, e, NC);
            return Err(Box::<dyn Error>::from(e));
        }
    };

    // Display the result
    println!("\n{}{}Extracted Content:{}{}", BOLD, GREEN, NC, NC);
    println!("{}", extracted_content);

    Ok(())
}