Skip to content

Commit ff9243a

Browse files
committed
refactor(server): Refactor Immutable class to use Java records and streamline McpServerComponentRegister registration methods
1 parent e5a974f commit ff9243a

File tree

2 files changed

+26
-36
lines changed

2 files changed

+26
-36
lines changed
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
package com.github.codeboyzhou.mcp.declarative.common;
22

3-
public final class Immutable<T> {
4-
private final T value;
3+
public record Immutable<T>(T value) {
54

6-
private Immutable(T value) {
7-
this.value = value;
5+
public static <T> Immutable<T> of(T value) {
6+
return new Immutable<>(value);
87
}
98

10-
public static <T> T of(T value) {
11-
return new Immutable<>(value).value;
9+
public T get() {
10+
return value;
1211
}
1312
}

src/main/java/com/github/codeboyzhou/mcp/declarative/server/component/McpServerComponentRegister.java

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,23 @@
77
import com.github.codeboyzhou.mcp.declarative.common.InjectorProvider;
88
import com.google.inject.Injector;
99
import io.modelcontextprotocol.server.McpSyncServer;
10+
import java.lang.annotation.Annotation;
1011
import java.lang.reflect.Method;
1112
import java.util.Set;
13+
import java.util.function.BiConsumer;
1214
import org.reflections.Reflections;
1315

1416
public final class McpServerComponentRegister {
1517

16-
private final McpSyncServer server;
18+
private final Injector injector;
19+
20+
private final Reflections reflections;
21+
22+
private final Immutable<McpSyncServer> server;
1723

1824
public McpServerComponentRegister(McpSyncServer server) {
25+
this.injector = InjectorProvider.getInstance().getInjector();
26+
this.reflections = injector.getInstance(Reflections.class);
1927
this.server = Immutable.of(server);
2028
}
2129

@@ -24,38 +32,21 @@ public static McpServerComponentRegister of(McpSyncServer server) {
2432
}
2533

2634
public void registerComponents() {
27-
registerResources();
28-
registerPrompts();
29-
registerTools();
35+
register(McpResource.class, McpServerResourceFactory.class, McpSyncServer::addResource);
36+
register(McpPrompt.class, McpServerPromptFactory.class, McpSyncServer::addPrompt);
37+
register(McpTool.class, McpServerToolFactory.class, McpSyncServer::addTool);
3038
}
3139

32-
private void registerResources() {
33-
Injector injector = InjectorProvider.getInstance().getInjector();
34-
McpServerResourceFactory resourceFactory = injector.getInstance(McpServerResourceFactory.class);
35-
Reflections reflections = injector.getInstance(Reflections.class);
36-
Set<Method> resourceMethods = reflections.getMethodsAnnotatedWith(McpResource.class);
37-
for (Method method : resourceMethods) {
38-
server.addResource(resourceFactory.create(method.getDeclaringClass(), method));
39-
}
40-
}
41-
42-
private void registerPrompts() {
43-
Injector injector = InjectorProvider.getInstance().getInjector();
44-
McpServerPromptFactory promptFactory = injector.getInstance(McpServerPromptFactory.class);
45-
Reflections reflections = injector.getInstance(Reflections.class);
46-
Set<Method> promptMethods = reflections.getMethodsAnnotatedWith(McpPrompt.class);
47-
for (Method method : promptMethods) {
48-
server.addPrompt(promptFactory.create(method.getDeclaringClass(), method));
49-
}
50-
}
40+
private <T> void register(
41+
Class<? extends Annotation> annotationClass,
42+
Class<? extends McpServerComponentFactory<T>> factoryClass,
43+
BiConsumer<McpSyncServer, T> serverAddComponent) {
5144

52-
private void registerTools() {
53-
Injector injector = InjectorProvider.getInstance().getInjector();
54-
McpServerToolFactory toolFactory = injector.getInstance(McpServerToolFactory.class);
55-
Reflections reflections = injector.getInstance(Reflections.class);
56-
Set<Method> toolMethods = reflections.getMethodsAnnotatedWith(McpTool.class);
57-
for (Method method : toolMethods) {
58-
server.addTool(toolFactory.create(method.getDeclaringClass(), method));
45+
Set<Method> methods = reflections.getMethodsAnnotatedWith(annotationClass);
46+
McpServerComponentFactory<T> factory = injector.getInstance(factoryClass);
47+
for (Method method : methods) {
48+
T component = factory.create(method.getDeclaringClass(), method);
49+
serverAddComponent.accept(server.get(), component);
5950
}
6051
}
6152
}

0 commit comments

Comments
 (0)