11#include < filesystem>
22#include < fstream>
33#include < iostream>
4+ #include < string>
45
56namespace fs = std::filesystem;
67
@@ -41,18 +42,26 @@ int main(int argc, char *argv[]) {
4142 ofs << " std::string error;\n " ;
4243 ofs << " };\n\n " ;
4344 ofs << " // Generator next function for streaming tools\n " ;
44- ofs << " static int " << argv[1 ] << " _next(void* generator, const char** result_json) {\n " ;
45+ ofs << " static int " << argv[1 ] << " _next(void* generator, const char** result_json, MCPError *error ) {\n " ;
4546 ofs << " // Add your streaming logic here\n " ;
4647 ofs << " // Return 0 to continue streaming, 1 to stop\n " ;
4748 ofs << " if (!generator) {\n " ;
4849 ofs << " *result_json = R\" ({\" error\" : \" Invalid generator pointer\" })\" ;\n " ;
50+ ofs << " if (error) {\n " ;
51+ ofs << " error->code = 1;\n " ;
52+ ofs << " error->message = \" Invalid generator pointer\" ;\n " ;
53+ ofs << " }\n " ;
4954 ofs << " return 1;\n " ;
5055 ofs << " }\n\n " ;
5156 ofs << " auto* gen = static_cast<" << argv[1 ] << " Generator*>(generator);\n " ;
5257 ofs << " \n " ;
5358 ofs << " // Check if there's an error\n " ;
5459 ofs << " if (!gen->error.empty()) {\n " ;
5560 ofs << " *result_json = gen->error.c_str();\n " ;
61+ ofs << " if (error) {\n " ;
62+ ofs << " error->code = 2;\n " ;
63+ ofs << " error->message = gen->error.c_str();\n " ;
64+ ofs << " }\n " ;
5665 ofs << " return 1;\n " ;
5766 ofs << " }\n\n " ;
5867 ofs << " // Check if streaming should stop\n " ;
@@ -68,6 +77,10 @@ int main(int argc, char *argv[]) {
6877 ofs << " {\" params\" , {{\" text\" , \" Example streamed content\" }}}})\n " ;
6978 ofs << " .dump();\n\n " ;
7079 ofs << " *result_json = buffer.c_str();\n " ;
80+ ofs << " if (error) {\n " ;
81+ ofs << " error->code = 0; // No error\n " ;
82+ ofs << " error->message = nullptr;\n " ;
83+ ofs << " }\n " ;
7184 ofs << " return 0; // Continue streaming\n " ;
7285 ofs << " }\n\n " ;
7386 ofs << " // Generator free function for streaming tools\n " ;
@@ -91,7 +104,7 @@ int main(int argc, char *argv[]) {
91104 ofs << " return nullptr;\n " ;
92105 ofs << " }\n " ;
93106 ofs << " }\n\n " ;
94- ofs << " extern \" C\" MCP_API const char *call_tool(const char *name, const char *args_json) {\n " ;
107+ ofs << " extern \" C\" MCP_API const char *call_tool(const char *name, const char *args_json, MCPError *error ) {\n " ;
95108 ofs << " try {\n " ;
96109 ofs << " auto args = nlohmann::json::parse(args_json);\n " ;
97110 ofs << " std::string tool_name = name;\n\n " ;
@@ -108,8 +121,16 @@ int main(int argc, char *argv[]) {
108121 ofs << " // // Initialize your generator here\n " ;
109122 ofs << " // return reinterpret_cast<const char*>(gen);\n " ;
110123 ofs << " // }\n\n " ;
124+ ofs << " if (error) {\n " ;
125+ ofs << " error->code = 3;\n " ;
126+ ofs << " error->message = (\" Unknown tool: \" + tool_name).c_str();\n " ;
127+ ofs << " }\n " ;
111128 ofs << " return strdup((nlohmann::json{{\" error\" , \" Unknown tool: \" + tool_name}}.dump()).c_str());\n " ;
112129 ofs << " } catch (const std::exception &e) {\n " ;
130+ ofs << " if (error) {\n " ;
131+ ofs << " error->code = 4;\n " ;
132+ ofs << " error->message = e.what();\n " ;
133+ ofs << " }\n " ;
113134 ofs << " return strdup((nlohmann::json{{\" error\" , e.what()}}.dump()).c_str());\n " ;
114135 ofs << " }\n " ;
115136 ofs << " }\n\n " ;
@@ -120,10 +141,10 @@ int main(int argc, char *argv[]) {
120141 ofs << " }\n\n " ;
121142 ofs << " // For streaming tools, implement these functions\n " ;
122143 ofs << " extern \" C\" MCP_API StreamGeneratorNext get_stream_next() {\n " ;
123- ofs << " return " << argv[1 ] << " _next;\n " ;
144+ ofs << " return reinterpret_cast<StreamGeneratorNext>( " << argv[1 ] << " _next) ;\n " ;
124145 ofs << " }\n\n " ;
125146 ofs << " extern \" C\" MCP_API StreamGeneratorFree get_stream_free() {\n " ;
126- ofs << " return " << argv[1 ] << " _free;\n " ;
147+ ofs << " return reinterpret_cast<StreamGeneratorFree>( " << argv[1 ] << " _free) ;\n " ;
127148 ofs << " }\n " ;
128149 }
129150
0 commit comments