mcp-execution-server 0.8.0

MCP server for progressive loading TypeScript code generation
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
//! Integration tests for skill generation.

use mcp_execution_core::metadata::{
    METADATA_FILE_NAME, METADATA_SCHEMA_VERSION, ParameterMetadata, ServerMetadata, ToolMetadata,
};
use mcp_execution_skill::{ParsedToolFile, ScanError, build_skill_context, scan_tools_directory};
use tempfile::TempDir;
use tokio::fs;

/// Build a `ServerMetadata` sidecar with `count` simple test tools, each with one
/// required string parameter, and write it as `_meta.json` into `dir`.
///
/// Also writes a matching stub `{typescript_name}.ts` file for each tool, since
/// `scan_tools_directory` now cross-checks the sidecar against files on disk.
async fn write_metadata_sidecar(dir: &std::path::Path, tool_names: &[&str]) {
    let meta = ServerMetadata {
        schema_version: METADATA_SCHEMA_VERSION,
        server_id: "test".to_string(),
        server_name: "Test Server".to_string(),
        server_version: "1.0.0".to_string(),
        tools: tool_names
            .iter()
            .map(|name| ToolMetadata {
                name: (*name).to_string(),
                typescript_name: to_camel_case(name),
                category: Some("test-category".to_string()),
                keywords: vec!["test".to_string(), (*name).to_string()],
                description: Some(format!("Test tool: {name}")),
                parameters: vec![
                    ParameterMetadata {
                        name: "required_param".to_string(),
                        typescript_type: "string".to_string(),
                        required: true,
                        description: Some("A required parameter".to_string()),
                    },
                    ParameterMetadata {
                        name: "optional_param".to_string(),
                        typescript_type: "number".to_string(),
                        required: false,
                        description: None,
                    },
                ],
            })
            .collect(),
    };

    let content = serde_json::to_string_pretty(&meta).unwrap();
    fs::write(dir.join(METADATA_FILE_NAME), content)
        .await
        .unwrap();

    for tool in &meta.tools {
        fs::write(
            dir.join(format!("{}.ts", tool.typescript_name)),
            "export {}",
        )
        .await
        .unwrap();
    }
}

fn to_camel_case(s: &str) -> String {
    let mut parts = s.split('_');
    let Some(first) = parts.next() else {
        return String::new();
    };
    let mut result = first.to_string();
    for part in parts {
        let mut chars = part.chars();
        if let Some(c) = chars.next() {
            result.push(c.to_ascii_uppercase());
            result.push_str(chars.as_str());
        }
    }
    result
}

#[tokio::test]
async fn test_scan_tools_directory_integration() {
    let temp_dir = TempDir::new().unwrap();
    let dir = temp_dir.path();

    write_metadata_sidecar(dir, &["create_issue", "list_repos", "get_user"]).await;

    let scan_result = scan_tools_directory(dir).await.unwrap();
    let tools = scan_result.tools;

    assert_eq!(tools.len(), 3);
    assert!(tools.iter().any(|t| t.name == "create_issue"));
    assert!(tools.iter().any(|t| t.name == "list_repos"));
    assert!(tools.iter().any(|t| t.name == "get_user"));

    let create_issue = tools.iter().find(|t| t.name == "create_issue").unwrap();
    assert_eq!(create_issue.server_id, "test");
    assert_eq!(create_issue.category, Some("test-category".to_string()));
    assert_eq!(create_issue.parameters.len(), 2);

    let required_count = create_issue
        .parameters
        .iter()
        .filter(|p| p.required)
        .count();
    let optional_count = create_issue
        .parameters
        .iter()
        .filter(|p| !p.required)
        .count();
    assert_eq!(required_count, 1);
    assert_eq!(optional_count, 1);

    // Issue #141 regression: parameter descriptions must survive the round-trip.
    let required = create_issue
        .parameters
        .iter()
        .find(|p| p.name == "required_param")
        .unwrap();
    assert_eq!(
        required.description,
        Some("A required parameter".to_string())
    );
}

#[tokio::test]
async fn test_build_skill_context_integration() {
    let tools = vec![
        ParsedToolFile {
            name: "create_issue".to_string(),
            typescript_name: "createIssue".to_string(),
            server_id: "github".to_string(),
            category: Some("issues".to_string()),
            keywords: vec!["create".to_string(), "issue".to_string()],
            description: Some("Create a new issue".to_string()),
            parameters: vec![],
        },
        ParsedToolFile {
            name: "list_repos".to_string(),
            typescript_name: "listRepos".to_string(),
            server_id: "github".to_string(),
            category: Some("repos".to_string()),
            keywords: vec!["list".to_string(), "repos".to_string()],
            description: Some("List repositories".to_string()),
            parameters: vec![],
        },
    ];

    let use_case_hints = vec!["CI/CD".to_string()];
    let context = build_skill_context("github", &tools, Some(&use_case_hints));

    assert_eq!(context.server_id, "github");
    assert_eq!(context.skill_name, "github-progressive");
    assert_eq!(context.tool_count, 2);
    assert_eq!(context.categories.len(), 2);
    assert!(!context.generation_prompt.is_empty());
    assert!(context.generation_prompt.contains("CI/CD"));
}

