Connection management method and device based on mcp protocol, electronic equipment, medium and product
By separating the transport proxy instance from the actual transport instance and using a reactive programming framework, combined with an exponential backoff retry strategy, automatic fault recovery and elastic management of the connection between the AI client and the MCP server are achieved. This solves the problem of sending application layer messages to the failed connection after the connection is interrupted, thus improving the stability and availability of the system.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- BEIJING THREATBOOK TECHNOLOGY CO LTD
- Filing Date
- 2026-05-06
- Publication Date
- 2026-06-02
AI Technical Summary
The AI client cannot automatically recover its connection to the MCP server after the connection is interrupted, causing application layer messages to be continuously sent to the failed connection, which affects the availability and stability of the application.
By adopting a design that separates the transport proxy instance from the actual transport instance, a reconnection signal bus and a timed health monitoring task are built through a reactive programming framework. Combined with an exponential backoff retry strategy, a three-stage orderly reconnection operation is achieved, ensuring automatic fault recovery and elastic management of the connection.
It enables automatic fault recovery and elastic management of MCP protocol connections, improves the stability, reliability and overall system availability of communication between the client and the MCP server, and reduces access costs and maintenance difficulty.
Smart Images

Figure CN122137880A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of computer technology, specifically to a connection management method, apparatus, electronic device, readable storage medium, and computer program product based on the MCP protocol. Background Technology
[0002] Currently, with the rapid development of large language model technology, artificial intelligence applications are becoming increasingly widespread. During use, AI needs to invoke external tools and data sources to enhance the capabilities of large language models, thereby achieving complex functions such as automated task execution and intelligent decision-making. In existing technologies, after communication is established between the AI client and the MCP server of an external tool, if the connection is lost due to MCP server restarts or network switching, the client cannot automatically restore the connection. This causes the application layer to continuously send messages on the failed transmission instance without the client's knowledge, leading to tool call failures, task execution interruptions, and other problems, severely impacting the availability and stability of AI applications. Summary of the Invention
[0003] In view of the above problems, this application provides a connection management method, apparatus, electronic device, readable storage medium and computer program product based on the MCP protocol, which can solve the problem in the prior art that the client cannot automatically recover after the connection between the client and the MCP server is interrupted, resulting in the continuous sending of application layer messages to the failed connection.
[0004] Firstly, this application provides a connection management method based on the MCP protocol, applied to a client, including: Create a corresponding transport proxy instance and a first actual transport instance for the MCP server to be connected; wherein, the reference held internally in the transport proxy instance points to the first actual transport instance; A first dual-channel connection is established between the first actual transmission instance and the MCP server. An initial handshake is performed between the first dual-channel connection and the MCP server. The connection status between the client and the MCP server is monitored, and a three-stage ordered reconnection operation is performed when a connection failure is detected. The three-stage ordered reconnection operation includes re-establishing the second actual transmission instance and the second dual-channel connection, re-initializing the handshake based on the second dual-channel connection, and closing the first dual-channel connection. After the three-stage ordered reconnection operation is completed, the reference held inside the transport proxy instance is updated from pointing to the first actual transport instance to pointing to the second actual transport instance.
[0005] In the above technical solution, the method can complete orderly reconnection and seamless switching of internal references when the connection is abnormal by separating the transmission proxy instance and the actual transmission instance. This enables automatic fault recovery and elastic management of MCP protocol connection, and ensures that the upper layer application is unaware of the connection reconstruction process. In this way, it effectively improves the stability, reliability and overall availability of communication between the client and the MCP server.
[0006] In some implementations, the first dual-channel connection includes a first event transmission channel and a first request channel; The first sending event channel is used to receive messages from the MCP server; The first request channel is used to send client messages; The initial handshake includes protocol version negotiation and capability negotiation between the client and the MCP server.
[0007] In the above technical solution, the method can clearly define the responsibilities of sending and receiving in the dual channels and the initialization specifications of the handshake process, ensure that the communication link between the client and the MCP server is clear and reliable, and complete the protocol adaptation and capability alignment at the beginning of the connection establishment, providing effective support for subsequent stable communication.
[0008] In some implementations, after establishing a first dual-channel connection between the first actual transmission instance and the MCP server, the method further includes: Use a multicast signal bus from a reactive programming framework to build a reconnection signal bus; The reactive programming framework is used to create a timed health monitoring task; wherein the timed health monitoring task is used to trigger the detection of the connection status between the client and the MCP server at preset time intervals.
[0009] In the above technical solution, the method can build a non-blocking reconnection signal bus and a timed monitoring task with the help of a reactive programming framework, so as to realize asynchronous perception and periodic detection of connection faults.
[0010] In some implementations, monitoring the connection status between the client and the MCP server includes: The connection status between the client and the MCP server is periodically checked based on a scheduled health monitoring task. When an abnormality is detected in the connection status between the client and the MCP server, a target detection request is sent to the MCP server. If no response is received from the MCP server within the preset timeout period, the delay time for this retry is calculated based on the exponential backoff retry strategy and the preset initial delay. If no response is received from the MCP server after all probe retries have been performed according to the aforementioned delay time, it is determined that the connection between the client and the MCP server has failed, and the step of performing the three-stage ordered reconnection operation is triggered.
[0011] In the above technical solution, the method can accurately identify connection anomalies through periodic monitoring and MCP protocol layer detection, and effectively filter instantaneous network fluctuations by combining the exponential backoff retry strategy. While avoiding invalid reconnections, it can accurately determine real faults and provide reliable triggering conditions for orderly reconnection.
[0012] In some implementations, sending a target detection request to the MCP server includes: Construct an initial probe request; The initial detection request is serialized to obtain the target detection request; Based on the first dual-channel connection, the target detection request is sent to the MCP server.
[0013] In the above technical solution, the method can standardize the construction, serialization and dual-channel transmission process of probe requests, thereby ensuring that the probe message format is compliant and the transmission is reliable, thus improving the accuracy of connection status detection.
[0014] In some embodiments, the method further includes: After determining that the connection between the client and the MCP server has failed, a reconnection signal is sent through the decoupled reconnection signal bus. When the reconnection signal is detected through the reconnection signal bus, the step of performing the three-stage ordered reconnection operation is triggered.
[0015] In the above technical solution, the method can decouple fault perception and reconnection execution through the reconnection signal bus, and promptly initiate the reconnection process after identifying the connection fault, ensuring timely fault handling response and clear logic.
[0016] In some implementations, the three-phase ordered reconnection operation includes: Obtain the pre-saved network client builder and message processor based on the transport proxy instance; A second actual transmission instance is established based on the network client builder and message processor; A second dual-channel connection is established between the second actual transmission instance and the MCP server. Based on the second dual-channel connection, the initial handshake between the client and the MCP server is re-executed; After the initial handshake between the client and the MCP server is completed based on the second dual-channel connection, the first dual-channel connection is closed and the resources occupied by the first dual-channel connection are released.
[0017] In the above technical solution, the method can realize layered and orderly reconnection execution, ensure that the transmission link and protocol state are restored synchronously, and avoid resource leakage.
[0018] Secondly, this application provides a connection management device based on the MCP protocol, comprising: A creation unit is used to create a corresponding transport proxy instance and a first actual transport instance for the MCP server to be connected; wherein, the reference held internally in the transport proxy instance points to the first actual transport instance; A connection establishment unit is used to establish a first dual-channel connection between the first actual transmission instance and the MCP server. The initial handshake unit is used to perform an initial handshake between the first dual-channel connection and the MCP server. The monitoring unit is used to monitor the connection status between the client and the MCP server; The reconnection unit is used to perform a three-stage ordered reconnection operation when a connection failure is detected; wherein, the three-stage ordered reconnection operation includes re-establishing the second actual transmission instance and the second dual-channel connection, re-initializing the handshake based on the second dual-channel connection, and closing the first dual-channel connection. The update unit is used to update the reference held inside the transport proxy instance from pointing to the first actual transport instance to pointing to the second actual transport instance after the three-stage ordered reconnection operation is completed.
[0019] In the above technical solution, the device can complete orderly reconnection and seamlessly switch internal references when the connection is abnormal by separating the transmission proxy instance and the actual transmission instance. This enables automatic fault recovery and elastic management of MCP protocol connection, and ensures that the upper layer application is unaware of the connection reconstruction process. In this way, it effectively improves the stability, reliability and overall availability of communication between the client and the MCP server.
[0020] In some embodiments, the reconnection unit includes: The acquisition subunit is used to acquire a pre-saved network client builder and message processor based on the transport proxy instance; The first establishment subunit is used to establish a second actual transmission instance based on the network client builder and message processor; The second establishment subunit is used to establish a second dual-channel connection between the second actual transmission instance and the MCP server. The handshake subunit is used to re-execute the initial handshake between the client and the MCP server based on the second dual-channel connection; The closing unit is used to close the first dual-channel connection and release the resources occupied by the first dual-channel connection after the handshake subunit completes the initial handshake between the client and the MCP server based on the second dual-channel connection.
[0021] In the above technical solution, the device can realize layered and orderly reconnection execution, ensuring that the transmission link and protocol state are restored synchronously, while avoiding resource leakage.
[0022] Thirdly, this application provides an electronic device, which includes a memory and a processor. The memory stores a computer program, and the processor runs the computer program to enable the electronic device to perform the connection management method based on the MCP protocol as described in any one of the first aspects.
[0023] Fourthly, this application provides a readable storage medium storing a computer program, which, when executed by a processor, performs the connection management method based on the MCP protocol as described in the first aspect.
[0024] Fifthly, this application provides a computer program product, which includes a computer program that, when executed by a processor, performs the connection management method based on the MCP protocol as described in the first aspect.
[0025] The beneficial effects of this application are as follows: it enables end-to-end elastic connectivity covering fault detection, automatic reconnection, and protocol state recovery, thereby effectively addressing scenarios such as server restarts and network fluctuations to improve system availability; simultaneously, through hot replacement of transmission instances, it makes connection recovery completely transparent to upper-layer AI Agent applications, significantly reducing access costs and maintenance difficulty; by using Ping at the MCP protocol layer for application-level health monitoring, combined with an exponential backoff retry strategy, it can accurately identify the true availability status of services and avoid invalid reconnections; relying on the ordered three-stage reconnection mechanism to strictly guarantee the execution order, it can also ensure that the transport layer and protocol layer states recover synchronously, avoiding connection inconsistency issues; in addition, based on zero-configuration rapid access, it can also independently create monitoring and reconnection components for different MCP server connections, thereby further improving the stability and ease of use of this method. Attached Figure Description
[0026] To more clearly illustrate the technical solutions of the embodiments of this application, the accompanying drawings used in the embodiments of this application will be briefly introduced below. It should be understood that the following drawings only show some embodiments of this application and should not be regarded as a limitation of the scope. For those skilled in the art, other related drawings can be obtained based on these drawings without creative effort.
[0027] Figure 1 This is a flowchart illustrating a connection management method based on the MCP protocol in some embodiments of this application; Figure 2 This is a schematic diagram of the structure of a connection management device based on the MCP protocol in some embodiments of this application; Figure 3 This is a schematic diagram of the structure of an electronic device in some embodiments of this application. Detailed Implementation
[0028] The embodiments of the technical solution of this application will now be described in detail with reference to the accompanying drawings. These embodiments are only used to more clearly illustrate the technical solution of this application and are therefore merely examples, and should not be used to limit the scope of protection of this application.
[0029] Unless otherwise defined, all technical and scientific terms used herein have the same meaning as commonly understood by one of ordinary skill in the art to which this application pertains; the terminology used herein is for the purpose of describing particular embodiments only and is not intended to limit the application; the terms “comprising” and “having”, and any variations thereof, in the specification, claims, and foregoing description of the drawings are intended to cover non-exclusive inclusion.
[0030] In the description of the embodiments of this application, technical terms such as "first" and "second" are used only to distinguish different objects and should not be construed as indicating or implying relative importance or implicitly specifying the number, specific order, or primary and secondary relationship of the indicated technical features. In the description of the embodiments of this application, "multiple" means two or more (including two), similarly, "multiple sets" refers to two or more sets (including two sets), and "multiple pieces" refers to two or more pieces (including two pieces) unless otherwise explicitly defined.
[0031] In this document, the term "embodiment" means that a particular feature, structure, or characteristic described in connection with an embodiment may be included in at least one embodiment of this application. The appearance of this phrase in various places throughout the specification does not necessarily refer to the same embodiment, nor is it a separate or alternative embodiment mutually exclusive with other embodiments. It will be explicitly and implicitly understood by those skilled in the art that the embodiments described herein can be combined with other embodiments.
[0032] In the description of the embodiments in this application, the term "and / or" is merely a description of the relationship between related objects, indicating that three relationships can exist. For example, A and / or B can represent: A existing alone, A and B existing simultaneously, and B existing alone. Additionally, the character " / " in this document generally indicates that the preceding and following related objects have an "or" relationship.
[0033] To address the existing technical problems, this application provides a connection management method based on the MCP protocol. Specifically, in terms of the connection management architecture, this method employs a proxy pattern combined with volatile (Java keyword) semantics to construct a reactive signal bus-driven hot-swap mechanism for transmission instances. Simultaneously, by decoupling fault detection and reconnection execution logic from the signal bus through stable external proxy instance references, it achieves independent module evolution and flexible expansion of fault sources, and relies on volatile to ensure the immediate visibility of instance switching under multi-threading. In health monitoring, this method uses the built-in detection method of the MCP protocol to realize application-level service detection. It can not only be processed by the complete protocol stack to accurately reflect the actual processing capacity from end to end, but also balance the detection sensitivity and false judgment rate through the exponential backoff retry strategy, further ensuring the continuous and stable operation of the monitoring link. In terms of protocol state recovery, this method designs a three-stage ordered reconnection process of connection establishment, protocol initialization, and resource cleanup, and ensures that the execution order is strictly controllable through reactive orchestration. This allows for the simultaneous reconstruction of the complete protocol state, such as protocol version negotiation, capability declaration, and tool list, while restoring the underlying SSE transmission connection, effectively avoiding hidden business call failures caused by inconsistencies in inter-layer states.
[0034] like Figure 1 As shown, some embodiments of this application provide a connection management method based on the MCP protocol, which is applied to a client and includes: S100. Create a corresponding transport proxy instance and a first actual transport instance for the MCP server to be connected; wherein, the reference held internally in the transport proxy instance points to the first actual transport instance.
[0035] In this embodiment, the MCP server is a server that provides services related to the model control protocol. It is used to establish a communication connection with the client and respond to the client's requests for protocol interaction, capability negotiation, and function calls.
[0036] In this embodiment, the method can construct a reconnectable transport proxy instance using the proxy pattern. This proxy instance inherits from the standard transport instance, internally holds a reference to the actual transport instance modified by the volatile (Java keyword) keyword, and delegates all external interfaces to the currently active actual transport instance for execution.
[0037] In this embodiment, the transport proxy instance has a built-in reactive reconnection signal bus, a network client builder, a serializer, a message processor, and an associated MCP client instance, thus forming a complete reconnectable communication component.
[0038] In this embodiment, the upper-layer application always holds a stable reference to the proxy instance and is completely unaware of the switching process of the internal transport instance.
[0039] In this embodiment, the volatile semantics can ensure the immediate visibility of transport instance switching in a multi-threaded environment, ensuring that all request threads can be quickly routed to the new instance.
[0040] S200. Establish a first dual-channel connection between the first actual transmission instance and the MCP server.
[0041] In this embodiment, the method can establish an SSE dual-channel connection by calling the connection method of the first actual transmission instance; the SSE dual-channel includes an SSE channel and an HTTP channel.
[0042] The SSE channel is used to receive messages from the server, and the HTTP request channel is used to send messages to the client. Together, the two channels form a bidirectional communication link.
[0043] In this embodiment, the above connection method uniformly completes the dual-channel establishment and reconnection signal subscription operations, realizing the integrated configuration of connection initialization and reconnection mechanism.
[0044] S300 uses a multicast signal bus built with a reactive programming framework to construct a reconnection signal bus.
[0045] In this embodiment, the method can construct a non-blocking reconnection signal bus in multicast mode based on a reactive programming framework, thereby achieving decoupling design of fault detection and reconnection execution logic through the signal bus while configuring the backpressure buffer strategy.
[0046] In this embodiment, the signal sender and signal processor are independent of each other and can evolve independently, and the reconnection process is executed asynchronously on an independent scheduler. Based on this, the method can effectively avoid blocking the main event loop and ensure the smoothness of the overall communication link.
[0047] For example, the reconnection signal bus is bound to a dedicated reconnection processor, which strictly guarantees the execution order of the three-stage reconnection through reactive orchestration to avoid inconsistency issues.
[0048] S400. Use a reactive programming framework to create a scheduled health monitoring task; the scheduled health monitoring task is used to trigger the detection of the connection status between the client and the MCP server at preset time intervals.
[0049] In this embodiment, the method can create periodic monitoring tasks through a responsive timer trigger and continuously trigger the connection detection process at configurable fixed time intervals to ensure that the connection status can be continuously monitored.
[0050] In this embodiment, the timed health monitoring task is implemented by a separate MCP connected to a health monitor, which has built-in configurable detection interval, timeout threshold, exponential backoff delay and maximum number of retries.
[0051] S500 performs an initial handshake with the MCP server based on the first dual-channel connection.
[0052] In this embodiment, the first dual-channel connection includes a first event sending channel and a first request channel; the first event sending channel is used to receive messages from the MCP server; and the first request channel is used to send messages from the client.
[0053] In this embodiment, the method can complete protocol version negotiation and client-server capability negotiation through initial handshake, and automatically obtain the list of tools registered by the server, so that the client and server can reach a consistent communication state.
[0054] S600: Monitor the connection status between the client and the MCP server, and when a connection failure is detected, perform a three-stage ordered reconnection operation; wherein, the three-stage ordered reconnection operation includes re-establishing the second actual transmission instance and the second dual-channel connection, re-initializing the handshake based on the second dual-channel connection, and closing the first dual-channel connection.
[0055] For example, the three-phase ordered reconnection operation includes: Phase 1: Create a second actual transmission instance and establish a second dual-channel connection with the MCP server through this new instance; Phase 2: Based on the new second dual-channel connection, re-execute the initial handshake between the client and the MCP server (protocol version negotiation, capability negotiation, etc.). Phase 3: Close the original first dual-channel connection and release the resources occupied by the old connection.
[0056] In this embodiment, the initial handshake includes protocol version negotiation and capability negotiation between the client and the MCP server.
[0057] In this embodiment, the method can perform health checks using the application layer Ping method built into the MCP protocol. This check follows a complete JSON-RPC (a lightweight, JSON-based remote procedure call protocol) request-response chain, enabling it to identify anomalies such as service spoofing and internal faults that cannot be detected by TCP layer (Transmission Control Protocol) probes.
[0058] In this embodiment, a single detection is terminated normally to avoid interruption of the monitoring link due to a single abnormality and to ensure the continuous operation of the monitoring task.
[0059] In this embodiment, when the detection fails, the monitoring process only ends the current retry lifecycle and does not throw an exception to interrupt the overall monitoring link, thus ensuring the continuous and stable operation of the connection monitoring.
[0060] In this embodiment, the reconnection signal is sent through an asynchronous thread, which does not block the core monitoring process and improves the system's concurrent processing capability.
[0061] S700 After the three-phase ordered reconnection operation is completed, the reference held inside the transport agent instance is updated from pointing to the first actual transport instance to pointing to the second actual transport instance.
[0062] In this embodiment, the method leverages the memory visibility guarantee of volatile to automatically route all subsequent requests to the new actual transport instance. Throughout the reconnection process, the reference to the proxy instance remains unchanged, and the upper-layer application does not need to be aware of the internal instance switch, thus achieving a seamless and transparent recovery.
[0063] In this embodiment, the transmission instance reference update is completed before the new connection is established, and request anomalies within the short window period can be automatically recovered through the upper-layer retry mechanism.
[0064] In this embodiment, if any stage of the three-stage reconnection fails, the subsequent stages will stop executing and capture and record the exception to prevent error propagation from causing the overall process to crash.
[0065] In the above embodiments, the method can achieve orderly reconnection and seamless switching of internal references when the connection is abnormal by separating the transport proxy instance and the actual transport instance. This enables automatic fault recovery and elastic management of the MCP protocol connection, and ensures that the upper layer application is unaware of the connection reconstruction process. In this way, it effectively improves the stability, reliability and overall availability of communication between the client and the MCP server.
[0066] In some embodiments, step S600 may include: S610: Periodically check the connection status between the client and the MCP server based on the timed health monitoring task.
[0067] In this embodiment, the timed task continuously triggers Ping probes at the MCP protocol layer according to a preset cycle, thereby achieving uninterrupted connection status inspection.
[0068] S620. When an abnormality is detected in the connection status between the client and the MCP server, a target detection request is sent to the MCP server.
[0069] In this embodiment, the method can initiate application-layer probing after monitoring is triggered to truly reflect the actual processing capabilities of the server.
[0070] To improve the accuracy of connection status detection, step S620 may further include: S621. Construct an initial probe request.
[0071] In this embodiment, the method can construct an initial probe request of type Ping in accordance with the MCP protocol specification.
[0072] S622. The initial probe request is serialized to obtain the target probe request.
[0073] In this embodiment, the method can serialize the probe request into JSON-RPC format to adapt to protocol transmission requirements.
[0074] S623. Based on the first dual-channel connection, the target detection request is sent to the MCP server.
[0075] In this embodiment, the method can complete the sending of probe requests and the receiving of responses through the established dual channels.
[0076] S630. If no response information is received from the MCP server within the preset timeout period, calculate the delay time for this retry based on the exponential backoff retry strategy and the preset initial delay.
[0077] In this embodiment, the method can employ an exponentially increasing delay for retries. The delay time increases exponentially and has an upper limit, filtering out instantaneous network jitter while preventing the delay from expanding indefinitely.
[0078] S640. After performing all probe retries according to the delay time, if no response information is received from the MCP server, it is determined that the connection status between the client and the MCP server has failed, and a reconnection signal is sent through the decoupled reconnection signal bus.
[0079] In this embodiment, the fault detection and reconnection execution processes are decoupled. Specifically, this method sends a reconnection signal through a decoupled reconnection signal bus, avoiding the fault detection process directly invoking the reconnection mechanism.
[0080] In this embodiment, the method can send a trigger signal to the reconnection signal bus to initiate the reconnection process after confirming the existence of the fault through multiple retries.
[0081] S650: When a reconnection signal is detected through the reconnection signal bus, a three-stage ordered reconnection operation is performed.
[0082] In this embodiment, the method can asynchronously start an ordered reconnection process after the reconnection signal is received by the subscriber, ensuring that normal business scheduling is not affected.
[0083] To achieve hierarchical and ordered reconnection execution, step S650 may further include: S651. Obtain the pre-saved network client builder and message processor based on the transport agent instance.
[0084] In this embodiment, the method can reuse the original configuration parameters to ensure that the new instance behaves consistently with the original instance.
[0085] S652. Establish a second actual transport instance based on the network client builder and message processor.
[0086] In this embodiment, the method can create entirely new actual transmission instances that do not interfere with the original instances.
[0087] S653. Establish a second dual-channel connection between the second actual transmission instance and the MCP server.
[0088] In this embodiment, the method can establish an independent SSE bidirectional channel for new transmission instances and complete the underlying transmission recovery.
[0089] S654. Re-execute the initial handshake between the client and the MCP server based on the second dual-channel connection.
[0090] In this embodiment, the method can re-complete the protocol version negotiation, capability declaration exchange, and tool list synchronization, ensuring that the protocol layer state is fully synchronized with the new transport connection.
[0091] S655. After completing the initial handshake between the client and the MCP server based on the second dual-channel connection, close the first dual-channel connection and release the resources occupied by the first dual-channel connection.
[0092] In this embodiment, the method can seamlessly close old connections and asynchronously release related resources such as connection pools and event sources without blocking the use of new connections.
[0093] In the above embodiments, the method can decouple fault perception and reconnection execution through the reconnection signal bus, and promptly initiate the reconnection process after identifying the connection fault, ensuring timely fault handling response and clear logic.
[0094] In some embodiments, in a multi-MCP server connection scenario, each server connection is configured with an independent transport proxy instance and health monitor. The connection monitoring and reconnection processes are isolated from each other and do not interfere with each other.
[0095] In this embodiment, the method can automatically create corresponding instances and complete associations based on the configuration through an automatic assembly mechanism, thereby achieving zero-configuration access and independent elastic recovery.
[0096] In this embodiment, the method can traverse the MCP server connection list in the configuration file by custom auto-configuration class, independently create a reconnectable transport agent and health monitor for each connection, and bind the MCP client by name, thereby achieving seamless integration in multi-server scenarios.
[0097] For example, the connection management method based on the MCP protocol may include: Phase 1: Initialization and Connection Establishment Phase S1. Create a reconnectable transport proxy instance (ReconnectableWebFluxSseClient) Transport); internally, it holds a volatile reference that points to the actual transport instance.
[0098] S2. Establish an SSE connection through an actual transmission instance and complete the MCP protocol initialization handshake (connect + initialize).
[0099] S3. Initialize the reactive reconnection signal bus (Sinks.Many) and subscribe to the reconnection signal trigger.
[0100] Phase Two: Proactive Health Monitoring Phase S4. Start the scheduled health monitoring task (Flux.interval, default fixed interval of 5 seconds, configurable) to enter the active health monitoring process.
[0101] S5. Send an MCP protocol layer Ping probe (call mcpClient.ping()). This probe is an application-level health check, which is different from the traditional TCP layer probe.
[0102] S6. Determine if the Ping response is within the preset timeout threshold (default 1 second, configurable). If yes (detection successful), return to S4 and wait for the next timed trigger to resume health monitoring. If no (timeout / abnormal), proceed to the fault detection and retry process.
[0103] Phase 3: Fault Detection and Retry Phase S7. Execute the exponential backoff retry strategy, with a delay time of 2 seconds. n Calculated in seconds, with a maximum delay capped at 10 seconds (initial delay and cap value are configurable).
[0104] S8. Determine if the number of retries has been exhausted. If not (continue retries), return to S5 and resend the Ping probe. If yes (retries exhausted), confirm that there is a real connection failure and trigger the reconnection process.
[0105] Phase 4: Three-Phase Ordered Reconnection Phase S9. Send a reconnection signal via the Sinks.Many signal bus to trigger the reconnection execution logic.
[0106] S10 [One of the three stages]: Create a new WebFluxSseClientTransport instance and pass in the original handler to establish a new SSE connection.
[0107] S11 [Phase Two of Three] Execute MCPClient reinitialization, renegotiate protocol version and capabilities, and refresh the server-side tool list.
[0108] S12 [Phase 3]: Asynchronously and gracefully shut down old transport instances, releasing old SSE connection resources without blocking the normal use of new connections.
[0109] Phase 5: Instance Switchover and Recovery Monitoring Phase S13. Update the volatile-modified realTransport reference to point to the new instance; the entire process is completely transparent and imperceptible to the upper-layer Agent application.
[0110] After completing the reference update, return to S4, resume scheduled health monitoring, and enter the next monitoring cycle.
[0111] like Figure 2 As shown, some embodiments of this application provide a schematic diagram of a connection management device based on the MCP protocol. It should be understood that this device is related to... Figure 1 The method executed in the middle corresponds to the steps involved in the aforementioned method. The specific functions and effects of the device can be found in the description above. To avoid repetition, detailed descriptions are omitted here.
[0112] The connection management device based on the MCP protocol includes: Creation unit 810 is used to create a corresponding transport proxy instance and a first actual transport instance for the MCP server to be connected; wherein, the reference held internally in the transport proxy instance points to the first actual transport instance; Establishment unit 820 is used to establish a first dual-channel connection between the first actual transmission instance and the MCP server; The initial handshake unit 830 is used to perform an initial handshake between the first dual-channel connection and the MCP server. Monitoring unit 840 is used to monitor the connection status between the client and the MCP server; The reconnection unit 850 is used to perform a three-stage ordered reconnection operation when a connection failure is detected; wherein, the three-stage ordered reconnection operation includes re-establishing the second actual transmission instance and the second dual-channel connection, re-initializing the handshake based on the second dual-channel connection, and closing the first dual-channel connection. Update unit 860 is used to update the reference held inside the transport agent instance from pointing to the first actual transport instance to pointing to the second actual transport instance after the three-phase ordered reconnection operation is completed.
[0113] In some embodiments, the first dual-channel connection includes a first event transmission channel and a first request channel; The first event sending channel is used to receive messages from the MCP server; The first request channel is used to send client messages; The initial handshake includes protocol version negotiation and capability negotiation between the client and the MCP server.
[0114] In some embodiments, the connection management device based on the MCP protocol further includes: The construction unit 870 is used to construct a reconnection signal bus using the multicast signal bus of the reactive programming framework after the establishment unit 820 establishes a first dual-channel connection with the MCP server through the first actual transmission instance. Building unit 870 is also used to create timed health monitoring tasks using a reactive programming framework; wherein, the timed health monitoring task is used to trigger the detection of the connection status between the client and the MCP server at preset time intervals.
[0115] In some embodiments, the monitoring unit 840 includes: Inspection subunit 841 is used to periodically check the connection status between the client and the MCP server based on a timed health monitoring task; The sending subunit 842 is used to send a target detection request to the MCP server when an abnormality is detected in the connection status between the client and the MCP server. The calculation subunit 843 is used to calculate the delay time of this retry based on the exponential backoff retry strategy and the preset initial delay when no response information is received from the MCP server within the preset timeout period. The determination subunit 844 is used to determine that the connection status between the client and the MCP server has failed if no response information is received from the MCP server after all probe retries have been performed according to the delay time, and to trigger the three-stage ordered reconnection operation.
[0116] In some embodiments, the sending subunit 842 is specifically used to construct an initial probe request; serialize the initial probe request to obtain a target probe request; and send the target probe request to the MCP server based on the first dual-channel connection.
[0117] In some embodiments, the determining subunit 844 is specifically used to send a reconnection signal through a decoupled reconnection signal bus after determining that the connection status between the client and the MCP server has failed; and to perform a three-stage ordered reconnection operation when a reconnection signal is detected through the reconnection signal bus.
[0118] In some embodiments, the reconnection unit 850 includes: Acquire subunit 851, which is used to obtain a pre-saved network client builder and message processor based on the transport proxy instance; The first establishment subunit 852 is used to establish a second actual transmission instance based on the network client builder and message processor; The second establishment subunit 853 is used to establish a second dual-channel connection between the second actual transmission instance and the MCP server. Handshake subunit 854 is used to re-execute the initial handshake between the client and the MCP server based on the second dual-channel connection; The closing unit 855 is used to close the first dual-channel connection and release the resources occupied by the first dual-channel connection after the handshake subunit 854 completes the initial handshake between the client and the MCP server based on the second dual-channel connection.
[0119] like Figure 3 As shown, this application provides an electronic device 900, which includes a processor 901 and a memory 902. The processor 901 and the memory 902 are interconnected and communicate with each other through a communication bus 903 and / or other forms of connection mechanism (not shown). The memory 902 stores a computer program that can be executed by the processor 901. When the computing device is running, the processor 901 executes the computer program to perform the method in any of the aforementioned optional implementations.
[0120] This application provides a computer-readable storage medium storing a computer program, which, when executed by a processor, performs the method in any of the aforementioned optional implementations.
[0121] The computer-readable storage medium can be implemented by any type of volatile or non-volatile storage device or a combination thereof, such as Static Random Access Memory (SRAM), Electrically Erasable Programmable Read-Only Memory (EEPROM), Erasable Programmable Read Only Memory (EPROM), Programmable Red-Only Memory (PROM), Read-Only Memory (ROM), magnetic storage, flash memory, magnetic disk, or optical disk.
[0122] This application provides a computer program product, which includes a computer program that, when run by a processor, executes the method in any of the aforementioned optional implementations.
[0123] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of this application, and not to limit them. Although this application has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that modifications can still be made to the technical solutions described in the foregoing embodiments, or equivalent substitutions can be made to some or all of the technical features therein. These modifications or substitutions do not cause the essence of the corresponding technical solutions to deviate from the scope of the technical solutions of the embodiments of this application, and they should all be covered within the scope of the claims and specification of this application. In particular, as long as there is no conflict, the various technical features mentioned in the embodiments can be combined in any way. This application is not limited to the specific embodiments disclosed herein, but includes all technical solutions falling within the scope of the claims.
Claims
1. A connection management method based on the MCP protocol, characterized in that, Applied to the client side, including: Create a corresponding transport proxy instance and a first actual transport instance for the MCP server to be connected; wherein, the reference held internally in the transport proxy instance points to the first actual transport instance; A first dual-channel connection is established between the first actual transmission instance and the MCP server. An initial handshake is performed between the first dual-channel connection and the MCP server. The connection status between the client and the MCP server is monitored, and a three-stage ordered reconnection operation is performed when a connection failure is detected. The three-stage ordered reconnection operation includes re-establishing the second actual transmission instance and the second dual-channel connection, re-initializing the handshake based on the second dual-channel connection, and closing the first dual-channel connection. After the three-stage ordered reconnection operation is completed, the reference held inside the transport proxy instance is updated from pointing to the first actual transport instance to pointing to the second actual transport instance.
2. The connection management method based on the MCP protocol according to claim 1, characterized in that, The first dual-channel connection includes a first event transmission channel and a first request channel; The first sending event channel is used to receive messages from the MCP server; The first request channel is used to send client messages; The initial handshake includes protocol version negotiation and capability negotiation between the client and the MCP server.
3. The connection management method based on the MCP protocol according to claim 1, characterized in that, After establishing a first dual-channel connection between the first actual transmission instance and the MCP server, the method further includes: Use a multicast signal bus from a reactive programming framework to build a reconnection signal bus; The reactive programming framework is used to create a timed health monitoring task; wherein the timed health monitoring task is used to trigger the detection of the connection status between the client and the MCP server at preset time intervals.
4. The connection management method based on the MCP protocol according to claim 1, characterized in that, The monitoring of the connection status between the client and the MCP server includes: The connection status between the client and the MCP server is periodically checked based on a scheduled health monitoring task. When an abnormality is detected in the connection status between the client and the MCP server, a target detection request is sent to the MCP server. If no response is received from the MCP server within the preset timeout period, the delay time for this retry is calculated based on the exponential backoff retry strategy and the preset initial delay. If no response is received from the MCP server after all probe retries have been performed according to the aforementioned delay time, it is determined that the connection between the client and the MCP server has failed, and the step of performing the three-stage ordered reconnection operation is triggered.
5. The connection management method based on the MCP protocol according to claim 4, characterized in that, Sending a target detection request to the MCP server includes: Construct an initial probe request; The initial detection request is serialized to obtain the target detection request; Based on the first dual-channel connection, the target detection request is sent to the MCP server.
6. The connection management method based on the MCP protocol according to claim 4, characterized in that, The method further includes: After determining that the connection between the client and the MCP server has failed, a reconnection signal is sent through the decoupled reconnection signal bus. When the reconnection signal is detected through the reconnection signal bus, the step of performing the three-stage ordered reconnection operation is triggered.
7. The connection management method based on the MCP protocol according to claim 1, characterized in that, The three-phase ordered reconnection operation includes: Obtain the pre-saved network client builder and message processor based on the transport proxy instance; A second actual transmission instance is established based on the network client builder and message processor; A second dual-channel connection is established between the second actual transmission instance and the MCP server. Based on the second dual-channel connection, the initial handshake between the client and the MCP server is re-executed; After the initial handshake between the client and the MCP server is completed based on the second dual-channel connection, the first dual-channel connection is closed and the resources occupied by the first dual-channel connection are released.
8. A connection management device based on the MCP protocol, characterized in that, The connection management device based on the MCP protocol includes: A creation unit is used to create a corresponding transport proxy instance and a first actual transport instance for the MCP server to be connected; wherein, the reference held internally in the transport proxy instance points to the first actual transport instance; A connection establishment unit is used to establish a first dual-channel connection between the first actual transmission instance and the MCP server. The initial handshake unit is used to perform an initial handshake between the first dual-channel connection and the MCP server. The monitoring unit is used to monitor the connection status between the client and the MCP server; The reconnection unit is used to perform a three-stage ordered reconnection operation when a connection failure is detected; wherein, the three-stage ordered reconnection operation includes re-establishing the second actual transmission instance and the second dual-channel connection, re-initializing the handshake based on the second dual-channel connection, and closing the first dual-channel connection. The update unit is used to update the reference held inside the transport proxy instance from pointing to the first actual transport instance to pointing to the second actual transport instance after the three-stage ordered reconnection operation is completed.
9. The connection management device based on the MCP protocol according to claim 8, characterized in that, The reconnection unit includes: The acquisition subunit is used to acquire a pre-saved network client builder and message processor based on the transport proxy instance; The first establishment subunit is used to establish a second actual transmission instance based on the network client builder and message processor; The second establishment subunit is used to establish a second dual-channel connection between the second actual transmission instance and the MCP server. The handshake subunit is used to re-execute the initial handshake between the client and the MCP server based on the second dual-channel connection; The closing unit is used to close the first dual-channel connection and release the resources occupied by the first dual-channel connection after the handshake subunit completes the initial handshake between the client and the MCP server based on the second dual-channel connection.
10. An electronic device, characterized in that, The electronic device includes a memory and a processor, the memory being used to store a computer program, and the processor running the computer program to enable the electronic device to perform the connection management method based on the MCP protocol as described in any one of claims 1 to 7.
11. A readable storage medium, characterized in that, The readable storage medium stores a computer program, which, when executed by a processor, performs the connection management method based on the MCP protocol as described in any one of claims 1 to 7.
12. A computer program product, characterized in that, The computer program product includes a computer program that, when executed by a processor, performs the connection management method based on the MCP protocol as described in any one of claims 1 to 7.