Skip to content

Commit ba2fa7e

Browse files
iroquetaBeta Bot
authored andcommitted
Cherry pick branch 'genexuslabs:MCPServer' into beta
1 parent 7394f7d commit ba2fa7e

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

gxspringboot/pom.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,16 @@
4747
<artifactId>urlrewritefilter</artifactId>
4848
<version>5.1.3</version>
4949
</dependency>
50+
<dependency>
51+
<groupId>org.springframework.ai</groupId>
52+
<artifactId>spring-ai-model</artifactId>
53+
<version>1.0.1</version>
54+
</dependency>
55+
<dependency>
56+
<groupId>org.reflections</groupId>
57+
<artifactId>reflections</artifactId>
58+
<version>0.10.2</version>
59+
</dependency>
5060
</dependencies>
5161

5262
<build>
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.genexus.springboot;
2+
3+
import com.genexus.diagnostics.core.ILogger;
4+
import com.genexus.specific.java.Connect;
5+
import com.genexus.specific.java.LogManager;
6+
import org.springframework.ai.tool.ToolCallbackProvider;
7+
import org.springframework.ai.tool.method.MethodToolCallbackProvider;
8+
import org.springframework.ai.tool.annotation.Tool;
9+
import org.reflections.Reflections;
10+
11+
import java.lang.reflect.Method;
12+
import java.util.Set;
13+
14+
public class GXUtils {
15+
public static final ILogger logger = com.genexus.diagnostics.core.LogManager.getLogger(GXUtils.class);
16+
17+
public static ToolCallbackProvider operationsTools(String packageName) {
18+
Connect.init();
19+
LogManager.initialize(".");
20+
21+
Reflections reflections = new Reflections(
22+
new org.reflections.util.ConfigurationBuilder()
23+
.forPackages(packageName)
24+
.addScanners(org.reflections.scanners.Scanners.MethodsAnnotated)
25+
);
26+
27+
Set<Method> toolMethods = reflections.getMethodsAnnotatedWith(Tool.class);
28+
29+
MethodToolCallbackProvider.Builder builder = MethodToolCallbackProvider.builder();
30+
31+
// Keep track of classes that have already been added
32+
Set<Class<?>> processedClasses = new java.util.HashSet<>();
33+
34+
for (Method method : toolMethods) {
35+
Class<?> clazz = method.getDeclaringClass();
36+
37+
// Skip if we've already processed this class
38+
if (processedClasses.contains(clazz)) {
39+
continue;
40+
}
41+
42+
try {
43+
Object instance = clazz.getConstructor(int.class).newInstance(-1);
44+
builder.toolObjects(instance);
45+
processedClasses.add(clazz);
46+
47+
Tool toolAnnotation = method.getAnnotation(Tool.class);
48+
logger.debug(String.format("Registered tool: %s - %s",
49+
toolAnnotation.name().isEmpty() ? method.getName() : toolAnnotation.name(),
50+
toolAnnotation.description()));
51+
} catch (Exception e) {
52+
logger.error("Error instantiating tool class: " + clazz.getName(), e);
53+
}
54+
}
55+
56+
if (!toolMethods.isEmpty())
57+
return builder.build();
58+
59+
return null;
60+
}
61+
}

0 commit comments

Comments
 (0)