MCP service transmission data conversion method, system and device and storage medium
By introducing a data conversion layer and a network interaction layer into the MCP service, seamless conversion between the stdio protocol and the SSE or Streamable HTTP protocol is achieved, solving the data latency and compatibility issues of the MCP service during the migration process, and enabling the efficient and seamless access of the MCP service to web applications and cloud services.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-01-13
- Publication Date
- 2026-03-27
AI Technical Summary
When migrating existing MCP services to web clients or the cloud, the local blocking interaction method of the stdio protocol cannot support remote access across networks, and it lacks support for long connections and streaming transmission, resulting in data latency and connection interruptions, serious compatibility issues, and high transformation costs.
By employing a data conversion layer and a network interaction layer without modifying the MCP service code, a seamless bidirectional conversion between the stdio protocol and the SSE protocol or the Streamable HTTP protocol is achieved. This includes data block segmentation, header encapsulation, and session management, ensuring seamless data transmission between different protocols.
It reduces the cost of data conversion for MCP service transmission, improves compatibility and efficiency, supports the smooth evolution of MCP service to a distributed, cloud-native architecture, and enables remote access to existing MCP services and seamless integration with web applications.
Smart Images

Figure CN121750758A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the fields of artificial intelligence and network communication technology, and in particular to a method, system, device and storage medium for converting MCP service transmission data. Background Technology
[0002] stdio (Standard Input / Output), as a classic input / output method, has become the first choice for many developers to build MCP (Model Context Protocol) services due to its lightweight nature, stability, and ease of development. It is widely used in scenarios such as local deployment and offline debugging.
[0003] However, as AI applications migrate to web clients and the cloud, the technical limitations of stdio have become increasingly apparent, becoming a bottleneck restricting the evolution of MCP services towards distributed and real-time capabilities. On the one hand, stdio is a local process-level blocking interaction method that relies on terminals or local pipes for data transmission, and cannot directly support remote access across networks, making it difficult for stdio-based MCP services to connect to cloud applications or web clients. On the other hand, stdio lacks native support for long connections and streaming transmission, which can easily lead to data latency and connection interruptions between MCP services and clients when handling scenarios such as real-time inference result pushes and concurrent access from multiple clients, significantly conflicting with the high concurrency and low latency requirements of AI applications.
[0004] In related technologies, focusing on the consistency of IoT gateway protocols relies on hardware or static configuration; or integrating stdio-based MCP services with cloud applications or web clients to achieve data conversion between MCP services and cloud applications or web clients. However, hardware-based heterogeneous data conversion lacks flexibility, and stdio and web protocols have compatibility issues. Integrating stdio-based MCP services with cloud applications or web clients requires deep modification of the MCP service code, including reconstructing data transmission protocols, adapting network protocol stacks, and optimizing concurrent processing logic, which may compromise the stability of the MCP service, resulting in extremely high migration costs, and compatibility issues between the MCP service and cloud applications or web clients.
[0005] Therefore, the data conversion of MCP service transmission in related technologies requires in-depth modification of the MCP service code, which is costly, time-consuming, and has compatibility issues. Summary of the Invention
[0006] To address or partially address the problems existing in related technologies, this application provides a method, system, device, and storage medium for converting MCP service transmission data. This method enables seamless conversion of MCP service transmission data without modifying the MCP service code, reduces the cost of MCP service transmission data conversion, and improves the efficiency and compatibility of MCP service transmission data conversion.
[0007] The first aspect of this application provides a method for converting MCP service transmission data, the method comprising: According to the client's first transmission protocol, the first output data output by the MCP service is converted into first input data that conforms to the first transmission protocol, and the first input data is transmitted to the client. According to the second transmission protocol of the MCP service, the second output data output by the client is converted into second input data that conforms to the second transmission protocol, and the second input data is transmitted to the MCP service.
[0008] In one implementation, the second transmission protocol is the stdio protocol, and the first transmission protocol is the SSE protocol; The step of converting the first output data output by the MCP service into first input data conforming to the first transmission protocol according to the client's first transmission protocol, and transmitting the first input data to the client, includes: Obtain the first output data output by the MCP service, convert the first output data into first input data conforming to the SSE protocol according to the SSE protocol, and push the first input data to the client; The step of converting the second output data output by the client into second input data conforming to the second transmission protocol according to the second transmission protocol of the MCP service, and transmitting the second input data to the MCP service, includes: The system receives the second output data from the client, converts the second output data into second input data conforming to the stdio protocol according to the stdio protocol, and writes the second input data into the MCP service.
[0009] In one implementation, the step of converting the first output data into first input data conforming to the SSE protocol according to the SSE protocol, and pushing the first input data to the client, includes: The first output data is segmented based on the newline character of the first output data to obtain data blocks of the first output data; According to the SSE protocol, an SSE header is added to the data block of the first output data, and the first output data is encapsulated into an event stream conforming to the SSE protocol; The event stream is pushed to the client.
[0010] In one implementation, receiving the second output data from the client, converting the second output data into second input data conforming to the stdio protocol according to the stdio protocol, and writing the second input data into the MCP service includes: Receive the second output data from the client; The SSE header of the second output data is stripped off. According to the stdio protocol, a newline character is added to the second output data after the SSE header has been stripped off, and the second output data is encapsulated into second input data that conforms to the stdio protocol. Write the second input data into the MCP service.
[0011] In one implementation, the second transport protocol is the stdio protocol, and the first transport protocol is Streamable HTTP; The step of converting the first output data output by the MCP service into first input data conforming to the first transmission protocol according to the client's first transmission protocol, and transmitting the first input data to the client, includes: Obtain the first output data output by the MCP service, convert the first output data into first input data conforming to the Streamable HTTP according to the Streamable HTTP, and push the first input data to the client; The step of converting the second output data output by the client into second input data conforming to the second transmission protocol according to the second transmission protocol of the MCP service, and transmitting the second input data to the MCP service, includes: The system receives the second output data from the client, converts the second output data into second input data conforming to the stdio protocol according to the stdio protocol, and writes the second input data into the MCP service.
[0012] In one implementation, the step of obtaining the first output data output by the MCP service, converting the first output data into first input data conforming to the Streamable HTTP according to the Streamable HTTP, and pushing the first input data to the client includes: Obtain the first output data output by the MCP service; According to the Streamable HTTP, chunked transfer encoding is used to divide the first output data into first input data that conforms to the Streamable HTTP. If the session management mode between the MCP service and the client is stateless, a first transmission instance is created for the first output data, and the first input data is pushed to the client through the first transmission instance; If the session management mode adopted by the MCP service and the client is stateful mode, the second transmission instance is reused through the session ID of the MCP service and the client, and the first input data is pushed to the client through the second transmission instance.
[0013] In one implementation, receiving the second output data from the client, converting the second output data into second input data conforming to the stdio protocol according to the stdio protocol, and writing the second input data into the MCP service includes: Receive the second output data output by the client; The HTTP header of the second output data is stripped off. According to the stdio protocol, a newline character is added to the second output data after the HTTP header is stripped off, and the second output data is encapsulated into second input data that conforms to the stdio protocol. Write the second input data into the MCP service.
[0014] A second aspect of this application provides an MCP service transmission data conversion system, the conversion system comprising: A data conversion layer is used to convert first output data output by the MCP service into first input data conforming to the first transmission protocol according to the first transmission protocol of the client, and to convert second output data output by the client into second input data conforming to the second transmission protocol according to the second transmission protocol of the MCP service. A network interaction layer is used to transmit the first input data converted by the data conversion layer to the client, and to transmit the second input data converted by the data conversion layer to the MCP service.
[0015] A third aspect of this application provides an electronic device, comprising: Processor; and A memory that stores executable code, which, when executed by the processor, causes the processor to perform the method described above.
[0016] A fourth aspect of this application provides a computer-readable storage medium having executable code stored thereon, which, when executed by a processor of an electronic device, causes the processor to perform the method described above.
[0017] The fifth aspect of this application provides a computer program product comprising computer instructions that, when executed by a processor, implement the method described above.
[0018] The technical solution provided in this application may include the following beneficial results: The technical solution of this application can achieve seamless bidirectional conversion between stdio protocol data transmission and SSE protocol data transmission, or seamless bidirectional conversion between stdio protocol data transmission and StreamableHTTP data transmission, without modifying the code of the existing MCP service. This significantly reduces the cost of MCP service data transmission conversion, improves the efficiency of MCP service data transmission conversion and the compatibility of MCP service, provides technical support for promoting the smooth evolution of MCP service towards distributed, cloud-native architecture, enhances the adaptability and scalability of MCP service in cross-platform collaboration, and provides strong support for the efficient interaction and ecosystem prosperity of AI tools. The MCP service transmission protocol conversion method of this application embodiment resolves the conflict between the local blocking nature of stdio protocol data transmission in MCP service and the need for remote real-time interaction, allowing existing MCP services to easily access web applications and remote services, realizing remote access, debugging, and client connection of existing MCP services.
[0019] It should be understood that the above general description and the following detailed description are exemplary and explanatory only, and do not limit this application. Attached Figure Description
[0020] The above and other objects, features and advantages of this application will become more apparent from the more detailed description of exemplary embodiments thereof in conjunction with the accompanying drawings, wherein the same reference numerals generally represent the same components in the exemplary embodiments thereof.
[0021] Figure 1 This is a flowchart illustrating the MCP service transmission data conversion method according to an embodiment of this application; Figure 2 This is another flowchart illustrating the MCP service transmission data conversion method shown in the embodiments of this application; Figure 3 This is a schematic diagram of the structure of the MCP service transmission data conversion system shown in the embodiments of this application; Figure 4 This is another structural schematic diagram of the MCP service transmission data conversion system shown in the embodiments of this application; Figure 5 This is a schematic diagram of the structure of an electronic device shown in an embodiment of this application. Detailed Implementation
[0022] Embodiments of this application will now be described in more detail with reference to the accompanying drawings. While embodiments of this application are shown in the drawings, it should be understood that this application may be implemented in various forms and should not be limited to the embodiments set forth herein. Rather, these embodiments are provided to make this application more thorough and complete, and to fully convey the scope of this application to those skilled in the art.
[0023] The terminology used in this application is for the purpose of describing particular embodiments only and is not intended to be limiting of the application. The singular forms “a,” “the,” and “the” used in this application and the appended claims are also intended to include the plural forms unless the context clearly indicates otherwise. It should also be understood that the term “and / or” as used herein refers to and includes any or all possible combinations of one or more of the associated listed items.
[0024] It should be understood that although the terms "first," "second," "third," etc., may be used in this application to describe various information, this information should not be limited to these terms. These terms are only used to distinguish information of the same type from one another. For example, without departing from the scope of this application, first information may also be referred to as second information, and similarly, second information may also be referred to as first information. Thus, a feature defined as "first" or "second" may explicitly or implicitly include one or more of that feature. In the description of this application, "multiple" means two or more, unless otherwise explicitly specified.
[0025] This application provides a method for converting MCP service transmission data, which can achieve seamless conversion of MCP service transmission data without modifying the MCP service code, reduce the cost of MCP service transmission data conversion, and improve the efficiency and compatibility of MCP service transmission data conversion.
[0026] The technical solutions of the embodiments of this application are described in detail below with reference to the accompanying drawings.
[0027] Figure 1 This is a flowchart illustrating the MCP service transmission data conversion method according to an embodiment of this application.
[0028] See Figure 1 A method for converting MCP service transmission data, comprising: Step 110: According to the client's first transmission protocol, convert the first output data output by the MCP service into first input data that conforms to the first transmission protocol, and transmit the first input data to the client.
[0029] In one embodiment, the MCP service uses the stdio protocol to implement data input and output. The client's transport protocol includes, but is not limited to, SSE protocol and Streamable HTTP.
[0030] In one embodiment, if the client's transport protocol is the SSE protocol, the first output data output by the MCP service according to the stdio protocol can be converted into first input data conforming to the SSE protocol according to the client's SSE protocol, and the first input data can be transmitted to the client.
[0031] In one embodiment, SSE (Server-Sent Events) is a Web (World Wide Web) technology that allows the MCP service to push updates to the client in real time. It can maintain a long connection between the client and the MCP service, continuously sending event streams to achieve real-time data transmission.
[0032] In one embodiment, if the client's transport protocol is Streamable HTTP, the first output data output by the MCP service according to the stdio protocol can be converted into first input data conforming to Streamable HTTP based on the client's Streamable HTTP, and the first input data can be transmitted to the client.
[0033] In one embodiment, Streamable HTTP (Streamable Hypertext Transfer Protocol) is an enhanced HTTP (Hypertext Transfer Protocol) designed for real-time data transmission within the MCP framework. It is an upgraded and optimized version of HTTP+SSE. Based on ordinary HTTP requests, it supports clients initiating requests using POST or GET, and can upgrade responses to streaming transmission as needed. It features decentralized and non-forced long-connection characteristics, supports stateless mode to reduce the pressure on MCP services, while retaining the advantages of streaming responses. It is compatible with existing HTTP infrastructure and suitable for model interaction data transmission in high-concurrency scenarios.
[0034] Step 120: According to the second transmission protocol of the MCP service, the second output data output by the client is converted into second input data that conforms to the second transmission protocol, and the second input data is transmitted to the MCP service.
[0035] In one embodiment, if the client's transport protocol is the SSE protocol, the second output data output by the client according to the SSE protocol can be converted into second input data conforming to the MCP service's stdio protocol according to the stdio protocol of the MCP service, and the second input data can be transmitted to the MCP service.
[0036] In one embodiment, if the client's transport protocol is Streamable HTTP, the second output data output by the client according to the Streamable HTTP protocol can be converted into second input data conforming to the MCP service's stdio protocol, and the second input data can be transmitted to the MCP service.
[0037] The MCP service transmission protocol conversion method in this application embodiment can achieve seamless bidirectional conversion between stdio protocol data transmission and SSE protocol data transmission, or seamless bidirectional conversion between stdio protocol data transmission and Streamable HTTP data transmission, without modifying the code of the existing MCP service. This significantly reduces the cost of MCP service transmission data conversion, improves the efficiency of MCP service transmission data conversion and the compatibility of MCP services, provides technical support for promoting the smooth evolution of MCP services towards distributed, cloud-native architectures, enhances the adaptability and scalability of MCP services in cross-platform collaboration, and provides strong support for the efficient interaction and ecosystem prosperity of AI tools. The MCP service transmission protocol conversion method in this application embodiment resolves the conflict between the local blocking nature of stdio protocol data transmission in MCP services and the need for remote real-time interaction, allowing existing MCP services to easily access web applications and remote services, enabling remote access, debugging, and client connection of existing MCP services.
[0038] Figure 2 This is another flowchart illustrating the MCP service transmission data conversion method in an embodiment of this application. Figure 2 Compared to Figure 1 The scheme of this application is described in more detail.
[0039] See Figure 2 A method for converting MCP service transmission data, comprising: Step 210: Determine the client's transport protocol; if the client's transport protocol is SSE, proceed to step 221; if the client's transport protocol is Streamable HTTP, proceed to step 231.
[0040] In one embodiment, the client's transport protocol can be detected based on the request, message, or data sent by the client to the MCP service. If the client's transport protocol is SSE, then step 221 is executed; if the client's transport protocol is Streamable HTTP, then step 231 is executed.
[0041] Step 221: According to the client's SSE protocol, convert the first output data output by the MCP service into first input data that conforms to the SSE protocol, and push the first input data to the client.
[0042] In one embodiment, if the client's transport protocol is the SSE protocol, the process of pushing standard output data of the MCP service to the client includes: Step 2211: Configure the configuration parameters for the MCP service.
[0043] In one embodiment, if the client's transport protocol is the SSE protocol, the configuration parameters of the MCP service can be configured through the command line of the MCP service. The configuration parameters of the MCP service include, but are not limited to, the start command of the MCP service, the port, the SSE path (ssePath), and the message path (messagePath).
[0044] In one embodiment, the MCP service uses the stdio protocol, inputting data through standard input (stdin) and outputting data through standard output (stdout). The startup command for the MCP service can be specified through the command line of the MCP service, which can be used to initialize the MCP service and run the MCP server.
[0045] In one embodiment, the port of the MCP service can be modified via command-line parameters. For example, the port of the MCP service can be configured to the default port 8000.
[0046] In one embodiment, the SSE path (ssePath) and message path of the custom MCP service can be implemented by modifying the routing. The ssePath defines the SSE event stream push path, and the messagePath specifies the client message receiving path. The ssePath and messagePath are used to realize the data interaction between the client and the MCP service.
[0047] In one embodiment, if the client's transport protocol is the SSE protocol, the MCP service can be run by starting a subprocess, and the standard input pipe (stdin pipe) for data input to the MCP service and the standard output pipe (stdout pipe) for data output to the MCP service can be created through the system call function pipe(), and a connection with the stdio pipe of the subprocess can be established; the HTTP service can be initialized, port 8000 can be bound, and ssePath and messagePath routes can be registered.
[0048] Step 2212: Obtain the first output data from the MCP service, and convert the first output data into first input data that conforms to the SSE protocol according to the SSE protocol.
[0049] In one embodiment, the stdio protocol of the MCP service is a transport protocol based on the JSON-RPC 2.0 protocol, enabling bidirectional communication through input data via standard input (stdin) and output data via standard output (stdout). Stdio transmission uses line-separated JSON format for message exchange, with each JSON-RPC message occupying one line and separated by newline characters.
[0050] In one embodiment, the stdout pipe of the MCP service can be monitored through the select() system call. When the MCP service outputs the first output data according to the stdio protocol, a read event is triggered to read the first output data into a buffer, which can be configured to 4KB by default. The first output data is read from the buffer, and the first output data is split according to the newline character of the first output data to obtain the data block of the first output data. According to the SSE protocol, an SSE header is added to the data block of the first output data, and the first output data is encapsulated into an event stream conforming to the SSE protocol.
[0051] In one embodiment, first output data can be read from a buffer, invalid information (e.g., log data) can be filtered from the first output data, and the first output data can be segmented according to the newline character to obtain data blocks of the first output data. An SSE header can be added to each data block of the first output data to ensure that each data block conforms to the SSE protocol specification, thus encapsulating the first output data into an event stream conforming to the SSE protocol. For example, one data block of the first output data can be encapsulated as: event:message (marking output type), id:[session ID]-[sequence number] (unique identifier), data:{payload} (data body). The SSE format of one data block of the first output data is as follows: .
[0052] Step 2213: Push the first input data to the client.
[0053] In one embodiment, the event stream of the first output data can be pushed to the client via the SSE path of the MCP service. The client can establish a long connection with the MCP service via GET http: / / localhost:8000 / sse, and the concurrent processing module can allocate an independent thread for each connection. When a new SSE event stream is generated, the thread writes the event stream to the output stream of the connection. The client listens to this output stream through the Event Source API and receives the event stream of the first output data in real time.
[0054] Step 222: Receive the second output data from the client, convert the second output data into second input data conforming to the stdio protocol according to the stdio protocol, and write the second input data into the MCP service.
[0055] In one embodiment, the client can send messages (e.g., user-input inference parameters) to the MCP service via POST http: / / localhost:8000 / message. The message format can conform to the client's transport protocol specifications. The process of pushing the second output data from the client to the MCP service includes: Step 2221, initialize the client.
[0056] In one embodiment, the SSEClientTransport, based on the Node.js SDK (Software Development Kit), can connect to the SSE URL (Uniform ResourceLocator) specified by the client, configure request headers (e.g., authentication information), and listen for connection errors and close events.
[0057] Step 2222: Send a request for the MCP service to the client.
[0058] In one embodiment, the StdioServerTransport of the Node.js SDK can listen to the stdio input pipeline of the MCP service (e.g., commands entered by the developer through the terminal); when stdio input is received (e.g., { "jsonrpc": "2.0", "method": "tool1", "params": {}, "id": 1}), the stdio input is encapsulated into a request conforming to the SSE protocol specification (first input data), and the request conforming to the SSE protocol specification is sent to the client through SSEClientTransport.
[0059] Step 2223: Receive the second output data from the client; strip the SSE header of the second output data; add a newline character to the stripped SSE header second output data according to the stdio protocol; encapsulate the second output data into second input data conforming to the stdio protocol; write the second input data into the MCP service.
[0060] In one embodiment, the client responds to a request conforming to the SSE protocol specification, outputs an SSE response (second output data), and returns the SSE response to the MCP service. The SSE response can be received via SSEClientTransport, based on the Node.js SDK. The SSE header of the SSE response can be stripped, the payload extracted, and a newline character conforming to the stdio protocol added. The header-stripped SSE response is then converted into second input data conforming to the stdio protocol. This second input data conforming to the stdio protocol is then output to the terminal of the MCP service, or it can be written to the MCP service via a stdio input pipe for processing, thus enabling interaction between the client and the MCP service.
[0061] Step 231: Based on Streamable HTTP, convert the first output data output by the MCP service into first input data that conforms to Streamable HTTP, and push the first input data to the client.
[0062] In one embodiment, if the client's transport protocol is Streamable HTTP, a connection between the client and the MCP service can be established through an HTTP POST request or an HTTP GET request. The MCP service can provide services through a custom path (e.g., the / stream path), eliminating the need to maintain a long connection between the MCP service and the client. It can dynamically send chunked responses based on the data generation situation to achieve streaming transmission.
[0063] In one embodiment, if the client's transport protocol is Streamable HTTP, the process of pushing the standard output data of the MCP service to the client includes: listening to the stdout pipe of the MCP service through the select() system call and obtaining the first output data output by the MCP service through the stdout pipe; dividing the first output data into first input data conforming to Streamable HTTP using chunked transfer encoding according to Streamable HTTP; detecting the session management mode between the MCP service and the client based on the first output data; if the session management mode between the MCP service and the client is stateless, creating a first transport instance for the first output data and pushing the first input data to the client through the first transport instance; if the session management mode between the MCP service and the client is stateful, reusing a second transport instance through the session ID of the MCP service and the client, and pushing the first input data to the client through the second transport instance.
[0064] In one embodiment, the first input data (e.g., the request headers) can be identified to determine the session management mode between the MCP service and the client. If the client's transport protocol is Streamable HTTP, the first output data from the MCP service can be obtained, and the session management mode between the MCP service and the client can be detected based on the first output data. If the session management mode adopted by the MCP service and the client is stateless, a new MCP service subprocess can be started by each HTTP POST request from the client, binding an independent stdio pipe, and creating an independent first transport instance of Streamable HTTP Server Transport based on the Node.js SDK for each client request, ensuring isolation. This is achieved by identifying the delimiter of the first output data (e.g., ...). The process involves several steps: First, the first output data is stripped of invalid information (e.g., log data). Then, based on Streamable HTTP, chunked transfer encoding is used to divide the first output data into chunks. A hexadecimal length identifier (HTTP Chunked encoding identifier (Transfer-Encoding: chunked), e.g., 5\r\nhello\r\n) is added to each chunk. The first output data in stdio format is then encapsulated into an HTTP response (first input data) conforming to Streamable HTTP's Transfer-Encoding: chunked format. Finally, the first input data is pushed to the client via a custom path in the MCP service using a first transfer instance. When processing the first input data in Transfer-Encoding: chunked format, the hexadecimal length identifier of each chunk maintains the correspondence between requests and responses, preventing data confusion and ensuring reliable bidirectional communication between the MCP service and the client even in non-long-connection mode (stateless mode). After request processing, the child process and connection are closed. The MCP service and client use a stateless session management model, suitable for short-term interaction scenarios between the MCP service and the client.
[0065] In one embodiment, if the session management mode adopted by the MCP service and the client is stateful, the transport instance can be reused through the session ID of the MCP service and the client to maintain a persistent connection state between the MCP service and the client. Upon the client's first request (initialization), the MCP service can generate a sessionId and return it to the client via the MCP-Session-Id response header. Simultaneously, a second transport instance based on the Node.js SDK's StreamableHTTPServerTransport is created, associated with the session ID and a child process. The first output data in stdio protocol format is encapsulated into an HTTP response (first input data) conforming to the Streamable HTTP Transfer-Encoding: chunked format. The second transport instance is reused via the MCP-Session-Id header, and the first input data is pushed to the client via the second transport instance. Subsequently, when the MCP service processes requests and returns data to the client, it can reuse the second transport instance via the MCP-Session-Id header to maintain the session context (e.g., request sequence, incomplete tasks).
[0066] In one embodiment, a session timeout (e.g., 300 seconds) can be configured to monitor the client's access frequency through a custom SessionAccessCounter (a mechanism for recording the number of client accesses). After the timeout, child processes and session resources are automatically cleaned up, memory is released, and the effective use of resources and the security and integrity of the session are ensured.
[0067] Step 232: Receive the second output data from the client, convert the second output data into second input data conforming to the stdio protocol according to the stdio protocol, and write the second input data into the MCP service.
[0068] In one embodiment, if the client's transport protocol is StreamableHTTP, the process of pushing the second output data output by the client to the MCP service includes: Step 2321, initialize the client.
[0069] In one embodiment, a StreamableHTTPClient Transport based on the Node.js SDK can be used to connect to a client using StreamableHTTP, supporting HTTP GET requests (for pulling data) and HTTP POST requests (for sending data).
[0070] Step 2322: Send a request for the MCP service to the client.
[0071] In one embodiment, the stdio output of the MCP service standard output pipe can be monitored to obtain the stdio input from the MCP service to the client, the stdio input can be converted into first input data conforming to the Streamable HTTP chunked transfer format (Transfer-Encoding: chunked), and the first input data can be pushed to the client through the StreamableHTTPServerTransport instance.
[0072] Step 2323: Receive the second output data from the client, and according to the stdio protocol, add a newline character to the second output data after stripping the HTTP header, encapsulate the second output data into second input data conforming to the stdio protocol, and write the second input data into the MCP service.
[0073] In one embodiment, the client can receive a chunked response based on the first input data (the client's second output data), merge the chunked responses, remove the HTTP headers from the chunked responses, add a newline character to the chunked responses after removing the HTTP headers according to the stdio protocol, convert the chunked responses into second input data conforming to the stdio protocol, output the second input data conforming to the stdio protocol to the terminal of the MCP service, or write the second input data conforming to the stdio protocol to the MCP service through the stdio input pipe for processing by the MCP service, thereby realizing the interaction between the client and the MCP service.
[0074] In one embodiment, the first output data may be data output by the MCP service in response to a client request or message, or a request or message output by the MCP service to the client. The second output data may be data output by the client in response to an MCP service request or message, or a request or message output by the client to the MCP service.
[0075] In one embodiment, requests from the client or the MCP service can be verified using CRC32 (a 32-bit algorithm for cyclic redundancy check) to enhance the reliability, security, and integrity of data transmission. For example, the request's Content-Type and signature can be verified. After verifying the request's legitimacy, the client's request is converted into a stdio-compatible string (e.g., removing quotes and adding newlines), and written to the MCP service via the stdin pipe. Alternatively, the MCP service's request can be converted into client transport protocol (SSE protocol or Streamable HTTP) compatible data, and the client transport protocol (SSE protocol or Streamable HTTP) compatible data is pushed to the client, forming a closed loop and enabling data interaction between the MCP service and the client.
[0076] The MCP service data conversion method in this application, based on Node.js, enables the mutual conversion of data transmitted between the MCP service and the client. It achieves seamless bidirectional conversion between stdio protocol data and SSE protocol data, or between stdio protocol data and Streamable HTTP data, without modifying the existing MCP service code. This reduces the cost of MCP service data conversion, improves its efficiency and compatibility, and provides technical support for the smooth evolution of MCP services towards distributed, cloud-native architectures. It also enhances the adaptability and scalability of MCP services in cross-platform collaboration, providing strong support for efficient interaction and ecosystem prosperity of AI tools. Furthermore, it shortens the web-based transformation cycle of MCP services from 3-6 people / month to within one week; avoids stability risks caused by MCP service code refactoring; and protects existing MCP services. It solves the compatibility problem between MCP service and client transmission protocols, overcomes the limitations of local blocking data transmission of the stdio protocol, and enables MCP service based on stdio protocol to seamlessly connect to real-time transmission scenarios of SSE protocol or Streamable HTTP through standardized conversion logic, thus solving the problem that legacy systems cannot directly connect to web applications and cloud services.
[0077] Corresponding to the aforementioned application function implementation method embodiments, this application also provides an MCP service transmission data conversion system, electronic device, and corresponding embodiments.
[0078] Figure 3 This is a schematic diagram of the structure of the MCP service transmission data conversion system shown in the embodiments of this application.
[0079] See Figure 3 A data conversion system 300 for MCP service transmission includes a data conversion layer 310 and a network interaction layer 320.
[0080] The data conversion layer 310 is used to convert the first output data output by the MCP service into first input data conforming to the first transmission protocol according to the first transmission protocol of the client, and to convert the second output data output by the client into second input data conforming to the second transmission protocol according to the second transmission protocol of the MCP service.
[0081] The network interaction layer 320 is used to transmit the first input data converted by the data conversion layer 310 to the client, and to transmit the second input data converted by the data conversion layer 310 to the MCP service.
[0082] In one embodiment, the MCP service uses the stdio protocol to implement data input and output. The client's transport protocol includes, but is not limited to, SSE protocol and Streamable HTTP.
[0083] In one embodiment, if the client's transport protocol is the SSE protocol, the data conversion layer 310 can convert the first output data output by the MCP service according to the stdio protocol into first input data conforming to the SSE protocol, based on the client's SSE protocol. The network interaction layer 320 transmits the first input data to the client. The data conversion layer 310, according to the MCP service's stdio protocol, converts the second output data output by the client according to the SSE protocol into second input data conforming to the MCP service's stdio protocol, and the network interaction layer 320 transmits the second input data to the MCP service.
[0084] In one embodiment, if the client's transport protocol is Streamable HTTP, the data conversion layer 310 can convert the first output data output by the MCP service according to the stdio protocol into first input data conforming to Streamable HTTP, based on the client's Streamable HTTP. The network interaction layer 320 then transmits the first input data to the client. Similarly, the data conversion layer 310 can convert the second output data output by the client according to Streamable HTTP into second input data conforming to the MCP service's stdio protocol, based on the MCP service's stdio protocol, and the network interaction layer 320 then transmits the second input data to the MCP service.
[0085] The technical solution of this application embodiment can achieve seamless bidirectional conversion between stdio protocol data transmission and SSE protocol data transmission, or seamless bidirectional conversion between stdio protocol data transmission and Streamable HTTP data transmission, without modifying the code of the existing MCP service. This reduces the cost of MCP service data transmission conversion, improves the efficiency of MCP service data transmission conversion and the compatibility of MCP service, provides technical support for promoting the smooth evolution of MCP service towards distributed, cloud-native architecture, enhances the adaptability and scalability of MCP service in cross-platform collaboration, and provides strong support for the efficient interaction and ecosystem prosperity of AI tools. The MCP service transmission protocol conversion method of this application embodiment resolves the conflict between the local blocking nature of MCP service stdio protocol data transmission and the need for remote real-time interaction, allowing existing MCP services to easily access web applications and remote services, realizing remote access, debugging, and client connection of existing MCP services.
[0086] Figure 4This is another structural schematic diagram of the MCP service transmission data conversion system shown in the embodiments of this application.
[0087] See Figure 4 A data conversion system 300 for MCP service transmission includes a data conversion layer 310, a network interaction layer 320, and a protocol adaptation layer 330.
[0088] In one embodiment, the protocol adaptation layer 330, based on the modular mechanism of Node.js, is responsible for the protocol interface with the MCP service and the client. It includes a stdio adaptation module, an SSE adaptation module, and a Streamable HTTP adaptation module. Each adaptation module independently encapsulates the protocol details and interacts with the data conversion layer 310 and the network interaction layer 320 through a unified interface.
[0089] In one embodiment, the stdio adapter module creates an MCP service child process using Node.js's child_process module. This child process listens to the MCP service's standard input (stdin) and standard output (stdout), establishes an inter-process communication (IPC) pipe using Node.js's pipe() method, and enables real-time data capture and forwarding. The data processing flow is triggered by Node.js's on('data') event, and data is written to the MCP service using Node.js's write() method. Based on Node.js's child_process module and IPC mechanism, the MCP service code can be seamlessly integrated with the MCP service and its data conversion system without modification. Data interaction is achieved through the inter-process pipe, and the MCP service data conversion system is compatible with all MCP services based on the stdio protocol, enabling lossless data transmission.
[0090] In one embodiment, the SSE adapter module, implemented based on the EventEmitter module of Node.js, handles the long-lived connection between the MCP service and the client, establishes SSE communication with the client through an HTTP long-lived connection, supports event subscription (e.g., on('message')) and push (e.g., emit('data')), automatically encapsulates the SSE protocol header (e.g., event:, data:, etc. fields), and parses the SSE events sent by the client.
[0091] In one embodiment, the Streamable HTTP Adapter module (which supports HTTP-based streaming) is based on the Node.js HTTP module and supports HTTP 1.1 chunked transfer encoding. It identifies the session management mode between the client and the MCP service through request headers. When the session management mode is stateful, it maintains the session context through a Map object.
[0092] In one embodiment, the data transformation layer 310, based on the Node.js Stream module, implements efficient data processing and is the core logic layer of the transformation system. As the core of data transformation, it includes a data parsing engine, a protocol encapsulation engine, and a state mapper. The data parsing engine identifies the delimiters (e.g., ...) of the stdio protocol data stream output by the MCP service. The system extracts the payload using metadata tags (e.g., the [MCP] prefix); the protocol encapsulation engine adds header information (e.g., event:, data: fields for SSE, and Transfer-Encoding: chunked flag for Streamable HTTP) to the stdio protocol data stream according to the target protocol (client's transport protocol: SSE or Streamable HTTP); the state mapper resolves the context differences between the stateless nature of stdio protocol data streams and long-connection protocols, associating request-response sequences through session IDs to ensure consistency between the MCP service and the client across transport protocols. Node.js's Stream module is used to implement streaming data processing, avoiding excessive memory consumption caused by full data loading, achieving efficient data processing, and controlling data conversion latency to within 5ms.
[0093] In one embodiment, the data parsing engine, implemented based on Node.js's Transform Streams, identifies delimiters in the stdio protocol data stream (e.g., It can strip away invalid information (e.g., log data) and extract the payload; it supports dynamic configuration of parsing rules via regular expressions.
[0094] In one embodiment, the protocol encapsulation engine encapsulates data based on the JSON-RPC protocol through a Stream pipeline, depending on the target transport protocol type (client transport protocol: SSE protocol or Streamable HTTP). For example, when converting to SSE protocol data, "event: message\nid: {sessionId}\ndata: {payload}\n\n" is added; when converting to Streamable HTTP data, a chunk length identifier (e.g., "5\r\nhello\r\n") is added.
[0095] In one embodiment, the state mapper can associate session IDs with request-response sequences using JavaScript's Map object, thus resolving the contextual differences between the stateless nature of stdio protocol data streams and long-connection protocols. In the stateful session management mode, it records the last activity time of each session and uses JavaScript's setTimeout to implement session timeout management, ensuring efficient resource utilization and session security and integrity, and resolving the problem of packet loss caused by network fluctuations.
[0096] In one embodiment, the network interaction layer 320 provides HTTP service capabilities, including a port listening module, a path routing module, and a concurrency processing module.
[0097] In one embodiment, the port listening module can create an HTTP service using http.createServer() and bind it to the user-specified MCP service port (default 8000).
[0098] In one embodiment, the path routing module can be implemented based on the Express framework, mapping the SSE path ( / sse) and the message path ( / message).
[0099] In one embodiment, the concurrent processing module can create multiple worker processes (the number matching the number of CPU cores) based on Node.js's cluster module, enabling simultaneous connections between the MCP service and multiple clients. The thread pool size is dynamically adjusted through the master process. Leveraging Node.js's non-blocking I / O features, it supports concurrent processing of thousands of requests per second. The conversion system achieves multi-process load balancing through the cluster module, and combined with the non-blocking I / O model, it can adapt to high-concurrency scenarios with thousands of requests per second, improving throughput.
[0100] In one embodiment, Node.js is a JavaScript runtime environment based on the Chrome V8 engine, using an event-driven, non-blocking I / O model, suitable for data-intensive real-time applications running on distributed devices. Node.js allows users to write server-side code in JavaScript to build scalable web applications.
[0101] The MCP service transmission data conversion system of this application embodiment is based on Node.js. It utilizes Node.js's process management, stream processing, and network service capabilities. Through layered decoupling, modular adaptation, and layered architecture design, it constructs a three-layer architecture of "protocol adaptation layer - data conversion layer - network interaction layer" for the MCP service transmission data conversion system. Without modifying the existing MCP service code, it realizes bidirectional conversion of MCP service transmission data and completes seamless bidirectional conversion between stdio protocol data and SSE protocol data, and between stdio protocol data and Streamable HTTP data.
[0102] Furthermore, the MCP service transmission data conversion system of this application embodiment is based on the modularity and EventEmitter mechanism of Node.js. Through the EventEmitter module, new adaptation modules can be dynamically loaded. Adding a new transmission protocol only requires developing the corresponding adaptation module and registering it to the data conversion layer. There is no need to modify the logic of other adaptation modules, which can improve the maintainability and scalability of the MCP service transmission data conversion system.
[0103] Regarding the system in the above embodiments, the specific ways in which each layer and each module performs operations have been described in detail in the embodiments related to the method, and will not be elaborated further here.
[0104] Figure 5 This is a schematic diagram of the structure of an electronic device shown in an embodiment of this application.
[0105] See Figure 5 The electronic device 1000 includes a memory 1010 and a processor 1020.
[0106] The processor 1020 can be a Central Processing Unit (CPU), or other general-purpose processors, digital signal processors (DSPs), application-specific integrated circuits (ASICs), field-programmable gate arrays (FPGAs), or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. The general-purpose processor can be a microprocessor or any conventional processor.
[0107] Memory 1010 may include various types of storage units, such as system memory, read-only memory (ROM), and permanent storage devices. ROM may store static data or instructions required by processor 1020 or other modules of the computer. Permanent storage devices may be read-write storage devices. Permanent storage devices may be non-volatile storage devices that retain stored instructions and data even when the computer is powered off. In some embodiments, permanent storage devices use mass storage devices (e.g., magnetic or optical disks, flash memory) as permanent storage devices. In other embodiments, permanent storage devices may be removable storage devices (e.g., floppy disks, optical drives). System memory may be a read-write storage device or a volatile read-write storage device, such as dynamic random access memory. System memory may store some or all of the instructions and data required by the processor during operation. Furthermore, memory 1010 may include any combination of computer-readable storage media, including various types of semiconductor memory chips (e.g., DRAM, SRAM, SDRAM, flash memory, programmable read-only memory), and disks and / or optical disks may also be used. In some embodiments, memory 1010 may include a removable storage device that is readable and / or writable, such as a laser disc (CD), a read-only digital multifunction optical disc (e.g., DVD-ROM, dual-layer DVD-ROM), a read-only Blu-ray disc, an ultra-high density optical disc, a flash memory card (e.g., SD card, mini SD card, Micro-SD card, etc.), a magnetic floppy disk, etc. Computer-readable storage media do not contain carrier waves or transient electronic signals transmitted wirelessly or via wired connections.
[0108] The memory 1010 stores executable code, which, when processed by the processor 1020, can cause the processor 1020 to execute part or all of the methods described above.
[0109] Furthermore, the method according to this application can also be implemented as a computer program or computer program product, which includes computer program code instructions for performing some or all of the steps in the method described above.
[0110] Alternatively, this application may also be implemented as a computer-readable storage medium (or a non-transitory machine-readable storage medium or a machine-readable storage medium) storing executable code (or computer program or computer instruction code) that, when executed by a processor of a conversion system (or electronic device, or server, etc.), causes the processor to perform part or all of the steps of the methods described above according to this application.
[0111] This application also provides a computer program product, which includes computer instructions that, when executed by a processor, implement the method described above.
[0112] The various embodiments of this application have been described above. These descriptions are exemplary and not exhaustive, nor are they limited to the disclosed embodiments. Many modifications and variations will be apparent to those skilled in the art without departing from the scope and spirit of the described embodiments. The terminology used herein is chosen to best explain the principles, practical application, or improvement of the technology in the market, or to enable others skilled in the art to understand the embodiments disclosed herein.
Claims
1. A method for converting MCP service transmission data, characterized in that, include: According to the client's first transmission protocol, the first output data output by the MCP service is converted into first input data that conforms to the first transmission protocol, and the first input data is transmitted to the client. According to the second transmission protocol of the MCP service, the second output data output by the client is converted into second input data that conforms to the second transmission protocol, and the second input data is transmitted to the MCP service.
2. The method according to claim 1, characterized in that, The second transmission protocol is the stdio protocol, and the first transmission protocol is the SSE protocol; The step of converting the first output data output by the MCP service into first input data conforming to the first transmission protocol according to the client's first transmission protocol, and transmitting the first input data to the client, includes: Obtain the first output data output by the MCP service, convert the first output data into first input data conforming to the SSE protocol according to the SSE protocol, and push the first input data to the client; The step of converting the second output data output by the client into second input data conforming to the second transmission protocol according to the second transmission protocol of the MCP service, and transmitting the second input data to the MCP service, includes: The system receives the second output data from the client, converts the second output data into second input data conforming to the stdio protocol according to the stdio protocol, and writes the second input data into the MCP service.
3. The method according to claim 2, characterized in that, The step of converting the first output data into first input data conforming to the SSE protocol according to the SSE protocol, and pushing the first input data to the client, includes: The first output data is segmented based on the newline character of the first output data to obtain data blocks of the first output data; According to the SSE protocol, an SSE header is added to the data block of the first output data, and the first output data is encapsulated into an event stream conforming to the SSE protocol; The event stream is pushed to the client.
4. The method according to claim 2 or 3, characterized in that, The step of receiving the second output data from the client, converting the second output data into second input data conforming to the stdio protocol according to the stdio protocol, and writing the second input data into the MCP service includes: Receive the second output data from the client; The SSE header of the second output data is stripped off. According to the stdio protocol, a newline character is added to the second output data after the SSE header has been stripped off, and the second output data is encapsulated into second input data that conforms to the stdio protocol. Write the second input data into the MCP service.
5. The method according to claim 1, characterized in that, The second transmission protocol is stdio, and the first transmission protocol is Streamable HTTP; The step of converting the first output data output by the MCP service into first input data conforming to the first transmission protocol according to the client's first transmission protocol, and transmitting the first input data to the client, includes: Obtain the first output data output by the MCP service, convert the first output data into first input data conforming to the Streamable HTTP according to the Streamable HTTP, and push the first input data to the client; The step of converting the second output data output by the client into second input data conforming to the second transmission protocol according to the second transmission protocol of the MCP service, and transmitting the second input data to the MCP service, includes: The system receives the second output data from the client, converts the second output data into second input data conforming to the stdio protocol according to the stdio protocol, and writes the second input data into the MCP service.
6. The method according to claim 5, characterized in that, The step of obtaining the first output data from the MCP service, converting the first output data into first input data conforming to StreamableHTTP according to StreamableHTTP, and pushing the first input data to the client includes: Obtain the first output data output by the MCP service; According to the Streamable HTTP, chunked transfer encoding is used to divide the first output data into first input data that conforms to the Streamable HTTP. If the session management mode between the MCP service and the client is stateless, a first transmission instance is created for the first output data, and the first input data is pushed to the client through the first transmission instance; If the session management mode adopted by the MCP service and the client is stateful mode, the second transmission instance is reused through the session ID of the MCP service and the client, and the first input data is pushed to the client through the second transmission instance.
7. The method according to claim 5 or 6, characterized in that, The step of receiving the second output data from the client, converting the second output data into second input data conforming to the stdio protocol according to the stdio protocol, and writing the second input data into the MCP service includes: Receive the second output data output by the client; The HTTP header of the second output data is stripped off. According to the stdio protocol, a newline character is added to the second output data after the HTTP header is stripped off, and the second output data is encapsulated into second input data that conforms to the stdio protocol. Write the second input data into the MCP service.
8. A data conversion system for MCP service transmission, characterized in that, include: A data conversion layer is used to convert first output data output by the MCP service into first input data conforming to the first transmission protocol according to the first transmission protocol of the client, and to convert second output data output by the client into second input data conforming to the second transmission protocol according to the second transmission protocol of the MCP service. A network interaction layer is used to transmit the first input data converted by the data conversion layer to the client, and to transmit the second input data converted by the data conversion layer to the MCP service.
9. An electronic device, characterized in that, include: processor; as well as A memory having executable code stored thereon, which, when executed by the processor, causes the processor to perform the method as described in any one of claims 1-7.
10. A computer-readable storage medium, characterized in that: It stores executable code that, when executed by a processor of an electronic device, causes the processor to perform the method as described in any one of claims 1-7.