Skip to main content

client_example_json/
client_example_json.rs

1//! Example using JSON server configuration.
2
3use gopher_mcp_rust::{ConfigBuilder, GopherAgent};
4use std::env;
5
6/// Server configuration for local MCP servers
7const SERVER_CONFIG: &str = r#"{
8    "succeeded": true,
9    "code": 200000000,
10    "message": "success",
11    "data": {
12        "servers": [
13            {
14                "version": "2025-01-09",
15                "serverId": "1",
16                "name": "server1",
17                "transport": "http_sse",
18                "config": {"url": "http://127.0.0.1:3001/mcp", "headers": {}},
19                "connectTimeout": 5000,
20                "requestTimeout": 30000
21            },
22            {
23                "version": "2025-01-09",
24                "serverId": "2",
25                "name": "server2",
26                "transport": "http_sse",
27                "config": {"url": "http://127.0.0.1:3002/mcp", "headers": {}},
28                "connectTimeout": 5000,
29                "requestTimeout": 30000
30            }
31        ]
32    }
33}"#;
34
35fn main() {
36    let provider = "AnthropicProvider";
37    let model = "claude-3-haiku-20240307";
38
39    // Create agent with JSON server configuration
40    let config = ConfigBuilder::new()
41        .with_provider(provider)
42        .with_model(model)
43        .with_server_config(SERVER_CONFIG)
44        .build();
45
46    let agent = match GopherAgent::create(config) {
47        Ok(agent) => agent,
48        Err(e) => {
49            eprintln!("Error: {}", e);
50            std::process::exit(1);
51        }
52    };
53
54    println!("GopherAgent created!");
55
56    // Get question from command line args or use default
57    let args: Vec<String> = env::args().collect();
58    let question = if args.len() > 1 {
59        args[1..].join(" ")
60    } else {
61        "What is the weather like in New York?".to_string()
62    };
63
64    println!("Question: {}", question);
65
66    // Run the query
67    match agent.run(&question) {
68        Ok(answer) => {
69            println!("Answer:");
70            println!("{}", answer);
71        }
72        Err(e) => {
73            eprintln!("Error: {}", e);
74            std::process::exit(1);
75        }
76    }
77}