use anyhow;
use futures::StreamExt;
use kodegen_mcp_schema::github::{
SearchIssuesArgs, SearchIssuesPrompts, GitHubSearchIssuesOutput,
GITHUB_SEARCH_ISSUES,
};
use kodegen_mcp_schema::github::search_issues::GitHubIssueSummary;
use kodegen_mcp_schema::{Tool, ToolExecutionContext, ToolResponse, McpError};
#[derive(Clone)]
pub struct SearchIssuesTool;
impl Tool for SearchIssuesTool {
type Args = SearchIssuesArgs;
type Prompts = SearchIssuesPrompts;
fn name() -> &'static str {
GITHUB_SEARCH_ISSUES
}
fn description() -> &'static str {
"Search for issues across GitHub using GitHub's powerful search syntax. \
Supports filtering by repository, state, labels, assignee, author, dates, and more. \
Returns matching issues with relevance ranking. \
Requires GITHUB_TOKEN environment variable. Note: Search API has stricter rate limits."
}
fn read_only() -> bool {
true
}
fn destructive() -> bool {
false
}
fn idempotent() -> bool {
true
}
fn open_world() -> bool {
true }
async fn execute(&self, args: Self::Args, _ctx: ToolExecutionContext) -> Result<ToolResponse<<Self::Args as kodegen_mcp_schema::ToolArgs>::Output>, McpError> {
let token = std::env::var("GITHUB_TOKEN").map_err(|_| {
McpError::Other(anyhow::anyhow!("GITHUB_TOKEN environment variable not set"))
})?;
let client = crate::GitHubClient::builder()
.personal_token(token)
.build()
.map_err(|e| McpError::Other(anyhow::anyhow!("Failed to create GitHub client: {e}")))?;
let per_page = args.per_page.map(|p| p.min(100) as u8);
let query = args.query.clone();
let mut issue_stream =
client.search_issues(args.query, args.sort, args.order, args.page, per_page);
let mut issues = Vec::new();
while let Some(result) = issue_stream.next().await {
let issue =
result.map_err(|e| McpError::Other(anyhow::anyhow!("GitHub API error: {e}")))?;
issues.push(issue);
}
let issue_summaries: Vec<GitHubIssueSummary> = issues
.iter()
.map(|issue| {
let state_str = match issue.state {
octocrab::models::IssueState::Open => "open",
octocrab::models::IssueState::Closed => "closed",
_ => "unknown",
};
let labels: Vec<String> = issue.labels.iter().map(|l| l.name.clone()).collect();
GitHubIssueSummary {
number: issue.number,
title: issue.title.clone(),
state: state_str.to_string(),
author: issue.user.login.clone(),
created_at: issue.created_at.to_rfc3339(),
labels,
}
})
.collect();
let output = GitHubSearchIssuesOutput {
success: true,
query: query.clone(),
total_count: issue_summaries.len() as u32,
items: issue_summaries,
};
let display = format!(
"GitHub Issues Search Results\n\nQuery: {}\nTotal Results: {}\nResults Returned: {}\n\nSearch completed successfully.",
query,
output.total_count,
output.items.len()
);
Ok(ToolResponse::new(display, output))
}
}