2323#include "mcp_adapter.h"
2424#include "mcp_defines.h"
2525#include "mcp_adapter_tool_list.h"
26+ #include "mcp_adapter_utils.h"
2627#include "mcp_json_templates.h"
2728#include "../../version.h"
2829#include "../../verbosity.h"
@@ -36,6 +37,8 @@ typedef struct
3637 char method [128 ];
3738 char tool_name [128 ];
3839 char protocol_version [32 ];
40+ char args_json [MCP_JSON_MAX_REQUEST ];
41+ size_t args_len ;
3942 bool is_notification ;
4043} mcp_request_t ;
4144
@@ -46,6 +49,9 @@ static bool mcp_parse_request(const char *json, size_t len, mcp_request_t *req)
4649 int depth = 0 ;
4750 bool in_params = false;
4851 int params_depth = 0 ;
52+ bool in_arguments = false;
53+ int args_depth = 0 ;
54+ const char * args_key = NULL ;
4955 const char * key = NULL ;
5056 size_t key_len = 0 ;
5157
@@ -65,8 +71,24 @@ static bool mcp_parse_request(const char *json, size_t len, mcp_request_t *req)
6571 if (type == RJSON_OBJECT )
6672 {
6773 depth ++ ;
68- if (in_params )
74+ if (in_arguments )
75+ {
76+ args_depth ++ ;
77+ }
78+ else if (in_params )
79+ {
6980 params_depth ++ ;
81+ if (params_depth == 2 && key && string_is_equal (key , "arguments" ))
82+ {
83+ in_arguments = true;
84+ args_depth = 1 ;
85+ args_key = NULL ;
86+ req -> args_json [0 ] = '{' ;
87+ req -> args_json [1 ] = '\0' ;
88+ req -> args_len = 1 ;
89+ key = NULL ;
90+ }
91+ }
7092 else if (depth == 2 && key && string_is_equal (key , "params" ))
7193 {
7294 in_params = true;
@@ -77,6 +99,19 @@ static bool mcp_parse_request(const char *json, size_t len, mcp_request_t *req)
7799 }
78100 if (type == RJSON_OBJECT_END )
79101 {
102+ if (in_arguments )
103+ {
104+ args_depth -- ;
105+ if (args_depth <= 0 )
106+ {
107+ if (req -> args_len + 2 < sizeof (req -> args_json ))
108+ {
109+ req -> args_json [req -> args_len ++ ] = '}' ;
110+ req -> args_json [req -> args_len ] = '\0' ;
111+ }
112+ in_arguments = false;
113+ }
114+ }
80115 if (in_params )
81116 {
82117 params_depth -- ;
@@ -88,17 +123,78 @@ static bool mcp_parse_request(const char *json, size_t len, mcp_request_t *req)
88123 }
89124 if (type == RJSON_ARRAY )
90125 {
91- if (in_params )
126+ if (in_arguments )
127+ args_depth ++ ;
128+ else if (in_params )
92129 params_depth ++ ;
93130 continue ;
94131 }
95132 if (type == RJSON_ARRAY_END )
96133 {
97- if (in_params )
134+ if (in_arguments )
135+ args_depth -- ;
136+ else if (in_params )
98137 params_depth -- ;
99138 continue ;
100139 }
101140
141+ /* inside arguments - capture flat key/value pairs */
142+ if (in_arguments && args_depth == 1 )
143+ {
144+ if (type == RJSON_STRING )
145+ {
146+ size_t slen ;
147+ const char * s = rjson_get_string (parser , & slen );
148+ if (!args_key )
149+ args_key = s ;
150+ else
151+ {
152+ /* string value: append "key":"value" */
153+ char tmp [512 ];
154+ char escaped [256 ];
155+ mcp_json_escape (escaped , sizeof (escaped ), s );
156+ if (req -> args_len > 1 )
157+ req -> args_json [req -> args_len ++ ] = ',' ;
158+ snprintf (tmp , sizeof (tmp ), "\"%s\":\"%s\"" , args_key , escaped );
159+ req -> args_len += strlcpy (
160+ req -> args_json + req -> args_len ,
161+ tmp ,
162+ sizeof (req -> args_json ) - req -> args_len );
163+ args_key = NULL ;
164+ }
165+ }
166+ else if (type == RJSON_NUMBER && args_key )
167+ {
168+ /* number value: append "key":123 */
169+ char tmp [256 ];
170+ if (req -> args_len > 1 )
171+ req -> args_json [req -> args_len ++ ] = ',' ;
172+ snprintf (tmp , sizeof (tmp ), "\"%s\":%d" , args_key ,
173+ rjson_get_int (parser ));
174+ req -> args_len += strlcpy (
175+ req -> args_json + req -> args_len ,
176+ tmp ,
177+ sizeof (req -> args_json ) - req -> args_len );
178+ args_key = NULL ;
179+ }
180+ else if ((type == RJSON_TRUE || type == RJSON_FALSE ) && args_key )
181+ {
182+ char tmp [256 ];
183+ if (req -> args_len > 1 )
184+ req -> args_json [req -> args_len ++ ] = ',' ;
185+ snprintf (tmp , sizeof (tmp ), "\"%s\":%s" , args_key ,
186+ type == RJSON_TRUE ? "true" : "false" );
187+ req -> args_len += strlcpy (
188+ req -> args_json + req -> args_len ,
189+ tmp ,
190+ sizeof (req -> args_json ) - req -> args_len );
191+ args_key = NULL ;
192+ }
193+ else
194+ args_key = NULL ;
195+ continue ;
196+ }
197+
102198 /* inside params - look for "name" key for tools/call */
103199 if (in_params && params_depth == 1 && type == RJSON_STRING )
104200 {
@@ -233,7 +329,9 @@ void mcp_adapter_handle_request(const char *json_request, size_t len,
233329 "Invalid params: missing tool name" );
234330 return ;
235331 }
236- mcp_tools_call (req .id , req .tool_name , response , response_size );
332+ mcp_tools_call (req .id , req .tool_name ,
333+ req .args_json , req .args_len ,
334+ response , response_size );
237335 return ;
238336 }
239337
0 commit comments