@@ -81,6 +81,14 @@ static MCP_SERVER_CTX_T s_server_ctx;
8181/***********************************************************
8282***********************function define**********************
8383***********************************************************/
84+ static const char * __json_get_string (const cJSON * item )
85+ {
86+ if (item == NULL || !cJSON_IsString (item ) || item -> valuestring == NULL ) {
87+ return NULL ;
88+ }
89+ return item -> valuestring ;
90+ }
91+
8492MCP_PROPERTY_T * ai_mcp_property_create (const char * name , MCP_PROPERTY_TYPE_E type , const char * description )
8593{
8694 if (!name )
@@ -981,8 +989,10 @@ static OPERATE_RET __handle_tools_list(cJSON *params, const char *id)
981989 /* Parse parameters */
982990 if (params ) {
983991 cJSON * cursor = cJSON_GetObjectItem (params , "cursor" );
984- if (cJSON_IsString (cursor ))
985- cursor_str = cursor -> valuestring ;
992+ const char * cursor_value = __json_get_string (cursor );
993+ if (cursor_value != NULL ) {
994+ cursor_str = cursor_value ;
995+ }
986996 }
987997
988998 result = cJSON_CreateObject ();
@@ -1085,12 +1095,12 @@ static OPERATE_RET __parse_property_value(MCP_PROPERTY_LIST_T *prop_list,
10851095 break ;
10861096
10871097 case MCP_PROPERTY_TYPE_STRING :
1088- if (!cJSON_IsString (value ))
1098+ if (!__json_get_string (value ))
10891099 return OPRT_INVALID_PARM ;
10901100 prop -> default_val .type = MCP_PROPERTY_TYPE_STRING ;
10911101 if (prop -> has_default && prop -> default_val .str_val )
10921102 AI_MCP_FREE (prop -> default_val .str_val ); // Free existing string
1093- prop -> default_val .str_val = mm_strdup (value -> valuestring );
1103+ prop -> default_val .str_val = mm_strdup (__json_get_string ( value ) );
10941104 if (!prop -> default_val .str_val )
10951105 return OPRT_MALLOC_FAILED ;
10961106 prop -> has_default = true;
@@ -1121,12 +1131,12 @@ static OPERATE_RET __handle_tools_call(cJSON *params, const char *id)
11211131 }
11221132
11231133 tool_name_json = cJSON_GetObjectItem (params , "name" );
1124- if (!cJSON_IsString (tool_name_json )) {
1134+ tool_name = __json_get_string (tool_name_json );
1135+ if (tool_name == NULL ) {
11251136 error_code = MCP_ERROR_INVALID_PARAMS ;
11261137 error_msg = "Missing tool name" ;
11271138 goto err ;
11281139 }
1129- tool_name = tool_name_json -> valuestring ;
11301140
11311141 tool_arguments = cJSON_GetObjectItem (params , "arguments" );
11321142 if (tool_arguments && !cJSON_IsObject (tool_arguments )) {
@@ -1217,18 +1227,18 @@ OPERATE_RET ai_mcp_server_parse_message(const cJSON *json, VOID *user_data)
12171227
12181228 /* Check JSONRPC version */
12191229 node = cJSON_GetObjectItem (json , "jsonrpc" );
1220- if (! node || ! cJSON_IsString (node ) || strcmp (node -> valuestring , "2.0" ) != 0 ) {
1230+ if (__json_get_string (node ) == NULL || strcmp (__json_get_string ( node ) , "2.0" ) != 0 ) {
12211231 PR_ERR ("Invalid JSONRPC version" );
12221232 return OPRT_INVALID_PARM ;
12231233 }
12241234
12251235 /* Check method */
12261236 node = cJSON_GetObjectItem (json , "method" );
1227- if (!node || !cJSON_IsString (node )) {
1237+ method = __json_get_string (node );
1238+ if (method == NULL ) {
12281239 PR_ERR ("Missing method" );
12291240 return OPRT_INVALID_PARM ;
12301241 }
1231- method = node -> valuestring ;
12321242
12331243 /* Skip notifications */
12341244 if (strncmp (method , "notifications" , 13 ) == 0 )
@@ -1241,11 +1251,11 @@ OPERATE_RET ai_mcp_server_parse_message(const cJSON *json, VOID *user_data)
12411251 return OPRT_INVALID_PARM ;
12421252 }
12431253
1244- if (!node || !cJSON_IsString (node ) || node -> valuestring == NULL ) {
1254+ id = __json_get_string (node );
1255+ if (id == NULL ) {
12451256 PR_ERR ("Missing ID or Invalid ID type for method: %s" , method );
12461257 return OPRT_INVALID_PARM ;
12471258 }
1248- id = node -> valuestring ;
12491259
12501260 /* Get params */
12511261 node = cJSON_GetObjectItem (json , "params" );
0 commit comments