Skip to main content

claude_rust_tools/infrastructure/
skill_tool.rs

1use claude_rust_errors::AppResult;
2use claude_rust_types::{PermissionLevel, SearchReadInfo, Tool};
3use serde_json::{Value, json};
4
5/// Tool to invoke a skill by reading its SKILL.md file.
6pub struct SkillTool;
7
8impl SkillTool {
9    pub fn new() -> Self {
10        Self
11    }
12}
13
14#[async_trait::async_trait]
15impl Tool for SkillTool {
16    fn name(&self) -> &str {
17        "skill"
18    }
19
20    fn description(&self) -> &str {
21        "Invoke a skill by name. Reads the skill definition from ~/.claude/skills/{name}/SKILL.md."
22    }
23
24    fn input_schema(&self) -> Value {
25        json!({
26            "type": "object",
27            "properties": {
28                "skill": {
29                    "type": "string",
30                    "description": "The skill name to invoke"
31                },
32                "args": {
33                    "type": "string",
34                    "description": "Optional arguments for the skill"
35                }
36            },
37            "required": ["skill"]
38        })
39    }
40
41    fn permission_level(&self) -> PermissionLevel {
42        PermissionLevel::ReadOnly
43    }
44
45    fn is_read_only(&self, _input: &Value) -> bool { true }
46    fn is_concurrent_safe(&self, _input: &Value) -> bool { true }
47
48    fn is_search_or_read_command(&self, _input: &Value) -> SearchReadInfo {
49        SearchReadInfo { is_search: false, is_read: true, is_list: false }
50    }
51
52    async fn execute(&self, input: Value) -> AppResult<String> {
53        let skill = input
54            .get("skill")
55            .and_then(|v| v.as_str())
56            .ok_or_else(|| claude_rust_errors::AppError::Tool("missing 'skill' field".into()))?;
57
58        let args = input
59            .get("args")
60            .and_then(|v| v.as_str())
61            .unwrap_or("");
62
63        tracing::info!(skill, args, "invoking skill");
64
65        let home = claude_rust_config::home_dir()
66            .map(|p| p.to_string_lossy().to_string())
67            .ok_or_else(|| claude_rust_errors::AppError::Tool("cannot determine home directory".into()))?;
68
69        let skill_path = format!("{home}/.claude/skills/{skill}/SKILL.md");
70
71        let content = tokio::fs::read_to_string(&skill_path)
72            .await
73            .map_err(|e| claude_rust_errors::AppError::Tool(
74                format!("skill '{skill}' not found at {skill_path}: {e}")
75            ))?;
76
77        if args.is_empty() {
78            Ok(content)
79        } else {
80            Ok(format!("{content}\n\n---\nArgs: {args}"))
81        }
82    }
83}