File size: 7,448 Bytes
e5034c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
use std::fmt::Display;

use regex::Regex;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
#[serde(transparent)]
pub struct ToolName(String);

impl ToolName {
    pub fn new(value: impl ToString) -> Self {
        ToolName(value.to_string())
    }

    /// Transforms the tool_name to remove whitespaces and converts to
    /// lower_snake_case
    pub fn sanitized(input: &str) -> Self {
        // Convert to lowercase
        let input = input.to_lowercase();

        // Replace all non-alphanumeric characters (excluding underscore) with
        // underscores
        let re_special = Regex::new(r"[^a-z0-9_]+").unwrap();
        let cleaned = re_special.replace_all(&input, "_");

        // Remove leading/trailing underscores and collapse consecutive underscores
        let re_trimmed = Regex::new(r"_+").unwrap();

        let sanitized_str = re_trimmed
            .replace_all(&cleaned, "_")
            .trim_matches('_')
            .to_string();

        Self(sanitized_str)
    }
}

impl ToolName {
    pub fn into_string(self) -> String {
        self.0
    }

    pub fn as_str(&self) -> &str {
        &self.0
    }

    pub fn into_sanitized(self) -> Self {
        ToolName::sanitized(self.0.as_str())
    }

    /// Converts a Claude Code format MCP tool name (`mcp__{server}__{tool}`) to
    /// Forge's internal legacy format (`mcp_{server}_tool_{tool}`).
    ///
    /// Returns `None` if the name does not match the Claude Code MCP format.
    /// Sanitized names never contain `__`, so the first `__` is always the
    /// server/tool separator.
    pub fn to_legacy_mcp_name(&self) -> Option<ToolName> {
        let rest = self.0.strip_prefix("mcp__")?;
        let (server, tool) = rest.split_once("__")?;
        Some(ToolName::new(format!("mcp_{server}_tool_{tool}")))
    }
}

impl From<String> for ToolName {
    fn from(value: String) -> Self {
        ToolName::new(value)
    }
}

impl From<&str> for ToolName {
    fn from(value: &str) -> Self {
        ToolName::new(value)
    }
}

pub trait NamedTool {
    fn tool_name() -> ToolName;
}

impl Display for ToolName {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}

#[cfg(test)]
mod tests {
    use pretty_assertions::assert_eq;

    use super::*;

    #[test]
    fn test_sanitize_camel_case() {
        let tool_name = ToolName::new("camelCase");
        let actual = tool_name.into_sanitized();
        let expected = ToolName::new("camelcase");
        assert_eq!(actual, expected);
    }

    #[test]
    fn test_sanitize_pascal_case() {
        let tool_name = ToolName::new("PascalCase");
        let actual = tool_name.into_sanitized();
        let expected = ToolName::new("pascalcase");
        assert_eq!(actual, expected);
    }

    #[test]
    fn test_sanitize_mixed_case_with_numbers() {
        let tool_name = ToolName::new("myTool2Name");
        let actual = tool_name.into_sanitized();
        let expected = ToolName::new("mytool2name");
        assert_eq!(actual, expected);
    }

    #[test]
    fn test_sanitize_special_characters() {
        let tool_name = ToolName::new("tool-name@with#special$chars");
        let actual = tool_name.into_sanitized();
        let expected = ToolName::new("tool_name_with_special_chars");
        assert_eq!(actual, expected);
    }

    #[test]
    fn test_sanitize_whitespace() {
        let tool_name = ToolName::new("tool name with spaces");
        let actual = tool_name.into_sanitized();
        let expected = ToolName::new("tool_name_with_spaces");
        assert_eq!(actual, expected);
    }

    #[test]
    fn test_sanitize_consecutive_special_chars() {
        let tool_name = ToolName::new("tool---name___with@@@special");
        let actual = tool_name.into_sanitized();
        let expected = ToolName::new("tool_name_with_special");
        assert_eq!(actual, expected);
    }

    #[test]
    fn test_sanitize_leading_trailing_special_chars() {
        let tool_name = ToolName::new("___tool_name___");
        let actual = tool_name.into_sanitized();
        let expected = ToolName::new("tool_name");
        assert_eq!(actual, expected);
    }

    #[test]
    fn test_sanitize_already_snake_case() {
        let tool_name = ToolName::new("already_snake_case");
        let actual = tool_name.into_sanitized();
        let expected = ToolName::new("already_snake_case");
        assert_eq!(actual, expected);
    }

    #[test]
    fn test_sanitize_uppercase_letters() {
        let tool_name = ToolName::new("UPPERCASE_TOOL_NAME");
        let actual = tool_name.into_sanitized();
        let expected = ToolName::new("uppercase_tool_name");
        assert_eq!(actual, expected);
    }

    #[test]
    fn test_sanitize_numbers_only() {
        let tool_name = ToolName::new("123456");
        let actual = tool_name.into_sanitized();
        let expected = ToolName::new("123456");
        assert_eq!(actual, expected);
    }

    #[test]
    fn test_sanitize_mixed_numbers_and_letters() {
        let tool_name = ToolName::new("tool1Name2Test3");
        let actual = tool_name.into_sanitized();
        let expected = ToolName::new("tool1name2test3");
        assert_eq!(actual, expected);
    }

    #[test]
    fn test_sanitize_empty_string() {
        let tool_name = ToolName::new("");
        let actual = tool_name.into_sanitized();
        let expected = ToolName::new("");
        assert_eq!(actual, expected);
    }

    #[test]
    fn test_sanitize_only_special_chars() {
        let tool_name = ToolName::new("@#$%^&*()");
        let actual = tool_name.into_sanitized();
        let expected = ToolName::new("");
        assert_eq!(actual, expected);
    }

    #[test]
    fn test_sanitize_complex_mixed_case() {
        let tool_name = ToolName::new("XMLHttpRequest2Handler");
        let actual = tool_name.into_sanitized();
        let expected = ToolName::new("xmlhttprequest2handler");
        assert_eq!(actual, expected);
    }

    #[test]
    fn test_sanitize_dots_and_slashes() {
        let tool_name = ToolName::new("tool.name/with.dots");
        let actual = tool_name.into_sanitized();
        let expected = ToolName::new("tool_name_with_dots");
        assert_eq!(actual, expected);
    }

    #[test]
    fn test_sanitize_single_underscore_preserved() {
        let tool_name = ToolName::new("tool_name");
        let actual = tool_name.into_sanitized();
        let expected = ToolName::new("tool_name");
        assert_eq!(actual, expected);
    }

    #[test]
    fn test_sanitize_camel_case_with_underscore() {
        let tool_name = ToolName::new("camelCase_withUnderscore");
        let actual = tool_name.into_sanitized();
        let expected = ToolName::new("camelcase_withunderscore");
        assert_eq!(actual, expected);
    }

    #[test]
    fn test_sanitize_numbers_between_letters() {
        let tool_name = ToolName::new("tool1tool2tool3");
        let actual = tool_name.into_sanitized();
        let expected = ToolName::new("tool1tool2tool3");
        assert_eq!(actual, expected);
    }

    #[test]
    fn test_sanitize_mixed_case_preserves_numbers() {
        let tool_name = ToolName::new("Test123Case");
        let actual = tool_name.into_sanitized();
        let expected = ToolName::new("test123case");
        assert_eq!(actual, expected);
    }
}