Skip to content

Commit 0947de8

Browse files
committed
refactor(annotation): Support @McpComponentScan and descriptionI18nKey() for backward compatibility
1 parent c8ec6ad commit 0947de8

File tree

4 files changed

+43
-6
lines changed

4 files changed

+43
-6
lines changed

src/main/java/com/github/codeboyzhou/mcp/declarative/common/GuiceInjectorModule.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import static org.reflections.scanners.Scanners.MethodsAnnotated;
66
import static org.reflections.scanners.Scanners.TypesAnnotated;
77

8+
import com.github.codeboyzhou.mcp.declarative.annotation.McpComponentScan;
89
import com.github.codeboyzhou.mcp.declarative.annotation.McpI18nEnabled;
910
import com.github.codeboyzhou.mcp.declarative.annotation.McpPrompts;
1011
import com.github.codeboyzhou.mcp.declarative.annotation.McpResources;
@@ -34,6 +35,12 @@ public GuiceInjectorModule(Class<?> mainClass) {
3435
@SuppressWarnings("unused")
3536
public Reflections provideReflections() {
3637
McpServerApplication application = mainClass.getAnnotation(McpServerApplication.class);
38+
// Support @McpComponentScan for backward compatibility, will be removed in next version
39+
if (application == null) {
40+
McpComponentScan scan = mainClass.getAnnotation(McpComponentScan.class);
41+
final String basePackage = determineBasePackage(scan);
42+
return new Reflections(basePackage, TypesAnnotated, MethodsAnnotated, FieldsAnnotated);
43+
}
3744
final String basePackage = determineBasePackage(application);
3845
return new Reflections(basePackage, TypesAnnotated, MethodsAnnotated, FieldsAnnotated);
3946
}
@@ -71,4 +78,17 @@ private String determineBasePackage(McpServerApplication application) {
7178
}
7279
return mainClass.getPackageName();
7380
}
81+
82+
@Deprecated
83+
private String determineBasePackage(McpComponentScan scan) {
84+
if (scan != null) {
85+
if (!scan.basePackage().trim().isBlank()) {
86+
return scan.basePackage();
87+
}
88+
if (scan.basePackageClass() != Object.class) {
89+
return scan.basePackageClass().getPackageName();
90+
}
91+
}
92+
return mainClass.getPackageName();
93+
}
7494
}

src/main/java/com/github/codeboyzhou/mcp/declarative/server/factory/McpServerPromptFactory.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@ public McpServerFeatures.AsyncPromptSpecification create(Class<?> clazz, Method
4444
McpPrompt promptMethod = method.getAnnotation(McpPrompt.class);
4545
final String name = StringHelper.defaultIfBlank(promptMethod.name(), method.getName());
4646
final String title = resolveComponentAttributeValue(promptMethod.title());
47-
final String description = resolveComponentAttributeValue(promptMethod.description());
47+
final String description =
48+
resolveComponentAttributeValue(
49+
StringHelper.defaultIfBlank(
50+
promptMethod.description(), promptMethod.descriptionI18nKey()));
4851
List<McpSchema.PromptArgument> promptArguments = createPromptArguments(method);
4952
McpSchema.Prompt prompt = new McpSchema.Prompt(name, title, description, promptArguments);
5053
logger.debug("Registering prompt: {}", JsonHelper.toJson(prompt));
@@ -96,7 +99,10 @@ private List<McpSchema.PromptArgument> createPromptArguments(Method method) {
9699
McpPromptParam promptParam = param.getAnnotation(McpPromptParam.class);
97100
final String name = promptParam.name();
98101
final String title = resolveComponentAttributeValue(promptParam.title());
99-
final String description = resolveComponentAttributeValue(promptParam.description());
102+
final String description =
103+
resolveComponentAttributeValue(
104+
StringHelper.defaultIfBlank(
105+
promptParam.description(), promptParam.descriptionI18nKey()));
100106
final boolean required = promptParam.required();
101107
McpSchema.PromptArgument promptArgument =
102108
new McpSchema.PromptArgument(name, title, description, required);

src/main/java/com/github/codeboyzhou/mcp/declarative/server/factory/McpServerResourceFactory.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ public McpServerFeatures.AsyncResourceSpecification create(Class<?> clazz, Metho
3737
McpResource res = method.getAnnotation(McpResource.class);
3838
final String name = StringHelper.defaultIfBlank(res.name(), method.getName());
3939
final String title = resolveComponentAttributeValue(res.title());
40-
final String description = resolveComponentAttributeValue(res.description());
40+
final String description =
41+
resolveComponentAttributeValue(
42+
StringHelper.defaultIfBlank(res.description(), res.descriptionI18nKey()));
4143
McpSchema.Resource resource =
4244
McpSchema.Resource.builder()
4345
.uri(res.uri())

src/main/java/com/github/codeboyzhou/mcp/declarative/server/factory/McpServerToolFactory.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ public McpServerFeatures.AsyncToolSpecification create(Class<?> clazz, Method me
4949
McpTool toolMethod = method.getAnnotation(McpTool.class);
5050
final String name = StringHelper.defaultIfBlank(toolMethod.name(), method.getName());
5151
final String title = resolveComponentAttributeValue(toolMethod.title());
52-
final String description = resolveComponentAttributeValue(toolMethod.description());
52+
final String description =
53+
resolveComponentAttributeValue(
54+
StringHelper.defaultIfBlank(toolMethod.description(), toolMethod.descriptionI18nKey()));
5355
McpSchema.JsonSchema paramSchema = createJsonSchema(method);
5456
McpSchema.Tool tool =
5557
McpSchema.Tool.builder()
@@ -117,7 +119,11 @@ private McpSchema.JsonSchema createJsonSchema(Method method) {
117119

118120
if (parameterType.getAnnotation(McpJsonSchemaDefinition.class) == null) {
119121
property.put("type", parameterType.getSimpleName().toLowerCase());
120-
property.put("description", resolveComponentAttributeValue(toolParam.description()));
122+
property.put(
123+
"description",
124+
resolveComponentAttributeValue(
125+
StringHelper.defaultIfBlank(
126+
toolParam.description(), toolParam.descriptionI18nKey())));
121127
} else {
122128
final String parameterTypeSimpleName = parameterType.getSimpleName();
123129
property.put("$ref", "#/definitions/" + parameterTypeSimpleName);
@@ -163,7 +169,10 @@ private Map<String, Object> createJsonSchemaDefinition(Class<?> definitionClass)
163169

164170
Map<String, Object> fieldProperties = new HashMap<>();
165171
fieldProperties.put("type", field.getType().getSimpleName().toLowerCase());
166-
fieldProperties.put("description", resolveComponentAttributeValue(property.description()));
172+
fieldProperties.put(
173+
"description",
174+
resolveComponentAttributeValue(
175+
StringHelper.defaultIfBlank(property.description(), property.descriptionI18nKey())));
167176

168177
final String fieldName = StringHelper.defaultIfBlank(property.name(), field.getName());
169178
properties.put(fieldName, fieldProperties);

0 commit comments

Comments
 (0)