|
18 | 18 |
|
19 | 19 | @mcp.tool() |
20 | 20 | async def generate(description: str, requirements: str) -> str: |
21 | | - """Generate a new Rust cargo project from the description and requirements""" |
| 21 | + """ |
| 22 | + Generate a new Rust cargo project from the description and requirements. The input arguments are |
22 | 23 |
|
23 | | - async with httpx.AsyncClient() as client: |
24 | | - response = await client.post(f"{API_BASE_URL}/generate-sync", json={'description': description, 'requirements': requirements}) |
25 | | - return response.text |
| 24 | + * description: a text string description of the generated Rust project. |
| 25 | + * requiremenets: functional requirements on what the generated Rust project. |
| 26 | +
|
| 27 | + The return value is a text string that contains all files in the project. Each file is seperated by a [filename: path_to_file] line. For example, a project that contains a Cargo.toml file and a src/main.rs file will be returned as the following. |
| 28 | +
|
| 29 | +[filename: Cargo.toml] |
| 30 | +[package] |
| 31 | +name = "a_command_line_calcu" |
| 32 | +version = "0.1.0" |
| 33 | +edition = "2021" |
| 34 | +
|
| 35 | +[dependencies] |
| 36 | +
|
| 37 | +
|
| 38 | +[filename: src/main.rs] |
| 39 | +fn main() { |
| 40 | + println!("Hello, world!"); |
| 41 | +} |
| 42 | +
|
| 43 | + """ |
| 44 | + |
| 45 | + async with httpx.AsyncClient(timeout=60.0) as client: |
| 46 | + try: |
| 47 | + response = await client.post(f"{API_BASE_URL}/generate-sync", json={'description': description, 'requirements': requirements}) |
| 48 | + response.raise_for_status() |
| 49 | + return response.text |
| 50 | + except httpx.HTTPError as e: |
| 51 | + print(f"HTTP error occurred: {e}") |
| 52 | + return f"Error trying to generate a Rust project: {str(e)}" |
26 | 53 |
|
27 | 54 | @mcp.tool() |
28 | 55 | async def compile_and_fix(code: str, description: str = "A Rust project", max_attempts: int = 3) -> str: |
29 | | - """Compile a Rust cargo project and fix any compiler errors""" |
| 56 | + """ |
| 57 | + Compile a Rust cargo project and fix any compiler errors. |
| 58 | +
|
| 59 | + The argument `code` is a text string that contains all files in the project. Each file is seperated by a [filename: path_to_file] line. For example, a project that contains a Cargo.toml file and a src/main.rs file will be returned as the following. |
| 60 | +
|
| 61 | +[filename: Cargo.toml] |
| 62 | +[package] |
| 63 | +name = "a_command_line_calcu" |
| 64 | +version = "0.1.0" |
| 65 | +edition = "2021" |
30 | 66 |
|
| 67 | +[dependencies] |
| 68 | +
|
| 69 | +
|
| 70 | +[filename: src/main.rs] |
| 71 | +fn main() { |
| 72 | + println!("Hello, world!"); |
| 73 | +} |
| 74 | +
|
| 75 | + The return value is also a text string that contains all files in the project. It is in the same format as the input `code` argument. |
| 76 | + """ |
31 | 77 | async with httpx.AsyncClient(timeout=60.0) as client: |
32 | 78 | try: |
33 | 79 | response = await client.post( |
34 | 80 | f"{API_BASE_URL}/compile-and-fix", |
35 | 81 | json={'code': code, 'description': description, 'max_attempts': max_attempts} |
36 | 82 | ) |
37 | 83 | response.raise_for_status() |
38 | | - return response.text |
| 84 | + |
| 85 | + resp_json = json.loads(response.text) |
| 86 | + if "combined_text" in resp_json: |
| 87 | + return ["combined_text"] |
| 88 | + else: |
| 89 | + return "Cannot fix the Rust compiler error." |
| 90 | + # return response.text |
39 | 91 | except httpx.HTTPError as e: |
40 | 92 | print(f"HTTP error occurred: {e}") |
41 | | - return f"Error calling compile-and-fix API: {str(e)}" |
| 93 | + return f"Error trying to fixing the Rust compiler error: {str(e)}" |
42 | 94 |
|
43 | 95 | @mcp.tool() |
44 | 96 | async def compile(code: str) -> str: |
45 | | - """Compile a Rust cargo project""" |
| 97 | + """ |
| 98 | + Compile a Rust cargo project and return the compiler output. |
| 99 | +
|
| 100 | + The argument `code` is a text string that contains all files in the project. Each file is seperated by a [filename: path_to_file] line. For example, a project that contains a Cargo.toml file and a src/main.rs file will be returned as the following. |
| 101 | +
|
| 102 | +[filename: Cargo.toml] |
| 103 | +[package] |
| 104 | +name = "a_command_line_calcu" |
| 105 | +version = "0.1.0" |
| 106 | +edition = "2021" |
| 107 | +
|
| 108 | +[dependencies] |
46 | 109 |
|
47 | | - async with httpx.AsyncClient() as client: |
48 | | - response = await client.post(f"{API_BASE_URL}/compile", json={'code': code}) |
49 | | - return response.text |
| 110 | +
|
| 111 | +[filename: src/main.rs] |
| 112 | +fn main() { |
| 113 | + println!("Hello, world!"); |
| 114 | +} |
| 115 | +
|
| 116 | + The return value is a text string that contains the Rust compiler output. |
| 117 | + """ |
| 118 | + async with httpx.AsyncClient(timeout=60.0) as client: |
| 119 | + try: |
| 120 | + response = await client.post(f"{API_BASE_URL}/compile", json={'code': code}) |
| 121 | + response.raise_for_status() |
| 122 | + |
| 123 | + resp_json = json.loads(response.text) |
| 124 | + if "build_output" in resp_json: |
| 125 | + return ["build_output"] |
| 126 | + else: |
| 127 | + return "Rust compiler error." |
| 128 | + except httpx.HTTPError as e: |
| 129 | + print(f"HTTP error occurred: {e}") |
| 130 | + return f"Rust compiler error: {str(e)}" |
50 | 131 |
|
51 | 132 | if __name__ == "__main__": |
52 | 133 | # Use transport from environment variable or default to stdio |
|
0 commit comments