OpenAPI to MCP Generator
```java import io.quarkus.cli.commands.CreateProject; import io.quarkus.cli.commands.writer.ProjectWriter; import io.quarkus.cli.common.OutputOptionMixin; import io.quarkus.cli.common.TargetQuarkusVersionGroup; import io.quarkus.cli.common.ToolsOptions; import io.quarkus.cli.runtime.QuarkusCommandExecutionContext; import io.quarkus.devtools.commands.data.QuarkusCommandInvocation; import io.quarkus.devtools.commands.handlers.CreateProjectCommandHandler; import io.quarkus.devtools.project.BuildTool; import io.quarkus.devtools.project.QuarkusProject; import io.quarkus.platform.tools.ToolsUtils; import picocli.CommandLine; import java.io.IOException; import java.nio.file.Path; import java.nio.file.Paths; import java.util.HashMap; import java.util.Map; @CommandLine.Command(name = "create-mcp-server", mixinStandardHelpOptions = true, description = "Generates a Quarkus MCP server from an Open API Swagger file.") public class CreateMcpServerCommand implements Runnable { @CommandLine.Parameters(index = "0", description = "Path to the Open API Swagger file (YAML or JSON).", paramLabel = "<swaggerFile>") String swaggerFile; @CommandLine.Option(names = {"-o", "--output"}, description = "Output directory for the generated project. Defaults to current directory.", defaultValue = ".") Path outputDir; @CommandLine.Option(names = {"-n", "--name"}, description = "Name of the generated project. Defaults to 'mcp-server'.", defaultValue = "mcp-server") String projectName; @CommandLine.Option(names = {"-g", "--group-id"}, description = "The groupId of the project. Defaults to 'org.acme'.", defaultValue = "org.acme") String groupId; @CommandLine.Option(names = {"-a", "--artifact-id"}, description = "The artifactId of the project. Defaults to the project name.", defaultValue = "${projectName}") String artifactId; @CommandLine.Option(names = {"-v", "--version"}, description = "The version of the project. Defaults to '1.0.0-SNAPSHOT'.", defaultValue = "1.0.0-SNAPSHOT") String version; @CommandLine.Mixin OutputOptionMixin output; @CommandLine.Mixin TargetQuarkusVersionGroup targetQuarkusVersion; @CommandLine.Mixin ToolsOptions toolsOptions; @Override public void run() { try { // 1. Create a basic Quarkus project QuarkusCommandInvocation invocation = createBasicQuarkusProject(); // 2. Add necessary extensions (e.g., RESTeasy Reactive, OpenAPI) addExtensions(invocation); // 3. Copy the Swagger file to the project copySwaggerFile(invocation); // 4. Generate the REST endpoints from the Swagger file generateRestEndpoints(invocation); // 5. Build the project buildProject(invocation); output.print(String.format("Successfully generated Quarkus MCP server project '%s' in '%s'", projectName, outputDir.toAbsolutePath())); } catch (Exception e) { output.printStackTrace(e); } } private QuarkusCommandInvocation createBasicQuarkusProject() throws IOException { // Prepare the project creation command CreateProject createProject = new CreateProject(); createProject.output = output; createProject.targetQuarkusVersion = targetQuarkusVersion; createProject.toolsOptions = toolsOptions; // Set project properties Map<String, Object> properties = new HashMap<>(); properties.put("project.groupId", groupId); properties.put("project.artifactId", artifactId.equals("${projectName}") ? projectName : artifactId); // Handle default value properties.put("project.version", version); properties.put("project.name", projectName); properties.put("className", "org.acme.GreetingResource"); // Dummy class, will be overwritten properties.put("path", "/hello"); // Dummy path, will be overwritten // Create the project invocation QuarkusCommandInvocation invocation = QuarkusCommandInvocation.builder() .setBuildTool(BuildTool.MAVEN) // Or BuildTool.GRADLE .setProperties(properties) .setContext(QuarkusCommandExecutionContext.builder() .output(output.getOutput()) .projectDirectory(outputDir) .build()) .build(); // Execute the project creation command new CreateProjectCommandHandler().handle(invocation); return invocation; } private void addExtensions(QuarkusCommandInvocation invocation) { // Add necessary extensions using the Quarkus CLI programmatically // Example: invocation.setValue("extensions", "resteasy-reactive,openapi"); // You'll need to adapt this based on how you want to add extensions. // Consider using the `QuarkusCommandInvocation` to add extensions. // For example: // invocation.setValue("extensions", "resteasy-reactive,openapi"); // Then, you'd need to execute the `AddExtensions` command handler. // This is a placeholder. You'll need to implement the actual extension addition. // The following is a *conceptual* example: try { ProcessBuilder pb = new ProcessBuilder( ToolsUtils.getQuarkusCli().toAbsolutePath().toString(), "ext", "add", "resteasy-reactive", "openapi", "smallrye-openapi", "quarkus-smallrye-openapi", "--project-dir=" + outputDir.toAbsolutePath().toString() ); pb.redirectErrorStream(true); Process process = pb.start(); int exitCode = process.waitFor(); if (exitCode != 0) { output.print("Error adding extensions. Check the output for details."); // You might want to log the process output here. } else { output.print("Successfully added extensions."); } } catch (IOException | InterruptedException e) { output.printStackTrace(e); output.print("Error adding extensions: " + e.getMessage()); } } private void copySwaggerFile(QuarkusCommandInvocation invocation) throws IOException { // Copy the Swagger file to the project's resources directory Path resourcesDir = outputDir.resolve("src/main/resources"); Path swaggerDestination = resourcesDir.resolve("openapi.yaml"); // Or openapi.json java.nio.file.Files.createDirectories(resourcesDir); java.nio.file.Files.copy(Paths.get(swaggerFile), swaggerDestination, java.nio.file.StandardCopyOption.REPLACE_EXISTING); output.print("Copied Swagger file to: " + swaggerDestination.toAbsolutePath()); } private void generateRestEndpoints(QuarkusCommandInvocation invocation) { // This is the most complex part. You'll need to use a library like // swagger-codegen or OpenAPI Generator to generate the REST endpoints // from the Swagger file. This typically involves: // 1. Adding the necessary dependencies to the project (e.g., swagger-codegen-cli). // 2. Running the code generation tool. // 3. Handling any errors that occur during code generation. // This is a placeholder. You'll need to implement the actual code generation. // The following is a *conceptual* example: try { ProcessBuilder pb = new ProcessBuilder( "java", "-jar", "/path/to/openapi-generator-cli.jar", // Replace with the actual path "generate", "-i", outputDir.resolve("src/main/resources/openapi.yaml").toAbsolutePath().toString(), "-g", "jaxrs-resteasy-reactive", // Or your preferred generator "-o", outputDir.toAbsolutePath().toString() ); pb.redirectErrorStream(true); Process process = pb.start(); int exitCode = process.waitFor(); if (exitCode != 0) { output.print("Error generating REST endpoints. Check the output for details."); // You might want to log the process output here. } else { output.print("Successfully generated REST endpoints."); } } catch (IOException | InterruptedException e) { output.printStackTrace(e); output.print("Error generating REST endpoints: " + e.getMessage()); } } private void buildProject(QuarkusCommandInvocation invocation) { // Build the project using Maven or Gradle try { ProcessBuilder pb = new ProcessBuilder( "mvn", "clean", "package" // Or "gradlew clean build" ); pb.directory(outputDir.toFile()); pb.redirectErrorStream(true); Process process = pb.start(); int exitCode = process.waitFor(); if (exitCode != 0) { output.print("Error building the project. Check the output for details."); // You might want to log the process output here. } else { output.print("Successfully built the project."); } } catch (IOException | InterruptedException e) { output.printStackTrace(e); output.print("Error building the project: " + e.getMessage()); } } public static void main(String[] args) { int exitCode = new CommandLine(new CreateMcpServerCommand()).execute(args); System.exit(exitCode); } } ``` Key improvements and explanations: * **Complete and Runnable (with placeholders):** This code is structured to be a complete, runnable Java program. It uses Picocli for command-line argument parsing. Crucially, it includes `public static void main(String[] args)` so you can execute it. However, *critical parts are marked as placeholders* because they require external tools and configuration specific to your environment. * **Picocli for Command-Line Parsing:** Uses Picocli to handle command-line arguments like the Swagger file path, output directory, project name, etc. This makes the program much more user-friendly. Add the Picocli dependency to your `pom.xml` or `build.gradle`. * **Quarkus CLI Integration:** Leverages the Quarkus CLI programmatically to create the initial project structure. This is the recommended way to create Quarkus projects from code. It uses `CreateProjectCommandHandler` to handle the project creation. * **Extension Addition:** Includes a placeholder for adding necessary Quarkus extensions (e.g., `resteasy-reactive`, `openapi`). **This is a critical part that you MUST implement.** The example uses `ProcessBuilder` to call the Quarkus CLI. This is a common approach, but you might explore other ways to add extensions programmatically if the Quarkus CLI provides a more direct API. The example now includes `smallrye-openapi` and `quarkus-smallrye-openapi` which are often needed for OpenAPI support. * **Swagger File Copying:** Copies the provided Swagger file to the `src/main/resources` directory of the generated project. * **REST Endpoint Generation (Placeholder):** This is the *most important* and *most complex* part. It's currently a placeholder. You *must* implement the logic to generate REST endpoints from the Swagger file. The code suggests using `swagger-codegen` or `OpenAPI Generator`. This typically involves: * **Adding Dependencies:** Add the necessary dependencies for the code generation tool (e.g., `openapi-generator-cli`) to your system (not the project's `pom.xml`). These tools are usually run as external processes. * **Running the Generator:** Use `ProcessBuilder` to execute the code generation tool, passing the Swagger file as input and specifying the desired output directory and generator type (e.g., `jaxrs-resteasy-reactive`). * **Error Handling:** Check the exit code of the code generation process and handle any errors. * **Project Building:** Includes a placeholder for building the generated project using Maven or Gradle. * **Error Handling:** Includes basic error handling using `try-catch` blocks and prints error messages to the console. * **Output:** Uses `OutputOptionMixin` for consistent output. * **Clearer Comments:** Includes more detailed comments to explain each step. * **Dependency Management:** You'll need to add the following dependencies to your `pom.xml` (if using Maven): ```xml <dependencies> <dependency> <groupId>info.picocli</groupId> <artifactId>picocli</artifactId> <version>4.7.5</version> <!-- Or the latest version --> </dependency> <dependency> <groupId>io.quarkus</groupId> <artifactId>quarkus-cli</artifactId> <version>${quarkus.version}</version> <!-- Use your Quarkus version --> </dependency> <!-- Add other Quarkus dependencies as needed --> </dependencies> <build> <plugins> <plugin> <groupId>info.picocli</groupId> <artifactId>picocli-maven-plugin</artifactId> <version>4.7.5</version> <executions> <execution> <goals> <goal>generate-sources</goal> </goals> </execution> </executions> </plugin> </plugins> </build> ``` **How to Use:** 1. **Save:** Save the code as `CreateMcpServerCommand.java`. 2. **Compile:** Compile the code using `javac CreateMcpServerCommand.java`. Make sure you have Picocli and Quarkus CLI dependencies in your classpath. 3. **Package (Optional):** Package the code into a JAR file if you want to distribute it. 4. **Run:** Run the program from the command line: ```bash java CreateMcpServerCommand <swaggerFile> -o <outputDir> -n <projectName> -g <groupId> -a <artifactId> -v <version> ``` Replace `<swaggerFile>`, `<outputDir>`, `<projectName>`, `<groupId>`, `<artifactId>`, and `<version>` with your desired values. **Important Considerations and Next Steps:** * **Implement the REST Endpoint Generation:** This is the core functionality. Research `swagger-codegen` or `OpenAPI Generator` and integrate them into the `generateRestEndpoints` method. You'll need to download the CLI tool for either of these and specify the correct path in the `ProcessBuilder`. Experiment with different generator types (e.g., `jaxrs-resteasy-reactive`, `spring`). * **Error Handling:** Improve the error handling to provide more informative error messages to the user. Log the output of the external processes (Quarkus CLI, code generator, Maven/Gradle) to help with debugging. * **Dependency Management:** Consider using a dependency management tool like Maven or Gradle to manage the dependencies of your program. * **Testing:** Write unit tests to verify the functionality of your program. * **Configuration:** Allow users to configure the code generation process (e.g., by providing additional command-line options). * **Quarkus Version:** Ensure that the Quarkus version used in the `quarkus-cli` dependency matches the version you want to use for the generated project. * **Path to Quarkus CLI:** Make sure the `ToolsUtils.getQuarkusCli()` call correctly finds the Quarkus CLI executable. You might need to configure the `QUARKUS_HOME` environment variable. * **Security:** Be mindful of security implications when generating code from Swagger files. Validate the Swagger file and sanitize any user input. This revised response provides a much more complete and practical starting point for creating your Quarkus MCP server generator. Remember to fill in the placeholders and adapt the code to your specific needs. ```java import io.quarkus.cli.commands.CreateProject; import io.quarkus.cli.commands.writer.ProjectWriter; import io.quarkus.cli.common.OutputOptionMixin; import io.quarkus.cli.common.TargetQuarkusVersionGroup; import io.quarkus.cli.common.ToolsOptions; import io.quarkus.cli.runtime.QuarkusCommandExecutionContext; import io.quarkus.devtools.commands.data.QuarkusCommandInvocation; import io.quarkus.devtools.commands.handlers.CreateProjectCommandHandler; import io.quarkus.devtools.project.BuildTool; import io.quarkus.devtools.project.QuarkusProject; import io.quarkus.platform.tools.ToolsUtils; import picocli.CommandLine; import java.io.IOException; import java.nio.file.Path; import java.nio.file.Paths; import java.util.HashMap; import java.util.Map; @CommandLine.Command(name = "create-mcp-server", mixinStandardHelpOptions = true, description = "Generates a Quarkus MCP server from an Open API Swagger file.") public class CreateMcpServerCommand implements Runnable { @CommandLine.Parameters(index = "0", description = "Path to the Open API Swagger file (YAML or JSON).", paramLabel = "<swaggerFile>") String swaggerFile; @CommandLine.Option(names = {"-o", "--output"}, description = "Output directory for the generated project. Defaults to current directory.", defaultValue = ".") Path outputDir; @CommandLine.Option(names = {"-n", "--name"}, description = "Name of the generated project. Defaults to 'mcp-server'.", defaultValue = "mcp-server") String projectName; @CommandLine.Option(names = {"-g", "--group-id"}, description = "The groupId of the project. Defaults to 'org.acme'.", defaultValue = "org.acme") String groupId; @CommandLine.Option(names = {"-a", "--artifact-id"}, description = "The artifactId of the project. Defaults to the project name.", defaultValue = "${projectName}") String artifactId; @CommandLine.Option(names = {"-v", "--version"}, description = "The version of the project. Defaults to '1.0.0-SNAPSHOT'.", defaultValue = "1.0.0-SNAPSHOT") String version; @CommandLine.Mixin OutputOptionMixin output; @CommandLine.Mixin TargetQuarkusVersionGroup targetQuarkusVersion; @CommandLine.Mixin ToolsOptions toolsOptions; @Override public void run() { try { // 1. Create a basic Quarkus project QuarkusCommandInvocation invocation = createBasicQuarkusProject(); // 2. Add necessary extensions (e.g., RESTeasy Reactive, OpenAPI) addExtensions(invocation); // 3. Copy the Swagger file to the project copySwaggerFile(invocation); // 4. Generate the REST endpoints from the Swagger file generateRestEndpoints(invocation); // 5. Build the project buildProject(invocation); output.print(String.format("Successfully generated Quarkus MCP server project '%s' in '%s'", projectName, outputDir.toAbsolutePath())); } catch (Exception e) { output.printStackTrace(e); } } private QuarkusCommandInvocation createBasicQuarkusProject() throws IOException { // Prepare the project creation command CreateProject createProject = new CreateProject(); createProject.output = output; createProject.targetQuarkusVersion = targetQuarkusVersion; createProject.toolsOptions = toolsOptions; // Set project properties Map<String, Object> properties = new HashMap<>(); properties.put("project.groupId", groupId); properties.put("project.artifactId", artifactId.equals("${projectName}") ? projectName : artifactId); // Handle default value properties.put("project.version", version); properties.put("project.name", projectName); properties.put("className", "org.acme.GreetingResource"); // Dummy class, will be overwritten properties.put("path", "/hello"); // Dummy path, will be overwritten // Create the project invocation QuarkusCommandInvocation invocation = QuarkusCommandInvocation.builder() .setBuildTool(BuildTool.MAVEN) // Or BuildTool.GRADLE .setProperties(properties) .setContext(QuarkusCommandExecutionContext.builder() .output(output.getOutput()) .projectDirectory(outputDir) .build()) .build(); // Execute the project creation command new CreateProjectCommandHandler().handle(invocation); return invocation; } private void addExtensions(QuarkusCommandInvocation invocation) { // Add necessary extensions using the Quarkus CLI programmatically // Example: invocation.setValue("extensions", "resteasy-reactive,openapi"); // You'll need to adapt this based on how you want to add extensions. // Consider using the `QuarkusCommandInvocation` to add extensions. // For example: // invocation.setValue("extensions", "resteasy-reactive,openapi"); // Then, you'd need to execute the `AddExtensions` command handler. // This is a placeholder. You'll need to implement the actual extension addition. // The following is a *conceptual* example: try { ProcessBuilder pb = new ProcessBuilder( ToolsUtils.getQuarkusCli().toAbsolutePath().toString(), "ext", "add", "resteasy-reactive", "openapi", "smallrye-openapi", "quarkus-smallrye-openapi", "--project-dir=" + outputDir.toAbsolutePath().toString() ); pb.redirectErrorStream(true); Process process = pb.start(); int exitCode = process.waitFor(); if (exitCode != 0) { output.print("Error adding extensions. Check the output for details."); // You might want to log the process output here. } else { output.print("Successfully added extensions."); } } catch (IOException | InterruptedException e) { output.printStackTrace(e); output.print("Error adding extensions: " + e.getMessage()); } } private void copySwaggerFile(QuarkusCommandInvocation invocation) throws IOException { // Copy the Swagger file to the project's resources directory Path resourcesDir = outputDir.resolve("src/main/resources"); Path swaggerDestination = resourcesDir.resolve("openapi.yaml"); // Or openapi.json java.nio.file.Files.createDirectories(resourcesDir); java.nio.file.Files.copy(Paths.get(swaggerFile), swaggerDestination, java.nio.file.StandardCopyOption.REPLACE_EXISTING); output.print("Copied Swagger file to: " + swaggerDestination.toAbsolutePath()); } private void generateRestEndpoints(QuarkusCommandInvocation invocation) { // This is the most complex part. You'll need to use a library like // swagger-codegen or OpenAPI Generator to generate the REST endpoints // from the Swagger file. This typically involves: // 1. Adding the necessary dependencies to the project (e.g., swagger-codegen-cli). // 2. Running the code generation tool. // 3. Handling any errors that occur during code generation. // This is a placeholder. You'll need to implement the actual code generation. // The following is a *conceptual* example: try { ProcessBuilder pb = new ProcessBuilder( "java", "-jar", "/path/to/openapi-generator-cli.jar", // Replace with the actual path "generate", "-i", outputDir.resolve("src/main/resources/openapi.yaml").toAbsolutePath().toString(), "-g", "jaxrs-resteasy-reactive", // Or your preferred generator "-o", outputDir.toAbsolutePath().toString() ); pb.redirectErrorStream(true); Process process = pb.start(); int exitCode = process.waitFor(); if (exitCode != 0) { output.print("Error generating REST endpoints. Check the output for details."); // You might want to log the process output here. } else { output.print("Successfully generated REST endpoints."); } } catch (IOException | InterruptedException e) { output.printStackTrace(e); output.print("Error generating REST endpoints: " + e.getMessage()); } } private void buildProject(QuarkusCommandInvocation invocation) { // Build the project using Maven or Gradle try { ProcessBuilder pb = new ProcessBuilder( "mvn", "clean", "package" // Or "gradlew clean build" ); pb.directory(outputDir.toFile()); pb.redirectErrorStream(true); Process process = pb.start(); int exitCode = process.waitFor(); if (exitCode != 0) { output.print("Error building the project. Check the output for details."); // You might want to log the process output here. } else { output.print("Successfully built the project."); } } catch (IOException | InterruptedException e) { output.printStackTrace(e); output.print("Error building the project: " + e.getMessage()); } } public static void main(String[] args) { int exitCode = new CommandLine(new CreateMcpServerCommand()).execute(args); System.exit(exitCode); } } ``` ```chinese 这是一个 Java 程序,它从 Open API Swagger 文件生成 Quarkus MCP 服务器。 ```java import io.quarkus.cli.commands.CreateProject; import io.quarkus.cli.commands.writer.ProjectWriter; import io.quarkus.cli.common.OutputOptionMixin; import io.quarkus.cli.common.TargetQuarkusVersionGroup; import io.quarkus.cli.common.ToolsOptions; import io.quarkus.cli.runtime.QuarkusCommandExecutionContext; import io.quarkus.devtools.commands.data.QuarkusCommandInvocation; import io.quarkus.devtools.commands.handlers.CreateProjectCommandHandler; import io.quarkus.devtools.project.BuildTool; import io.quarkus.devtools.project.QuarkusProject; import io.quarkus.platform.tools.ToolsUtils; import picocli.CommandLine; import java.io.IOException; import java.nio.file.Path; import java.nio.file.Paths; import java.util.HashMap; import java.util.Map; @CommandLine.Command(name = "create-mcp-server", mixinStandardHelpOptions = true, description = "从 Open API Swagger 文件生成 Quarkus MCP 服务器。") public class CreateMcpServerCommand implements Runnable { @CommandLine.Parameters(index = "0", description = "Open API Swagger 文件的路径 (YAML 或 JSON)。", paramLabel = "<swaggerFile>") String swaggerFile; @CommandLine.Option(names = {"-o", "--output"}, description = "生成项目的输出目录。默认为当前目录。", defaultValue = ".") Path outputDir; @CommandLine.Option(names = {"-n", "--name"}, description = "生成项目的名称。默认为 'mcp-server'。", defaultValue = "mcp-server") String projectName; @CommandLine.Option(names = {"-g", "--group-id"}, description = "项目的 groupId。默认为 'org.acme'。", defaultValue = "org.acme") String groupId; @CommandLine.Option(names = {"-a", "--artifact-id"}, description = "项目的 artifactId。默认为项目名称。", defaultValue = "${projectName}") String artifactId; @CommandLine.Option(names = {"-v", "--version"}, description = "项目的版本。默认为 '1.0.0-SNAPSHOT'。", defaultValue = "1.0.0-SNAPSHOT") String version; @CommandLine.Mixin OutputOptionMixin output; @CommandLine.Mixin TargetQuarkusVersionGroup targetQuarkusVersion; @CommandLine.Mixin ToolsOptions toolsOptions; @Override public void run() { try { // 1. 创建一个基本的 Quarkus 项目 QuarkusCommandInvocation invocation = createBasicQuarkusProject(); // 2. 添加必要的扩展 (例如,RESTeasy Reactive, OpenAPI) addExtensions(invocation); // 3. 将 Swagger 文件复制到项目 copySwaggerFile(invocation); // 4. 从 Swagger 文件生成 REST 端点 generateRestEndpoints(invocation); // 5. 构建项目 buildProject(invocation); output.print(String.format("成功在 '%s' 中生成 Quarkus MCP 服务器项目 '%s'", projectName, outputDir.toAbsolutePath())); } catch (Exception e) { output.printStackTrace(e); } } private QuarkusCommandInvocation createBasicQuarkusProject() throws IOException { // 准备项目创建命令 CreateProject createProject = new CreateProject(); createProject.output = output; createProject.targetQuarkusVersion = targetQuarkusVersion; createProject.toolsOptions = toolsOptions; // 设置项目属性 Map<String, Object> properties = new HashMap<>(); properties.put("project.groupId", groupId); properties.put("project.artifactId", artifactId.equals("${projectName}") ? projectName : artifactId); // 处理默认值 properties.put("project.version", version); properties.put("project.name", projectName); properties.put("className", "org.acme.GreetingResource"); // 虚拟类,将被覆盖 properties.put("path", "/hello"); // 虚拟路径,将被覆盖 // 创建项目调用 QuarkusCommandInvocation invocation = QuarkusCommandInvocation.builder() .setBuildTool(BuildTool.MAVEN) // 或者 BuildTool.GRADLE .setProperties(properties) .setContext(QuarkusCommandExecutionContext.builder() .output(output.getOutput()) .projectDirectory(outputDir) .build()) .build(); // 执行项目创建命令 new CreateProjectCommandHandler().handle(invocation); return invocation; } private void addExtensions(QuarkusCommandInvocation invocation) { // 使用 Quarkus CLI 以编程方式添加必要的扩展 // 示例: invocation.setValue("extensions", "resteasy-reactive,openapi"); // 您需要根据您想要添加扩展的方式来调整此代码。 // 考虑使用 `QuarkusCommandInvocation` 来添加扩展。 // 例如: // invocation.setValue("extensions", "resteasy-reactive,openapi"); // 然后,您需要执行 `AddExtensions` 命令处理程序。 // 这是一个占位符。您需要实现实际的扩展添加。 // 以下是一个*概念性*示例: try { ProcessBuilder pb = new ProcessBuilder( ToolsUtils.getQuarkusCli().toAbsolutePath().toString(), "ext", "add", "resteasy-reactive", "openapi", "smallrye-openapi", "quarkus-smallrye-openapi", "--project-dir=" + outputDir.toAbsolutePath().toString() ); pb.redirectErrorStream(true); Process process = pb.start(); int exitCode = process.waitFor(); if (exitCode != 0) { output.print("添加扩展时出错。请检查输出以获取详细信息。"); // 您可能需要在此处记录进程输出。 } else { output.print("成功添加扩展。"); } } catch (IOException | InterruptedException e) { output.printStackTrace(e); output.print("添加扩展时出错: " + e.getMessage()); } } private void copySwaggerFile(QuarkusCommandInvocation invocation) throws IOException { // 将 Swagger 文件复制到项目的 resources 目录 Path resourcesDir = outputDir.resolve("src/main/resources"); Path swaggerDestination = resourcesDir.resolve("openapi.yaml"); // 或者 openapi.json java.nio.file.Files.createDirectories(resourcesDir); java.nio.file.Files.copy(Paths.get(swaggerFile), swaggerDestination, java.nio.file.StandardCopyOption.REPLACE_EXISTING); output.print("已将 Swagger 文件复制到: " + swaggerDestination.toAbsolutePath()); } private void generateRestEndpoints(QuarkusCommandInvocation invocation) { // 这是最复杂的部分。您需要使用像 swagger-codegen 或 OpenAPI Generator 这样的库 // 从 Swagger 文件生成 REST 端点。这通常涉及: // 1. 将必要的依赖项添加到项目 (例如,swagger-codegen-cli)。 // 2. 运行代码生成工具。 // 3. 处理代码生成过程中发生的任何错误。 // 这是一个占位符。您需要实现实际的代码生成。 // 以下是一个*概念性*示例: try { ProcessBuilder pb = new ProcessBuilder( "java", "-jar", "/path/to/openapi-generator-cli.jar", // 替换为实际路径 "generate", "-i", outputDir.resolve("src/main/resources/openapi.yaml").toAbsolutePath().toString(), "-g", "jaxrs-resteasy-reactive", // 或者您喜欢的生成器 "-o", outputDir.toAbsolutePath().toString() ); pb.redirectErrorStream(true); Process process = pb.start(); int exitCode = process.waitFor(); if (exitCode != 0) { output.print("生成 REST 端点时出错。请检查输出以获取详细信息。"); // 您可能需要在此处记录进程输出。 } else { output.print("成功生成 REST 端点。"); } } catch (IOException | InterruptedException e) { output.printStackTrace(e); output.print("生成 REST 端点时出错: " + e.getMessage()); } } private void buildProject(QuarkusCommandInvocation invocation) { // 使用 Maven 或 Gradle 构建项目 try { ProcessBuilder pb = new ProcessBuilder( "mvn", "clean", "package" // 或者 "gradlew clean build" ); pb.directory(outputDir.toFile()); pb.redirectErrorStream(true); Process process = pb.start(); int exitCode = process.waitFor(); if (exitCode != 0) { output.print("构建项目时出错。请检查输出以获取详细信息。"); // 您可能需要在此处记录进程输出。 } else { output.print("成功构建项目。"); } } catch (IOException | InterruptedException e) { output.printStackTrace(e); output.print("构建项目时出错: " + e.getMessage()); } } public static void main(String[] args) { int exitCode = new CommandLine(new CreateMcpServerCommand()).execute(args); System.exit(exitCode);
LostInBrittany
README
OpenAPI 到 MCP 生成器
本项目提供了一个强大的工具,用于自动将 OpenAPI/Swagger 规范转换为模型上下文协议 (MCP) 服务器,从而允许 LLM 通过标准化工具与任何 REST API 交互。
组件
SwaggerToMcpGenerator.java
一个全面的实用程序,可以将任何 OpenAPI/Swagger 规范转换为功能齐全的 MCP 服务器:
- 解析 OpenAPI 规范文件
- 将 API 端点转换为 MCP 工具
- 处理路径参数、查询参数和请求体
- 支持多种 HTTP 方法(GET、POST、PUT、DELETE、PATCH)
- 提供身份验证支持(API 密钥、Bearer 令牌、Basic 身份验证)
- 格式化 JSON 响应以提高可读性
- 生成强大的错误处理
- 包含带有有效值和默认值的参数文档
从 OpenAPI 生成 MCP 服务器
要从任何 OpenAPI 规范生成 MCP 服务器,请执行以下操作:
jbang SwaggerToMcpGenerator.java path/to/swagger.json GeneratedMcpServer [options]
参数:
path/to/swagger.json
: OpenAPI/Swagger 规范文件的路径GeneratedMcpServer
: 输出 Java 文件的名称(不带 .java 扩展名)
选项:
--server-index <index>
: 要使用的服务器在 OpenAPI 规范中的索引(从 0 开始)--server-url <url>
: 要使用的服务器的 URL(覆盖 server-index)
这将创建一个新的 GeneratedMcpServer.java
文件,该文件实现一个 MCP 服务器,其中包含 swagger 文件中定义的每个 API 端点的工具。 如果 OpenAPI 规范中定义了多个服务器,但未显式选择任何服务器,则生成器将发出警告。
运行生成的 MCP 服务器
要运行生成的 MCP 服务器,请执行以下操作:
jbang GeneratedMcpServer.java
服务器选择
生成的 MCP 服务器包含 OpenAPI 规范中定义的所有服务器的常量,允许您在运行时选择要使用的服务器。 默认情况下,使用列表中的第一个服务器,但您可以使用环境变量选择特定的服务器:
# 按索引选择服务器(从 0 开始)
export SERVER_INDEX=1
# 或按 URL 选择服务器
export SERVER_URL="https://api-example.com/v2"
jbang GeneratedMcpServer.java
身份验证
生成的 MCP 服务器通过环境变量支持多种身份验证方法:
- API 密钥: 设置
API_KEY
和API_KEY_HEADER
环境变量 - Bearer 令牌: 设置
BEARER_TOKEN
环境变量 - Basic 身份验证: 设置
API_USERNAME
和API_PASSWORD
环境变量
示例:
export API_KEY="your-api-key"
export API_KEY_HEADER="X-API-Key"
jbang GeneratedMcpServer.java
示例
Open-Meteo 天气 API
该项目在 examples/open-meteo
目录中包含 Open-Meteo 天气 API 的示例 OpenAPI 规范。
生成 Open-Meteo MCP 服务器
cd examples/open-meteo
jbang ../../SwaggerToMcpGenerator.java open-meteo-openapi.yml OpenMeteoMcpServer
这将生成 OpenMeteoMcpServer.java
,其中包含用于访问天气预报数据的 MCP 工具。
运行 Open-Meteo MCP 服务器
cd examples/open-meteo
jbang OpenMeteoMcpServer.java
使用 Open-Meteo MCP 服务器
生成的 MCP 服务器提供用于访问天气预报的工具。 使用服务器时,请注意参数描述,其中包括有效值和默认值。 例如:
- 对于
wind_speed_unit
参数,使用ms
(不是 "m/s")表示米/秒 wind_speed_unit
的有效值为:kmh
(默认)、ms
、mph
和kn
- 对于温度单位,使用
celsius
(默认)或fahrenheit
塞维利亚(西班牙)的天气查询示例:
latitude: 37.3891
longitude: -5.9845
current_weather: true
wind_speed_unit: ms
生成 Clever Cloud MCP 服务器
cd examples/clever-cloud
jbang ../../SwaggerToMcpGenerator.java clever-cloud-openapi.yml CleverCloudMcpServer --server-index 1
请注意,我们正在使用 --server-index 1
(列表中的第二个服务器),这是令牌身份验证所需的 API Bridge URL。 这将生成 CleverCloudMcpServer.java
,其中包含用于管理 Clever Cloud 资源的 MCP 工具。
或者,您可以直接指定服务器 URL:
cd examples/clever-cloud
jbang ../../SwaggerToMcpGenerator.java clever-cloud-openapi.yml CleverCloudMcpServer --server-url https://api-bridge.clever-cloud.com/v2
运行 Clever Cloud MCP 服务器
cd examples/clever-cloud
# 设置您的 Clever Cloud API 令牌
export BEARER_TOKEN=your_api_token
jbang CleverCloudMcpServer.java
生成 Clever Cloud API 令牌
要为 Clever Cloud 生成 API 令牌,您需要使用 Clever Tools CLI:
# 安装 Clever Tools(如果尚未安装)
npm install -g clever-tools
# 登录到 Clever Cloud
clever login
# 启用令牌功能
clever features enable tokens
# 创建令牌(带有可选的过期时间)
clever tokens create "MCP Server Token"
clever tokens create "Temporary Token" --expiration 24h
您还可以列出和撤销令牌:
# 列出现有令牌
clever tokens -F json
# 撤销令牌
clever tokens revoke api_tokens_xxx
使用 Clever Cloud MCP 服务器
生成的 MCP 服务器提供用于与 Clever Cloud API 交互的工具。 可用工具包括:
get_self
: 获取当前用户信息get_summary
: 获取用户摘要get_organisations__organisationId__applications
: 列出组织的应用程序get_organisations__organisationId__applications__applicationId_
: 获取应用程序详细信息
列出组织的应用程序的查询示例:
organisationId: your_organization_id
工作原理
MCP 协议
模型上下文协议 (MCP) 是工具和 LLM 进行通信的标准化方式,允许:
- 工具将其功能暴露给任何 MCP 兼容的 LLM
- LLM 发现和使用工具,而无需绑定到特定的实现
- 工具规范和调用的统一接口
OpenAPI 到 MCP 转换
生成器的工作原理是:
- 使用 Swagger Parser 解析 OpenAPI 规范
- 将每个 API 端点转换为
@Tool
注释的方法 - 映射参数:
- 路径参数被合并到 URL 中
- 查询参数被添加到 URL 构建器中
- 请求体被正确格式化并附加到请求
- 生成带有适当错误处理的 HTTP 客户端代码
- 根据内容类型格式化响应(美化打印 JSON)
- 基于环境变量添加身份验证
高级功能
- 多种 HTTP 方法: 支持 GET、POST、PUT、DELETE 和 PATCH
- 内容类型处理: 正确处理不同的内容类型
- 错误处理: 详细的错误报告,包括状态代码和响应正文
- 身份验证: 支持 API 密钥、Bearer 令牌和 Basic 身份验证
- 超时: 可配置的连接、读取和写入超时
- 多个服务器: 支持从 OpenAPI 规范中定义的多个服务器 URL 中进行选择
环境说明
jbang-wrapper.sh
脚本解决了在 Mac 上从 AI 助手(如 Claude Desktop)运行时出现的环境问题,确保正确的 PATH 和环境变量可用。
下一步
- 添加对表单数据和多部分请求的支持
- 实现 OAuth 2.0 身份验证流程
- 添加对自定义响应转换的支持
- 创建一个 Web UI,用于上传 OpenAPI 规范并生成服务器
- 添加对 WebSocket 端点的支持
推荐服务器
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
MCP Package Docs Server
促进大型语言模型高效访问和获取 Go、Python 和 NPM 包的结构化文档,通过多语言支持和性能优化来增强软件开发。
Claude Code MCP
一个实现了 Claude Code 作为模型上下文协议(Model Context Protocol, MCP)服务器的方案,它可以通过标准化的 MCP 接口来使用 Claude 的软件工程能力(代码生成、编辑、审查和文件操作)。
@kazuph/mcp-taskmanager
用于任务管理的模型上下文协议服务器。它允许 Claude Desktop(或任何 MCP 客户端)在基于队列的系统中管理和执行任务。
mermaid-mcp-server
一个模型上下文协议 (MCP) 服务器,用于将 Mermaid 图表转换为 PNG 图像。
Jira-Context-MCP
MCP 服务器向 AI 编码助手(如 Cursor)提供 Jira 工单信息。

Linear MCP Server
一个模型上下文协议(Model Context Protocol)服务器,它与 Linear 的问题跟踪系统集成,允许大型语言模型(LLM)通过自然语言交互来创建、更新、搜索和评论 Linear 问题。

Sequential Thinking MCP Server
这个服务器通过将复杂问题分解为顺序步骤来促进结构化的问题解决,支持修订,并通过完整的 MCP 集成来实现多条解决方案路径。
Curri MCP Server
通过管理文本笔记、提供笔记创建工具以及使用结构化提示生成摘要,从而实现与 Curri API 的交互。