agenterra 0.1.3

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
# Agenterra Template System

This document describes the template system used by Agenterra for code generation from OpenAPI specifications.

## Table of Contents
- [Template Structure]#template-structure
- [Manifest Format]#manifest-format
- [Available Template Variables]#available-template-variables
- [Example Templates]#example-templates
- [Template Context]#template-context
- [Conditional Logic]#conditional-logic
- [Including Other Templates]#including-other-templates
- [Built-in Filters]#built-in-filters

## Template Structure

Templates are organized hierarchically by protocol and role:

```
templates/
├── mcp/                    # Model Context Protocol templates
│   ├── server/             # MCP server templates
│   │   └── rust_axum/      # Rust Axum server template
│   │       ├── manifest.yml
│   │       ├── Cargo.toml.tera
│   │       ├── src/
│   │       │   ├── main.rs.tera
│   │       │   ├── server.rs.tera
│   │       │   └── handlers/
│   │       │       └── mod.rs.tera
│   │       └── README.md.tera
│   └── client/             # MCP client templates
│       └── rust_reqwest/   # Rust reqwest client template
│           ├── manifest.yml
│           ├── Cargo.toml.tera
│           ├── src/
│           │   ├── main.rs.tera
│           │   ├── client.rs.tera
│           │   └── repl.rs.tera
│           └── README.md.tera
└── future-protocols/       # Space for future protocol templates
    ├── a2a/               # Agent-to-Agent protocol (planned)
    └── custom/            # Custom protocol templates
```

Each template directory contains:
- `manifest.yml` - Required template manifest
- `*.tera` - Template files using Tera templating engine
- Subdirectories for organized template structure

## Template Types

### Server Templates
Server templates generate MCP servers from OpenAPI specifications. They require:
- OpenAPI schema as input
- Base URL configuration
- Server-specific options (port, logging)

**Available Server Templates:**
- `rust_axum` - Rust MCP server using Axum web framework with rmcp protocol support

### Client Templates
Client templates generate MCP clients that can connect to MCP servers. They:
- Don't require OpenAPI schemas (discover tools at runtime)
- Focus on connection management and tool invocation
- Often include REPL or CLI interfaces

**Available Client Templates:**
- `rust_reqwest` - Rust MCP client with REPL interface using rmcp protocol

## Manifest Format

The `manifest.yml` file defines the template's metadata and configuration:

```yaml
name: "rust_reqwest"     # Required: Template name
version: "0.1.0"         # Required: Template version  
description: >           # Optional: Template description
  Rust MCP client using reqwest HTTP client and rmcp protocol
language: "rust"         # Optional: Programming language
author: "Agenterra Team" # Optional: Author information

# Template options (client templates typically have fewer options)
options:
  # Client configuration
  client:
    timeout: 10           # Default connection timeout
    repl_enabled: true    # Include REPL interface
  
  # For server templates, you might have:
  # server:
  #   port: 8080          # Default server port
  #   log_file: app.log   # Default log file path
  #   all_operations: true # Include all operations

# Template files configuration
files:
  - source: "Cargo.toml.tera"
    destination: "Cargo.toml"
    
  - source: "src/main.rs.tera"
    destination: "src/main.rs"
    
  - source: "src/client.rs.tera"
    destination: "src/client.rs"
    context:              # Optional: Additional context for this file
      is_client: true
    
  - source: "README.md.tera"
    destination: "README.md"

# Hooks (optional)
hooks:
  post_generate: hooks/post-generate.sh  # Script to run after generation
```

## Available Template Variables

### Global Variables

| Variable           | Type     | Description                                      |
|--------------------|----------|--------------------------------------------------|
| `project_name`    | String   | Name of the generated project                    |
| `api_version`     | String   | API version from OpenAPI spec                    |
| `spec`            | Object   | The complete OpenAPI specification object        |
| `endpoints`       | Array    | List of endpoint contexts (see below)            |
| `current_time`    | DateTime | Current date and time                            |
| `template_opts`   | Object   | Template options from manifest                   |

### Endpoint Context

Each endpoint in the `endpoints` array has the following structure:

```rust
{
  endpoint: String,           // e.g., "get_pets"
  endpoint_cap: String,       // e.g., "GET_PETS"
  fn_name: String,           // e.g., "get_pets"
  parameters_type: String,   // e.g., "GetPetsParams"
  properties_type: String,   // e.g., "PetProperties"
  response_type: String,     // e.g., "PetResponse"
  envelope_properties: Value, // JSON schema of response properties
  properties: Vec<PropertyInfo>,
  properties_for_handler: Vec<String>,
  parameters: Vec<ParameterInfo>,
  summary: String,
  description: String,
  tags: Vec<String>,
  properties_schema: Map<String, Value>,
  response_schema: Value,
  spec_file_name: Option<String>,
  valid_fields: Vec<String>
}
```

