emMCP (Easy mcu MCP) is an adapter library for rapid development based on Ai-Thinker UART-MCP. It provides a set of simple and easy-to-use APIs to help developers quickly use MCU to access AI models for MCP interaction.
AiPi-PalChatV1's UART-MCP protocol is a UART-based communication protocol mainly used for MCP interaction between MCU and AI models, with content structure primarily in JSON format. In actual development, developers often need to spend a lot of time handling low-level tasks such as UART serial communication adaptation between MCU and modules, protocol parsing, and data format conversion.
emMCP builds a set of simple, easy-to-use, and highly encapsulated APIs (Application Programming Interfaces) that encapsulate all complex low-level operations (such as serial port initialization and configuration, MCP command packaging and parsing, data validation, exception handling, etc.). Developers only need to call the encapsulated interfaces to complete operations like "establishing connection between MCU and module", "sending MCP commands", and "receiving MCP commands", greatly reducing development difficulty and shortening the development cycle.
- Support for customizing MCP tools to AI agents
- Support for actively waking up Ai-Thinker AI devices
- Support for setting communication baud rate, default 115200
- Support for setting conversation volume
- Built-in small state machine with freely customizable callback functions
- Built-in MCP tool management
- Memory and FLASH usage (minimum): RAM: 62 Byte, FLASH: 1708 Byte
It is recommended to use GIT to download the source code with the following command:
git clone https://github.com/Ai-Thinker-Open/emMCP.git
Copy the port and uart-mcp folders to your project, and include the emMCP.h
file to use all resources of emMCP.
The example provides a CMake example for STM32F103 (STM32F10xRTOS_MCP), which you can refer to configure your project.
emMCP is a library for serial communication, so you only need to implement the underlying functions for serial communication.
- First, open the
port/uartPort.c
file - In the function
int uartPortSendData(char *data, int len)
, call your serial port sending function, passingdata
andlen
as parameters. For example:int uartPortSendData(char *data, int len) { // Implement serial port sending function here if (data == NULL || len <= 0) { return -1; } return HAL_UART_Transmit(&huart2, (uint8_t *)data, len, 100); }
- In the MCU's serial port receiving function, call the receiving function
int uartPortRecvData(char *data, int len)
. For example:void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size) { if (huart->Instance == USART2) { HAL_UARTEx_ReceiveToIdle_DMA(huart, (uint8_t *)rxBuffer, sizeof(rxBuffer)); // Call emMCP receiving function uartPortRecvData((char *)rxBuffer, Size); __HAL_DMA_ENABLE_IT(&hdma_usart2_rx, DMA_IT_TC); } }
- Implementation of delay function, in the
port/port.h
file, implement the delay function in the macro#define emMCP_delay
. For example:#define emMCP_delay osDelay
- Implementation of memory management functions, in the
port/port.h
file, implement the memory management functions in the macros#define emMCP_malloc
and#define emMCP_free
. For example:#define emMCP_malloc malloc #define emMCP_free free
You can initialize emMCP anywhere before executing the main function, for example, before the main()
function or in a separate initialization function. The code example for initializing emMCP is as follows:
static emMCP_t emMCP_dev;
int main(void)
{
emMCP_Init(&emMCP_dev);
while(1)
{
// Main loop execution
}
}
emMCP has a built-in simple state machine that requires cyclically calling emMCP_TickHandler()
to process data, reducing the burden when using interrupt functions. It is recommended to call emMCP_TickHandler()
in the main loop, for example:
static emMCP_t emMCP_dev;
int main(void)
{
emMCP_Init(&emMCP_dev);
while(1)
{
emMCP_TickHandler(10);
}
}
After completing the above steps, compile your project to ensure it compiles successfully without errors. Follow the steps below to verify if the porting is successful:
- Download the program to the MCU
- Say "Hello Xiao'an" to the AI module
- Observe the serial port output. If you see output similar to the following, it indicates that emMCP has been successfully ported to your MCU:
[DEBUG] emMCP_EventCallback:78: emMCP_EventCallback: event:8,type:4,param:2.WakeUP