agenterra 0.1.1

Generate production-ready MCP (Model Context Protocol) servers and clients from OpenAPI specs
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
//! Template type definitions and discovery for AgentERRA.
//!
//! This module defines the supported template types and provides functionality
//! for discovering template directories in the filesystem. It supports both
//! built-in templates and custom template paths.
//!
//! # Examples
//!
//! ```
//! use agenterra_mcp::ServerTemplateKind;
//! use std::str::FromStr;
//!
//! // Parse a template from a string
//! let template = ServerTemplateKind::from_str("rust_axum").unwrap();
//! assert_eq!(template, ServerTemplateKind::RustAxum);
//! assert_eq!(template.as_str(), "rust_axum");
//!
//! // You can also use the Display trait
//! assert_eq!(template.to_string(), "rust_axum");
//!
//! // The default template is RustAxum
//! assert_eq!(ServerTemplateKind::default(), ServerTemplateKind::RustAxum);
//! ```
//!
//! For template directory discovery, use the `TemplateDir::discover()` method from the
//! `template_dir` module, which handles finding template directories automatically.
//!
//! # Template Discovery
//!
//! The module searches for templates in the following locations:
//! 1. Directory specified by `AGENTERRA_TEMPLATE_DIR` environment variable
//! 2. `templates/` directory in the project root (for development)
//! 3. `~/.agenterra/templates/` in the user's home directory
//! 4. `/usr/local/share/agenterra/templates/` for system-wide installation
//! 5. `./templates/` in the current working directory

// Internal imports (std, crate)
use std::fmt;
use std::str::FromStr;

/// Template role (server or client)
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum TemplateRole {
    /// Server-side template
    Server,
    /// Client-side template
    #[allow(dead_code)]
    Client,
}

impl TemplateRole {
    /// Returns the role as a string slice
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Server => "server",
            Self::Client => "client",
        }
    }
}

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

/// Server-side template kinds for MCP server generation
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum ServerTemplateKind {
    /// Rust with Axum web framework
    #[default]
    RustAxum,
    /// Python with FastAPI
    PythonFastAPI,
    /// TypeScript with Express
    TypeScriptExpress,
    /// Custom template path
    Custom,
}

/// Client-side template kinds for client library generation
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum ClientTemplateKind {
    /// Rust with reqwest HTTP client
    #[default]
    RustReqwest,
    /// Python with requests library
    PythonRequests,
    /// TypeScript with axios library
    TypeScriptAxios,
    /// Custom template path
    Custom,
}

impl FromStr for ServerTemplateKind {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_lowercase().as_str() {
            "rust_axum" => Ok(ServerTemplateKind::RustAxum),
            "python_fastapi" => Ok(ServerTemplateKind::PythonFastAPI),
            "typescript_express" => Ok(ServerTemplateKind::TypeScriptExpress),
            "custom" => Ok(ServerTemplateKind::Custom),
            _ => Err(format!("Unknown server template kind: {}", s)),
        }
    }
}

impl FromStr for ClientTemplateKind {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_lowercase().as_str() {
            "rust_reqwest" => Ok(ClientTemplateKind::RustReqwest),
            "python_requests" => Ok(ClientTemplateKind::PythonRequests),
            "typescript_axios" => Ok(ClientTemplateKind::TypeScriptAxios),
            "custom" => Ok(ClientTemplateKind::Custom),
            _ => Err(format!("Unknown client template kind: {}", s)),
        }
    }
}

impl ServerTemplateKind {
    /// Returns the template identifier as a string slice
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::RustAxum => "rust_axum",
            Self::PythonFastAPI => "python_fastapi",
            Self::TypeScriptExpress => "typescript_express",
            Self::Custom => "custom",
        }
    }

    /// Returns the template role (always server)
    pub fn role(&self) -> TemplateRole {
        TemplateRole::Server
    }

    /// Returns the language/framework name
    #[allow(dead_code)]
    pub fn framework(&self) -> &'static str {
        match self {
            Self::RustAxum => "rust",
            Self::PythonFastAPI => "python",
            Self::TypeScriptExpress => "typescript",
            Self::Custom => "custom",
        }
    }

    /// Returns an iterator over all available server template kinds
    #[allow(dead_code)]
    pub fn all() -> impl Iterator<Item = Self> {
        use ServerTemplateKind::*;
        [RustAxum, PythonFastAPI, TypeScriptExpress, Custom]
            .iter()
            .copied()
    }
}

impl ClientTemplateKind {
    /// Returns the template identifier as a string slice
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::RustReqwest => "rust_reqwest",
            Self::PythonRequests => "python_requests",
            Self::TypeScriptAxios => "typescript_axios",
            Self::Custom => "custom",
        }
    }

    /// Returns the template role (always client)
    #[allow(dead_code)]
    pub fn role(&self) -> TemplateRole {
        TemplateRole::Client
    }

    /// Returns the language/framework name
    #[allow(dead_code)]
    pub fn framework(&self) -> &'static str {
        match self {
            Self::RustReqwest => "rust",
            Self::PythonRequests => "python",
            Self::TypeScriptAxios => "typescript",
            Self::Custom => "custom",
        }
    }

    /// Returns an iterator over all available client template kinds
    #[allow(dead_code)]
    pub fn all() -> impl Iterator<Item = Self> {
        use ClientTemplateKind::*;
        [RustReqwest, PythonRequests, TypeScriptAxios, Custom]
            .iter()
            .copied()
    }
}

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

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