### PropertyInfo

```rust
struct PropertyInfo {
    name: String,
    rust_type: String,
    title: Option<String>,
    description: Option<String>,
    example: Option<Value>
}
```

### ParameterInfo

```rust
struct ParameterInfo {
    name: String,
    rust_type: String,
    description: Option<String>,
    example: Option<Value>
}
```

## Example Templates

### MCP Client Template Example (`client.rs.tera`)

```rust
// Generated by Agenterra - {{ current_time }}
// MCP Client for {{ project_name | default(value="MCP Server") }}

use anyhow::{Result, Context};
use rmcp::{Client, ToolCall, ToolResult};
use serde_json::Value;
use std::collections::HashMap;
use std::time::Duration;

pub struct McpClient {
    client: Option<Client>,
    server_url: String,
    timeout: Duration,
    tools: HashMap<String, Value>,
}

impl McpClient {
    pub async fn new(server_url: &str, timeout_secs: u64) -> Result<Self> {
        Ok(Self {
            client: None,
            server_url: server_url.to_string(),
            timeout: Duration::from_secs(timeout_secs),
            tools: HashMap::new(),
        })
    }

    pub async fn connect(&mut self) -> Result<()> {
        let client = Client::stdio()
            .context("Failed to create stdio MCP client")?;
        self.client = Some(client);
        self.discover_tools().await?;
        Ok(())
    }

    async fn discover_tools(&mut self) -> Result<()> {
        // Tool discovery implementation
        Ok(())
    }
}
```

### MCP Server Template Example (`handlers/mod.rs.tera`)

```rust
// Generated by Agenterra - {{ current_time }}
// MCP Handlers for {{ project_name | default(value="MCP Server") }}

use rmcp::McpService;
use serde_json::Value;

pub struct McpServer {
    // Server state
}

#[rmcp::async_trait]
impl McpService for McpServer {
    async fn list_tools(&self) -> rmcp::Result<Vec<rmcp::Tool>> {
        let tools = vec![
            {% for endpoint in endpoints %}
            rmcp::Tool {
                name: "{{ endpoint.fn_name }}".to_string(),
                description: Some("{{ endpoint.summary }}".to_string()),
                input_schema: serde_json::json!({
                    "type": "object",
                    "properties": {
                        {% for param in endpoint.parameters %}
                        "{{ param.name }}": {
                            "type": "{{ param.rust_type | type_rs }}",
                            "description": "{{ param.description | default(value="") }}"
                        }{% if not loop.last %},{% endif %}
                        {% endfor %}
                    }
                }),
            },
            {% endfor %}
        ];
        Ok(tools)
    }

    async fn call_tool(&self, call: rmcp::ToolCall) -> rmcp::Result<Vec<rmcp::ToolResult>> {
        match call.name.as_str() {
            {% for endpoint in endpoints %}
            "{{ endpoint.fn_name }}" => {
                // Implementation for {{ endpoint.fn_name }}
                Ok(vec![rmcp::ToolResult {
                    content: vec![rmcp::TextContent {
                        text: "Result from {{ endpoint.fn_name }}".to_string(),
                    }.into()],
                    is_error: Some(false),
                }])
            }
            {% endfor %}
            _ => Err(rmcp::McpError::MethodNotFound(call.name)),
        }
    }
}
```

### REPL Template Example (`repl.rs.tera`)

```rust
// Generated by Agenterra - {{ current_time }}
// REPL interface for {{ project_name | default(value="MCP Client") }}

use anyhow::Result;
use rustyline::{Editor, Result as RustylineResult};
use crate::client::McpClient;

pub struct McpRepl {
    client: McpClient,
    editor: Editor<()>,
}

impl McpRepl {
    pub fn new(client: McpClient) -> Self {
        let editor = Editor::new();
        Self { client, editor }
    }

    pub async fn run(&mut self) -> Result<()> {
        println!("🚀 {{ project_name | default(value="MCP Client") }} REPL");
        println!("Type 'help' for available commands, 'quit' to exit");
        
        loop {
            match self.editor.readline("> ") {
                Ok(line) => {
                    self.editor.add_history_entry(&line);
                    
                    match line.trim() {
                        "quit" | "exit" => break,
                        "help" => self.show_help(),
                        "tools" => self.list_tools().await?,
                        cmd if cmd.starts_with("call ") => {
                            let tool_name = &cmd[5..];
                            self.call_tool(tool_name).await?;
                        }
                        _ => println!("Unknown command: {}", line.trim()),
                    }
                }
                Err(_) => break,
            }
        }
        
        Ok(())
    }

    fn show_help(&self) {
        println!("Available commands:");
        println!("  tools           - List available tools");
        println!("  call <tool>     - Call a specific tool");
        println!("  help            - Show this help");
        println!("  quit/exit       - Exit the REPL");
    }

    async fn list_tools(&mut self) -> Result<()> {
        let tools = self.client.get_tools();
        if tools.is_empty() {
            println!("No tools available");
        } else {
            println!("Available tools:");
            for (name, tool) in tools {
                println!("  {} - {}", name, tool.description.as_deref().unwrap_or("No description"));
            }
        }
        Ok(())
    }

    async fn call_tool(&mut self, tool_name: &str) -> Result<()> {
        println!("Calling tool: {}", tool_name);
        // Tool invocation implementation
        Ok(())
    }
}
```

