Skip to content

Commit 55781bf

Browse files
committed
refactor: clarify component registration scope and improve documentation
Update documentation and code comments to enhance clarity around component registration scope, replacing terms like "package scanning path" with "registration scope." Adjust exception messages and method descriptions to reflect the new terminology and improve understanding of component registration processes. Ensure consistency across annotations and exception handling related to component registration.
1 parent 8b303cc commit 55781bf

10 files changed

Lines changed: 43 additions & 33 deletions

File tree

docs/components.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ public class MyPromptCompletions {
211211

212212
## Automatic Registration
213213

214-
After defining MCP components, they will be automatically registered to the server. You just need to ensure that the component classes are in the package scanning path of the server application.
214+
After defining MCP components, they will be automatically registered to the server. You just need to ensure that the component classes are within the registration scope of the server application.
215215

216216
### One instance per component class
217217

@@ -235,7 +235,7 @@ If you need to specify a specific package path, you can use the following method
235235
@McpServerApplication(basePackage = "com.example.mcp.components")
236236
```
237237

238-
If no package path is specified, the package of the class passed to `McpApplication.run()` will be scanned.
238+
If no package path is specified, the package of the class passed to `McpApplication.run()` is used as the default registration scope.
239239

240240
## Structured Content
241241

src/main/java/com/github/thought2code/mcp/annotated/McpApplication.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
* <p>This class provides the entry point for running MCP servers with annotation-based
2323
* configuration. It supports STDIO and STREAMABLE modes. {@link ServerMode#SSE} is deprecated but
2424
* still supported for backward compatibility. The application automatically loads configuration,
25-
* initializes reflection scanning, and starts the appropriate server based on the configuration
26-
* settings.
25+
* resolves component registration scope, and starts the appropriate server based on the
26+
* configuration settings.
2727
*
2828
* @author codeboyzhou
2929
*/
@@ -39,10 +39,11 @@ private McpApplication() {
3939
/**
4040
* Runs the MCP application with the specified main class and command-line arguments.
4141
*
42-
* <p>This method initializes the reflection provider and starts the MCP server based on the
42+
* <p>This method resolves application context scope and starts the MCP server based on the
4343
* configuration settings.
4444
*
45-
* @param mainClass the main class of the application, used as the base for reflection scanning
45+
* @param mainClass the main class of the application, used to resolve component registration
46+
* scope
4647
* @param args the command-line arguments passed to the application
4748
* @param configFileName the name of the configuration file to load
4849
* @see McpApplicationContext
@@ -58,10 +59,11 @@ public static void run(Class<?> mainClass, String[] args, String configFileName)
5859
* Runs the MCP application with the specified main class and command-line arguments using the
5960
* default configuration file.
6061
*
61-
* <p>This method initializes the reflection provider and starts the MCP server based on the
62+
* <p>This method resolves application context scope and starts the MCP server based on the
6263
* configuration settings.
6364
*
64-
* @param mainClass the main class of the application, used as the base for reflection scanning
65+
* @param mainClass the main class of the application, used to resolve component registration
66+
* scope
6567
* @param args the command-line arguments passed to the application
6668
*/
6769
public static void run(Class<?> mainClass, String[] args) {

src/main/java/com/github/thought2code/mcp/annotated/McpApplicationContext.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
/**
1212
* Runtime context for one annotated MCP application.
1313
*
14-
* <p>In the build-time component architecture, this context no longer performs classpath scanning.
15-
* Instead, it centralizes runtime concerns that component invokers still need:
14+
* <p>In the build-time component architecture, this context no longer performs runtime classpath
15+
* discovery. Instead, it centralizes runtime concerns that component invokers still need:
1616
*
1717
* <ul>
1818
* <li>Component instance lifecycle (lazy creation with one cached instance per component class)

src/main/java/com/github/thought2code/mcp/annotated/annotation/McpServerApplication.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
/**
1010
* This annotation is used to mark a class as an MCP (Model Context Protocol) server application.
1111
*
12-
* <p>The base package for component scanning can be specified via the {@code basePackage}
12+
* <p>The base package for component registration scope can be specified via the {@code basePackage}
1313
* attribute. If not specified, the package of the annotated class will be used.
1414
*
1515
* <p>Example usage:
@@ -29,19 +29,20 @@
2929
@Retention(RetentionPolicy.RUNTIME)
3030
public @interface McpServerApplication {
3131
/**
32-
* The base package for component scanning. Defaults to the package of the annotated class.
32+
* The base package for component registration scope. Defaults to the package of the annotated
33+
* class.
3334
*
34-
* @return the base package for component scanning
35+
* @return the base package for component registration scope
3536
*/
3637
String basePackage() default StringHelper.EMPTY;
3738

3839
/**
39-
* The base package class for component scanning. Defaults to {@code Object.class}.
40+
* The base package class for component registration scope. Defaults to {@code Object.class}.
4041
*
4142
* <p>Note: This attribute is intended to be used when the base package cannot be determined
4243
* statically. In most cases, {@link #basePackage()} should be used instead.
4344
*
44-
* @return the base package class for component scanning
45+
* @return the base package class for component registration scope
4546
*/
4647
Class<?> basePackageClass() default Object.class;
4748
}

src/main/java/com/github/thought2code/mcp/annotated/exception/McpServerComponentRegistrationException.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* <li>Missing or incorrect annotations on component methods
1313
* <li>Dependency injection failures during component creation
1414
* <li>Configuration errors in component metadata
15-
* <li>Reflection-related errors during component discovery
15+
* <li>Build-time model loading or runtime registration failures
1616
* </ul>
1717
*
1818
* <p>This exception extends {@link McpServerException} and is part of the MCP annotated framework's
@@ -45,9 +45,9 @@ public McpServerComponentRegistrationException(String message) {
4545
* another exception. The cause exception is preserved and can be accessed later for detailed
4646
* error analysis and debugging.
4747
*
48-
* <p>Common causes include reflection exceptions, dependency injection failures, or validation
49-
* errors during component creation. The message should provide context about the registration
50-
* operation that failed.
48+
* <p>Common causes include component instantiation failures, build-time model loading issues, or
49+
* validation errors during component registration. The message should provide context about the
50+
* registration operation that failed.
5151
*
5252
* @param message the detail message explaining the reason for the exception, may be null but
5353
* should provide meaningful error information

src/main/java/com/github/thought2code/mcp/annotated/server/AnnotatedMcpServer.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,8 @@ public interface AnnotatedMcpServer {
8181
* Registers all MCP server components (resources, prompts, tools) with the specified synchronous
8282
* server instance.
8383
*
84-
* <p>This method should scan for annotated methods and register them as appropriate MCP
85-
* components with the server. Components are discovered using reflection and registered through
86-
* the component registry.
84+
* <p>This method should load build-time generated component definitions and register the in-scope
85+
* components with the server.
8786
*
8887
* @param mcpSyncServer the synchronous server instance to register components with
8988
*/
@@ -93,9 +92,8 @@ public interface AnnotatedMcpServer {
9392
* Registers all MCP server components (resources, prompts, tools) with the specified asynchronous
9493
* server instance.
9594
*
96-
* <p>This method should scan for annotated methods and register them as appropriate MCP
97-
* components with the server. Components are discovered using reflection and registered through
98-
* the component registry.
95+
* <p>This method should load build-time generated component definitions and register the in-scope
96+
* components with the server.
9997
*
10098
* @param mcpAsyncServer the asynchronous server instance to register components with
10199
*/

src/main/java/com/github/thought2code/mcp/annotated/server/component/AnnotationProcessor.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,10 @@ private void writeProvider() throws IOException {
368368
writer.write("import org.slf4j.Logger;\n");
369369
writer.write("import org.slf4j.LoggerFactory;\n\n");
370370
writer.write("public final class " + className + " implements ComponentProvider {\n\n");
371-
writer.write(" private static final Logger log = LoggerFactory.getLogger(" + className + ".class);\n\n");
371+
writer.write(
372+
" private static final Logger log = LoggerFactory.getLogger("
373+
+ className
374+
+ ".class);\n\n");
372375

373376
writer.write(" @Override\n");
374377
writer.write(" public List<ToolDefinition> tools() {\n");

src/main/java/com/github/thought2code/mcp/annotated/server/component/Invocation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import org.jetbrains.annotations.NotNull;
44

55
/**
6-
* This record represents the result of reflection invocation of Java method.
6+
* This record represents the result of invoking one generated MCP component binding.
77
*
88
* @author codeboyzhou
99
*/

src/main/java/com/github/thought2code/mcp/annotated/server/component/InvocationLogMessageHelper.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ public final class InvocationLogMessageHelper {
55

66
private InvocationLogMessageHelper() {}
77

8-
public static final String TOOL_INVOCATION_FAILED =
9-
"Tool invocation failed for sourceMethod={}";
8+
public static final String TOOL_INVOCATION_FAILED = "Tool invocation failed for sourceMethod={}";
109

1110
public static final String PROMPT_INVOCATION_FAILED =
1211
"Prompt invocation failed for sourceMethod={}";

src/test/java/com/github/thought2code/mcp/annotated/server/component/DuplicateComponentMessageHelperTest.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,24 @@ class DuplicateComponentMessageHelperTest {
1313
@ParameterizedTest
1414
@MethodSource("duplicateNameMessageCases")
1515
void duplicateNameMessage_shouldMatchTemplate(
16-
String componentKind, String name, String previousMethod, String currentMethod, String expected) {
16+
String componentKind,
17+
String name,
18+
String previousMethod,
19+
String currentMethod,
20+
String expected) {
1721
String actual =
1822
switch (componentKind) {
1923
case "tool" ->
20-
DuplicateComponentMessageHelper.duplicateToolName(name, previousMethod, currentMethod);
24+
DuplicateComponentMessageHelper.duplicateToolName(
25+
name, previousMethod, currentMethod);
2126
case "prompt" ->
2227
DuplicateComponentMessageHelper.duplicatePromptName(
2328
name, previousMethod, currentMethod);
2429
case "resource" ->
2530
DuplicateComponentMessageHelper.duplicateResourceName(
2631
name, previousMethod, currentMethod);
27-
default -> throw new IllegalArgumentException("Unsupported component kind: " + componentKind);
32+
default ->
33+
throw new IllegalArgumentException("Unsupported component kind: " + componentKind);
2834
};
2935
assertEquals(expected, actual);
3036
}
@@ -86,7 +92,8 @@ private static Stream<Arguments> duplicateCompletionMessageCases() {
8692
private static Stream<Arguments> completionReferenceDescriptionCases() {
8793
return Stream.of(
8894
Arguments.of(
89-
McpSchema.PromptReference.builder("generateCode").build(), "prompt name 'generateCode'"),
95+
McpSchema.PromptReference.builder("generateCode").build(),
96+
"prompt name 'generateCode'"),
9097
Arguments.of(new McpSchema.ResourceReference("file://"), "resource uri 'file://'"),
9198
Arguments.of(null, "'null'"));
9299
}

0 commit comments

Comments
 (0)