remote-mcp-kernel 0.1.0-alpha.2

A microkernel-based MCP (Model Context Protocol) server with OAuth authentication and multiple transport protocols
Documentation
//! Server information management for MCP OAuth server
//!
//! This module provides centralized server information and metadata
//! management, following the DRY principle and single source of truth.

use rmcp::model::*;
use serde_json::json;

/// Server information constants
pub struct ServerInfo {
    pub name: &'static str,
    pub version: &'static str,
    pub description: &'static str,
    pub protocol_version: ProtocolVersion,
}

impl ServerInfo {
    /// Get the default server information
    pub const fn default() -> Self {
        Self {
            name: "MCP OAuth Server",
            version: env!("CARGO_PKG_VERSION"),
            description: "MCP server with OAuth authentication capabilities",
            protocol_version: ProtocolVersion::V_2025_03_26,
        }
    }

    /// Get server capabilities for tools only
    pub fn tool_capabilities() -> ServerCapabilities {
        ServerCapabilities::builder().enable_tools().build()
    }

    /// Get full server capabilities
    pub fn full_capabilities() -> ServerCapabilities {
        ServerCapabilities::builder()
            .enable_tools()
            .enable_resources()
            .build()
    }

    /// Get implementation info
    pub fn implementation(&self) -> Implementation {
        Implementation {
            name: self.name.to_string(),
            version: self.version.to_string(),
        }
    }

    /// Get server info for MCP responses
    pub fn server_info(&self) -> rmcp::model::ServerInfo {
        rmcp::model::ServerInfo {
            protocol_version: self.protocol_version.clone(),
            capabilities: Self::tool_capabilities(),
            server_info: self.implementation(),
            instructions: Some(self.description.to_string()),
        }
    }

    /// Get initialize result
    pub fn initialize_result(&self) -> InitializeResult {
        InitializeResult {
            protocol_version: self.protocol_version.clone(),
            capabilities: Self::tool_capabilities(),
            server_info: self.implementation(),
            instructions: Some(self.description.to_string()),
        }
    }

    /// Get server info as JSON for API responses
    pub fn api_info(&self) -> serde_json::Value {
        json!({
            "name": self.name,
            "version": self.version,
            "description": self.description,
            "protocol_version": format!("{:?}", self.protocol_version),
            "capabilities": {
                "tools": true,
                "resources": false,
                "prompts": false
            },
            "endpoints": {
                "authorization": "/oauth/authorize",
                "token": "/oauth/token",
                "register": "/oauth/register",
                "callback": "/oauth/callback",
                "streamable": "/mcp/streamable",
                "info": "/info"
            }
        })
    }

    /// Get health check response
    pub fn health_response(&self) -> serde_json::Value {
        json!({
            "status": "healthy",
            "service": self.name,
            "version": self.version,
            "timestamp": chrono::Utc::now().to_rfc3339()
        })
    }

    /// Get OAuth provider specific info
    pub fn oauth_provider_info(&self) -> serde_json::Value {
        json!({
            "name": "MCP GitHub OAuth Provider",
            "version": self.version,
            "description": "OAuth provider for MCP server with GitHub authentication",
            "endpoints": {
                "authorization": "/oauth/authorize",
                "token": "/oauth/token",
                "register": "/oauth/register",
                "callback": "/oauth/callback"
            },
            "supported_scopes": ["read", "write"],
            "github_integration": true
        })
    }
}

/// Global server info instance
pub static SERVER_INFO: ServerInfo = ServerInfo::default();

/// Helper functions for common server info operations
pub mod helpers {
    use super::*;

    /// Get standard server info for MCP handlers
    pub fn mcp_server_info() -> rmcp::model::ServerInfo {
        SERVER_INFO.server_info()
    }

    /// Get standard initialize result
    pub fn mcp_initialize_result() -> InitializeResult {
        SERVER_INFO.initialize_result()
    }

    /// Get standard health check response
    pub fn health_check_response() -> serde_json::Value {
        SERVER_INFO.health_response()
    }

    /// Get standard API info response
    pub fn api_info_response() -> serde_json::Value {
        SERVER_INFO.api_info()
    }

    /// Get OAuth provider info response
    pub fn oauth_provider_info_response() -> serde_json::Value {
        SERVER_INFO.oauth_provider_info()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_server_info_consistency() {
        let info = ServerInfo::default();
        let server_info = info.server_info();
        let init_result = info.initialize_result();

        assert_eq!(server_info.server_info.name, init_result.server_info.name);
        assert_eq!(
            server_info.server_info.version,
            init_result.server_info.version
        );
        assert_eq!(server_info.protocol_version, init_result.protocol_version);
    }

    #[test]
    fn test_api_info_structure() {
        let info = ServerInfo::default();
        let api_info = info.api_info();

        assert_eq!(api_info["name"], info.name);
        assert_eq!(api_info["version"], info.version);
        assert_eq!(api_info["description"], info.description);
        assert!(api_info["endpoints"].is_object());
        assert!(api_info["capabilities"].is_object());
    }

    #[test]
    fn test_health_response_format() {
        let info = ServerInfo::default();
        let health = info.health_response();

        assert_eq!(health["status"], "healthy");
        assert_eq!(health["service"], info.name);
        assert_eq!(health["version"], info.version);
        assert!(health["timestamp"].is_string());
    }

    #[test]
    fn test_oauth_provider_info() {
        let info = ServerInfo::default();
        let oauth_info = info.oauth_provider_info();

        assert_eq!(oauth_info["name"], "MCP GitHub OAuth Provider");
        assert_eq!(oauth_info["version"], info.version);
        assert_eq!(oauth_info["github_integration"], true);
        assert!(oauth_info["supported_scopes"].is_array());
    }
}