Mcp service calling method and device, electronic equipment and storage medium
By introducing a first agent between the agent and the MCP service backend API, and optimizing the template using a template library and a large language model, the problem of insufficient data processing when the agents interact directly is solved, and more accurate data transmission and user information feedback are achieved.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- ASIAINFO TECH CHINA INC
- Filing Date
- 2026-04-30
- Publication Date
- 2026-07-31
AI Technical Summary
In existing technologies, when intelligent agents interact directly with the backend API of MCP services, their data processing capabilities are insufficient, resulting in inaccurate user feedback information.
A first intelligent agent is introduced as an intermediate layer to receive MCP service call requests, obtain call templates, and forward them to the backend API to ensure data format consistency. Templates are optimized through a pre-set template library and a large language model to adapt to user needs.
It improves the accuracy of data retrieval for MCP services, ensuring users receive more accurate information and enhancing user experience and data processing capabilities.
Smart Images

Figure CN122496545A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of computer technology, and more specifically, to an MCP service invocation method, apparatus, electronic device, and storage medium. Background Technology
[0002] The emergence of the Model Context Protocol (MCP) has helped traditional information technology (IT) applications quickly adapt to the artificial intelligence (AI) industry through protocol conversion, greatly improving development efficiency. From a technical standpoint, AI agents can call any IT system interface tool via the MCP protocol. However, existing solutions typically involve agents that interact directly with users calling the backend application programming interface (API) of the relevant MCP service. Since agents that interact directly with users are often designed primarily for user interaction, they often lack the ability to understand the data returned by the backend API, resulting in poor data processing capabilities and ultimately inaccurate information fed back to the user. Summary of the Invention
[0003] The purpose of this application is to at least solve one of the aforementioned technical defects. The technical solution provided by the embodiments of this application is as follows: In a first aspect, embodiments of this application provide an MCP service invocation method, including: Receive the MCP service call request sent by the second intelligent agent; the MCP service call request includes the address information and requirement information of the MCP service, and the requirement information is used to describe the target requirement of the second intelligent agent to call the MCP service; Retrieve the call template corresponding to the MCP service from the preset template library, obtain the backend API of the MCP service based on the address information, and send the requirement information and call template to the backend API; the call template is used to indicate the format of the first call result generated by the backend API; Receive the first call result generated by the backend API based on the call template and requirement information, and send the first call result to the second intelligent agent.
[0004] Secondly, embodiments of this application provide an MCP service invocation device, comprising: The call request receiving module is used to receive the MCP service call request sent by the second intelligent agent; the MCP service call request includes the address information and requirement information of the MCP service, and the requirement information is used to describe the target requirement of the second intelligent agent to call the MCP service; The call request forwarding module is used to obtain the call template corresponding to the MCP service from the preset template library, obtain the backend API of the MCP service based on the address information, and send the request information and call template to the backend API; the call template is used to indicate the format of the first call result generated by the backend API; The call result return module is used to receive the first call result generated by the backend API based on the call template and requirement information, and send the first call result to the second intelligent agent.
[0005] Thirdly, embodiments of this application provide an electronic device, including a memory, a processor, and a computer program stored in the memory; The processor executes a computer program to implement the method provided in the first aspect embodiment or any alternative embodiment of the first aspect.
[0006] Fourthly, embodiments of this application provide a computer-readable storage medium storing a computer program, which, when executed by a processor, implements the method provided in the first aspect embodiment or any optional embodiment of the first aspect.
[0007] The beneficial effects of the technical solutions provided in this application are: By placing a first agent between the second agent that directly interacts with the user and the backend API of the relevant MCP service, the MCP service call is no longer performed directly by the second agent that interacts with the user. Instead, the first agent, specifically designed for calling MCP services, performs the call, avoiding the potential problem of the second agent having poor data processing capabilities returned by the backend API due to its design flaws. Furthermore, when the first agent initiates a call to the backend API of the MCP service, it sends a corresponding call template to the backend API. This allows the backend API to return data in the appropriate format based on the call template, resulting in more accurate data. The first agent then forwards the data returned by the backend API to the second agent, which then displays it to the user, ensuring that the information received by the user is more accurate. Attached Figure Description
[0008] To more clearly illustrate the technical solutions in the embodiments of this application, the accompanying drawings used in the description of the embodiments of this application will be briefly introduced below.
[0009] Figure 1A flowchart illustrating an MCP service invocation method provided in this application embodiment; Figure 2 This is a schematic diagram of the functional architecture of an MCP service invocation method in one example of an embodiment of this application; Figure 3 This is a schematic diagram of the overall process of an MCP service invocation method in an example of an embodiment of this application; Figure 4 A structural block diagram of an MCP service invocation device provided in an embodiment of this application; Figure 5 This is a schematic diagram of the structure of an electronic device provided in an embodiment of this application. Detailed Implementation
[0010] The embodiments of this application are described below with reference to the accompanying drawings. It should be understood that the embodiments described below with reference to the accompanying drawings are exemplary descriptions for explaining the technical solutions of the embodiments of this application, and do not constitute a limitation on the technical solutions of the embodiments of this application.
[0011] Those skilled in the art will understand that, unless specifically stated otherwise, the singular forms “a,” “an,” “the,” and “the” used herein may also include the plural forms. It should be further understood that the terms “comprising” and “including” as used in embodiments of this application mean that the corresponding feature can be implemented as the presented feature, information, data, step, operation, element, and / or component, but do not exclude implementation as other features, information, data, step, operation, element, component, and / or combinations thereof supported by the art. It should be understood that when we say that an element is “connected” or “coupled” to another element, the one element can be directly connected or coupled to the other element, or it can mean that the one element and the other element establish a connection relationship through an intermediate element. Furthermore, “connected” or “coupled” as used herein can include wireless connection or wireless coupling. The term “and / or” as used herein indicates at least one of the items defined by the term; for example, “A and / or B” can be implemented as “A,” or as “B,” or as “A and B.”
[0012] To make the objectives, technical solutions, and advantages of this application clearer, the embodiments of this application will be described in further detail below with reference to the accompanying drawings.
[0013] The technical solutions of this application and their effects are described below through several exemplary embodiments. It should be noted that the following embodiments can be referenced, borrowed from, or combined with each other. Identical terms, similar features, and similar implementation steps in different embodiments will not be repeated.
[0014] Figure 1 This application provides a flowchart illustrating an MCP service invocation method, where the executing entity can be a first intelligent agent, etc. Figure 1 As shown, the method may include: Step S101: Receive the MCP service call request sent by the second intelligent agent; the MCP service call request includes the address information and requirement information of the MCP service, and the requirement information is used to describe the target requirement of the second intelligent agent to call the MCP service.
[0015] In some embodiments, the intelligent agent can be a software program (such as a chatbot) or a physical device (such as a robot), etc. This application does not specifically limit the embodiments. The first intelligent agent does not interact directly with the user, but only interacts with other intelligent agents or the API of the MCP service. The second intelligent agent, however, can directly interact with the user and can only receive data returned by the first intelligent agent. The MCP service call request can be generated by the second intelligent agent after receiving the user's input request information, determining the address information of the corresponding MCP service based on the request information, and then generating the request based on the address information and the request information.
[0016] Specifically, for example, the second agent is a task planning agent, and the first agent is middleware with MCP service scheduling capabilities. The MCP service call request is encapsulated in JSON format, containing the MCP service's address information and requirement information. The address information can be the MCP service's URL or service discovery registration name; the requirement information uses natural language or structured descriptions to express the second agent's target requirement for calling the service, such as "query the weather conditions in Beijing on April 12, 2026, and return the temperature, humidity, and precipitation probability." Upon receiving the request, the first agent parses the address information to locate the target MCP service, and simultaneously parses the requirement information and converts it into the input parameters required by the service, thereby completing the subsequent service call and result return. In this way, the second agent does not need to know the specific interface details of the MCP service in advance; it only needs to clarify its target requirement to trigger a cross-agent service call.
[0017] Step S102: Obtain the call template corresponding to the MCP service from the preset template library, obtain the backend API of the MCP service based on the address information, and send the requirement information and call template to the backend API; the call template is used to indicate the format of the first call result generated by the backend API.
[0018] In some embodiments, the preset template library can be a database used to store call templates corresponding to multiple MCP services. The backend API can be an executable interface of the MCP service. The first call result can be data or text returned by the backend API in response to the request information sent by the first agent, and this application embodiment does not limit it further.
[0019] Specifically, after receiving the MCP service call request from the second agent, the first agent can first parse the service identifier of the MCP service from the call request and obtain the call template corresponding to that service from a preset template library. The preset template library stores call templates corresponding to different MCP services in key-value pairs, with each template defining the mapping rules for request parameters and the format constraints of the expected return result. Based on the address information in the MCP service call request, the first agent obtains the access point to the backend API of the MCP service through service discovery or direct request. Then, the first agent sends the request information along with the call template to the backend API. In this process, the call template instructs the backend API to format the output according to the field structure, data type, and unit defined in the template when generating the first call result. For example, if the call template specifies that the result should include three fields: "temperature," "humidity," and "precipitation_probability," and the temperature unit is degrees Celsius, then even if the backend API has different internal representations, it will convert the final returned result to the format required by the template.
[0020] Step S103: Receive the first call result generated by the backend API based on the call template and requirement information, and send the first call result to the second intelligent agent.
[0021] Specifically, after sending the request information and call template to the backend API, the first intelligent agent waits for the processing result from the backend API. The backend API executes the corresponding business logic based on the received request information and generates the first call result according to the format constraints specified in the call template. For example, if the request information is "Query the weather conditions in Beijing on April 12, 2026", and the call template specifies the result format as {"temperature": value, "unit": "℃", "humidity": value,"precipitation_probability": value}, then after completing the query, the backend API directly outputs a JSON object conforming to the structure of the call template as the first call result. The first intelligent agent receives this first call result via an HTTP response or message queue. Subsequently, the first intelligent agent sends the first call result to the second intelligent agent either as is or after necessary encapsulation. The second intelligent agent can then display the first call result to the user upon receiving it.
[0022] The solution provided in this application sets up a first intelligent agent between the second intelligent agent that directly interacts with the user and the backend API of the relevant MCP service. This ensures that when calling the MCP service, the second intelligent agent, which directly interacts with the user, no longer directly calls the MCP service; instead, the first intelligent agent, specifically designed for calling the MCP service, does so. This avoids the problem of the second intelligent agent having poor data processing capabilities returned by the backend API, which might be due to design flaws. Furthermore, when the first intelligent agent initiates a call to the backend API of the MCP service, it sends a corresponding call template to the backend API. The backend API then returns data in the appropriate format based on the call template, making the returned data more accurate. The first intelligent agent then forwards the data returned by the backend API to the second intelligent agent, which then displays it to the user, thus ensuring that the information obtained by the user is more accurate.
[0023] Based on the above embodiments, as an optional embodiment, the call request may also include user information of the user who initiated the MCP service call request through the second intelligent agent; Retrieve the call template corresponding to the MCP service from the preset template library, specifically including: If a personalized template for the MCP service corresponding to the user information exists in the preset template library, then the personalized template will be selected as the template to be called. If the user information does not have a personalized template for the MCP service in the preset template library, then the standard template corresponding to the MCP service is obtained and determined as the calling template; the standard template is the calling template used when the MCP service is successfully called for the first time. If the standard template corresponding to the MCP service does not exist in the preset template library, the requirement information and preset prompt words will be sent to the preset large language model to instruct the preset large language model to generate a call template based on the requirement information.
[0024] In some embodiments, a personalized template can be a call template tailored to a user's individual needs. It can be designed by the user or improved from an existing call template based on user feedback; this embodiment does not impose such limitations. A standard template can be the default call template used by the second agent when calling the corresponding MCP service. Preset prompts can be prompts used to instruct a preset large language model to generate a call template.
[0025] Specifically, when determining the invocation template corresponding to the MCP service, the first intelligent agent first queries the preset template library and matches it based on user information. User information may include user identifiers. The specific judgment logic is as follows: If a personalized template exists in the preset template library that corresponds to both the user information and the MCP service, the first intelligent agent will identify that personalized template as the calling template. For example, if user A specified in a historical call that the output format of the weather service includes the "feeling temperature" field and the temperature unit is Fahrenheit, the system will generate a unique personalized template for that user. When the user calls the same MCP service again, this personalized template will be used first.
[0026] If the preset template library does not contain a personalized template for the MCP service corresponding to the user's information, the first agent further obtains the standard template corresponding to the MCP service and identifies it as the invocation template. The standard template is the invocation template used when the MCP service is successfully invoked for the first time, representing the general result format of the service. For example, if the template successfully used when the weather service is invoked for the first time contains the fields "temperature" and "humidity," then this template is recorded as the standard template for use by users who have not configured personalized templates.
[0027] If the preset template library contains neither a personalized template corresponding to the user information nor a standard template corresponding to the MCP service (i.e., the MCP service has never been successfully called, and therefore there is no reusable template), the first agent can send the request information and preset prompt words to the preset large language model. The preset prompt words instruct the large language model to automatically generate a well-structured and complete call template based on the natural language description in the request information. For example, if the request information is "Query the weather of a certain city, requiring temperature and precipitation probability," and the preset prompt words are "Please generate a JSON format call template, including field names, data types, and units," the large language model will output {"temperature": {"value": "number", "unit": "℃"}, "precipitation_probability": {"value": "number", "unit": "%"}} as the generated call template. After receiving this template, the first agent stores it in the preset template library as the standard template for the MCP service.
[0028] Through this mechanism, the first agent can flexibly respond to different scenarios: prioritizing personalized user needs, reusing validated standard templates, and dynamically generating new templates to support MCP services. This approach improves template matching efficiency while ensuring the first agent's adaptability to new MCP services.
[0029] Based on the above embodiments, as an optional embodiment, the standard template is generated in the following way: Based on the demand information, a predetermined number of template fragments semantically related to the demand information are retrieved from the preset MCP service knowledge base; the preset MCP service knowledge base stores relevant technical information for each MCP service. Based on the information of each template fragment and the preset requirement information, the template fragments are sent to the preset large language model, so that the preset large language model splices the template fragments based on the preset requirement information to obtain the initial template; Send the preset requirements and initial template to the backend API; If the backend API returns a call result for the preset requirements, the initial template will be used as the standard template. If no call result for the preset requirement information is received from the backend API, the initial template will be adjusted at least once until a call result based on the adjusted initial template and the preset requirement information is received from the backend API. Use the initial template obtained from the last adjustment as the standard template; Each round of adjustments includes: Retrieve call failure information from the backend API regarding the lack of a call result; the call failure information includes the reason why the backend API did not generate a call result; The call failure information and the initial template are sent to the preset large language model so that the preset large language model can adjust the initial call template based on the call failure information; Send the adjusted call template and preset requirement information to the backend API.
[0030] In some embodiments, the preset MCP service knowledge base can also be a database used to store relevant technical information for each MCP service, including but not limited to API documentation, interface specifications, parameter descriptions, example requests and responses, etc. Furthermore, the preset MCP service knowledge base and the preset template library in this application embodiment can share the same database; this application embodiment does not impose any limitations on this. Template fragments can be keywords constituting the calling template, such as "city code parameters," "temperature field format," "humidity value range," etc. Preset requirement information can be requirement information temporarily entered when constructing the standard template for the corresponding MCP service.
[0031] Specifically, standard templates are generated in advance or dynamically in the following way: When a standard template for a certain MCP service needs to be generated, the first intelligent agent obtains the preset requirement information corresponding to that MCP service, and retrieves a preset number of template fragments semantically related to the requirement information from the preset MCP service knowledge base based on the requirement information. For example, for a weather MCP service, the knowledge base may contain template fragments such as "city code parameters," "temperature field format," and "humidity value range." The first intelligent agent can use vector retrieval or keyword matching methods to select the preset number of template fragments most semantically related to the requirement information.
[0032] Subsequently, the first intelligent agent sends the retrieved template fragment information along with the preset requirement information to the preset large language model, and instructs the preset large language model to concatenate the template fragments based on the preset requirement information to obtain an initial template with a complete structure and coherent fields. For example, the large language model combines the "city parameter fragment" and the "temperature unit fragment" into a JSON template in the form of {"city":"string","temperature":{"value":"number","unit":"℃"}}.
[0033] The first agent sends the preset requirements information and the initial template to the backend API of the MCP service to verify whether the initial template can be accepted by the backend API and successfully generate a call result. If the first agent receives the call result returned by the backend API for the preset requirements information, it determines that the initial template is valid and stores it as the standard template of the MCP service in the preset template library.
[0034] If the first agent does not receive a call result from the backend API (e.g., receiving an error code or request timeout), it enters the adjustment process. The first agent retrieves call failure information from the backend API, including the reason why the backend API did not generate a call result, such as "missing required field," "field type should be integer instead of string," or "enumeration value is not within the allowed range," etc. The first agent sends the call failure information and the current initial template to the preset large language model, instructing the large language model to make targeted corrections to the initial template based on the failure reason. After the large language model outputs the corrected initial template, the first agent sends the adjusted initial template and preset requirement information to the backend API again. If a call result is still not received from the backend API, the above steps are repeated until the backend API successfully returns a call result. At this point, the call template obtained from the last adjustment can be determined as the standard template for this MCP service.
[0035] The method provided in the above embodiments combines knowledge base fragments through a pre-set large language model and verifies and corrects them through actual interaction with the backend API, thereby generating accurate and usable calling templates. This method gradually obtains the correct template format through iterative feedback, significantly reducing the threshold and cost of MCP service calls.
[0036] Based on the above embodiments, as an optional embodiment, receiving a first call result generated by the backend API based on the call template and requirement information, and sending the first call result to the second intelligent agent, specifically includes: The first call result is processed into a structured form to generate a structured first call result. The first call result after structured processing is then subjected to contextualized processing to obtain the first call result after contextualized processing. The first call result, after being processed in a contextualized manner, is sent to the second intelligent agent.
[0037] In some embodiments, structured processing includes, but is not limited to: field name standardization, data type conversion, missing field completion, and redundant information filtering, etc.
[0038] Specifically, after receiving the first call result returned by the backend API, the first agent can choose not to directly forward it to the second agent. Instead, it can perform a two-stage processing: structured processing and contextualized processing. First, the first agent performs structured processing on the first call result, generating a structured result. For example, if the original first call result returned by the backend API is {"tmp": "25", "h": "60", "warn":null}, where the fields are abbreviated, the first agent can use structured processing to adjust these abbreviations to complete fields, resulting in a standardized JSON object: {"temperature": 25, "humidity": 60, "unit": "℃"}. Optionally, the structured processing can be performed according to the field mapping rules defined in the call template to ensure that the output format conforms to the unified data specifications within the first agent.
[0039] Subsequently, the first agent performs contextual processing on the structured first call result, obtaining a contextualized first call result. The details of this contextualization will be described in detail below and will not be elaborated upon here. After the contextualization is complete, the first agent sends the contextualized first call result to the second agent. The second agent can then directly display the processed first call result to the user.
[0040] Based on the above embodiments, as an optional embodiment, the first call result after structured processing is further processed in a contextualized manner to obtain a first call result after contextualized processing, specifically including: Extract sub-description information representing the application scenario of the call request from the description information, and send the sub-description information and the structured first call result to the preset large language model so that the large language model can adjust the structured first call result based on the sub-description information to obtain the scenario-based first call result.
[0041] Specifically, after completing the structured processing of the first call result, the first intelligent agent will further perform scenario-based processing. Specifically, the first intelligent agent extracts a natural language description of the application scenario from the MCP service call request sent by the second intelligent agent. The first intelligent agent then identifies and extracts sub-description information representing this description. Application scenarios include, for example, "travel planning," "health management," "smart home control," and "educational tutoring." The sub-description information can be scenario keywords or a short description of the scenario.
[0042] Subsequently, the first intelligent agent sends the extracted sub-description information and the structured first call result together to the preset large language model, and sends corresponding prompt words to the preset large language model to instruct the preset large language model to adjust the structured first call result based on the sub-description information. The adjustment methods may include adding scene-related fields, converting numerical expressions into natural language descriptions, filtering scene-irrelevant fields, or reorganizing the presentation order of information according to the scene, etc., which are not limited in this embodiment.
[0043] Then the first agent can receive the adjusted result returned by the preset large language model, use it as the first call result after contextual processing, and then the first agent sends the result to the second agent.
[0044] Based on the above embodiments, as an optional embodiment, after sending the first call result to the second intelligent agent, the method further includes: Receive feedback information from the second intelligent agent regarding the result of the first call; wherein the feedback information includes user information of the user who initiated the call request; If the feedback indicates that the user is satisfied with the result of the first call, then the generated call template will be determined as the personalized template corresponding to the user information and stored in the preset template library.
[0045] In some embodiments, feedback information may be in the form of a rating (such as a five-star rating, like / dislike, etc.), or it may be a satisfactory or unsatisfactory evaluation. This application embodiment does not limit this.
[0046] After the first intelligent agent sends the context-processed first call result to the second intelligent agent, it further receives feedback information from the second intelligent agent regarding the first call result. The first intelligent agent can then parse the feedback information to determine whether it indicates that the user's evaluation of the first call result is satisfactory. If the result is satisfactory, the first intelligent agent determines the call template used when generating the first call result from the backend API as the personalized template corresponding to the user and stores this personalized template in a preset template library. During storage, a mapping relationship can be established with user information as the key and call template as the key-value pair, so that the personalized template will be used preferentially when the user calls the same MCP service again.
[0047] For example, User B invokes the MCP service for obtaining weather information through a second agent. The first agent returns the corresponding first invocation result using a standard template. At this point, User B clicks the "Helpful" button on the second agent's interface. Based on this, the second agent generates feedback information containing User B's identifier and a satisfaction rating of "Satisfied," and sends it to the first agent. Upon receiving this feedback, the first agent copies the standard template used this time, marks it as User B's personalized template, and stores it in the preset template library. Subsequent times User B invokes the same weather service, the first agent will directly select this personalized template, without needing to go through the template selection or generation process again.
[0048] If the feedback indicates user dissatisfaction, the first agent may choose not to store the template or trigger a template revision process. Through this mechanism, the first agent can continuously optimize the template based on the user's actual experience, thereby improving the accuracy of service calls and user satisfaction.
[0049] Based on the above embodiments, as an optional embodiment, if the feedback information indicates that the user is not satisfied with the first call result, the method further includes: The call template is optimized at least once based on the feedback information until the feedback information indicates that the user is satisfied with the returned second call result; The template obtained from the last optimization operation is stored in the preset template library as the personalized template corresponding to the user information; The optimization operations include: The feedback information and the call template are input into the preset large language model, so that the preset large language model can adjust the call template based on the feedback information to obtain the adjusted call template; Send the adjusted call template and requirement information to the backend API, and receive the second call result generated by the backend API based on the adjusted call template and requirement information; The result of the second call is processed into a structured form to obtain the structured result of the second call. The structured second call result is then subjected to contextual processing to obtain a contextualized second call result. The second call result, after being processed in a specific context, is returned to the second intelligent agent, and feedback information regarding the second call result is received from the second intelligent agent.
[0050] Specifically, the first agent inputs the feedback information and the currently used call template into a preset large language model, allowing the model to adjust the call template based on the reasons for dissatisfaction in the feedback, resulting in an adjusted call template. Subsequently, the first agent sends the adjusted call template and the original requirement information to the backend API and receives the second call result generated by the backend API based on the adjusted template and requirement information. The first agent then performs structured and contextualized processing on this second call result again, obtaining a contextualized second call result, which is then returned to the second agent.
[0051] The first agent receives feedback from the second agent regarding the result of the second call, and determines whether the feedback indicates user satisfaction. If the user is still not satisfied, the optimization process is repeated until the received feedback indicates user satisfaction.
[0052] When the feedback information shows that the user is satisfied after a certain iteration, the first agent determines the call template obtained from the last optimization operation as the personalized template corresponding to the user and stores it in the preset template library.
[0053] Through the above mechanism, the first intelligent agent can dynamically and adaptively modify the calling template, making it continuously approach the personalized needs of specific users, thereby significantly improving the user experience and the personalization of the template library while ensuring the success rate of the call.
[0054] The following is combined Figure 2 The functional architecture of the MCP service invocation method provided in the embodiments of this application is described, such as... Figure 2 As shown in the figure, this embodiment provides an intelligent agent interaction system architecture based on MCP service invocation, specifically including a user evaluation system, a first intelligent agent (i.e., the MCP invocation intelligent agent in the figure), a second intelligent agent, and multiple functional modules. The first intelligent agent and the second intelligent agent interact through the A2A (Agent-to-Agent) communication protocol, where the second intelligent agent acts as the service invoker, and the first intelligent agent acts as the MCP invocation intelligent agent, responsible for the actual discovery, invocation, template management, and optimization of invocation results of the MCP service.
[0055] The first intelligent agent integrates a request optimization module, a failure handling module, a parameter template module, and an API to MCP conversion module. The request optimization module preprocesses received MCP service call requests, including parameter extraction and structure adjustment: parameter extraction extracts key requirement information from the original request, while structure adjustment converts the request format into a unified structure recognizable by downstream modules. The failure handling module captures error information returned by the backend API when an MCP service call fails and triggers a retry or template adjustment process. The parameter template module retrieves a standard or customized template matching the currently invoked MCP service from the MCP template knowledge base, supporting parsing and generation of both YAML and JSON formats. The API to MCP conversion module converts interfaces conforming to the Open API specification into a call format recognizable by the MCP service and enables actual interaction with the backend API.
[0056] The MCP management module is responsible for the full lifecycle management of MCP services, including sub-functions such as MCP discovery, persistent storage, and data updates. The MCP discovery module retrieves relevant service information from the MCP template library through RAG search; the persistent storage module saves verified templates, call records, and user feedback in a pre-defined MCP knowledge base; and the data update module is responsible for periodically synchronizing backend API interface changes and updating the corresponding entries in the template library.
[0057] The user evaluation system operates independently of the intelligent agent. It collects end-user satisfaction ratings (e.g., satisfied / unsatisfied) for each call result and feeds these ratings back to the feedback optimization module of the first intelligent agent. The feedback optimization module triggers iterative optimization of the template based on the feedback information. It adjusts the call template using a pre-defined large language model and then re-calls the backend API via the API to MCP module until the user is satisfied. Finally, the user-satisfied call template is stored as their personalized template in the MCP template knowledge base and associated with the corresponding user information.
[0058] The following is combined Figure 3 This paper describes the overall process of the MCP service invocation method provided in the embodiments of this application, such as... Figure 3 As shown, the MCP service invocation method provided in this application embodiment can be divided into the following steps: Step S1: The second agent sends a call request for a certain MCP service to the first agent. The call request includes the address information of the MCP service and the specific requirements.
[0059] In step S2, the first agent performs standard formatting on the call request sent by the second agent, and then searches for the call template corresponding to the MCP service in the preset database according to the standard formatted call request.
[0060] Step S2.1: If a personalized call template for the user's MCP service exists in the preset template library, then the personalized call template will be directly used as the call template.
[0061] Step S2.2: If the user's personalized call template for the MCP service does not exist in the preset template library, but a standard template for the MCP service does exist, then the standard template is used as the call template.
[0062] Step S2.3: If the preset template library does not contain either the user's personalized call template for the MCP service or the standard template for the MCP service, the first AI will send the standard formatted call request and related prompts to the preset large language model, so that the preset large language model can generate the corresponding call template.
[0063] Step S3: Send the call template to the backend API of the MCP service according to the address information, so that the backend API can generate the corresponding first call result according to the call template and return the first call result to the first intelligent agent.
[0064] Step S4: The first intelligent agent cleans the parameters included in the first call result and performs structured processing on the cleaned parameters to obtain a structured first call result.
[0065] Step S5: The first agent combines the application scenario described in the description information to perform scenario-based processing on the first call result, obtains a first call result that conforms to the application scenario, and returns the scenario-based first call result to the second agent.
[0066] Step S6: Receive feedback information from the user via the second intelligent agent regarding the first call result, send the feedback information and the call template to the large language model, so that the preset large language model can optimize the call template based on the feedback information, and store the optimized call template as the user's personalized template in the preset database.
[0067] Figure 4 A structural block diagram of an MCP service invocation device provided in this application embodiment is shown below. Figure 4 As shown, the MCP service invocation device 400 may include: an invocation request receiving module 401, an invocation request forwarding module 402, and an invocation result returning module 403, wherein, The call request receiving module 401 is used to receive the MCP service call request sent by the second intelligent agent; the MCP service call request includes the address information and requirement information of the MCP service, and the requirement information is used to describe the target requirement of the second intelligent agent to call the MCP service; The call request forwarding module 402 is used to obtain the call template corresponding to the MCP service from the preset template library, obtain the backend API of the MCP service based on the address information, and send the request information and call template to the backend API; the call template is used to indicate the format of the first call result generated by the backend API; The call result return module 403 is used to receive the first call result generated by the backend API based on the call template and requirement information, and send the first call result to the second intelligent agent.
[0068] The solution provided in this application sets up a first intelligent agent between the second intelligent agent that directly interacts with the user and the backend API of the relevant MCP service. This ensures that when calling the MCP service, the second intelligent agent, which directly interacts with the user, no longer directly calls the MCP service; instead, the first intelligent agent, specifically designed for calling the MCP service, does so. This avoids the problem of the second intelligent agent having poor data processing capabilities returned by the backend API, which might be due to design flaws. Furthermore, when the first intelligent agent initiates a call to the backend API of the MCP service, it sends a corresponding call template to the backend API. The backend API then returns data in the appropriate format based on the call template, making the returned data more accurate. The first intelligent agent then forwards the data returned by the backend API to the second intelligent agent, which then displays it to the user, thus ensuring that the information obtained by the user is more accurate.
[0069] Based on the above embodiments, as an optional embodiment, the call request may also include user information of the user who initiated the MCP service call request through the second intelligent agent; The request forwarding module is specifically used for: If a personalized template for the MCP service corresponding to the user information exists in the preset template library, then the personalized template will be selected as the template to be called. If the user information does not have a personalized template for the MCP service in the preset template library, then the standard template corresponding to the MCP service is obtained and determined as the calling template; the standard template is the calling template used when the MCP service is successfully called for the first time. If the standard template corresponding to the MCP service does not exist in the preset template library, the requirement information and preset prompt words will be sent to the preset large language model to instruct the preset large language model to generate a call template based on the requirement information.
[0070] Based on the above embodiments, as an optional embodiment, the device further includes a standard module generation module, specifically used for: Based on the demand information, a predetermined number of template fragments semantically related to the demand information are retrieved from the preset MCP service knowledge base; the preset MCP service knowledge base stores relevant technical information for each MCP service. Based on the information of each template fragment and the preset requirement information, the template fragments are sent to the preset large language model, so that the preset large language model splices the template fragments based on the preset requirement information to obtain the initial template; Send the preset requirements and initial template to the backend API; If the backend API returns a call result for the preset requirements, the initial template will be used as the standard template. If no call result for the preset requirement information is received from the backend API, the initial template will be adjusted at least once until a call result based on the adjusted initial template and the preset requirement information is received from the backend API. Use the initial template obtained from the last adjustment as the standard template; Each round of adjustments includes: Retrieve call failure information from the backend API regarding the lack of a call result; the call failure information includes the reason why the backend API did not generate a call result; The call failure information and the initial template are sent to the preset large language model so that the preset large language model can adjust the initial call template based on the call failure information; Send the adjusted call template and preset requirement information to the backend API.
[0071] Based on the above embodiments, as an optional embodiment, the result return module is specifically used for: The first call result is processed into a structured form to generate a structured first call result. The first call result after structured processing is then subjected to contextualized processing to obtain the first call result after contextualized processing. The first call result, after being processed in a contextualized manner, is sent to the second intelligent agent.
[0072] Based on the above embodiments, as an optional embodiment, the result return module is further used for; Extract sub-description information representing the application scenario of the call request from the description information, and send the sub-description information and the structured first call result to the preset large language model so that the large language model can adjust the structured first call result based on the sub-description information to obtain the scenario-based first call result.
[0073] Based on the above embodiments, as an optional embodiment, the device further includes a template optimization module, specifically used for: Receive feedback information from the second intelligent agent regarding the result of the first call; wherein the feedback information includes user information of the user who initiated the call request; If the feedback indicates that the user is satisfied with the result of the first call, then the generated call template will be determined as the personalized template corresponding to the user information and stored in the preset template library.
[0074] Based on the above embodiments, as an optional embodiment, the template optimization module is further used for: The call template is optimized at least once based on the feedback information until the feedback information indicates that the user is satisfied with the returned second call result; The template obtained from the last optimization operation is stored in the preset template library as the personalized template corresponding to the user information; The optimization operations include: The feedback information and the call template are input into the preset large language model, so that the preset large language model can adjust the call template based on the feedback information to obtain the adjusted call template; Send the adjusted call template and requirement information to the backend API, and receive the second call result generated by the backend API based on the adjusted call template and requirement information; The result of the second call is processed into a structured form to obtain the structured result of the second call. The structured second call result is then subjected to contextual processing to obtain a contextualized second call result. The second call result, after being processed in a specific context, is returned to the second intelligent agent, and feedback information regarding the second call result is received from the second intelligent agent.
[0075] The following is for reference. Figure 5 It illustrates an electronic device suitable for implementing embodiments of this application (e.g., performing...). Figure 1 The diagram shows the structure of the terminal device or server 500 of the method shown. The electronic devices in the embodiments of this application may include, but are not limited to, mobile terminals such as mobile phones, laptops, digital broadcast receivers, PDAs (personal digital assistants), PADs (tablet computers), PMPs (portable multimedia players), in-vehicle terminals (e.g., in-vehicle navigation terminals), wearable devices, etc., as well as fixed terminals such as digital TVs, desktop computers, etc. Figure 5 The electronic device shown is merely an example and should not impose any limitation on the functionality and scope of use of the embodiments of this application.
[0076] The electronic device includes a memory and a processor. The memory stores a program for executing the methods described in the various method embodiments above. The processor is configured to execute the program stored in the memory. The processor may be referred to as processing device 501 as described below. The memory may include at least one of read-only memory (ROM) 502, random access memory (RAM) 503, and storage device 508 as described below, as follows: like Figure 5 As shown, the electronic device 500 may include a processing unit (e.g., a central processing unit, a graphics processing unit, etc.) 501, which can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM) 502 or a program loaded from a storage device 508 into a random access memory (RAM) 503. The RAM 503 also stores various programs and data required for the operation of the electronic device 500. The processing unit 501, ROM 502, and RAM 503 are interconnected via a bus 504. An input / output (I / O) interface 505 is also connected to the bus 504.
[0077] Typically, the following devices can be connected to I / O interface 505: input devices 506 including, for example, touchscreens, touchpads, keyboards, mice, cameras, microphones, accelerometers, gyroscopes, etc.; output devices 507 including, for example, liquid crystal displays (LCDs), speakers, vibrators, etc.; storage devices 508 including, for example, magnetic tapes, hard disks, etc.; and communication devices 509. Communication device 509 allows electronic device 500 to communicate wirelessly or wiredly with other devices to exchange data. Although Figure 5 Electronic devices with various devices are 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 have alternatively.
[0078] Specifically, according to embodiments of this application, the processes described above with reference to the flowcharts can be implemented as computer software programs. For example, embodiments of this application 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 communication device 509, or installed from storage device 508, or installed from ROM 502. When the computer program is executed by processing device 501, it performs the functions defined in the methods of embodiments of this application.
[0079] It should be noted that the computer-readable storage medium described above in this application can be a computer-readable signal medium, a computer-readable storage medium, or any combination of the two. A computer-readable storage medium can be, for example,—but not limited to—an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or any combination thereof. More specific examples of a computer-readable storage medium may include, but are not limited to: an electrical connection having one or more wires, a portable computer disk, a hard disk, random access memory (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM or flash memory), optical fiber, portable compact disk read-only memory (CD-ROM), optical storage device, magnetic storage device, or any suitable combination thereof. In this application, a computer-readable storage medium can be any tangible medium containing or storing a program that can be used by or in conjunction with an instruction execution system, apparatus, or device. In this application, a computer-readable signal medium can include a data signal propagated in baseband or as part of a carrier wave, carrying computer-readable program code. Such propagated data signals can take various forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination thereof. A computer-readable signal medium can be any computer-readable medium other than a computer-readable storage medium, which can send, propagate, or transmit a program for use by or in connection with an instruction execution system, apparatus, or device. The program code contained on the computer-readable medium can be transmitted using any suitable medium, including but not limited to: wires, optical fibers, RF (radio frequency), etc., or any suitable combination thereof.
[0080] In some implementations, clients and servers can communicate using any currently known or future-developed network protocol such as HTTP (Hypertext Transfer Protocol) and can interconnect with digital data communication (e.g., communication networks) of any form or medium. Examples of communication networks include local area networks (“LANs”), wide area networks (“WANs”), the Internet (e.g., the Internet of Things), and peer-to-peer networks (e.g., ad hoc peer-to-peer networks), as well as any currently known or future-developed networks.
[0081] The aforementioned computer-readable medium may be included in the aforementioned electronic device; or it may exist independently and not assembled into the electronic device.
[0082] The aforementioned computer-readable medium carries one or more programs that, when executed by the electronic device, cause the electronic device to: The system receives an MCP service call request from a second agent. The MCP service call request includes the address information and requirement information of the MCP service. The requirement information describes the target requirement of the second agent to call the MCP service. The system retrieves the call template corresponding to the MCP service from a preset template library, obtains the backend API of the MCP service based on the address information, and sends the requirement information and call template to the backend API. The call template is used to indicate the format of the first call result generated by the backend API. The system receives the first call result generated by the backend API based on the call template and requirement information, and sends the first call result to the second agent.
[0083] Computer program code for performing the operations of this application can be written in one or more programming languages or a combination thereof, including but not limited to object-oriented programming languages such as Java, Smalltalk, and C++, as well as conventional procedural programming languages such as the "C" language or similar programming languages. The program code can be executed entirely on the user's computer, partially on the user's computer, as a standalone software package, partially on the user's computer and partially on a remote computer, or entirely on a remote computer or server. In cases involving remote computers, the remote computer can be connected to the user's computer via any type of network—including a local area network (LAN) or a wide area network (WAN)—or can be connected to an external computer (e.g., via the Internet using an Internet service provider).
[0084] The flowcharts and block diagrams in the accompanying drawings illustrate the architecture, functionality, and operation of possible implementations of systems, methods, and computer program products according to various embodiments of this application. In this regard, each block in a flowchart or block diagram may represent a module, segment, or portion of code containing one or more executable instructions for implementing a specified logical function. It should also be noted that in some alternative implementations, the functions indicated in the blocks may occur in a different order than those indicated in the drawings. For example, two consecutively indicated blocks may actually be executed substantially in parallel, and they may sometimes be executed in reverse order, depending on the functions involved. It should also be noted that each block in the block diagrams and / or flowcharts, and combinations of blocks in the block diagrams and / or flowcharts, can be implemented using a dedicated hardware-based system that performs the specified function or operation, or using a combination of dedicated hardware and computer instructions.
[0085] The modules or units described in the embodiments of this application can be implemented in software or hardware. The names of modules or units do not necessarily limit the specific unit; for example, a first constraint acquisition module can also be described as a "module for acquiring the first constraint".
[0086] The functions described above in this document can be performed at least in part by one or more hardware logic components. For example, exemplary types of hardware logic components that can be used, without limitation, include: field-programmable gate arrays (FPGAs), application-specific integrated circuits (ASICs), application-specific standard products (ASSPs), system-on-a-chip (SoCs), complex programmable logic devices (CPLDs), and so on.
[0087] In the context of this application, a machine-readable medium can be a tangible medium that may contain or store a program for use by or in conjunction with an instruction execution system, apparatus, or device. A machine-readable medium can be a machine-readable signal medium or a machine-readable storage medium. Machine-readable media can be, but is not limited to, electronic, magnetic, optical, electromagnetic, infrared, or semiconductor systems, apparatus, or devices, or any suitable combination of the foregoing. More specific examples of machine-readable storage media include electrical connections based on one or more wires, portable computer disks, hard disks, random access memory (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM or flash memory), optical fibers, portable compact disk read-only memory (CD-ROM), optical storage devices, magnetic storage devices, or any suitable combination of the foregoing.
[0088] It should be understood that although the steps in the flowcharts of the accompanying figures are shown sequentially as indicated by the arrows, these steps are not necessarily executed in the order indicated by the arrows. Unless explicitly stated herein, there is no strict order restriction on the execution of these steps, and they can be executed in other orders. Moreover, at least some steps in the flowcharts of the accompanying figures may include multiple sub-steps or multiple stages. These sub-steps or stages are not necessarily completed at the same time, but can be executed at different times, and their execution order is not necessarily sequential, but can be performed alternately or in turn with other steps or at least some of the sub-steps or stages of other steps.
[0089] The above description is only a partial embodiment of the present invention. It should be noted that for those skilled in the art, several improvements and modifications can be made without departing from the principle of the present invention, and these improvements and modifications should also be considered within the scope of protection of the present invention.
Claims
1. A method of invoking a MCP service, characterized by, Applied to the first intelligent agent, including: Receive a Model Context Protocol (MCP) service call request sent by a second intelligent agent; the MCP service call request includes the address information and requirement information of the MCP service, and the requirement information is used to describe the target requirement of the second intelligent agent to call the MCP service; Obtain the call template corresponding to the MCP service from the preset template library, obtain the backend API of the MCP service based on the address information, and send the requirement information and the call template to the backend API; the call template is used to indicate the format of the first call result generated by the backend API; Receive the first call result generated by the backend API based on the call template and the requirement information, and send the first call result to the second intelligent agent.
2. The method of claim 1, wherein, The call request also includes user information of the user who initiated the MCP service call request through the second intelligent agent; The step of obtaining the call template corresponding to the MCP service from the preset template library includes: If the preset template library contains a personalized template for the MCP service corresponding to the user information, then the personalized template is determined as the calling template; If the preset template library does not contain a personalized template for the MCP service corresponding to the user information, then the standard template corresponding to the MCP service is obtained, and the standard template is determined as the calling template; wherein, the standard template is the calling template used when the MCP service is successfully called for the first time; If the preset template library does not contain a standard template corresponding to the MCP service, the requirement information and preset prompt words are sent to the preset large language model to instruct the preset large language model to generate the call template based on the requirement information.
3. The method of claim 2, wherein, The standard template is generated in the following way: Based on the demand information, a predetermined number of template fragments semantically related to the demand information are retrieved from a preset MCP service knowledge base; wherein, the preset MCP service knowledge base stores relevant technical information for each MCP service; Based on the information of each template fragment and the preset requirement information, the template fragments are sent to the preset large language model, so that the preset large language model splices the template fragments based on the preset requirement information to obtain the initial template. Send the preset requirements information and the initial template to the backend API; If the backend API returns a call result for the preset requirement information, then the initial template is used as the standard template. If the backend API does not return a call result for the preset requirement information, the initial template is adjusted at least once until the backend API returns a call result based on the adjusted initial template and the preset requirement information. The initial template obtained from the last adjustment is used as the standard template. Each round of adjustments includes: Obtain call failure information from the backend API regarding the failure to generate the call result; the call failure information includes the reason why the backend API did not generate the call result; The call failure information and the initial template are sent to a preset large language model so that the preset large language model adjusts the initial call template based on the call failure information; The adjusted call template and the preset requirement information are sent to the backend API.
4. The method of claim 1, wherein, The step of receiving the first call result generated by the backend API based on the call template and the requirement information, and sending the first call result to the second intelligent agent, includes: The first call result is processed into a structured form to generate a structured first call result. The first call result after the structured processing is subjected to scenario-based processing to obtain the first call result after scenario-based processing; The first call result after the scenario-based processing is sent to the second intelligent agent.
5. The method of claim 4, wherein, The step of performing scenario-based processing on the first call result after structured processing to obtain a scenario-based first call result includes: Extract sub-description information representing the application scenario of the call request from the description information, and send the sub-description information and the structured first call result to a preset large language model so that the large language model adjusts the structured first call result based on the sub-description information to obtain the scenario-based first call result.
6. The method of claim 1, wherein, After sending the first call result to the second intelligent agent, the method further includes: Receive feedback information from the second intelligent agent regarding the result of the first call; wherein the feedback information includes user information of the user who initiated the call request; If the feedback information indicates that the user is satisfied with the first call result, then the call template will be generated as the personalized template corresponding to the user information and stored in the preset template library.
7. The method of claim 6, wherein, If the feedback information indicates that the user is not satisfied with the first call result, the method further includes: Based on the feedback information, the call template is optimized at least once until the feedback information indicates that the user is satisfied with the returned second call result; The template obtained from the last optimization operation is stored as the personalized template corresponding to the user information in the preset template library; The optimization operations include: The feedback information and the call template are input into a preset large language model, so that the preset large language model adjusts the call template based on the feedback information to obtain an adjusted call template; The adjusted call template and the requirement information are sent to the backend API, and the second call result generated by the backend API based on the adjusted call template and the requirement information is received. The second call result is processed into a structured form to obtain the second call result after structure processing. The structured second call result is then subjected to contextualized processing to obtain a contextualized second call result. The second call result after the scenario-based processing is returned to the second intelligent agent, and feedback information regarding the second call result sent by the second intelligent agent is received.
8. A device for invoking a service of a MCP, characterized in that include: The call request receiving module is used to receive MCP service call requests sent by the second intelligent agent; The MCP service call request includes the address information and requirement information of the MCP service, and the requirement information is used to describe the target requirement of the second intelligent agent to call the MCP service. The request forwarding module is used to obtain the call template corresponding to the MCP service from the preset template library, obtain the backend API of the MCP service based on the address information, and send the requirement information and the call template to the backend API; the call template is used to indicate the format of the first call result generated by the backend API; The call result return module is used to receive the first call result generated by the backend API based on the call template and the requirement information, and send the first call result to the second intelligent agent.
9. An electronic device comprising a memory, a processor, and a computer program stored in the memory, characterized in that, The processor executes the computer program to implement the method of any one of claims 1-7.
10. A computer-readable storage medium having a computer program stored thereon, characterized in that, When the computer program is executed by a processor, it implements the method of any one of claims 1-7.