avalonia-mcp-tools 0.1.0

MCP tools for AvaloniaUI development assistance
Documentation
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
//! Project generator tool - Project scaffolding
//!
//! This tool generates new AvaloniaUI projects with proper structure and MVVM architecture.

use avalonia_mcp_core::error::AvaloniaMcpError;
use avalonia_mcp_core::markdown::MarkdownOutputBuilder;
use avalonia_mcp_services::async_file::AsyncFileService;
use rmcp::model::{CallToolResult, Content};
use rmcp::tool;
use serde::{Deserialize, Serialize};
use schemars::JsonSchema;

/// Project generator tool parameters
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub struct ProjectGeneratorParams {
    /// Project name
    pub name: String,
    /// Output directory path
    pub output_dir: String,
    /// Enable HTTP transport support
    pub enable_http: Option<bool>,
    /// Include test project
    pub include_tests: Option<bool>,
    /// Use ReactiveUI for MVVM
    pub use_reactiveui: Option<bool>,
}

/// Project generator tool for scaffolding new AvaloniaUI projects
#[derive(Debug, Clone, Default)]
pub struct ProjectGeneratorTool;

impl ProjectGeneratorTool {
    /// Create a new ProjectGeneratorTool instance
    pub fn new() -> Self {
        Self
    }

    /// Generate a new AvaloniaUI project
    #[tool(description = "Generate a new AvaloniaUI project with MVVM architecture. Creates a complete project structure with Cargo.toml, main.rs, and optional test project.")]
    pub async fn generate_project(
        &self,
        params: ProjectGeneratorParams,
    ) -> Result<CallToolResult, AvaloniaMcpError> {
        // Validate parameters
        if params.name.is_empty() {
            return Err(AvaloniaMcpError::validation(
                "Project name cannot be empty",
            ));
        }

        if params.output_dir.is_empty() {
            return Err(AvaloniaMcpError::validation(
                "Output directory cannot be empty",
            ));
        }

        let project_path = format!("{}/{}", params.output_dir, params.name);
        let enable_http = params.enable_http.unwrap_or(false);
        let include_tests = params.include_tests.unwrap_or(true);
        let use_reactiveui = params.use_reactiveui.unwrap_or(false);

        tracing::info!(
            name = %params.name,
            output_dir = %params.output_dir,
            enable_http,
            include_tests,
            use_reactiveui,
            "Generating AvaloniaUI project"
        );

        // Create directory structure
        self.create_directory_structure(&project_path, include_tests)
            .await?;

        // Generate Cargo.toml
        let cargo_toml = self.generate_cargo_toml(&params.name, enable_http, use_reactiveui);
        AsyncFileService::write_string(format!("{}/Cargo.toml", project_path), &cargo_toml)
            .await?;

        // Generate main.rs
        let main_rs = self.generate_main_rs(enable_http);
        AsyncFileService::write_string(format!("{}/src/main.rs", project_path), &main_rs)
            .await?;

        // Generate ViewModelBase if using ReactiveUI
        if use_reactiveui {
            let viewmodel_base = self.generate_viewmodel_base();
            AsyncFileService::write_string(
                format!("{}/src/ViewModelBase.rs", project_path),
                &viewmodel_base,
            )
            .await?;
        }

        // Generate test file if requested
        if include_tests {
            let test_content = self.generate_test_content(&params.name);
            AsyncFileService::write_string(
                format!("{}/tests/integration_tests.rs", project_path),
                &test_content,
            )
            .await?;
        }

        // Generate .gitignore
        let gitignore = self.generate_gitignore();
        AsyncFileService::write_string(format!("{}/.gitignore", project_path), &gitignore)
            .await?;

        // Build output
        let output = MarkdownOutputBuilder::new()
            .heading(1, &format!("Project {} Created", params.name))
            .paragraph("A new AvaloniaUI MCP project has been successfully generated!")
            .heading(2, "Project Structure")
            .code_block("text", &self.format_project_tree(&project_path, include_tests))
            .heading(2, "Next Steps")
            .numbered_list(vec![
                format!("cd {}", project_path),
                "cargo build".to_string(),
                "cargo run".to_string(),
                if include_tests { "cargo test".to_string() } else { "".to_string() },
            ])
            .heading(2, "Features Enabled")
            .task_list(vec![
                (true, "MVVM Architecture"),
                (enable_http, "HTTP Transport Support"),
                (include_tests, "Test Project"),
                (use_reactiveui, "ReactiveUI Integration"),
            ])
            .build();

        Ok(CallToolResult::success(vec![Content::text(output)]))
    }

    /// Create directory structure
    async fn create_directory_structure(
        &self,
        project_path: &str,
        include_tests: bool,
    ) -> Result<(), AvaloniaMcpError> {
        AsyncFileService::create_dir_all(format!("{}/src", project_path)).await?;

        if include_tests {
            AsyncFileService::create_dir_all(format!("{}/tests", project_path)).await?;
        }

        Ok(())
    }

