@@ -37,13 +37,11 @@ static std::string get_current_time() {
3737 ss << std::put_time (std::localtime (&time_t ), " %Y-%m-%d %H:%M:%S" );
3838
3939 // Return raw business data without RPC wrapper
40- return nlohmann::json{{" current_time" , ss.str ()}}. dump ( );
40+ return mcp::protocol::generate_result ( nlohmann::json{{" current_time" , ss.str ()}});
4141 } catch (const std::exception &e) {
4242 // Return custom error code and message
43- return nlohmann::json{
44- {" error" , {{" code" , -32000 },// Custom error code
45- {" message" , " Failed to get current time: " + std::string (e.what ())}}}}
46- .dump ();
43+ return mcp::protocol::generate_error (mcp::protocol::error_code::TOOL_NOT_FOUND,
44+ " Failed to get current time: " + std::string (e.what ()));
4745 }
4846}
4947
@@ -61,13 +59,11 @@ static std::string get_system_info() {
6159 std::string arch = sizeof (void *) == 8 ? " x86_64" : " x86" ;
6260
6361 // Return raw business data without RPC wrapper
64- return nlohmann::json{{" os" , os}, {" arch" , arch}}. dump ( );
62+ return mcp::protocol::generate_result ( nlohmann::json{{" os" , os}, {" arch" , arch}});
6563 } catch (const std::exception &e) {
6664 // Return custom error code and message
67- return nlohmann::json{
68- {" error" , {{" code" , -32000 },// Custom error code
69- {" message" , " Failed to get system info: " + std::string (e.what ())}}}}
70- .dump ();
65+ return mcp::protocol::generate_error (mcp::protocol::error_code::TOOL_NOT_FOUND,
66+ " Failed to get system info: " + std::string (e.what ()));
7167 }
7268}
7369
@@ -81,10 +77,8 @@ static std::string list_files(const std::string &path) {
8177 // Prevent path traversal attacks
8278 if (path.find (" .." ) != std::string::npos) {
8379 // Return custom error code and message
84- return nlohmann::json{
85- {" error" , {{" code" , -32000 },// Custom error code
86- {" message" , " Path traversal is not allowed" }}}}
87- .dump ();
80+ return mcp::protocol::generate_error (mcp::protocol::error_code::TOOL_NOT_FOUND,
81+ " Path traversal is not allowed" );
8882 }
8983
9084 std::array<char , 128 > buffer;
@@ -101,10 +95,8 @@ static std::string list_files(const std::string &path) {
10195 std::unique_ptr<FILE, decltype (&pclose)> pipe (popen (cmd.c_str (), " r" ), pclose);
10296 if (!pipe) {
10397 // Return custom error code and message
104- return nlohmann::json{
105- {" error" , {{" code" , -32000 },// Custom error code
106- {" message" , " Failed to list files: Pipe creation failed" }}}}
107- .dump ();
98+ return mcp::protocol::generate_error (mcp::protocol::error_code::TOOL_NOT_FOUND,
99+ " Failed to list files: Pipe creation failed" );
108100 }
109101
110102 // Read command output
@@ -113,13 +105,11 @@ static std::string list_files(const std::string &path) {
113105 }
114106
115107 // Return raw business data
116- return nlohmann::json{{" files" , result}}. dump ( );
108+ return mcp::protocol::generate_result ( nlohmann::json{{" files" , result}});
117109 } catch (const std::exception &e) {
118110 // Return custom error code and message
119- return nlohmann::json{
120- {" error" , {{" code" , -32000 },// Custom error code
121- {" message" , " Failed to list files: " + std::string (e.what ())}}}}
122- .dump ();
111+ return mcp::protocol::generate_error (mcp::protocol::error_code::TOOL_NOT_FOUND,
112+ " Failed to list files: " + std::string (e.what ()));
123113 }
124114}
125115
@@ -135,10 +125,8 @@ static std::string ping_host(const std::string &host) {
135125 return std::isalnum (c) || c == ' .' || c == ' -' ;
136126 })) {
137127 // Return custom error code and message
138- return nlohmann::json{
139- {" error" , {{" code" , -32000 },// Custom error code
140- {" message" , " Invalid host name format" }}}}
141- .dump ();
128+ return mcp::protocol::generate_error (mcp::protocol::error_code::TOOL_NOT_FOUND,
129+ " Invalid host name format" );
142130 }
143131
144132 std::array<char , 128 > buffer;
@@ -155,10 +143,8 @@ static std::string ping_host(const std::string &host) {
155143 std::unique_ptr<FILE, decltype (&pclose)> pipe (popen (cmd.c_str (), " r" ), pclose);
156144 if (!pipe) {
157145 // Return custom error code and message
158- return nlohmann::json{
159- {" error" , {{" code" , -32000 },// Custom error code
160- {" message" , " Ping command failed: Pipe creation failed" }}}}
161- .dump ();
146+ return mcp::protocol::generate_error (mcp::protocol::error_code::TOOL_NOT_FOUND,
147+ " Ping command failed: Pipe creation failed" );
162148 }
163149
164150 // Read ping output
@@ -171,13 +157,11 @@ static std::string ping_host(const std::string &host) {
171157 result.find (" time=" ) != std::string::npos;
172158
173159 // Return raw business data
174- return nlohmann::json{{" output" , result}, {" success" , success}}. dump ( );
160+ return mcp::protocol::generate_result ( nlohmann::json{{" output" , result}, {" success" , success}});
175161 } catch (const std::exception &e) {
176162 // Return custom error code and message
177- return nlohmann::json{
178- {" error" , {{" code" , -32000 },// Custom error code
179- {" message" , " Ping command failed: " + std::string (e.what ())}}}}
180- .dump ();
163+ return mcp::protocol::generate_error (mcp::protocol::error_code::TOOL_NOT_FOUND,
164+ " Ping command failed: " + std::string (e.what ()));
181165 }
182166}
183167
@@ -190,10 +174,8 @@ static std::string check_connectivity() {
190174 return ping_host (" 8.8.8.8" );// Use Google DNS as test target
191175 } catch (const std::exception &e) {
192176 // Return custom error code and message
193- return nlohmann::json{
194- {" error" , {{" code" , -32000 },// Custom error code
195- {" message" , " Failed to check connectivity: " + std::string (e.what ())}}}}
196- .dump ();
177+ return mcp::protocol::generate_error (mcp::protocol::error_code::TOOL_NOT_FOUND,
178+ " Failed to check connectivity: " + std::string (e.what ()));
197179 }
198180}
199181
@@ -222,10 +204,7 @@ static std::string get_public_ip() {
222204 std::unique_ptr<FILE, decltype (&pclose)> pipe (popen (cmd.c_str (), " r" ), pclose);
223205 if (!pipe) {
224206 // Return custom error code and message
225- return nlohmann::json{
226- {" error" , {{" code" , -32000 },// Custom error code
227- {" message" , " curl command not found" }}}}
228- .dump ();
207+ return mcp::protocol::generate_error (mcp::protocol::error_code::TOOL_NOT_FOUND, " curl command not found" );
229208 }
230209
231210 if (fgets (buffer.data (), buffer.size (), pipe.get ()) != nullptr ) {
@@ -235,7 +214,7 @@ static std::string get_public_ip() {
235214 }
236215
237216 if (!ip.empty () && ip.find (' .' ) != std::string::npos) {
238- return nlohmann::json{{" public_ip" , ip}}. dump ( );
217+ return mcp::protocol::generate_result ( nlohmann::json{{" public_ip" , ip}});
239218 } else {
240219// If the IP is not obtained correctly, return an error message
241220// Try to use foreign - accessible IP query services
@@ -246,32 +225,26 @@ static std::string get_public_ip() {
246225#endif
247226 std::unique_ptr<FILE, decltype (&pclose)> foreign_pipe (popen (foreign_cmd.c_str (), " r" ), pclose);
248227 if (!foreign_pipe) {
249- return nlohmann::json{
250- {" error" , {{" code" , -32000 },// Custom error code
251- {" message" , " curl command not found when trying foreign service" }}}}
252- .dump ();
228+ return mcp::protocol::generate_error (mcp::protocol::error_code::TOOL_NOT_FOUND,
229+ " curl command not found when trying foreign service" );
253230 }
254231 std::array<char , 128 > foreign_buffer;
255232 if (fgets (foreign_buffer.data (), foreign_buffer.size (), foreign_pipe.get ()) != nullptr ) {
256233 ip = foreign_buffer.data ();
257234 ip.erase (std::remove (ip.begin (), ip.end (), ' \n ' ), ip.end ());
258235 }
259236 if (!ip.empty () && ip.find (' .' ) != std::string::npos) {
260- return nlohmann::json{{" public_ip" , ip}}. dump ( );
237+ return mcp::protocol::generate_result ( nlohmann::json{{" public_ip" , ip}});
261238 } else {
262239 // Return custom error code and message if still failed
263- return nlohmann::json{
264- {" error" , {{" code" , -32000 },// Custom error code
265- {" message" , " Failed to get public IP after trying both domestic and foreign services" }}}}
266- .dump ();
240+ return mcp::protocol::generate_error (mcp::protocol::error_code::TOOL_NOT_FOUND,
241+ " Failed to get public IP after trying both domestic and foreign services" );
267242 }
268243 }
269244 } catch (const std::exception &e) {
270245 // If an exception occurs, return an error message with the exception details
271- return nlohmann::json{
272- {" error" , {{" code" , -32000 },// Custom error code
273- {" message" , " Failed to get public IP: " + std::string (e.what ())}}}}
274- .dump ();
246+ return mcp::protocol::generate_error (mcp::protocol::error_code::TOOL_NOT_FOUND,
247+ " Failed to get public IP: " + std::string (e.what ()));
275248 }
276249}
277250
0 commit comments