77import com .github .codeboyzhou .mcp .declarative .common .InjectorProvider ;
88import com .google .inject .Injector ;
99import io .modelcontextprotocol .server .McpSyncServer ;
10+ import java .lang .annotation .Annotation ;
1011import java .lang .reflect .Method ;
1112import java .util .Set ;
13+ import java .util .function .BiConsumer ;
1214import org .reflections .Reflections ;
1315
1416public 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