#[tokio::test]
async fn test_scan_nonexistent_directory() {
    let result = scan_tools_directory(std::path::Path::new("/nonexistent/path")).await;

    assert!(result.is_err());
}

#[tokio::test]
async fn test_scan_directory_missing_metadata() {
    // A directory that exists but was never generated with the sidecar (or was
    // generated by a pre-#141 version) must hard-error rather than silently
    // report zero tools.
    let temp_dir = TempDir::new().unwrap();

    let result = scan_tools_directory(temp_dir.path()).await;

    assert!(matches!(result, Err(ScanError::MissingMetadata { .. })));
}

#[tokio::test]
async fn test_skill_metadata_extraction() {
    let content = r"---
name: github-progressive
description: GitHub operations via MCP tools
---

# GitHub Progressive

Introduction paragraph.

## Quick Start

Steps here.

## Common Tasks

Tasks here.

## Troubleshooting

Troubleshooting here.
";

    // This tests the metadata extraction logic
    assert!(content.starts_with("---"));
    assert!(content.contains("name:"));
    assert!(content.contains("description:"));

    // Count sections
    let section_count = content.lines().filter(|l| l.starts_with("## ")).count();
    assert_eq!(section_count, 3);
}

// ============================================================================
// Large Metadata Handling Tests
// ============================================================================

#[tokio::test]
async fn test_scan_directory_with_many_tools() {
    let temp_dir = TempDir::new().unwrap();
    let dir = temp_dir.path();

    let tool_names: Vec<String> = (0..50).map(|i| format!("tool_{i}")).collect();
    let tool_name_refs: Vec<&str> = tool_names.iter().map(String::as_str).collect();
    write_metadata_sidecar(dir, &tool_name_refs).await;

    let tools = scan_tools_directory(dir).await.unwrap().tools;

    assert_eq!(tools.len(), 50);
    // Verify they're sorted
    for i in 1..tools.len() {
        assert!(tools[i - 1].name <= tools[i].name);
    }
}

#[tokio::test]
async fn test_scan_directory_ignores_stray_files() {
    // Tool metadata itself only ever comes from `_meta.json`; an `index.ts`, a
    // `_runtime` directory, or an unrelated stray file must not affect the scan
    // (they are not sidecar-referenced tool files, so at most they are logged).
    let temp_dir = TempDir::new().unwrap();
    let dir = temp_dir.path();

    write_metadata_sidecar(dir, &["valid_tool"]).await;

    fs::write(dir.join("index.ts"), "export * from './validTool';")
        .await
        .unwrap();
    fs::create_dir(dir.join("_runtime")).await.unwrap();
    fs::write(dir.join("_runtime/mcp-bridge.ts"), "// Bridge")
        .await
        .unwrap();
    fs::write(dir.join("readme.txt"), "Not a TypeScript file")
        .await
        .unwrap();

    let scan_result = scan_tools_directory(dir).await.unwrap();

    assert_eq!(scan_result.tools.len(), 1);
    assert_eq!(scan_result.tools[0].name, "valid_tool");
    assert!(
        scan_result.warnings.is_empty(),
        "index.ts, a nested _runtime file, and a non-.ts stray file must not be reported"
    );
}

#[tokio::test]
async fn test_build_skill_context_many_categories() {
    let tools: Vec<ParsedToolFile> = (0..20)
        .map(|i| ParsedToolFile {
            name: format!("tool_{i}"),
            typescript_name: format!("tool{i}"),
            server_id: "test".to_string(),
            category: Some(format!("category-{}", i % 5)), // 5 different categories
            keywords: vec![format!("keyword{i}")],
            description: Some(format!("Tool {i}")),
            parameters: vec![],
        })
        .collect();

    let context = build_skill_context("test", &tools, None);

    assert_eq!(context.tool_count, 20);
    assert_eq!(context.categories.len(), 5);

    // Verify each category has the right number of tools
    for category in &context.categories {
        assert_eq!(category.tools.len(), 4); // 20 tools / 5 categories = 4 per category
    }
}

#[tokio::test]
async fn test_build_skill_context_empty_tools() {
    let tools: Vec<ParsedToolFile> = vec![];

    let context = build_skill_context("test", &tools, None);

    assert_eq!(context.tool_count, 0);
    assert_eq!(context.categories.len(), 0);
    assert_eq!(context.example_tools.len(), 0);
    assert!(context.generation_prompt.contains('0')); // Total tools: 0
}