#[cfg(test)]
mod tests {
    use super::*;
    use std::collections::HashSet;

    // ServerTemplateKind tests
    #[test]
    fn test_server_as_str() {
        assert_eq!(ServerTemplateKind::RustAxum.as_str(), "rust_axum");
        assert_eq!(ServerTemplateKind::PythonFastAPI.as_str(), "python_fastapi");
        assert_eq!(
            ServerTemplateKind::TypeScriptExpress.as_str(),
            "typescript_express"
        );
        assert_eq!(ServerTemplateKind::Custom.as_str(), "custom");
    }

    #[test]
    fn test_server_display() {
        assert_eq!(format!("{}", ServerTemplateKind::RustAxum), "rust_axum");
        assert_eq!(
            format!("{}", ServerTemplateKind::PythonFastAPI),
            "python_fastapi"
        );
        assert_eq!(
            format!("{}", ServerTemplateKind::TypeScriptExpress),
            "typescript_express"
        );
        assert_eq!(format!("{}", ServerTemplateKind::Custom), "custom");
    }

    #[test]
    fn test_server_from_str() {
        assert_eq!(
            "rust_axum".parse::<ServerTemplateKind>().unwrap(),
            ServerTemplateKind::RustAxum
        );
        assert_eq!(
            "python_fastapi".parse::<ServerTemplateKind>().unwrap(),
            ServerTemplateKind::PythonFastAPI
        );
        assert_eq!(
            "typescript_express".parse::<ServerTemplateKind>().unwrap(),
            ServerTemplateKind::TypeScriptExpress
        );
        assert_eq!(
            "custom".parse::<ServerTemplateKind>().unwrap(),
            ServerTemplateKind::Custom
        );

        // Test case insensitivity
        assert_eq!(
            "RUST_AXUM".parse::<ServerTemplateKind>().unwrap(),
            ServerTemplateKind::RustAxum
        );

        // Test invalid variants
        assert!("invalid".parse::<ServerTemplateKind>().is_err());
        assert!("rust_reqwest".parse::<ServerTemplateKind>().is_err()); // Client template
    }

    #[test]
    fn test_server_default() {
        assert_eq!(ServerTemplateKind::default(), ServerTemplateKind::RustAxum);
    }

    #[test]
    fn test_server_all() {
        let all_kinds: Vec<_> = ServerTemplateKind::all().collect();
        assert_eq!(all_kinds.len(), 4);

        let unique_kinds: HashSet<_> = ServerTemplateKind::all().collect();
        assert_eq!(unique_kinds.len(), 4);

        assert!(unique_kinds.contains(&ServerTemplateKind::RustAxum));
        assert!(unique_kinds.contains(&ServerTemplateKind::PythonFastAPI));
        assert!(unique_kinds.contains(&ServerTemplateKind::TypeScriptExpress));
        assert!(unique_kinds.contains(&ServerTemplateKind::Custom));
    }

    #[test]
    fn test_server_role() {
        assert_eq!(ServerTemplateKind::RustAxum.role(), TemplateRole::Server);
        assert_eq!(
            ServerTemplateKind::PythonFastAPI.role(),
            TemplateRole::Server
        );
        assert_eq!(
            ServerTemplateKind::TypeScriptExpress.role(),
            TemplateRole::Server
        );
        assert_eq!(ServerTemplateKind::Custom.role(), TemplateRole::Server);
    }

    #[test]
    fn test_server_framework() {
        assert_eq!(ServerTemplateKind::RustAxum.framework(), "rust");
        assert_eq!(ServerTemplateKind::PythonFastAPI.framework(), "python");
        assert_eq!(
            ServerTemplateKind::TypeScriptExpress.framework(),
            "typescript"
        );
        assert_eq!(ServerTemplateKind::Custom.framework(), "custom");
    }

    // ClientTemplateKind tests
    #[test]
    fn test_client_as_str() {
        assert_eq!(ClientTemplateKind::RustReqwest.as_str(), "rust_reqwest");
        assert_eq!(
            ClientTemplateKind::PythonRequests.as_str(),
            "python_requests"
        );
        assert_eq!(
            ClientTemplateKind::TypeScriptAxios.as_str(),
            "typescript_axios"
        );
        assert_eq!(ClientTemplateKind::Custom.as_str(), "custom");
    }

    #[test]
    fn test_client_display() {
        assert_eq!(
            format!("{}", ClientTemplateKind::RustReqwest),
            "rust_reqwest"
        );
        assert_eq!(
            format!("{}", ClientTemplateKind::PythonRequests),
            "python_requests"
        );
        assert_eq!(
            format!("{}", ClientTemplateKind::TypeScriptAxios),
            "typescript_axios"
        );
        assert_eq!(format!("{}", ClientTemplateKind::Custom), "custom");
    }

