Skip to main content

gdrive_mcp_core/
lib.rs

1#![allow(clippy::field_reassign_with_default)]
2#![allow(clippy::result_large_err)]
3
4pub mod auth;
5pub mod client;
6pub mod config;
7pub mod convert;
8pub mod error;
9pub mod oauth;
10pub mod prompts;
11pub mod resources;
12pub mod server;
13pub mod tools;
14pub mod transport;
15
16use config::{AppConfig, Transport};
17
18/// Main entry point: build auth → client → server, then run on selected transport.
19pub async fn run_server(config: AppConfig) -> error::Result<()> {
20    match config.transport {
21        Transport::Stdio => {
22            // Stdio: single-user mode with eager Google auth
23            tracing::info!("Building Google Drive authenticator...");
24            let hub = auth::build_drive_hub(&config, true).await?;
25            let drive_client = client::DriveClient::new(hub);
26            let server = server::GDriveServer::new(drive_client);
27            transport::serve_stdio(server).await
28        }
29        Transport::Http => {
30            // HTTP: multi-user mode - each user authenticates via MCP OAuth → Google.
31            // Each MCP session gets its own DriveHub with the user's Google access token.
32            tracing::info!("Building Google Drive MCP server (multi-user mode)...");
33
34            // Parse Google OAuth credentials to create the OAuth proxy server
35            let creds_content = tokio::fs::read_to_string(&config.credentials_file)
36                .await
37                .map_err(|e| {
38                    error::GDriveError::OAuth2(format!("Cannot read credentials file: {e}"))
39                })?;
40            let google_config = oauth::parse_google_oauth_config(&creds_content).map_err(
41                |e| {
42                    error::GDriveError::OAuth2(format!(
43                        "Failed to parse Google OAuth config: {e}"
44                    ))
45                },
46            )?;
47            let base_url = format!("http://{}", config.http_addr);
48            let oauth_server = oauth::OAuthServer::new(&base_url, google_config);
49
50            // Build a shared hyper client (cloned into per-session DriveHubs)
51            let hyper_client = auth::build_shared_hyper_client()?;
52
53            // Build a template server (routers are reused, client is replaced per-session)
54            let placeholder_hub =
55                google_drive3::DriveHub::new(hyper_client.clone(), String::new());
56            let template_server =
57                server::GDriveServer::new(client::DriveClient::new(placeholder_hub));
58
59            transport::serve_http(template_server, hyper_client, oauth_server, &config)
60                .await
61        }
62    }
63}