@@ -55,6 +55,10 @@ public static function getConfig($id, $local_config = null, $protect = true)
5555
5656 /**
5757 * Override to handle custom_tools sync on save.
58+ *
59+ * On initial service create, $id is null here (the service has not been
60+ * inserted yet). The sync runs from storeConfig() below, which fires
61+ * after the service row is created with a real id.
5862 */
5963 public static function setConfig ($ id , $ config , $ local_config = null )
6064 {
@@ -63,12 +67,36 @@ public static function setConfig($id, $config, $local_config = null)
6367
6468 parent ::setConfig ($ id , $ config , $ local_config );
6569
66- if (is_array ($ customTools )) {
67- try {
68- McpCustomTool::syncToolsForService ($ id , $ customTools );
69- } catch (\Exception $ e ) {
70- // Table may not exist yet if migration hasn't run
71- }
70+ if ($ id && is_array ($ customTools )) {
71+ self ::syncCustomTools ((int ) $ id , $ customTools );
72+ }
73+ }
74+
75+ /**
76+ * Override to sync custom_tools when a new service's config is stored
77+ * for the first time (post-insert, when the service id is known).
78+ */
79+ public static function storeConfig ($ id , $ config )
80+ {
81+ $ customTools = $ config ['custom_tools ' ] ?? null ;
82+ unset($ config ['custom_tools ' ]);
83+
84+ parent ::storeConfig ($ id , $ config );
85+
86+ if ($ id && is_array ($ customTools )) {
87+ self ::syncCustomTools ((int ) $ id , $ customTools );
88+ }
89+ }
90+
91+ private static function syncCustomTools (int $ serviceId , array $ customTools ): void
92+ {
93+ try {
94+ McpCustomTool::syncToolsForService ($ serviceId , $ customTools );
95+ } catch (\Throwable $ e ) {
96+ \Log::error ('Failed to sync MCP custom tools ' , [
97+ 'service_id ' => $ serviceId ,
98+ 'error ' => $ e ->getMessage (),
99+ ]);
72100 }
73101 }
74102
0 commit comments