## Template Context

Templates have access to a rich context that includes:

1. **Global Context**: Available in all templates
   - `project_name`: Name of the generated project
   - `api_version`: Version from the OpenAPI spec
   - `current_time`: Timestamp of generation
   - `template_opts`: Options from the manifest

2. **Server-Specific Context**: Server templates get:
   - `endpoints`: Array of API endpoints from OpenAPI spec  
   - `spec`: Complete OpenAPI specification
   - All global context variables

3. **Client-Specific Context**: Client templates get:
   - No endpoint or spec data (clients discover at runtime)
   - Focus on connection and tool invocation patterns
   - All global context variables

## Conditional Logic

You can use Tera's control structures for conditional generation:

```jinja
{% if endpoint.tags contains "admin" %}
// This is an admin-only endpoint
#[requires_role("admin")]
{% endif %}
```

## Including Other Templates

Use Tera's `include` to reuse template fragments:

```jinja
{% include "common/header.tera" %}

// Your template content here

{% include "common/footer.tera" %}
```

## Built-in Filters

Agenterra includes several useful Tera filters:

- `camel_case`: Convert string to camelCase
- `pascal_case`: Convert string to PascalCase
- `snake_case`: Convert string to snake_case
- `kebab_case`: Convert string to kebab-case
- `json_encode`: Convert value to JSON string
- `type_rs`: Convert OpenAPI type to Rust type

Example:
```jinja
{{ "user_name" | snake_case }}  // user_name
{{ "user_name" | camel_case }}  // userName
{{ "user_name" | pascal_case }} // UserName
{{ "user_name" | kebab_case }}  // user-name
{{ endpoint.parameters | json_encode | safe }}
{{ "string" | type_rs }}  // String
```

## Creating Custom Templates

### Custom Server Template

To create a custom server template:

1. Create directory structure:
   ```
   templates/mcp/server/my_custom_server/
   ├── manifest.yml
   ├── Cargo.toml.tera
   └── src/
       ├── main.rs.tera
       └── lib.rs.tera
   ```

2. Use the template:
   ```bash
   agenterra scaffold mcp server --template my_custom_server --schema-path api.yaml
   ```

### Custom Client Template

To create a custom client template:

1. Create directory structure:
   ```
   templates/mcp/client/my_custom_client/
   ├── manifest.yml
   ├── package.json.tera  # For Node.js client
   └── src/
       ├── index.ts.tera
       └── client.ts.tera
   ```

2. Use the template:
   ```bash
   agenterra scaffold mcp client --template my_custom_client --project-name my-client
   ```

## Best Practices

1. **Organize by protocol and role**: Follow the `templates/{protocol}/{role}/{template}` structure
2. **Keep templates focused**: Server templates handle OpenAPI, client templates handle MCP communication
3. **Use includes**: Break down large templates into smaller, reusable components
4. **Document templates**: Add comments explaining non-obvious parts
5. **Test thoroughly**: Generate code and verify it compiles and works
6. **Handle optional fields**: Always check if fields exist before accessing them
7. **Consider the target use case**: Servers need robustness, clients need usability

## Template Context Differences

### Server Templates
Server templates receive:
- `endpoints` - Array of API endpoints from OpenAPI spec
- `spec` - Complete OpenAPI specification
- `api_version` - API version from spec
- `project_name` - Generated project name

### Client Templates  
Client templates receive:
- `project_name` - Generated project name
- `template_opts` - Template options from manifest
- `current_time` - Generation timestamp
- No `endpoints` or `spec` (clients discover tools at runtime)

## Troubleshooting

- **Missing variables**: Check if you're using server-specific variables in client templates
- **Template errors**: Check Tera's error messages for syntax issues
- **Incorrect output**: Verify your template logic and variable usage
- **Template not found**: Ensure template is in correct `templates/mcp/{role}/` directory
- **Compilation errors**: Verify generated code syntax and imports