Skip to main content

gopher_mcp_rust/
lib.rs

1//! # Gopher MCP Rust SDK
2//!
3//! Rust SDK for Gopher Orch - AI Agent orchestration framework with native C++ performance.
4//!
5//! ## Quick Start
6//!
7//! ```rust,no_run
8//! use gopher_mcp_rust::{GopherAgent, ConfigBuilder};
9//!
10//! fn main() -> Result<(), Box<dyn std::error::Error>> {
11//!     // Create agent with server configuration
12//!     let config = ConfigBuilder::new()
13//!         .with_provider("AnthropicProvider")
14//!         .with_model("claude-3-haiku-20240307")
15//!         .with_server_config(r#"{"succeeded": true, "data": {"servers": []}}"#)
16//!         .build();
17//!
18//!     let agent = GopherAgent::create(config)?;
19//!
20//!     // Run a query
21//!     let result = agent.run("What is the weather in Tokyo?")?;
22//!     println!("{}", result);
23//!
24//!     Ok(())
25//! }
26//! ```
27
28mod agent;
29mod config;
30mod error;
31mod ffi;
32mod result;
33
34pub use agent::GopherAgent;
35pub use config::{Config, ConfigBuilder};
36pub use error::{Error, Result};
37pub use result::{AgentResult, AgentResultStatus};
38
39// Re-export auth types when the auth feature is enabled
40#[cfg(feature = "auth")]
41pub use ffi::auth::{GopherAuthClient, TokenPayload, ValidationResult};
42
43use std::sync::atomic::{AtomicBool, Ordering};
44use std::sync::Once;
45
46static INIT: Once = Once::new();
47static INITIALIZED: AtomicBool = AtomicBool::new(false);
48
49/// Initialize the gopher-orch library.
50/// Called automatically by `GopherAgent::create()` if not already initialized.
51pub fn init() -> Result<()> {
52    let mut init_result = Ok(());
53
54    INIT.call_once(|| {
55        if !ffi::is_available() {
56            init_result = Err(Error::Library(
57                "Failed to load gopher-orch native library".into(),
58            ));
59            return;
60        }
61        INITIALIZED.store(true, Ordering::SeqCst);
62    });
63
64    init_result
65}
66
67/// Returns true if the library is initialized.
68pub fn is_initialized() -> bool {
69    INITIALIZED.load(Ordering::SeqCst)
70}
71
72/// Shutdown the gopher-orch library.
73pub fn shutdown() {
74    INITIALIZED.store(false, Ordering::SeqCst);
75}
76
77#[cfg(test)]
78mod tests {
79    use super::*;
80
81    #[test]
82    fn test_init() {
83        // Just test that init doesn't panic
84        let _ = init();
85    }
86
87    #[test]
88    fn test_config_builder() {
89        let config = ConfigBuilder::new()
90            .with_provider("TestProvider")
91            .with_model("test-model")
92            .with_api_key("test-key")
93            .build();
94
95        assert_eq!(config.provider(), "TestProvider");
96        assert_eq!(config.model(), "test-model");
97        assert!(config.has_api_key());
98    }
99}