    /// Generate Cargo.toml content
    fn generate_cargo_toml(
        &self,
        name: &str,
        enable_http: bool,
        use_reactiveui: bool,
    ) -> String {
        let http_deps = if enable_http {
            r#"
# HTTP Transport
axum = "0.8"
tower = "0.5"
tower-http = { version = "0.6", features = ["trace"] }
"#
        } else {
            ""
        };

        let reactiveui_deps = if use_reactiveui {
            r#"
# ReactiveUI for MVVM
reactive-macro = "0.5"
"#
        } else {
            ""
        };

        format!(
            r#"[package]
name = "{}"
version = "0.1.0"
edition = "2021"
rust-version = "1.85"

[dependencies]
# MCP Protocol
rmcp = {{ version = "0.11.0", features = ["reqwest", "transport-io", "uuid"] }}

# Async Runtime
tokio = {{ version = "1.48", features = ["full"] }}

# Serialization
serde = {{ version = "1.0", features = ["derive"] }}
serde_json = "1.0"

# Logging
tracing = "0.1"
tracing-subscriber = {{ version = "0.3", features = ["env-filter"] }}

# Error Handling
anyhow = "1.0"
thiserror = "2.0"
{}{}
"#,
            name, http_deps, reactiveui_deps
        )
    }

    /// Generate main.rs content
    fn generate_main_rs(&self, enable_http: bool) -> String {
        if enable_http {
            r#"//! AvaloniaUI MCP Server with HTTP transport

use anyhow::Result;
use rmcp::{ServiceExt, transport::stdio};
use tracing_subscriber::{layer::SubscriberExt, Registry};
use tracing_subscriber::filter::EnvFilter;

#[tokio::main]
async fn main() -> Result<()> {
    // Initialize logging
    Registry::default()
        .with(EnvFilter::from_default_env().add_directive(tracing::Level::INFO.into()))
        .with(tracing_subscriber::fmt::layer()
            .with_writer(std::io::stderr)
            .with_ansi(false))
        .init();

    tracing::info!("Starting AvaloniaUI MCP Server");

    // Create and serve MCP server
    let service = MyMcpServer::new()
        .serve(stdio())
        .await?;

    service.waiting().await?;
    Ok(())
}

// TODO: Define your MCP server with tools
#[derive(Clone)]
struct MyMcpServer;

impl MyMcpServer {
    fn new() -> Self {
        Self
    }
}
"#
            .to_string()
        } else {
            r#"//! AvaloniaUI MCP Server with STDIO transport

use anyhow::Result;
use rmcp::{ServiceExt, transport::stdio};
use tracing_subscriber::{layer::SubscriberExt, Registry};
use tracing_subscriber::filter::EnvFilter;

#[tokio::main]
async fn main() -> Result<()> {
    // Initialize logging to stderr
    Registry::default()
        .with(EnvFilter::from_default_env())
        .with(tracing_subscriber::fmt::layer()
            .with_writer(std::io::stderr)
            .with_ansi(false))
        .init();

    tracing::info!("Starting AvaloniaUI MCP Server (STDIO)");

    // Create and serve MCP server
    let service = MyMcpServer::new()
        .serve(stdio())
        .await?;

    service.waiting().await?;
    Ok(())
}

// TODO: Define your MCP server with tools
#[derive(Clone)]
struct MyMcpServer;

impl MyMcpServer {
    fn new() -> Self {
        Self
    }
}
"#
            .to_string()
        }
    }

    /// Generate ViewModelBase for ReactiveUI
    fn generate_viewmodel_base(&self) -> String {
        r#"//! ViewModelBase for ReactiveUI MVVM pattern

use reactive_macro::viewModel;

/// Base class for all ViewModels with INotifyPropertyChanged support
#[viewModel]
pub struct ViewModelBase {
    // Add common ViewModel properties here
}
"#
        .to_string()
    }

    /// Generate test content
    fn generate_test_content(&self, name: &str) -> String {
        format!(
            r#"//! Integration tests for {}

#[cfg(test)]
mod tests {{
    #[tokio::test]
    async fn test_app_starts() {{
        // TODO: Add integration tests
        assert!(true);
    }}
}}
"#,
            name
        )
    }

    /// Generate .gitignore
    fn generate_gitignore(&self) -> String {
        r#"# Rust
/target/
**/*.rs.bk
Cargo.lock

# Build
/dist/
/build/

# IDE
.idea/
.vscode/
*.swp
*.swo

# Environment
.env
.env.local

# Logs
*.log
logs/

# OS
.DS_Store
Thumbs.db
"#
        .to_string()
    }

    /// Format project tree for display
    fn format_project_tree(&self, project_path: &str, include_tests: bool) -> String {
        if include_tests {
            format!(
                r#"{}
├── Cargo.toml
├── .gitignore
├── src/
│   ├── main.rs
│   └── ViewModelBase.rs (if using ReactiveUI)
└── tests/
    └── integration_tests.rs
"#,
                project_path
            )
        } else {
            format!(
                r#"{}
├── Cargo.toml
├── .gitignore
└── src/
    └── main.rs
"#,
                project_path
            )
        }
    }
}

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

    #[test]
    fn test_generate_cargo_toml() {
        let tool = ProjectGeneratorTool::new();
        let cargo_toml = tool.generate_cargo_toml("test_project", false, false);

        assert!(cargo_toml.contains("name = \"test_project\""));
        assert!(cargo_toml.contains("rmcp"));
        assert!(cargo_toml.contains("tokio"));
        assert!(!cargo_toml.contains("axum")); // HTTP not enabled
    }

    #[test]
    fn test_generate_cargo_toml_with_http() {
        let tool = ProjectGeneratorTool::new();
        let cargo_toml = tool.generate_cargo_toml("test_project", true, false);

        assert!(cargo_toml.contains("axum"));
        assert!(cargo_toml.contains("tower"));
    }

    #[test]
    fn test_generate_gitignore() {
        let tool = ProjectGeneratorTool::new();
        let gitignore = tool.generate_gitignore();

        assert!(gitignore.contains("/target/"));
        assert!(gitignore.contains("Cargo.lock"));
    }
}