git_iris/mcp/tools/
changelog.rs

1//! MCP changelog tool implementation
2//!
3//! This module provides the MCP tool for generating changelogs.
4
5use crate::changes::ChangelogGenerator;
6use crate::config::Config as GitIrisConfig;
7use crate::git::GitRepo;
8use crate::log_debug;
9use crate::mcp::tools::utils::{
10    GitIrisTool, apply_custom_instructions, create_text_result, parse_detail_level,
11    resolve_git_repo, validate_repository_parameter,
12};
13
14use rmcp::handler::server::tool::cached_schema_for_type;
15use rmcp::model::{CallToolResult, Tool};
16use rmcp::schemars;
17
18use serde::{Deserialize, Serialize};
19use std::borrow::Cow;
20use std::sync::Arc;
21
22/// Changelog tool for generating comprehensive changelog documents
23#[derive(Debug, Deserialize, Serialize, schemars::JsonSchema)]
24pub struct ChangelogTool {
25    /// Starting reference (commit hash, tag, or branch name)
26    pub from: String,
27
28    /// Ending reference (commit hash, tag, or branch name). Defaults to HEAD if not specified.
29    #[serde(default)]
30    pub to: String,
31
32    /// Level of detail for the changelog
33    #[serde(default)]
34    pub detail_level: String,
35
36    /// Custom instructions for the AI
37    #[serde(default)]
38    pub custom_instructions: String,
39
40    /// Repository path (local) or URL (remote). Required.
41    pub repository: String,
42
43    /// Explicit version name to use in the changelog (optional)
44    #[serde(default)]
45    pub version_name: String,
46}
47
48impl ChangelogTool {
49    /// Returns the tool definition for the changelog tool
50    pub fn get_tool_definition() -> Tool {
51        Tool {
52            name: Cow::Borrowed("git_iris_changelog"),
53            description: Cow::Borrowed("Generate a detailed changelog between two Git references"),
54            input_schema: cached_schema_for_type::<Self>(),
55        }
56    }
57}
58
59#[async_trait::async_trait]
60impl GitIrisTool for ChangelogTool {
61    /// Execute the changelog tool with the provided repository and configuration
62    async fn execute(
63        &self,
64        git_repo: Arc<GitRepo>,
65        config: GitIrisConfig,
66    ) -> Result<CallToolResult, anyhow::Error> {
67        log_debug!("Generating changelog with: {:?}", self);
68
69        // Validate repository parameter
70        validate_repository_parameter(&self.repository)?;
71        let git_repo = resolve_git_repo(Some(self.repository.as_str()), git_repo)?;
72        log_debug!("Using repository: {}", git_repo.repo_path().display());
73
74        // Parse detail level using shared utility
75        let detail_level = parse_detail_level(&self.detail_level);
76
77        // Set up config with custom instructions if provided
78        let mut config = config.clone();
79        apply_custom_instructions(&mut config, &self.custom_instructions);
80
81        // Default to HEAD if to is empty
82        let to = if self.to.trim().is_empty() {
83            "HEAD".to_string()
84        } else {
85            self.to.clone()
86        };
87
88        // Generate the changelog using the generator
89        let content =
90            ChangelogGenerator::generate(git_repo.clone(), &self.from, &to, &config, detail_level)
91                .await?;
92
93        // If version_name is provided, update the changelog content to use that version
94        let version_opt = if self.version_name.is_empty() {
95            None
96        } else {
97            Some(self.version_name.clone())
98        };
99
100        // If version_name is provided, use it when updating the changelog
101        if let Some(version) = &version_opt {
102            log_debug!("Using custom version name: {}", version);
103        }
104
105        // Create and return the result using shared utility
106        Ok(create_text_result(content))
107    }
108}