spring-ai
spring-ai copied to clipboard
Cannot dynamic add or remove tool in mcp server
trafficstars
I manage tool definations as datatables in database, i can add or remove tool definations at runtime.
I use spring-ai-mcp-server to expose all tools defination as mcp service,
@Bean
public StaticToolCallbackProvider staticCallbackProvider(DatabaseService databaseService) {
List<PublishedTool> publishedTools = databaseService.listAll();
var toolCallbacks = publishedTools.stream().map(publishedTool -> {
return new ToolCallback() {
@Override
public ToolDefinition getToolDefinition() {
return ToolDefinition.builder()
.name(publishedTool.getToolCode())
.description(publishedTool.getToolSnapshot().getDescription())
.inputSchema(JsonUtil.writeValueAsString(publishedTool.inputSchema())).build();
}
@Override
public String call(String toolInput) {
Object result = toolService.call(toolInput, publishedTool.getToolCode());
return JsonUtil.writeValueAsString(result);
}
@Override
public String call(String toolInput, @Nullable ToolContext tooContext) {
return call(toolInput);
}
};
}).flatMap(Stream::of).toArray(ToolCallback[]::new);
return new StaticToolCallbackProvider(toolCallbacks);
}
I want to dynamic add/remove tools at runtime, that is when add a tool, add it to the mcp server, when remove a tool ,remove from mcp server