|
1 | 1 | package com.github.thought2code.mcp.annotated; |
2 | 2 |
|
3 | | -import com.github.thought2code.mcp.annotated.reflect.ReflectionsProvider; |
4 | | -import com.github.thought2code.mcp.annotated.server.component.DefaultMcpComponentInstanceFactory; |
5 | | -import com.github.thought2code.mcp.annotated.server.component.McpComponentInstanceFactory; |
6 | | -import java.lang.annotation.Annotation; |
7 | | -import java.lang.reflect.Field; |
8 | | -import java.lang.reflect.Method; |
| 3 | +import com.github.thought2code.mcp.annotated.annotation.McpServerApplication; |
| 4 | +import com.github.thought2code.mcp.annotated.enums.McpServerError; |
| 5 | +import com.github.thought2code.mcp.annotated.exception.McpServerException; |
| 6 | +import com.github.thought2code.mcp.annotated.util.StringHelper; |
9 | 7 | import java.util.Objects; |
10 | | -import java.util.Set; |
| 8 | +import java.util.concurrent.ConcurrentHashMap; |
| 9 | +import java.util.concurrent.ConcurrentMap; |
11 | 10 |
|
12 | 11 | /** |
13 | 12 | * Runtime context for one annotated MCP application. |
14 | 13 | * |
15 | | - * <p>The context owns application-scoped services such as classpath scanning. This keeps |
16 | | - * independent MCP applications from sharing global mutable state when they run in the same JVM. |
| 14 | + * <p>In the build-time compiled architecture, this context no longer performs classpath scanning. |
| 15 | + * Instead, it centralizes runtime concerns that compiled invokers still need: |
| 16 | + * |
| 17 | + * <ul> |
| 18 | + * <li>Component instance lifecycle (lazy creation with one cached instance per component class) |
| 19 | + * <li>Scope filtering for compiled model definitions based on the resolved application base |
| 20 | + * package |
| 21 | + * </ul> |
| 22 | + * |
| 23 | + * <p>Each application gets its own context instance so multiple MCP applications can run in the |
| 24 | + * same JVM without sharing component instances or scope rules. |
17 | 25 | */ |
18 | 26 | public final class McpApplicationContext { |
19 | | - /** The reflections provider used to scan the application's classpath for annotated components. */ |
20 | | - private final ReflectionsProvider reflectionsProvider; |
| 27 | + /** Base package resolved from {@link McpServerApplication}. */ |
| 28 | + private final String basePackage; |
21 | 29 |
|
22 | | - /** The factory used to create or locate annotated component instances. */ |
23 | | - private final McpComponentInstanceFactory componentInstanceFactory; |
| 30 | + /** Component instance cache; one instance per component class. */ |
| 31 | + private final ConcurrentMap<Class<?>, Object> componentInstances = new ConcurrentHashMap<>(); |
24 | 32 |
|
25 | | - private McpApplicationContext( |
26 | | - ReflectionsProvider reflectionsProvider, |
27 | | - McpComponentInstanceFactory componentInstanceFactory) { |
28 | | - this.reflectionsProvider = |
29 | | - Objects.requireNonNull(reflectionsProvider, "reflectionsProvider must not be null"); |
30 | | - this.componentInstanceFactory = |
31 | | - Objects.requireNonNull( |
32 | | - componentInstanceFactory, "componentInstanceFactory must not be null"); |
| 33 | + private McpApplicationContext(String basePackage) { |
| 34 | + this.basePackage = Objects.requireNonNull(basePackage, "basePackage must not be null"); |
33 | 35 | } |
34 | 36 |
|
35 | 37 | /** |
36 | 38 | * Creates a new context for the specified MCP application class. |
37 | 39 | * |
38 | | - * @param mainClass the application entry class used for package scanning |
| 40 | + * <p>The resolved base package follows this priority: |
| 41 | + * |
| 42 | + * <ol> |
| 43 | + * <li>{@link McpServerApplication#basePackageClass()} when explicitly set |
| 44 | + * <li>{@link McpServerApplication#basePackage()} when non-blank |
| 45 | + * <li>the package of {@code mainClass} as fallback |
| 46 | + * </ol> |
| 47 | + * |
| 48 | + * @param mainClass the application entry class used to resolve base-package scope |
39 | 49 | * @return a new application-scoped context |
40 | 50 | */ |
41 | 51 | public static McpApplicationContext from(Class<?> mainClass) { |
42 | | - ReflectionsProvider reflectionsProvider = |
43 | | - ReflectionsProvider.initializeReflectionsInstance(mainClass); |
44 | | - return new McpApplicationContext( |
45 | | - reflectionsProvider, DefaultMcpComponentInstanceFactory.create()); |
| 52 | + Objects.requireNonNull(mainClass, "mainClass must not be null"); |
| 53 | + return new McpApplicationContext(resolveBasePackage(mainClass)); |
46 | 54 | } |
47 | 55 |
|
48 | 56 | /** |
49 | | - * Returns all methods annotated with the specified annotation within this context's scan scope. |
| 57 | + * Returns a component instance for the specified component class. |
| 58 | + * |
| 59 | + * <p>Instances are created lazily via the no-argument constructor and cached per declaring class. |
| 60 | + * Repeated calls for the same class return the same instance. |
50 | 61 | * |
51 | | - * @param annotation the annotation class to search for |
52 | | - * @return methods annotated with the given annotation |
| 62 | + * @param componentClass the class declaring annotated MCP component methods |
| 63 | + * @return the component instance to invoke |
53 | 64 | */ |
54 | | - public Set<Method> getMethodsAnnotatedWith(Class<? extends Annotation> annotation) { |
55 | | - return reflectionsProvider.getMethodsAnnotatedWith(annotation); |
| 65 | + public Object getComponentInstance(Class<?> componentClass) { |
| 66 | + Objects.requireNonNull(componentClass, "componentClass must not be null"); |
| 67 | + return componentInstances.computeIfAbsent( |
| 68 | + componentClass, McpApplicationContext::createInstance); |
56 | 69 | } |
57 | 70 |
|
58 | 71 | /** |
59 | | - * Returns all fields annotated with the specified annotation within this context's scan scope. |
| 72 | + * Returns whether a compiled source method belongs to this application's base package scope. |
| 73 | + * |
| 74 | + * <p>This method is used while loading compiled definitions to prevent out-of-scope components |
| 75 | + * from being registered (for example, fixtures from other test packages or neighboring modules). |
60 | 76 | * |
61 | | - * @param annotation the annotation class to search for |
62 | | - * @return fields annotated with the given annotation |
| 77 | + * @param sourceMethod compiled source method descriptor in the form {@code fqcn#method(...)} |
| 78 | + * @return {@code true} when the source method declaring class is within the configured base |
| 79 | + * package |
63 | 80 | */ |
64 | | - public Set<Field> getFieldsAnnotatedWith(Class<? extends Annotation> annotation) { |
65 | | - return reflectionsProvider.getFieldsAnnotatedWith(annotation); |
| 81 | + public boolean isInScope(String sourceMethod) { |
| 82 | + if (StringHelper.isBlank(sourceMethod)) { |
| 83 | + return false; |
| 84 | + } |
| 85 | + |
| 86 | + final int hashIndex = sourceMethod.indexOf(StringHelper.HASH); |
| 87 | + final String declaringClassName = |
| 88 | + (hashIndex >= 0 ? sourceMethod.substring(0, hashIndex) : sourceMethod).trim(); |
| 89 | + if (declaringClassName.isEmpty()) { |
| 90 | + return false; |
| 91 | + } |
| 92 | + |
| 93 | + if (basePackage.isEmpty()) { |
| 94 | + return !declaringClassName.contains(StringHelper.DOT); |
| 95 | + } |
| 96 | + return declaringClassName.startsWith(basePackage + StringHelper.DOT); |
66 | 97 | } |
67 | 98 |
|
68 | 99 | /** |
69 | | - * Returns a component instance for the specified component class. |
| 100 | + * Resolves the base package used for compiled-definition scope filtering. |
70 | 101 | * |
71 | | - * @param componentClass the class declaring annotated MCP component methods |
72 | | - * @return the component instance to invoke |
| 102 | + * <p>Resolution priority: |
| 103 | + * |
| 104 | + * <ol> |
| 105 | + * <li>{@link McpServerApplication#basePackageClass()} when explicitly configured |
| 106 | + * <li>{@link McpServerApplication#basePackage()} when non-blank |
| 107 | + * <li>{@code mainClass.getPackageName()} as fallback |
| 108 | + * </ol> |
| 109 | + * |
| 110 | + * @param mainClass application entry class |
| 111 | + * @return resolved base package (never {@code null}) |
73 | 112 | */ |
74 | | - public Object getComponentInstance(Class<?> componentClass) { |
75 | | - return componentInstanceFactory.getInstance(componentClass); |
| 113 | + private static String resolveBasePackage(Class<?> mainClass) { |
| 114 | + McpServerApplication application = mainClass.getAnnotation(McpServerApplication.class); |
| 115 | + if (application == null) { |
| 116 | + return mainClass.getPackageName(); |
| 117 | + } |
| 118 | + |
| 119 | + if (application.basePackageClass() != Object.class) { |
| 120 | + return application.basePackageClass().getPackageName(); |
| 121 | + } |
| 122 | + |
| 123 | + final String configuredBasePackage = application.basePackage().trim(); |
| 124 | + return configuredBasePackage.isBlank() ? mainClass.getPackageName() : configuredBasePackage; |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Creates a component instance using the class no-argument constructor. |
| 129 | + * |
| 130 | + * <p>This method is used by the context cache on first access for each component class. |
| 131 | + * |
| 132 | + * @param clazz component class declaring MCP annotated methods |
| 133 | + * @return created component instance |
| 134 | + * @throws McpServerException when instance creation fails |
| 135 | + */ |
| 136 | + private static Object createInstance(Class<?> clazz) { |
| 137 | + try { |
| 138 | + return clazz.getDeclaredConstructor().newInstance(); |
| 139 | + } catch (Exception e) { |
| 140 | + throw new McpServerException( |
| 141 | + McpServerError.COMPONENT_INSTANCE_CREATE_ERROR.withDetail(clazz.getName()), e); |
| 142 | + } |
76 | 143 | } |
77 | 144 | } |
0 commit comments