#[tokio::test]
async fn test_scan_directory_concurrent_access() {
    let temp_dir = TempDir::new().unwrap();
    let dir = temp_dir.path();

    let tool_names: Vec<String> = (0..10).map(|i| format!("tool_{i}")).collect();
    let tool_name_refs: Vec<&str> = tool_names.iter().map(String::as_str).collect();
    write_metadata_sidecar(dir, &tool_name_refs).await;

    // Scan the same directory concurrently
    let dir_path = dir.to_path_buf();
    let handles: Vec<_> = (0..5)
        .map(|_| {
            let dir = dir_path.clone();
            tokio::spawn(async move { scan_tools_directory(&dir).await })
        })
        .collect();

    // All should succeed
    for handle in handles {
        let result = handle.await.unwrap();
        assert!(result.is_ok());
        assert_eq!(result.unwrap().tools.len(), 10);
    }
}

// ============================================================================
// Error Handling Tests
// ============================================================================

#[tokio::test]
async fn test_scan_directory_permission_denied() {
    // This test is platform-specific and might not work on all systems
    // We'll skip it on Windows
    if cfg!(windows) {
        return;
    }

    let temp_dir = TempDir::new().unwrap();
    let restricted_dir = temp_dir.path().join("restricted");
    fs::create_dir(&restricted_dir).await.unwrap();

    // Remove read permissions (Unix only)
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        let mut perms = tokio::fs::metadata(&restricted_dir)
            .await
            .unwrap()
            .permissions();
        perms.set_mode(0o000);
        tokio::fs::set_permissions(&restricted_dir, perms)
            .await
            .unwrap();

        let result = scan_tools_directory(&restricted_dir).await;
        assert!(result.is_err());

        // Restore permissions for cleanup
        let mut perms = tokio::fs::metadata(&restricted_dir)
            .await
            .unwrap()
            .permissions();
        perms.set_mode(0o755);
        tokio::fs::set_permissions(&restricted_dir, perms)
            .await
            .unwrap();
    }
}

#[tokio::test]
async fn test_scan_directory_too_many_tools() {
    use mcp_execution_skill::MAX_TOOL_FILES;

    let temp_dir = TempDir::new().unwrap();
    let dir = temp_dir.path();

    let tool_names: Vec<String> = (0..=MAX_TOOL_FILES).map(|i| format!("tool_{i}")).collect();
    let tool_name_refs: Vec<&str> = tool_names.iter().map(String::as_str).collect();
    write_metadata_sidecar(dir, &tool_name_refs).await;

    let result = scan_tools_directory(dir).await;

    assert!(result.is_err());
    match result.unwrap_err() {
        ScanError::TooManyFiles { count, limit } => {
            assert_eq!(count, MAX_TOOL_FILES + 1);
            assert_eq!(limit, MAX_TOOL_FILES);
        }
        other => panic!("Expected TooManyFiles error, got: {other:?}"),
    }
}

#[tokio::test]
async fn test_scan_directory_file_too_large() {
    use mcp_execution_skill::MAX_FILE_SIZE;

    let temp_dir = TempDir::new().unwrap();
    let dir = temp_dir.path();

    // Create a sidecar larger than MAX_FILE_SIZE (1MB) by padding one tool's description.
    #[allow(clippy::cast_possible_truncation)]
    let large_description = "a".repeat((MAX_FILE_SIZE as usize) + 1);

    let mut meta = ServerMetadata {
        schema_version: METADATA_SCHEMA_VERSION,
        server_id: "test".to_string(),
        server_name: "Test Server".to_string(),
        server_version: "1.0.0".to_string(),
        tools: vec![ToolMetadata {
            name: "large_tool".to_string(),
            typescript_name: "largeTool".to_string(),
            category: None,
            keywords: vec!["large".to_string()],
            description: Some(String::new()),
            parameters: vec![],
        }],
    };
    meta.tools[0].description = Some(large_description);

    let content = serde_json::to_string_pretty(&meta).unwrap();
    fs::write(dir.join(METADATA_FILE_NAME), &content)
        .await
        .unwrap();

    let result = scan_tools_directory(dir).await;

    assert!(result.is_err());
    match result.unwrap_err() {
        ScanError::FileTooLarge { path, size, limit } => {
            assert!(path.contains(mcp_execution_core::metadata::METADATA_FILE_NAME));
            assert!(size > MAX_FILE_SIZE);
            assert_eq!(limit, MAX_FILE_SIZE);
        }
        other => panic!("Expected FileTooLarge error, got: {other:?}"),
    }
}