@@ -69,6 +69,8 @@ public class McpServerPluginDataHandler implements PluginDataHandler {
6969
7070 private final ConcurrentMap <String , RuleData > rules = new ConcurrentHashMap <>();
7171
72+ private final ConcurrentMap <String , ToolRegistration > registrations = new ConcurrentHashMap <>();
73+
7274 public McpServerPluginDataHandler (
7375 final ShenyuMcpServerManager shenyuMcpServerManager ) {
7476 this .shenyuMcpServerManager = shenyuMcpServerManager ;
@@ -138,6 +140,7 @@ public synchronized void removeSelector(final SelectorData selectorData) {
138140 .collect (Collectors .toList ());
139141 removedRules .forEach (rule -> {
140142 rules .remove (rule .getId ());
143+ registrations .remove (rule .getId ());
141144 CACHED_TOOL .get ().removeHandle (CacheKeyUtils .INST .getKey (rule ));
142145 });
143146
@@ -156,15 +159,26 @@ public synchronized void handlerRule(final RuleData ruleData) {
156159 return ;
157160 }
158161 if (Objects .nonNull (ruleData .getId ())) {
162+ RuleData previous = rules .get (ruleData .getId ());
159163 RuleData snapshot = RuleData .builder ().id (ruleData .getId ()).selectorId (ruleData .getSelectorId ())
160164 .name (ruleData .getName ()).enabled (ruleData .getEnabled ()).handle (ruleData .getHandle ()).build ();
161165 rules .put (snapshot .getId (), snapshot );
166+ // an enabled -> disabled update arrives here, not through removeRule, so the tool
167+ // registered while the rule was enabled has to be revoked explicitly
168+ if (isRegistered (previous ) && Boolean .FALSE .equals (snapshot .getEnabled ())) {
169+ unregisterTool (previous );
170+ MetaDataCache .getInstance ().clean ();
171+ }
162172 registerTool (snapshot );
163173 return ;
164174 }
165175 registerTool (ruleData );
166176 }
167177
178+ private boolean isRegistered (final RuleData ruleData ) {
179+ return Objects .nonNull (ruleData ) && !Boolean .FALSE .equals (ruleData .getEnabled ());
180+ }
181+
168182 private void registerTool (final RuleData ruleData ) {
169183 if (Boolean .FALSE .equals (ruleData .getEnabled ())) {
170184 return ;
@@ -182,33 +196,88 @@ private void registerTool(final RuleData ruleData) {
182196 String inputSchema = JsonSchemaUtil .createParameterSchema (parameters );
183197 ShenyuMcpServer server = CACHED_SERVER .get ().obtainHandle (ruleData .getSelectorId ());
184198 if (Objects .nonNull (server ) && StringUtils .isNotBlank (server .getPath ())) {
185- shenyuMcpServerManager . addTool ( server . getPath (),
186- StringUtils . isBlank ( mcpServerTool . getName ()) ? ruleData . getName ()
187- : mcpServerTool . getName () ,
199+ String toolName = effectiveToolName ( ruleData , mcpServerTool );
200+ revokeRenamedRegistration ( ruleData . getId (), server . getPath (), toolName );
201+ shenyuMcpServerManager . addTool ( server . getPath (), toolName ,
188202 mcpServerTool .getDescription (),
189203 mcpServerTool .getRequestConfig (),
190204 inputSchema );
205+ trackRegistration (ruleData , server .getPath (), toolName );
191206 }
192207 });
193208 }
194209
195- @ Override
196- public synchronized void removeRule (final RuleData ruleData ) {
197- if (Objects .isNull (ruleData )) {
210+ /**
211+ * Revoke the registration applied for this rule when its effective server path or tool name
212+ * changed, so a rename or a move does not leave the previous name registered next to the new
213+ * one. The applied identity is tracked separately from the newest desired rule definition.
214+ *
215+ * @param ruleId the rule id
216+ * @param serverPath the server path the rule resolves to now
217+ * @param toolName the effective tool name now
218+ */
219+ private void revokeRenamedRegistration (final String ruleId , final String serverPath , final String toolName ) {
220+ if (Objects .isNull (ruleId )) {
198221 return ;
199222 }
200- if (Objects .nonNull (ruleData .getId ())) {
201- rules .remove (ruleData .getId ());
223+ ToolRegistration applied = registrations .get (ruleId );
224+ if (Objects .nonNull (applied ) && !applied .hasIdentity (serverPath , toolName )) {
225+ shenyuMcpServerManager .removeTool (applied .getServerPath (), applied .getToolName ());
226+ }
227+ }
228+
229+ private void trackRegistration (final RuleData ruleData , final String serverPath , final String toolName ) {
230+ if (Objects .nonNull (ruleData .getId ()) && StringUtils .isNotBlank (toolName )) {
231+ registrations .put (ruleData .getId (), new ToolRegistration (ruleData .getSelectorId (), serverPath , toolName ));
232+ }
233+ }
234+
235+ private boolean removeTrackedRegistration (final String ruleId ) {
236+ ToolRegistration applied = Objects .isNull (ruleId ) ? null : registrations .remove (ruleId );
237+ if (Objects .isNull (applied )) {
238+ return false ;
239+ }
240+ CACHED_TOOL .get ().removeHandle (CacheKeyUtils .INST .getKey (applied .getSelectorId (), ruleId ));
241+ if (StringUtils .isNotBlank (applied .getToolName ())) {
242+ shenyuMcpServerManager .removeTool (applied .getServerPath (), applied .getToolName ());
243+ }
244+ return true ;
245+ }
246+
247+ private String effectiveToolName (final RuleData ruleData , final ShenyuMcpServerTool tool ) {
248+ return StringUtils .isBlank (tool .getName ()) ? ruleData .getName () : tool .getName ();
249+ }
250+
251+ /**
252+ * Revoke whatever this rule has registered. The applied identity is authoritative because a
253+ * renamed or moved rule no longer matches what an incoming handle describes; the passed rule
254+ * data is only a fallback for rules that were never registered against a server.
255+ *
256+ * @param ruleData the rule to revoke
257+ */
258+ private void unregisterTool (final RuleData ruleData ) {
259+ if (removeTrackedRegistration (ruleData .getId ())) {
260+ return ;
202261 }
203262 Optional .ofNullable (ruleData .getHandle ()).ifPresent (s -> {
204263 CACHED_TOOL .get ().removeHandle (CacheKeyUtils .INST .getKey (ruleData ));
205264 ShenyuMcpServerTool mcpServerTool = GsonUtils .getInstance ().fromJson (s , ShenyuMcpServerTool .class );
206265 ShenyuMcpServer server = CACHED_SERVER .get ().obtainHandle (ruleData .getSelectorId ());
207266 if (Objects .nonNull (server ) && StringUtils .isNotBlank (server .getPath ())) {
208- shenyuMcpServerManager .removeTool (server .getPath (),
209- StringUtils .isBlank (mcpServerTool .getName ()) ? ruleData .getName () : mcpServerTool .getName ());
267+ shenyuMcpServerManager .removeTool (server .getPath (), effectiveToolName (ruleData , mcpServerTool ));
210268 }
211269 });
270+ }
271+
272+ @ Override
273+ public synchronized void removeRule (final RuleData ruleData ) {
274+ if (Objects .isNull (ruleData )) {
275+ return ;
276+ }
277+ if (Objects .nonNull (ruleData .getId ())) {
278+ rules .remove (ruleData .getId ());
279+ }
280+ unregisterTool (ruleData );
212281 MetaDataCache .getInstance ().clean ();
213282 }
214283
@@ -237,4 +306,40 @@ private String normalizeSelectorPath(final String selectorUri) {
237306 return StringUtils .defaultIfBlank (path , SLASH );
238307 }
239308
309+ /**
310+ * The registration this handler actually applied for one rule: the server path and tool name
311+ * present in the SDK server, kept apart from the newest desired rule definition so a later
312+ * update or deletion can tell what still has to be revoked.
313+ */
314+ private static final class ToolRegistration {
315+
316+ private final String selectorId ;
317+
318+ private final String serverPath ;
319+
320+ private final String toolName ;
321+
322+ ToolRegistration (final String selectorId , final String serverPath , final String toolName ) {
323+ this .selectorId = selectorId ;
324+ this .serverPath = serverPath ;
325+ this .toolName = toolName ;
326+ }
327+
328+ private String getSelectorId () {
329+ return selectorId ;
330+ }
331+
332+ private String getServerPath () {
333+ return serverPath ;
334+ }
335+
336+ private String getToolName () {
337+ return toolName ;
338+ }
339+
340+ private boolean hasIdentity (final String candidateServerPath , final String candidateToolName ) {
341+ return Objects .equals (serverPath , candidateServerPath ) && Objects .equals (toolName , candidateToolName );
342+ }
343+ }
344+
240345}
0 commit comments