Method, system and device for automatically generating code based on MCP protocol, and medium
By integrating a server-side code generation engine based on the MCP protocol with large models, the accuracy and adaptability issues of code generation in complex projects are resolved, achieving efficient and automated code generation and integration, and improving code quality and development efficiency.
Patent Information
- Application Number
- CN202511460898.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-10-14
- Publication Date
- 2026-01-09
AI Technical Summary
Existing code generation solutions suffer from poor accuracy, poor adaptability to projects, high requirements for human experience, and poor flexibility when dealing with complex projects, failing to meet the needs for efficient and accurate code generation.
Based on the MCP protocol, a server-side code generation engine is built to parse code generation requests, generate Java code text that matches the project, and integrate it into the business layer through a large model to achieve dynamic and flexible code generation and integration.
It improves the efficiency and quality of code generation, reduces manual intervention, ensures that the generated code conforms to project specifications, supports dynamic analysis of project structure and business semantics, and realizes an automated code generation process.
Smart Images

Figure CN121300756A_ABST
Abstract
Description
Technical Field
[0001] This disclosure relates to the field of automatic software code generation technology, and in particular to a method, system, apparatus, and medium for automatically generating code based on the MCP protocol. Background Technology
[0002] The MCP protocol, introduced and open-sourced by Anthrop, aims to establish a secure, bidirectional connection mechanism between large language models and external data sources and tools. This mechanism is crucial for achieving efficient integration of large language models with external systems, enabling them to access data and tool support from external systems, thereby enhancing the breadth and depth of their applications.
[0003] In the field of code generation, most existing code generation solutions rely on predefined template scripts. This approach can meet the code generation needs of simple projects to a certain extent, quickly generating code with a basic structure using preset templates. However, its limitations become apparent when faced with complex project structures involving multiple modules and services. Complex projects often have multiple interrelated modules and services, each with its unique functions and requirements. Predefined template scripts struggle to flexibly adapt to these diverse situations. They cannot dynamically adjust according to the specific needs of the project, resulting in generated code that fails to accurately reflect the actual architecture and functionality of the project.
[0004] To compensate for the shortcomings of predefined template scripts, some solutions attempt to introduce artificial intelligence (AI) technology to assist in code generation. AI possesses powerful learning and reasoning capabilities, and theoretically can generate code that meets input requirements. However, inconsistent code style is a significant problem. Different developers have different coding styles, and AI-generated code cannot adhere to a unified style standard, posing difficulties for subsequent code maintenance and team collaboration. Secondly, the accuracy of the generated code is low. AI may have biases in understanding complex business requirements and technical details, resulting in generated code that does not fully meet actual needs. In many cases, the generated code contains logical errors, syntax errors, or missing functionality. Furthermore, due to the low accuracy of the generated code, developers often need to interact with AI multiple times to correct and improve the generated code, which not only increases development time but also reduces development efficiency. Therefore, existing code generation solutions still cannot meet the requirements for efficient and accurate code generation when dealing with complex projects. Summary of the Invention
[0005] In view of this, the present disclosure provides a method, system, apparatus, and medium for automatically generating code based on the MCP protocol, which can solve the problems of poor accuracy of generated code, poor adaptability to projects, high requirements for human experience, and poor flexibility in the prior art.
[0006] In a first aspect, embodiments of this disclosure provide a method for automatically generating code based on the MCP protocol, including: A server-side code generation engine was built based on the MCP protocol. In response to the code generation request, the code generation request is parsed to obtain the data table structure, service name, and business name, and the generation of the calling instruction is triggered; Based on the invocation instruction, the server-side code generation engine is invoked to process the data table structure, the service name, and the business name to obtain first-dimensional information, second-dimensional information, and third-dimensional information; Based on the first dimension information, the second dimension information, and the third dimension information, Java code text matching the project is generated. Send the Java code text to the client; In response to the client receiving the Java code text, the large model is invoked to integrate the Java code text into the business layer corresponding to the business name, thereby obtaining the target code.
[0007] Secondly, this disclosure also provides a system for automatically generating code based on the MCP protocol, comprising: The building block is used to build a server-side code generation engine based on the MCP protocol. The parsing module is used to respond to code generation requests, parse the code generation requests to obtain the data table structure, service name, business name, and trigger the generation of call instructions; The processing module is used to call the server-side code generation engine to process the data table structure, the service name, and the business name based on the call instruction, and obtain first-dimensional information, second-dimensional information, and third-dimensional information. The code text generation module is used to trigger the generation of Java code text that matches the project based on the first dimension information, the second dimension information, and the third dimension information. The sending module is used to send the Java code text to the client; The integration module is used to respond to the signal that the client receives the Java code text, and call the large model to integrate the Java code text into the business layer corresponding to the business name to obtain the target code.
[0008] Thirdly, this disclosure also provides a computer device, which adopts the following technical solution: The computer device includes: At least one processor; and, A memory communicatively connected to the at least one processor; wherein, The memory stores instructions that can be executed by the at least one processor, which, when executed by the at least one processor, enables the at least one processor to perform any of the above-described methods for automatically generating code based on the MCP protocol.
[0009] Fourthly, embodiments of this disclosure also provide a computer-readable storage medium storing computer instructions for causing a computer to execute any of the methods described above for automatically generating code based on the MCP protocol.
[0010] Fifthly, embodiments of this disclosure also provide a computer program product, including a computer program / instructions that, when executed by a processor, implement the steps of any of the methods described above.
[0011] The method for automatically generating code based on the MCP protocol disclosed in this application first constructs a server-side code generation engine based on the MCP protocol. Responding to a user-inputted code generation request, the engine parses the request to obtain the data table structure, service name, and business name, and triggers a generation call instruction. Next, based on the call instruction, the server-side code generation engine processes the data table structure, service name, and business name to obtain first-dimensional information, second-dimensional information, and third-dimensional information. Based on these three dimensions, it triggers the generation of Java code text matching the project and sends the Java code text to the client. Finally, in response to the client receiving the Java code text, it calls a large model to integrate the Java code text into the business layer corresponding to the business name, obtaining the target code. This method supports dynamic analysis and integration of project structure and business semantic layers, does not rely on predefined templates, and can dynamically and flexibly adapt to the project structure corresponding to the requirements. Furthermore, based on the secure bidirectional connection feature of the MCP protocol, it automates the process from user input of a code generation request to the final generation and integration of the target code, effectively improving the efficiency and quality of code generation, reducing manual intervention, and accelerating project development.
[0012] The above description is merely an overview of the technical solution disclosed herein. In order to better understand the technical means of this disclosure and to implement it in accordance with the contents of the specification, and to make the above and other objects, features and advantages of this disclosure more apparent and understandable, preferred embodiments are described below in detail with reference to the accompanying drawings. Attached Figure Description
[0013] To more clearly illustrate the technical solutions of the embodiments of this disclosure, the drawings used in the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of this disclosure. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0014] Figure 1 This is a flowchart illustrating a method for automatically generating code based on the MCP protocol, as provided in an embodiment of this disclosure.
[0015] Figure 2 A schematic diagram illustrating the method for obtaining the first-dimensional information, the second-dimensional information, and the third-dimensional information provided in the embodiments of this disclosure.
[0016] Figure 3 This is a schematic diagram of the structure of a computer device provided in an embodiment of the present disclosure. Detailed Implementation
[0017] The embodiments of this disclosure will now be described in detail with reference to the accompanying drawings.
[0018] It should be understood that the following specific examples illustrate the implementation of this disclosure, and those skilled in the art can easily understand other advantages and effects of this disclosure from the content disclosed in this specification. Obviously, the described embodiments are only a part of the embodiments of this disclosure, and not all of them. This disclosure can also be implemented or applied through other different specific implementation methods, and the details in this specification can also be modified or changed based on different viewpoints and applications without departing from the spirit of this disclosure. It should be noted that, in the absence of conflict, the following embodiments and features in the embodiments can be combined with each other. Based on the embodiments in this disclosure, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this disclosure.
[0019] It should be noted that various aspects of embodiments within the scope of the appended claims are described below. It will be apparent that the aspects described herein can be embodied in a wide variety of forms, and any particular structure and / or function described herein is merely illustrative. Based on this disclosure, those skilled in the art will understand that one aspect described herein can be implemented independently of any other aspect, and two or more of these aspects can be combined in various ways. For example, any number of aspects set forth herein can be used to implement the device and / or practice the method. Additionally, this device and / or method can be implemented using structures and / or functionalities other than one or more of the aspects set forth herein.
[0020] It should also be noted that the illustrations provided in the following embodiments are only schematic representations of the basic concept of this disclosure. The drawings only show the components related to this disclosure and are not drawn according to the number, shape and size of the components in actual implementation. In actual implementation, the form, quantity and proportion of each component can be arbitrarily changed, and the layout of the components may also be more complex.
[0021] Furthermore, specific details are provided in the following description to facilitate a thorough understanding of the examples. However, those skilled in the art will understand that the described aspects can be practiced without these specific details.
[0022] Reference Figure 1 This application discloses a method for automatically generating code based on the MCP protocol, including: S100 is a server-side code generation engine built on the MCP protocol.
[0023] Specifically, it includes: S110, which uses Java to develop the MCP protocol server and establishes a connection with the client based on standard IO communication; S120, which configures a code generation engine in the MCP protocol server. The code generation engine is used to parse information and generate hierarchical code files according to predefined project templates and rules.
[0024] Specifically, S110 utilizes the secure bidirectional connection feature of the MCP protocol to build a communication channel between the server, client, and external data source, ensuring the security and integrity of data during transmission. For example, encryption algorithms are used to encrypt the data to prevent it from being stolen or tampered with.
[0025] Furthermore, a server-side socket is created using Java's ServerSocket class, listening on a specific port (e.g., 8080). When a client initiates a connection request, the server accepts the connection using the accept() method, establishing a standard I / O communication channel with the client. The client and server can perform read and write operations on data using InputStream and OutputStream. Java is a cross-platform programming language, ensuring stable server operation on different operating systems. Standard I / O communication is a universal communication method, facilitating connections with various types of clients and enhancing system compatibility.
[0026] For S120, developers can use open-source template engines (such as Freemarker) to implement a code generation engine. Predefined project templates (such as Java class templates, interface templates, etc.) are stored in a designated directory on the server. When a code generation request is received from a client, the code generation engine generates Java code files that conform to the project specifications based on the business information in the request (such as data table structure, service name, business name), combined with the templates and predefined rules. As an independent module, the code generation engine is easy to maintain and extend. When the project specifications or templates change, only the templates and rules in the code generation engine need to be modified, without large-scale modifications to the entire server. The use of predefined templates and rules ensures that the generated code conforms to the project's unified specifications.
[0027] In this step, a server-side application is developed using the Java language. This application implements the MCP (Model-Code Protocol) to receive code generation requests from clients. The server integrates a code generation engine that can generate Java code conforming to project specifications based on input business information (such as data table structure, service name, and business name).
[0028] The MCP protocol ensures unified and stable communication between the client and server, facilitating cross-platform and cross-language integration; the code generation engine, as an independent module, is easy to maintain and extend; and the rich Java class libraries and frameworks enhance development efficiency and system stability.
[0029] S200 responds to the user's input code generation request, parses the code generation request to obtain the data table structure, service name, and business name, and triggers the generation call instruction.
[0030] S300, based on the call command, calls the server-side code generation engine to process the data table structure, service name, and business name to obtain first-dimensional information, second-dimensional information, and third-dimensional information.
[0031] S400, based on the first-dimensional information, the second-dimensional information, and the third-dimensional information, triggers the generation of Java code text that matches the project.
[0032] The S500 sends Java code text to the client.
[0033] S600, in response to the client receiving a signal that Java code text has been received, calls the large model to integrate the Java code text into the business layer corresponding to the business name, and obtains the target code.
[0034] After receiving the Java code text, the client doesn't save the file directly. Instead, it calls a "large model" (referring to the intelligent project structure integration module). This module understands the current project's directory structure (such as MVC layering and module division), automatically places the generated code in the correct package path, and updates relevant configuration files (such as Spring's @ComponentScan and MyBatis' Mapper path scanning). For example, OrderServiceImpl is placed in the com.company.project.service.impl package and automatically registered in the Spring configuration. This step effectively avoids manual copying and pasting, prevents misplacement, automatically updates configurations, and ensures that new code is loaded correctly; even beginners can add code correctly without needing to be familiar with the project structure.
[0035] The method for automatically generating code based on the MCP protocol disclosed in this application, and the code generation system based on the MCP protocol implemented in Java, provide a complete end-to-end automated generation process from natural language input to AI intent recognition to code generation to code integration. It does not rely on specific AI programming tools, communicates with the client only through the MCP protocol, has good compatibility and scalability, the generated code strictly follows the project specifications and structure, reduces the cost of manual modification, automatically writes the code into the specified location of the project, and supports just-in-time compilation and execution. Simple AI generation takes 20-30 minutes, while this solution only takes 3-5 minutes.
[0036] The method for automatically generating code based on the MCP protocol disclosed in this application effectively reduces the workload of manual coding and significantly shortens the development cycle through automated code generation and integration processes. Developers can then focus more time and energy on implementing and optimizing business logic. Using dynamically matched predefined templates and rules to generate code ensures that the generated code conforms to the project's unified specifications. The large-model code integration function avoids errors that may occur during manual code integration, improving code accuracy and stability. The server-client communication method based on the MCP protocol guarantees the system's cross-platform and cross-language integration capabilities. Clients can use different programming languages and operating systems to communicate with the server, improving the system's applicability. Furthermore, the code generation engine and the large model are independent modules, facilitating system maintenance and expansion. When project specifications or requirements change, only the corresponding modules need to be modified, without requiring large-scale modifications to the entire system.
[0037] This application implements an MCP protocol server using Java, without relying on third-party AI programming tools. By building its own server in Java, it ensures stability and controllability, making it suitable for enterprise intranet deployment. It abstracts input into "data model + business logic + project structure," making code generation more intelligent and closely aligned with actual projects. Furthermore, through large-scale model-level project integration, it not only generates code but also "understands" the project hierarchy, automatically placing files and updating configurations, achieving "generation and readiness." From request initiation to code deployment, the entire process is based on the MCP protocol, forming a reusable automated pipeline. This solution, through Java + MCP protocol + intelligent code generation + automatic project integration, constructs a complete automated chain from "business input" to "usable code." It not only improves development efficiency but also fundamentally solves persistent problems such as "inconsistent code standards," "difficulty for beginners," and "excessive repetitive work." For Java project teams pursuing high quality, high efficiency, and sustainable evolution, it is an excellent, replicable, implementable, and scalable solution.
[0038] The method for "configuring the code generation engine within the MCP protocol server" in S120 specifically includes: S121 configures the mapping relationship between different services and different project structures based on database tables.
[0039] This step enables the system to quickly determine the corresponding project structure based on the mapping relationship when code generation requests are received from different services. This allows for the generation of targeted code that meets the requirements of the service, avoiding code structure chaos and improving the accuracy and adaptability of code generation.
[0040] S122, Configure project code generation rules.
[0041] Rules can cover aspects such as code naming conventions, commenting conventions, and code structure layout. For example, it can stipulate that entity class names must end with "Entity", method names must use camelCase, and each method must have a brief comment explaining its function. These rules can be stored in a configuration file, and the code generation engine will operate according to these rules when generating code. Through this step, it can be ensured that the generated code conforms to a unified standard, improve code readability and maintainability, make it easier for team members to understand and modify the code, and reduce communication costs and errors caused by inconsistent coding styles.
[0042] S123 can be configured to support multiple code templates for different project needs, and the code templates use the FreeMarker template engine.
[0043] For different types of projects, such as web projects, desktop application projects, and mobile application projects, different code templates are created. For example, web projects might have templates for the controller layer, service layer, and data access layer; desktop application projects might have templates for interface design and business logic processing. Each template is written using FreeMarker syntax and includes placeholders. During code generation, these placeholders are replaced based on specific business information. This step meets the diverse needs of different projects, improves the flexibility of code generation, and allows developers to choose the appropriate template for a specific project, quickly generating code that matches the project's characteristics and reducing the workload of repetitive code writing.
[0044] S124 configures a unified metadata parsing interface, which supports table structure parsing from multiple data sources.
[0045] Specifically, supported data sources can include common relational databases such as MySQL, Oracle, SQL Server, and PostgreSQL. Developers implement a unified interface and write different implementation classes for different data sources. For example, for a MySQL data source, the implementation class uses the MySQL JDBC driver to connect to the database and query table structure information; for an Oracle data source, it uses the Oracle JDBC driver to perform the corresponding operations. Regardless of the data source used by the project, the code generation engine can parse table structure information through a unified interface, improving system compatibility and scalability. Developers do not need to write different parsing logic for different data sources, reducing development difficulty and maintenance costs.
[0046] S125, determine the database framework for the query, which is Mybatis, Mybatis-Plus, or Hibernate.
[0047] Specifically, the appropriate database framework should be selected based on the project's requirements and the team's technology stack. If the project has high requirements for control over SQL statements and the team is familiar with MyBatis, MyBatis can be selected. If you want to simplify development and improve development efficiency on the basis of MyBatis, MyBatis-Plus can be selected. If the project focuses on object-relational mapping (ORM) and has high requirements for the level of abstraction of database operations, Hibernate can be preferred.
[0048] Different database frameworks have different characteristics and advantages. Choosing the right framework can improve the efficiency of database operations and the convenience of development. At the same time, the code generation engine can generate corresponding code based on the selected framework, so that the generated code matches the database operation method of the project.
[0049] S126, Reserved business layer package name parameter, to dynamically specify the business layer based on the input business layer.
[0050] In the code generation request, users can enter the package name of the business layer, such as "com.example.business.order". When generating code, the code generation engine will determine the storage location and namespace of the business layer code based on this package name. For example, the generated order business logic class will be stored in the "com.example.business.order" package. This step effectively increases the flexibility of code generation, allowing users to dynamically specify the business layer according to the actual needs of the project, making the generated code better adaptable to the project's architecture and organization.
[0051] S121-S126 are all executed internally within the MCP protocol server. In this embodiment, by configuring the mapping relationship between different services and project structures, multiple code templates, and dynamically specifying business layers, customized code can be generated according to the specific needs of different projects, meeting diverse business scenarios. The unified metadata parsing interface and clear code generation rules reduce manual operations and repetitive work for developers during the code generation process, accelerating the code generation speed. At the same time, selecting a suitable database framework can improve the development efficiency of database operations. Following unified code generation rules and specifications, the generated code has good readability and consistency, facilitating maintenance and expansion by team members. The clear mapping relationship between different services and project structures also helps with code organization and management. Supporting multiple data sources and different database frameworks enables the system to adapt to different technical environments. The reserved business layer package name parameters and modular configuration methods facilitate future functional expansion and upgrades of the system.
[0052] The S200 method, which "responds to a user-inputted code generation request, parses the code generation request to obtain the data table structure, service name, and business name, and triggers the generation of a call instruction," specifically includes: S210, obtain the code generation request input by the user on the client side; S220 calls the large model in the client to parse the code generation request, obtain the data table structure, service name, and business name, and triggers the generation call instruction. This instruction contains information such as the data table structure, service name, and business name, and is used to call the server-side code generation engine.
[0053] For example, a user inputs a prompt in the client, such as "We have a data table structure, and we need to generate code and business name in the current project and integrate it into the current project." The client then submits this prompt to a large model for recognition.
[0054] By parsing the user's input request information, the business information required for code generation can be accurately obtained. This information can be passed as input to the code generation engine to generate code that meets the user's needs. Triggering the generation call command can automate code generation and improve development efficiency.
[0055] Reference Figure 2 The method in S300 that "calls the server-side code generation engine to process the data table structure, service name, and business name to obtain first-dimensional information, second-dimensional information, and third-dimensional information" specifically includes the following methods for obtaining first-dimensional information, second-dimensional information, and third-dimensional information: A100 parses the data table structure to obtain the table name, field names, field types, primary key data, and foreign key data.
[0056] Suppose we have a database with a table named `orders` that records order information. Using the database's metadata query functions (such as SQL statements like `SHOW TABLES` and `DESCRIBE`), we can obtain the table name `orders`, field names like `order_id`, `customer_id`, and `order_date`, and their data types like INT, VARCHAR, and DATE, respectively. Simultaneously, by querying the database's system tables (such as MySQL's `information_schema`), we can determine that `order_id` is the primary key, `customer_id` is a foreign key, and it relates to the `customer_id` field in the `customers` table. Accurately obtaining the basic information of the data table is fundamental to subsequent code generation. This information will be used to generate entity classes, database operation code, etc., ensuring that the generated code is consistent with the database structure and avoiding data access errors caused by inaccurate information.
[0057] A200 converts table names into class names that conform to project specifications, based on the configured naming rules.
[0058] If the project uses camelCase for class names, the table name `orders` will be converted to `Orders` according to the rules; if the table name is `customer_orders`, it will be converted to `CustomerOrders`. The naming rules can be defined in the configuration file, and the code generation engine will perform the conversion based on those rules. This step ensures that the generated class names conform to the project's unified naming conventions, improving code readability and maintainability. Team members can more easily understand the business meaning represented by the classes, reducing communication costs caused by inconsistent naming.
[0059] A300 converts field names to compliant attribute names and maps them to Java data types based on the field type.
[0060] For example, the field name `order_id` is converted to `orderId` using camelCase; the field name `order_date` is converted to `orderDate`. For field types, the database type `INT` is mapped to the Java type `Integer`, `VARCHAR` to `String`, and `DATE` to `java.util.Date`. These mapping rules can be pre-configured in the code generation engine. This step ensures that the generated entity class attribute names conform to project specifications and correctly converts database field types to Java data types, enabling the entity classes to correctly interact with the database and improving code accuracy and compatibility.
[0061] A400 automatically generates relationship information based on primary key data and foreign key data.
[0062] Specifically, given that the `customer_id` field in the `orders` table is a foreign key, linked to the `customer_id` field in the `customers` table, the code generation engine will automatically generate the relationship information between the `Orders` and `Customers` classes. For example, it might add a `Customers` type property to the `Orders` class and add corresponding annotations (such as JPA's `@ManyToOne` annotation) to represent a many-to-one relationship. Automatically generating this relationship information reduces the workload for developers manually writing the relationship code, avoids errors that might occur when manually writing such code, and improves code accuracy and development efficiency.
[0063] A500 retrieves the corresponding project structure from the preset database based on the service name, denoted as the target project structure.
[0064] The pre-defined database contains several sets of mapping relationships between service names and project structures.
[0065] For example, the pre-defined database stores the following mapping relationships: the service name "E-commerce Service" corresponds to a project structure of "Controller Layer, Service Layer, Data Access Layer, Entity Layer"; the service name "Financial Services" corresponds to a project structure of "Account Management Module, Transaction Management Module, Risk Assessment Module". When the service name is "E-commerce Service", the corresponding project structure "Controller Layer, Service Layer, Data Access Layer, Entity Layer" is retrieved from the pre-defined database as the target project structure. Quickly obtaining the corresponding project structure based on the service name ensures that the generated code conforms to the overall project architecture, improving code organization and maintainability. Furthermore, the pre-defined mapping relationships facilitate unified management and adjustment of the project structures for different services.
[0066] A600: If a business name exists in the target project structure, obtain the hierarchical structure corresponding to the business name.
[0067] Assuming the target project structure is "Controller Layer, Service Layer, Data Access Layer, Entity Layer," and the business name is "Order Business," the hierarchical structure corresponding to "Order Business" in this project structure is "Controller Layer / Order Controller, Service Layer / Order Service, Data Access Layer / Order Data Access, Entity Layer / Order Entity." The code generation engine will extract this hierarchical structure information related to "Order Business" from the target project structure. This step further refines the hierarchical structure of the generated code, enabling the generated code to accurately locate the specific layer corresponding to the business, improving the code's relevance and maintainability. Developers can more clearly understand the code's position and role in the project.
[0068] The method disclosed in this embodiment ensures that the generated code is highly consistent with the database structure and project specifications through detailed analysis of the data table structure, application of naming rules, and automatic generation of relationships, reducing errors caused by code mismatch with actual business. It obtains the hierarchical structure corresponding to the target project structure and business names, enabling the generated code to be organized according to the overall project architecture and business logic, facilitating code maintenance and expansion by team members. The automated information processing and code generation process reduces the workload of developers manually writing code, accelerates code generation, and allows developers to focus more on the implementation and optimization of business logic. The preset mapping relationship between service names and project structures, as well as the dynamic acquisition of the hierarchical structure of business names, enable the system to adapt to different service and business needs, facilitating functional expansion and architectural adjustments during project development.
[0069] Specifically, if a business name exists in the target project structure, it means that the current project structure contains a hierarchical structure corresponding to the current business name.
[0070] The first dimension of information includes the class name, attribute name, data type, and relationships corresponding to the target project. The second dimension of information includes the structure of the target project. The third dimension of information includes the hierarchical structure corresponding to the business name.
[0071] Dividing business information into dimensions allows for clearer organization and processing of the information needed for code generation. Information from different dimensions can be used in different stages of code generation, improving the accuracy and flexibility of the generated code.
[0072] The S400 method, which "triggers the generation of Java code text matching the project based on first-dimensional, second-dimensional, and third-dimensional information," specifically includes: S410, based on the first-dimensional information, the second-dimensional information, and the third-dimensional information, obtain integrated information; specifically, integrate the class name, attribute name, data type, association relationship, target project structure, and hierarchical structure corresponding to the target project to obtain integrated information.
[0073] Assuming the target project has a class named `Order` with attributes like `orderId` and `orderDate`, and data types `Integer` and `Date` respectively, and a many-to-one relationship between the `Order` and `Customer` classes; and the target project structure is "Controller Layer, Service Layer, Data Access Layer, Entity Layer," with a hierarchy of "Controller Layer / Order Controller, Service Layer / Order Service, Data Access Layer / Order Data Access, Entity Layer / Order Entity." Integrating this information forms a unified set containing complete class information and its position within the project's hierarchy and structure. This unified information consolidates the key elements needed for code generation, providing a comprehensive and ordered data foundation for subsequent code template population. This avoids the confusion and errors that might result from dispersing different information in later steps, improving the accuracy and efficiency of code generation.
[0074] S420: Based on the integrated information and the second-dimensional information, obtain Java code text that matches the project; specifically, fill the integrated information into the preset code generation template to obtain Java code text that matches the project. The code generation template contains the basic structure and logic of the Java code.
[0075] The default code generation template is likely a generic Java entity class template containing placeholders, such as `${className}` for the class name, `${propertyName}` for the property name, and `${dataType}` for the data type. The class name, property name, and data type information from the integrated information are then filled into the corresponding placeholders. For example, the class name `Order` is filled into `${className}`, the property name `orderId` into `${propertyName}`, and the data type `Integer` into `${dataType}`, generating Java entity class code text that meets project requirements. A similar filling method is used for code templates at other layers such as the controller layer and service layer. The default code generation template provides the basic framework and logic of Java code. By filling in the integrated information, Java code that conforms to project specifications and business requirements can be quickly generated, reducing the workload of developers manually writing repetitive code while ensuring code standardization and consistency.
[0076] S430 generates Java code text that matches the project based on the Java code text and third-dimensional information; specifically, the Java code text is organized according to the target project structure and packaged into a code package, the structure of which matches the project's specifications and requirements.
[0077] Based on the target project structure of "controller layer, service layer, data access layer, and entity layer," the generated controller code is placed in the directory corresponding to the controller layer, the service layer code in the directory corresponding to the service layer, the data access layer code in the directory corresponding to the data access layer, and the entity layer code in the directory corresponding to the entity layer. These directories and the code are then packaged into a compressed file, such as a ZIP archive. The structure of this code package matches the project's specifications and requirements. Organizing and packaging the code according to the target project structure allows the generated code to be directly applied to the project, conforming to the overall project architecture and organization. This facilitates code integration by developers, reduces the difficulty of code deployment and debugging, and improves project development efficiency.
[0078] The method disclosed in this embodiment achieves automated Java code generation through information integration, template filling, and package organization processes, greatly improving code generation speed and saving development time and manpower costs. Using preset code generation templates and organizing code according to the project structure ensures that the generated Java code has good standardization and consistency, conforms to project development standards, and facilitates collaboration among team members and code maintenance. The generated code package structure matches the project's specifications and requirements, enabling seamless integration into the project, reducing integration problems caused by code structure mismatches, and improving the success rate and stability of project development. The clear project structure and standardized code organization make the generated code easy to understand and maintain, and easier to modify and extend during subsequent project development and upgrades.
[0079] Specifically, regarding the S500's "sending Java code text to the client," the server encapsulates the generated Java code text in JSON format and sends it to the client through the previously established standard IO communication channel. After receiving the code text, the client stores it in a specified directory on its local machine.
[0080] A standard I / O communication channel has already been established between the server and the client. The server sends encapsulated JSON data to the client through this channel. The server converts the JSON data into a byte stream and then sends it out as an output stream. The client receives these byte streams as an input stream. The standard I / O communication channel provides a reliable data transmission method, ensuring that data is transmitted accurately from the server to the client. This method is widely used in network programming, has high stability and versatility, and can adapt to different network environments.
[0081] After receiving the JSON data from the server, the client first parses the JSON object, extracting the filename and corresponding code text of each Java code file. Then, the client creates a specified directory locally and stores each Java code file in that directory according to its filename. Storing the received Java code text in a local directory makes it convenient for developers to view, modify, and use the generated code. Developers can directly open these code files in their local development environment to perform subsequent development work, improving development efficiency.
[0082] The S600 method, which "responds to the client receiving a signal of Java code text, calls the large model to integrate the Java code text into the corresponding business layer of the hierarchical structure, and obtains the target code," specifically includes: S610, in response to the signal that the client has received the Java code text, calls the large model, inserts the Java code text into the corresponding business module and class according to the hierarchical structure information, and obtains the integrated code; S620 performs correctness and completeness checks on the integration code. If the check conditions are met, the integration code that meets the conditions is used as the target code. If the check conditions are not met, the integration code is dynamically corrected, and the corrected integration code is used as the target code.
[0083] Assuming the hierarchical structure information indicates a controller layer, service layer, and entity layer, after receiving the Java code text, the client sends a signal to the system. The system calls the large model, which, based on information such as class names and functions in the code text and the hierarchical structure information, makes a judgment. For example, if the code text is order-related controller class code, the large model will insert it into the corresponding business module of the controller layer, find the appropriate class file, and accurately add the code text to the appropriate location within that class, completing the integration and generating integrated code. Leveraging the intelligent analysis capabilities of the large model, Java code text can be automatically and accurately integrated into the corresponding business modules and classes based on the hierarchical structure information, reducing the tediousness and errors of manual operations, improving the efficiency and accuracy of code integration, and moreover, the large model can understand the semantics and logic of the code, better completing the insertion and fusion of code.
[0084] The system performs multiple checks on the integration code. For example, it checks for syntax errors such as spelling mistakes and mismatched parentheses; logically, it checks for reasonable method calls and correct data flow; and for completeness, it checks for missing dependencies and incomplete method implementations. If the integration code passes all checks, it is used as the target code. If a problem is found, such as a mismatched parameter type in a method call, the system dynamically triggers the larger model to correct the integration code. The larger model analyzes and modifies the code based on the error information, and the corrected code becomes the target code. Checking and correcting the integration code ensures the quality of the target code, preventing defective code from entering subsequent development and use, reducing system failures and debugging costs caused by code issues. The dynamic correction mechanism can promptly resolve discovered problems, improving code reliability and stability.
[0085] By automatically invoking a large model for code integration, the time-consuming and error-prone manual code integration is avoided, significantly improving efficiency and accelerating project development. The inspection and correction mechanism ensures the correctness and integrity of the target code, improving code quality, reducing the difficulty of subsequent maintenance and debugging, and mitigating potential risks. Dynamically triggering the large model for code correction leverages its intelligent analysis and processing capabilities to quickly and accurately resolve code issues, enhancing the level of development intelligence. The entire process is highly automated, allowing developers to focus more on business logic innovation and optimization rather than spending significant time on code integration and error correction, thus improving their work experience and satisfaction.
[0086] Secondly, this application discloses a system for automatically generating code based on the MCP protocol, used to execute the method for automatically generating code based on the MCP protocol disclosed in the first aspect of this application. The system includes: The building block is used to build a server-side code generation engine based on the MCP protocol. The parsing module is used to respond to the code generation request input by the user, parse the code generation request to obtain the data table structure, service name, business name, and trigger the generation of call instructions; The processing module is used to call the server-side code generation engine to process the data table structure, the service name, and the business name based on the call instruction, and obtain first-dimensional information, second-dimensional information, and third-dimensional information. The code text generation module is used to trigger the generation of Java code text that matches the project based on the first dimension information, the second dimension information, and the third dimension information. The sending module is used to send the Java code text to the client; The integration module is used to respond to the signal that the client receives the Java code text, and call the large model to integrate the Java code text into the business layer corresponding to the business name to obtain the target code.
[0087] A computer device according to embodiments of the present disclosure includes a memory and a processor. The memory is used to store non-transitory computer-readable instructions. Specifically, the memory may include one or more computer program products, which may include various forms of computer-readable storage media, such as volatile memory and / or non-volatile memory. The volatile memory may, for example, include random access memory (RAM) and / or cache memory. The non-volatile memory may, for example, include read-only memory (ROM), hard disk, flash memory, etc.
[0088] The processor may be a central processing unit (CPU) or other form of processing unit with data processing capabilities and / or instruction execution capabilities, and may control other components in the computer device to perform desired functions. In one embodiment of this disclosure, the processor is used to execute computer-readable instructions stored in the memory, causing the computer device to perform all or part of the steps of the methods for automatically generating code based on the MCP protocol in the foregoing embodiments of this disclosure.
[0089] Those skilled in the art will understand that, in order to solve the technical problem of how to achieve a good user experience, this embodiment may also include well-known structures such as communication buses and interfaces, and these well-known structures should also be included within the protection scope of this disclosure.
[0090] like Figure 3 This is a schematic diagram of a computer device provided for an embodiment of the present disclosure. It illustrates a structural schematic diagram suitable for implementing the computer device in the embodiments of the present disclosure. Figure 3 The computer device shown is merely an example and should not be construed as limiting the functionality and scope of the embodiments disclosed herein.
[0091] like Figure 3 As shown, a computer device may include a processor (such as a central processing unit, graphics processing unit, etc.), which can perform various appropriate actions and processes based on programs stored in read-only memory (ROM) or programs loaded from storage devices into random access memory (RAM). The RAM also stores various programs and data required for the operation of the computer device. The processor, ROM, and RAM are interconnected via a bus. Input / output (I / O) interfaces are also connected to the bus.
[0092] Typically, the following devices can be connected to the I / O interface: input devices, such as sensors or visual information acquisition devices; output devices, such as displays; storage devices, such as magnetic tapes or hard drives; and communication devices. Communication devices allow the computer device to communicate wirelessly or wiredly with other devices (such as edge computing devices) to exchange data. Although Figure 3 A computer apparatus with various devices is shown, but it should be understood that it is not required to implement or have all of the devices shown. More or fewer devices may be implemented or included alternatively.
[0093] In particular, according to embodiments of this disclosure, the processes described above with reference to the flowcharts can be implemented as computer software programs. For example, embodiments of this disclosure include a computer program product comprising a computer program carried on a non-transitory computer-readable medium, the computer program containing program code for performing the methods shown in the flowcharts. In such embodiments, the computer program can be downloaded and installed from a network via a communication device, or installed from a storage device, or installed from ROM. When the computer program is executed by a processor, all or part of the steps of the method for automatically generating code based on the MCP protocol according to embodiments of this disclosure are performed.
[0094] For a detailed description of this embodiment, please refer to the corresponding descriptions in the foregoing embodiments, which will not be repeated here.
[0095] A computer-readable storage medium according to embodiments of the present disclosure stores non-transitory computer-readable instructions. When the non-transitory computer-readable instructions are executed by a processor, all or part of the steps of the methods for automatically generating code based on the MCP protocol according to the foregoing embodiments of the present disclosure are performed.
[0096] The aforementioned computer-readable storage media include, but are not limited to: optical storage media (e.g., CD-ROM and DVD), magneto-optical storage media (e.g., MO), magnetic storage media (e.g., magnetic tape or portable hard drive), media with built-in rewritable non-volatile memory (e.g., memory card), and media with built-in ROM (e.g., ROM cartridge).
[0097] For a detailed description of this embodiment, please refer to the corresponding descriptions in the foregoing embodiments, which will not be repeated here.
[0098] The basic principles of this disclosure have been described above with reference to specific embodiments. However, it should be noted that the advantages, benefits, and effects mentioned in this disclosure are merely examples and not limitations, and should not be considered as essential features of each embodiment of this disclosure. Furthermore, the specific details disclosed above are for illustrative and facilitative purposes only, and are not limitations. These details do not limit the scope of this disclosure to the necessity of employing the aforementioned specific details for implementation.
[0099] In this disclosure, relational terms such as "first" and "second" are used merely to distinguish one entity or operation from another, and do not necessarily require or imply any such actual relationship or order between these entities or operations. The block diagrams of devices, apparatuses, devices, and systems involved in this disclosure are merely illustrative examples and are not intended to require or imply that they must be connected, arranged, or configured in the manner shown in the block diagrams. As those skilled in the art will recognize, these devices, apparatuses, devices, and systems can be connected, arranged, and configured in any manner. Words such as "comprising," "including," "having," etc., are open-ended terms meaning "including but not limited to," and are used interchangeably with them. The terms "or" and "and" as used herein refer to the terms "and / or," and are used interchangeably with them unless the context clearly indicates otherwise. The term "such as" as used herein refers to the phrase "such as but not limited to," and is used interchangeably with it.
[0100] Additionally, as used herein, the "or" used in a list of items beginning with "at least one" indicates a separate list, such that a list of, for example, "at least one of A, B, or C" means A or B or C, or AB or AC or BC, or ABC (i.e., A and B and C). Furthermore, the word "exemplary" does not imply that the described example is preferred or better than other examples.
[0101] It should also be noted that in the systems and methods of this disclosure, the components or steps can be decomposed and / or recombined. These decompositions and / or recombinations should be considered as equivalent solutions to this disclosure.
[0102] Various changes, substitutions, and modifications can be made to the technology described herein without departing from the teachings defined by the appended claims. Furthermore, the scope of the claims of this disclosure is not limited to the specific aspects of the processes, machines, manufactures, events, means, methods, and actions described above. Currently existing or later-developed processes, machines, manufactures, events, means, methods, or actions that perform substantially the same function or achieve substantially the same result as the corresponding aspects described herein can be utilized. Therefore, the appended claims include such processes, machines, manufactures, events, means, methods, or actions within their scope.
[0103] The above description of the disclosed aspects is provided to enable any person skilled in the art to make or use this disclosure. Various modifications to these aspects will be readily apparent to those skilled in the art, and the general principles defined herein may be applied to other aspects without departing from the scope of this disclosure. Therefore, this disclosure is not intended to be limited to the aspects shown herein, but rather to be carried out within the widest scope consistent with the principles and novel features disclosed herein.
[0104] The above description has been given for purposes of illustration and description. Furthermore, this description is not intended to limit the embodiments of this disclosure to the forms disclosed herein. Although numerous exemplary aspects and embodiments have been discussed above, those skilled in the art will recognize certain variations, modifications, alterations, additions, and sub-combinations therein.
Claims
1. A method for automatically generating code based on the MCP protocol, characterized in that, include: A server-side code generation engine was built based on the MCP protocol. In response to the code generation request, the code generation request is parsed to obtain the data table structure, service name, and business name, and the generation of the calling instruction is triggered; Based on the invocation instruction, the server-side code generation engine is invoked to process the data table structure, the service name, and the business name to obtain first-dimensional information, second-dimensional information, and third-dimensional information; Based on the first dimension information, the second dimension information, and the third dimension information, Java code text matching the project is generated. Send the Java code text to the client; In response to the client receiving the Java code text, the large model is invoked to integrate the Java code text into the business layer corresponding to the business name, thereby obtaining the target code.
2. The method for automatically generating code based on the MCP protocol according to claim 1, characterized in that, The server-side code generation engine built based on the MCP protocol includes: Develop an MCP protocol server using the Java language and establish a connection with the client based on standard IO communication; A code generation engine is configured within the MCP protocol server. This engine is used to parse information and generate hierarchical code files based on predefined project templates and rules.
3. The method for automatically generating code based on the MCP protocol according to claim 1, characterized in that, The configuration of the code generation engine within the MCP protocol server includes: Configure mapping relationships between different services and different project structures, project code generation rules, multiple sets of code templates to support different project requirements, and a unified metadata parsing interface; wherein, the code templates adopt the FreeMarker template engine, and the metadata parsing interface supports table structure parsing from multiple data sources; Determine the database framework for the query, which is one of Mybatis, Mybatis-Plus, or Hibernate; Reserve the business layer package name parameter to dynamically specify the business layer based on the input business layer.
4. The method for automatically generating code based on the MCP protocol according to claim 1, characterized in that, The process of calling the server-side code generation engine to process the data table structure, the service name, and the business name to obtain first-dimensional information, second-dimensional information, and third-dimensional information includes: The data table structure is parsed to obtain one or more of the following: table name, field name, field type, primary key data, and foreign key data. According to the configured naming rules, the table names are converted into class names that conform to the project specifications; Convert the field names into attribute names that conform to the specifications, and map them to Java data types according to the field types; Based on the primary key data and the foreign key data, the association information is automatically generated; The corresponding project structure is retrieved from the preset database based on the service name, and is denoted as the target project structure; wherein, the preset database contains several sets of mapping relationships between service names and project structures; If the business name exists in the target project structure, obtain the hierarchical structure corresponding to the business name; The first dimension information includes the class name, attribute name, data type, and association relationship corresponding to the target project; the second dimension information includes the target project structure; and the third dimension information includes the hierarchical structure corresponding to the business name.
5. The method for automatically generating code based on the MCP protocol according to claim 4, characterized in that, The step of triggering the generation of Java code text matching the project based on the first dimension information, the second dimension information, and the third dimension information includes: Based on the first dimension information, the second dimension information, and the third dimension information, integrated information is obtained; Based on the integrated information and the second dimension information, obtain the Java code text that matches the project; Based on the Java code text and the third-dimensional information, generate Java code text that matches the project.
6. The method for automatically generating code based on the MCP protocol according to claim 5, characterized in that, The step of obtaining integrated information based on the first dimension information, the second dimension information, and the third dimension information includes: The class name, attribute name, data type, association, target project structure, and hierarchical structure corresponding to the target project are integrated to form an integrated information set containing complete information about the class and its level and structure within the project.
7. The method for automatically generating code based on the MCP protocol according to claim 5, characterized in that, The step of obtaining Java code text matching the project based on the integrated information and the second dimension information includes: Determine the preset code generation template corresponding to the project; The integrated information is filled into a preset code generation template to obtain Java code text that matches the project. The code generation template contains the basic structure and logic of the Java code.
8. The method for automatically generating code based on the MCP protocol according to claim 5, characterized in that, The step of generating Java code text matching the project based on the Java code text and the third-dimensional information includes: The Java code text is organized according to the target project structure and packaged into a code package, the structure of which matches the project's specifications and requirements.
9. The method for automatically generating code based on the MCP protocol according to claim 4, characterized in that, The step of responding to the client receiving the Java code text signal, invoking the large model to integrate the Java code text into the business layer corresponding to the business name, and obtaining the target code includes: In response to the client receiving the Java code text, the large model is invoked, and according to the hierarchical structure information, the Java code text is inserted into the business modules and classes corresponding to the hierarchical structure to obtain the integrated code; The integrated code is checked for correctness and completeness. If the check conditions are met, the integrated code that meets the conditions is used as the target code. If the check conditions are not met, the integrated code is dynamically corrected, and the corrected integrated code is used as the target code.
10. A system for automatically generating code based on the MCP protocol, characterized in that, include: The building block is used to build a server-side code generation engine based on the MCP protocol. The parsing module is used to respond to the code generation request input by the user, parse the code generation request to obtain the data table structure, service name, business name, and trigger the generation of call instructions; The processing module is used to call the server-side code generation engine to process the data table structure, the service name, and the business name based on the call instruction, and obtain first-dimensional information, second-dimensional information, and third-dimensional information. The code text generation module is used to trigger the generation of Java code text that matches the project based on the first dimension information, the second dimension information, and the third dimension information. The sending module is used to send the Java code text to the client; The integration module is used to respond to the signal that the client receives the Java code text, and call the large model to integrate the Java code text into the business layer corresponding to the business name to obtain the target code.
