CN-121985012-A - Method and system for expanding tool capacity of Internet of things equipment based on MCP protocol
Abstract
The invention provides an Internet of things equipment tool capability expansion method and system based on an MCP protocol. The method comprises the following steps: s1, establishing WebSocket or MQTT communication connection between Internet of things equipment and cloud service; s2, packaging JSON-RPC messages of a standard MCP protocol into device communication protocol messages and carrying out bidirectional transparent transmission through the communication connection; s3, the cloud service completes MCP initialization handshake with the Internet of things equipment through the MCP initialization message and the tools/list message, and obtains a hardware tool list and the like registered by the Internet of things equipment, the invention breaks through the resource limitation, so that the ESP32 and other limited IoT equipment do not need to operate a complete MCP service end, the WebSocket/MQTT transparent transmission of the MCP message is realized for the first time, the equipment hardware capacity is exposed to the LLM in the form of a standard MCP tool, the access threshold is greatly reduced, and the LLM does not need to sense the source difference of local, remote MCP and equipment end tools when calling tools through a universal MCPCLIENT interface and a unified registry, so that development is simplified, and maintenance cost is reduced.
Inventors
- WU BIN
- LAN FAQIN
Assignees
- 贵州万维赛搏人工智能科技有限公司
Dates
- Publication Date
- 20260505
- Application Date
- 20260320
Claims (9)
- 1. The method for expanding the tool capacity of the equipment of the internet of things based on the MCP protocol is characterized by comprising the following steps of: S1, establishing WebSocket or MQTT communication connection between Internet of things equipment and cloud service; s2, packaging JSON-RPC information of a standard MCP protocol into information of a device communication protocol and carrying out bidirectional transparent transmission through the communication connection; s3, the cloud service completes MCP initialization handshake with the Internet of things equipment through an MCP initialization message and a tools/list message, and a hardware tool list registered at the Internet of things equipment end is obtained; S4, uniformly registering a hardware tool at the equipment end, a local tool and a remote MCP server tool into a Function registry, so that a large language model can acquire a complete tool list through a Function paging interface; s5, when the large language model calls the tool in the reasoning process, traversing all tool clients by the tool manager, finding the client with the tool to execute the call, and returning the result to the large language model to continue the reasoning.
- 2. The method for extending the tool capability of the internet of things device based on the MCP protocol according to claim 1, wherein in the step S2, the specific way of encapsulating the JSON-RPC message of the standard MCP protocol into the device communication protocol message is to set a type identification field with a value of "MCP" at an outer layer of the device communication protocol message, and use the MCP JSON-RPC message as a payload field value of the outer layer, and the MCP message transparent transmission includes an asynchronous request-response matching mechanism, specifically including: distributing self-increasing unique ID for each MCP tool calling request, and creating a corresponding result channel storage mapping table; sending an MCP tools/call request carrying the ID through equipment communication connection; When receiving a message returned by the equipment, extracting an ID field in the message, searching a corresponding result channel in a mapping table, and delivering a result; the calling party returns a result through a select multipath waiting result channel, waits for the cancellation of the context or waits for the triggering of a timeout timer; and when the connection is overtime or disconnected, the corresponding entry in the mapping table is automatically cleared.
- 3. The method for extending the tool capability of the internet of things device based on the MCP protocol according to claim 1, wherein the MCP initialization handshake support tool list paging comprises the following specific procedures: after the tools/list request is sent, if the response data returned by the equipment contains a non-empty nextCursor field, automatically constructing a subsequent tools/list request carrying cursor parameters to continue to acquire; And carrying an access address and an authentication token of the visual identification service in the capabilities field of the initialization message, so that the Internet of things equipment can directly call the cloud visual identification service when the tool operation is executed.
- 4. The method for expanding the tool capacity of the equipment of the Internet of things based on the MCP protocol is characterized by further comprising the step of normalizing the tool names of the equipment, wherein the step of normalizing the tool names of the equipment is carried out, characters which are not supported by the large language model Function rolling specification are replaced by underlined characters, a tool name mapping table is maintained, the mapping relation from the normalized names to the original names is recorded, and the normalized names are externally registered and exposed and restored to the original names through the mapping table when an MCP request is sent to the equipment.
- 5. The method for extending tool capabilities of an internet of things device according to claim 1, wherein in the step S5, the tool manager uses a pre-initialization-delay binding strategy, wherein a plurality of tool manager instances are created to join a resource pool when a system is started, each instance is pre-completed with local tool registration and remote MCP server connection initialization when the system is created, when the internet of things device is connected, the pre-initialized tool manager instances are obtained from the resource pool, only a reference binding operation is performed, the device connection and function processor are bound to the manager instances, and the reference binding operation comprises tools for setting connection references, creating or binding a device-side MCP client, performing health check on the remote MCP client, and registering all ready clients.
- 6. The method for expanding the tool capacity of the equipment of the Internet of things based on the MCP protocol according to claim 5 is characterized in that the resource pool adopts an 'old innovation' recovery strategy, an old instance is not put back into the resource pool when the tool manager instance returns, a new tool manager instance is asynchronously created, the new instance is put into the resource pool after complete pre-initialization is carried out, the old instance carries out cleaning operation, and all MCP client connections are sequentially closed.
- 7. The method for extending the tool capability of the internet of things device based on the MCP protocol according to claim 1, wherein the three tool clients of the device side, the local MCP server and the remote MCP server share a unified client interface, and the interfaces include a start method, a stop method, a tool query method, a tool list obtaining method, a tool calling method, a ready checking method and a health checking method.
- 8. The system is characterized by comprising a device-side MCP client module, a local tool client module, a remote MCP server client module, a tool manager module and a resource pool module, wherein the device-side MCP client module is used for packaging JSON-RPC information of the MCP protocol into a device communication protocol for transmission, executing initialization handshake and tool discovery of the device of the Internet of things, realizing asynchronous request-response matching and tool name standardization mapping, the local tool client module is used for managing local tool functions directly called in a server process, the remote MCP server client module is used for connecting a remote MCP server in a stdio mode, an SSE mode or a StreamableHTTP mode to acquire and call remote tools, the tool manager module is used for managing the three tool clients to realize unified tool registration, discovery and scheduling, the resource pool module is used for pre-creating and caching tool manager examples to realize a pre-initialization-delay binding strategy, and the Function registry module is used for uniformly storing tool information of all sources, and providing a complete tool list for the Function of a large language model.
- 9. The MCP protocol based internet of things device tool capability extension system of claim 8, wherein the device side MCP client module further includes a connection adapter sub-module, the connection adapter including an MQTT connection adapter and a WebSocket connection adapter for adapting different types of device communication connections to a unified messaging interface.
Description
Method and system for expanding tool capacity of Internet of things equipment based on MCP protocol Technical Field The invention relates to the technical field of interaction between Internet of things equipment and artificial intelligence, in particular to an Internet of things equipment tool capability expansion method and system based on an MCP protocol. Background The large language model enters an Agent stage of tool call, and the MCP protocol issued by Anthropic is used as an important standard in the field of AI agents, so that a standardized interaction interface is established for the large model and an external tool, and is widely supported by mainstream AI manufacturers. Meanwhile, the Internet of things equipment such as ESP32 has rich hardware capabilities such as cameras and sensors, and if the Internet of things equipment can be called by a cloud large model, the function boundary of an AI assistant can be greatly expanded. The current large model can call a local tool and a remote MCP server tool, but the prior art still has a plurality of defects that the resource-limited Internet of things equipment cannot operate a standard MCP server because of no complete operation environment, the MCP tool cannot participate in ecology of the MCP tool, three types of tools of the local, remote MCP and the equipment end are different in calling mode and protocol, fragmentation is managed, the complexity of a system is increased, the MCP connection initialization step is complicated, the peak occupation of resources and the connection storm are easily caused under high concurrency, the interaction delay is increased, name conflicts are easily caused for the multi-source tool, the naming of the equipment end tool is not compatible with LLM specifications, the equipment end tool is called depending on shared communication connection, and the asynchronism and overtime processing lack of mature solutions, so that the calling reliability is affected. Disclosure of Invention In view of the above, the present invention proposes, in order to solve the problems existing in the technical background An internet of things equipment tool capability expansion method based on an MCP protocol comprises the following steps: S1, establishing WebSocket or MQTT communication connection between Internet of things equipment and cloud service; s2, packaging JSON-RPC information of a standard MCP protocol into information of a device communication protocol and carrying out bidirectional transparent transmission through the communication connection; s3, the cloud service completes MCP initialization handshake with the Internet of things equipment through an MCP initialization message and a tools/list message, and a hardware tool list registered at the Internet of things equipment end is obtained; S4, uniformly registering a hardware tool at the equipment end, a local tool and a remote MCP server tool into a Function registry, so that a large language model can acquire a complete tool list through a Function paging interface; s5, when the large language model calls the tool in the reasoning process, traversing all tool clients by the tool manager, finding the client with the tool to execute the call, and returning the result to the large language model to continue the reasoning. Further, in the step S2, the specific way of encapsulating the JSON-RPC message of the standard MCP protocol into the device communication protocol message is that a type identification field is set at the outer layer of the device communication protocol message, the value of the field is 'MCP', the MCP JSON-RPC message is taken as the payload field value of the outer layer, and the MCP message transparent transmission comprises an asynchronous request-response matching mechanism, specifically comprising the following steps: distributing self-increasing unique ID for each MCP tool calling request, and creating a corresponding result channel storage mapping table; sending an MCP tools/call request carrying the ID through equipment communication connection; When receiving a message returned by the equipment, extracting an ID field in the message, searching a corresponding result channel in a mapping table, and delivering a result; the calling party returns a result through a select multipath waiting result channel, waits for the cancellation of the context or waits for the triggering of a timeout timer; and when the connection is overtime or disconnected, the corresponding entry in the mapping table is automatically cleared. Further, the MCP initiates handshake support tool list paging, which specifically includes: after the tools/list request is sent, if the response data returned by the equipment contains a non-empty nextCursor field, automatically constructing a subsequent tools/list request carrying cursor parameters to continue to acquire; And carrying an access address and an authentication token of the visual identification service in the capabiliti