cleanpy 0.1.3

Python 3.12+ 最佳实践知识库 - MCP 服务器
Documentation
// Type definitions for cleanpy MCP server
// All data structures used across the application

use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Topic information structure with embedded content
/// All documentation content is embedded at compile time via build.rs
#[derive(Debug, Clone, Serialize)]
pub struct TopicInfo {
    pub topic: &'static str,
    pub title: &'static str,
    pub when_to_use: &'static str,
    pub keywords: &'static [&'static str],
    pub file: &'static str,
    #[serde(skip_serializing)] // Don't include content in JSON serialization
    pub content: &'static str, // Embedded content from markdown files
}

/// Topic summary for list_topics (without file path)
#[derive(Debug, Serialize)]
pub struct TopicSummary {
    pub topic: &'static str,
    pub title: &'static str,
    pub when_to_use: &'static str,
    pub keywords: &'static [&'static str],
}

// JSON-RPC Message Types
#[derive(Debug, Deserialize)]
pub struct JsonRpcRequest {
    #[allow(dead_code)]
    pub jsonrpc: String,
    pub id: Option<serde_json::Value>,
    pub method: String,
    #[serde(default)]
    pub params: Option<serde_json::Value>,
}

#[derive(Debug, Serialize)]
pub struct JsonRpcResponse {
    pub jsonrpc: &'static str,
    pub id: Option<serde_json::Value>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub result: Option<serde_json::Value>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub error: Option<JsonRpcError>,
}

#[derive(Debug, Serialize)]
pub struct JsonRpcError {
    pub code: i32,
    pub message: String,
}

// MCP Protocol Types
#[derive(Debug, Serialize)]
pub struct InitializeResult {
    #[serde(rename = "protocolVersion")]
    pub protocol_version: &'static str,
    pub capabilities: Capabilities,
    #[serde(rename = "serverInfo")]
    pub server_info: ServerInfo,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub _meta: Option<HashMap<String, serde_json::Value>>,
}

#[derive(Debug, Serialize)]
pub struct Capabilities {
    pub tools: std::collections::HashMap<String, serde_json::Value>,
}

#[derive(Debug, Serialize)]
pub struct ServerInfo {
    pub name: &'static str,
    pub version: &'static str,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub _meta: Option<HashMap<String, serde_json::Value>>,
}

#[derive(Debug, Serialize)]
pub struct ToolSchema {
    pub name: &'static str,
    pub description: &'static str,
    #[serde(rename = "inputSchema")]
    pub input_schema: serde_json::Value,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub title: Option<&'static str>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub _meta: Option<HashMap<String, serde_json::Value>>,
}

#[derive(Debug, Serialize)]
pub struct ToolsListResult {
    pub tools: Vec<ToolSchema>,
}

#[derive(Debug, Serialize)]
pub struct ToolResult {
    pub content: Vec<ToolContent>,
    #[serde(rename = "isError", skip_serializing_if = "Option::is_none")]
    pub is_error: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub _meta: Option<HashMap<String, serde_json::Value>>,
}

/// Tool content types supporting text, image, and resource
#[derive(Debug, Serialize)]
#[serde(tag = "type")]
pub enum ToolContent {
    #[serde(rename = "text")]
    Text {
        text: String,
        #[serde(rename = "isError", skip_serializing_if = "Option::is_none")]
        is_error: Option<bool>,
    },
    #[serde(rename = "image")]
    Image {
        /// Base64-encoded image data
        data: String,
        #[serde(rename = "mimeType")]
        mime_type: String,
        #[serde(rename = "isError", skip_serializing_if = "Option::is_none")]
        is_error: Option<bool>,
    },
    #[serde(rename = "resource")]
    Resource {
        uri: String,
        #[serde(skip_serializing_if = "Option::is_none")]
        text: Option<String>,
        #[serde(rename = "mimeType", skip_serializing_if = "Option::is_none")]
        mime_type: Option<String>,
        #[serde(rename = "isError", skip_serializing_if = "Option::is_none")]
        is_error: Option<bool>,
    },
}

/// Resource link structure for referencing external resources
#[derive(Debug, Serialize, Clone)]
pub struct ResourceLink {
    pub uri: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub text: Option<String>,
}

/// Document metadata and content for search results
#[derive(Debug, Clone)]
pub(crate) struct DocumentMatch {
    pub topic: String,
    pub title: String,
    pub when_to_use: String,
    pub keywords: Vec<String>,
    pub file_path: String,
    pub matched_keywords: Vec<String>,
    pub relevance_score: f32,
}