    #[test]
    fn test_client_from_str() {
        assert_eq!(
            "rust_reqwest".parse::<ClientTemplateKind>().unwrap(),
            ClientTemplateKind::RustReqwest
        );
        assert_eq!(
            "python_requests".parse::<ClientTemplateKind>().unwrap(),
            ClientTemplateKind::PythonRequests
        );
        assert_eq!(
            "typescript_axios".parse::<ClientTemplateKind>().unwrap(),
            ClientTemplateKind::TypeScriptAxios
        );
        assert_eq!(
            "custom".parse::<ClientTemplateKind>().unwrap(),
            ClientTemplateKind::Custom
        );

        // Test case insensitivity
        assert_eq!(
            "RUST_REQWEST".parse::<ClientTemplateKind>().unwrap(),
            ClientTemplateKind::RustReqwest
        );

        // Test invalid variants
        assert!("invalid".parse::<ClientTemplateKind>().is_err());
        assert!("rust_axum".parse::<ClientTemplateKind>().is_err()); // Server template
    }

    #[test]
    fn test_client_default() {
        assert_eq!(
            ClientTemplateKind::default(),
            ClientTemplateKind::RustReqwest
        );
    }

    #[test]
    fn test_client_all() {
        let all_kinds: Vec<_> = ClientTemplateKind::all().collect();
        assert_eq!(all_kinds.len(), 4);

        let unique_kinds: HashSet<_> = ClientTemplateKind::all().collect();
        assert_eq!(unique_kinds.len(), 4);

        assert!(unique_kinds.contains(&ClientTemplateKind::RustReqwest));
        assert!(unique_kinds.contains(&ClientTemplateKind::PythonRequests));
        assert!(unique_kinds.contains(&ClientTemplateKind::TypeScriptAxios));
        assert!(unique_kinds.contains(&ClientTemplateKind::Custom));
    }

    #[test]
    fn test_client_role() {
        assert_eq!(ClientTemplateKind::RustReqwest.role(), TemplateRole::Client);
        assert_eq!(
            ClientTemplateKind::PythonRequests.role(),
            TemplateRole::Client
        );
        assert_eq!(
            ClientTemplateKind::TypeScriptAxios.role(),
            TemplateRole::Client
        );
        assert_eq!(ClientTemplateKind::Custom.role(), TemplateRole::Client);
    }

    #[test]
    fn test_client_framework() {
        assert_eq!(ClientTemplateKind::RustReqwest.framework(), "rust");
        assert_eq!(ClientTemplateKind::PythonRequests.framework(), "python");
        assert_eq!(
            ClientTemplateKind::TypeScriptAxios.framework(),
            "typescript"
        );
        assert_eq!(ClientTemplateKind::Custom.framework(), "custom");
    }

    // TemplateRole tests
    #[test]
    fn test_template_role_as_str() {
        assert_eq!(TemplateRole::Server.as_str(), "server");
        assert_eq!(TemplateRole::Client.as_str(), "client");
    }

    #[test]
    fn test_template_role_display() {
        assert_eq!(format!("{}", TemplateRole::Server), "server");
        assert_eq!(format!("{}", TemplateRole::Client), "client");
    }

    // Protocol-aware template tests (TDD Red phase)
    #[test]
    fn test_template_kind_with_protocol() {
        use crate::core::protocol::Protocol;

        // Test that template kinds can be combined with protocols
        let server_kind = ServerTemplateKind::RustAxum;
        let protocol = Protocol::Mcp;

        // This should construct a path like: templates/mcp/server/rust_axum
        let expected_path = format!(
            "templates/{}/{}/{}",
            protocol.path_segment(),
            server_kind.role().as_str(),
            server_kind.as_str()
        );

        assert_eq!(expected_path, "templates/mcp/server/rust_axum");

        // Test client templates too
        let client_kind = ClientTemplateKind::RustReqwest;
        let expected_client_path = format!(
            "templates/{}/{}/{}",
            protocol.path_segment(),
            client_kind.role().as_str(),
            client_kind.as_str()
        );

        assert_eq!(expected_client_path, "templates/mcp/client/rust_reqwest");
    }

    #[test]
    fn test_template_kind_path_construction() {
        use crate::core::protocol::Protocol;

        // Test that we can build template paths for different protocols and kinds
        let test_cases = vec![
            (
                Protocol::Mcp,
                ServerTemplateKind::RustAxum,
                "templates/mcp/server/rust_axum",
            ),
            (
                Protocol::Mcp,
                ServerTemplateKind::PythonFastAPI,
                "templates/mcp/server/python_fastapi",
            ),
            (
                Protocol::Mcp,
                ServerTemplateKind::Custom,
                "templates/mcp/server/custom",
            ),
        ];

        for (protocol, template_kind, expected_path) in test_cases {
            let path = format!(
                "templates/{}/{}/{}",
                protocol.path_segment(),
                template_kind.role().as_str(),
                template_kind.as_str()
            );
            assert_eq!(path, expected_path);
        }
    }
}