Skip to content

Commit f8b7bb4

Browse files
committed
feat(annotations): Change default metadata values to use name attribute instead of "Not specified"
1 parent e79e10f commit f8b7bb4

File tree

11 files changed

+117
-84
lines changed

11 files changed

+117
-84
lines changed

src/main/java/com/github/codeboyzhou/mcp/declarative/annotation/McpJsonSchemaProperty.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,22 @@
2121
@Retention(RetentionPolicy.RUNTIME)
2222
public @interface McpJsonSchemaProperty {
2323
/**
24-
* The name of the JSON schema property. If not specified, the field name will be used.
24+
* The name of the JSON schema property. Defaults to the Java field name.
2525
*
2626
* @return the name of the JSON schema property
2727
*/
2828
String name() default StringHelper.EMPTY;
2929

3030
/**
31-
* The description of the JSON schema property. If not specified, the default value {@code "Not
32-
* specified"} will be used.
31+
* The description of the JSON schema property. Defaults to the value of the {@code name}
32+
* attribute.
3333
*
3434
* @return the description of the JSON schema property
3535
*/
3636
String description() default StringHelper.EMPTY;
3737

3838
/**
39-
* Whether the JSON schema property is required. If not specified, the default value {@code false}
40-
* will be used.
39+
* Whether the JSON schema property is required. Defaults to {@code false}.
4140
*
4241
* @return whether the JSON schema property is required
4342
*/

src/main/java/com/github/codeboyzhou/mcp/declarative/annotation/McpPrompt.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*
1212
* <p>The prompt's name defaults to the name of the annotated method. Prompt metadata such as title
1313
* and description can be specified via the corresponding attributes. If omitted, these metadata
14-
* fields will default to the literal string "Not specified".
14+
* fields will default to the value of the {@code name} attribute.
1515
*
1616
* <p>Example usage:
1717
*
@@ -38,14 +38,14 @@
3838
String name() default StringHelper.EMPTY;
3939

4040
/**
41-
* The title of the prompt. Defaults to the literal string "Not specified".
41+
* The title of the prompt. Defaults to the value of the {@code name} attribute.
4242
*
4343
* @return the title of the prompt
4444
*/
4545
String title() default StringHelper.EMPTY;
4646

4747
/**
48-
* The description of the prompt. Defaults to the literal string "Not specified".
48+
* The description of the prompt. Defaults to the value of the {@code name} attribute.
4949
*
5050
* @return the description of the prompt
5151
*/

src/main/java/com/github/codeboyzhou/mcp/declarative/annotation/McpPromptParam.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
*
1313
* <p>The parameter's name must be specified explicitly. Parameter metadata such as title,
1414
* description, and required can be specified via the corresponding attributes. If omitted, these
15-
* metadata fields will default to the literal string "Not specified" and {@code false}.
15+
* metadata fields will default to the value of the {@code name} attribute and {@code false}.
1616
*
1717
* <p>Example usage:
1818
*
@@ -39,14 +39,14 @@
3939
String name();
4040

4141
/**
42-
* The title of the prompt parameter. Defaults to the literal string "Not specified".
42+
* The title of the prompt parameter. Defaults to the value of the {@code name} attribute.
4343
*
4444
* @return the title of the prompt parameter
4545
*/
4646
String title() default StringHelper.EMPTY;
4747

4848
/**
49-
* The description of the prompt parameter. Defaults to the literal string "Not specified".
49+
* The description of the prompt parameter. Defaults to the value of the {@code name} attribute.
5050
*
5151
* @return the description of the prompt parameter
5252
*/

src/main/java/com/github/codeboyzhou/mcp/declarative/annotation/McpResource.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
*
1313
* <p>The resource's URI must be specified explicitly. Resource metadata such as name, title,
1414
* description, and MIME type can be specified via the corresponding attributes. If omitted, these
15-
* metadata fields will default to the literal string "Not specified" and "text/plain".
15+
* metadata fields will default to the value of the {@code name} attribute and "text/plain".
1616
*
1717
* <p>Example usage:
1818
*
@@ -46,14 +46,14 @@
4646
String name() default StringHelper.EMPTY;
4747

4848
/**
49-
* The title of the resource. Defaults to the literal string "Not specified".
49+
* The title of the resource. Defaults to the value of the {@code name} attribute.
5050
*
5151
* @return the title of the resource
5252
*/
5353
String title() default StringHelper.EMPTY;
5454

5555
/**
56-
* The description of the resource. Defaults to the literal string "Not specified".
56+
* The description of the resource. Defaults to the value of the {@code name} attribute.
5757
*
5858
* @return the description of the resource
5959
*/

src/main/java/com/github/codeboyzhou/mcp/declarative/annotation/McpTool.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*
1212
* <p>The tool's name defaults to the name of the annotated method. Tool metadata such as title and
1313
* description can be specified via the corresponding attributes. If omitted, these metadata fields
14-
* will default to the literal string "Not specified".
14+
* will default to the value of the {@code name} attribute.
1515
*
1616
* <p>Example usage:
1717
*
@@ -38,14 +38,14 @@
3838
String name() default StringHelper.EMPTY;
3939

4040
/**
41-
* The title of the tool. Defaults to the literal string "Not specified".
41+
* The title of the tool. Defaults to the value of the {@code name} attribute.
4242
*
4343
* @return the title of the tool
4444
*/
4545
String title() default StringHelper.EMPTY;
4646

4747
/**
48-
* The description of the tool. Defaults to the literal string "Not specified".
48+
* The description of the tool. Defaults to the value of the {@code name} attribute.
4949
*
5050
* @return the description of the tool
5151
*/

src/main/java/com/github/codeboyzhou/mcp/declarative/annotation/McpToolParam.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
*
1313
* <p>The parameter's name must be specified explicitly. Parameter metadata such as description and
1414
* required status can be specified via the corresponding attributes. If omitted, these metadata
15-
* fields will default to the literal string "Not specified" and {@code false}.
15+
* fields will default to the value of the {@code name} attribute and {@code false}.
1616
*
1717
* <p>Example usage:
1818
*
@@ -36,7 +36,7 @@
3636
String name();
3737

3838
/**
39-
* The description of the tool parameter. Defaults to the literal string "Not specified".
39+
* The description of the tool parameter. Defaults to the value of the {@code name} attribute.
4040
*
4141
* @return the description of the tool parameter
4242
*/

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@
1414
* @author codeboyzhou
1515
*/
1616
public abstract class AbstractMcpServerComponent<T> implements McpServerComponent<T> {
17-
/** The default value to use when a component attribute is not specified. */
18-
protected static final String NOT_SPECIFIED = "Not specified";
19-
2017
/** The dependency injector to use for injecting component attributes. */
2118
protected final DependencyInjector injector;
2219

@@ -30,16 +27,19 @@ protected AbstractMcpServerComponent() {
3027
}
3128

3229
/**
33-
* Resolves the value of a component attribute, using the resource bundle for localization if i18n
34-
* is enabled.
30+
* Localizes the attribute with the specified i18n key using the resource bundle, or returns the
31+
* default value if the key is not found in the bundle.
3532
*
36-
* @param attributeLiteralValue the literal value of the component attribute
37-
* @return the resolved value of the component attribute
33+
* @param i18nKey the i18n key of the attribute to localize
34+
* @param defaultValue the default value to return if the i18n key is not found in the bundle
35+
* @return the localized value of the attribute, or the default value if the i18n key is not found
36+
* in the bundle
3837
*/
39-
protected String resolveComponentAttributeValue(String attributeLiteralValue) {
40-
if (bundle != null && bundle.containsKey(attributeLiteralValue)) {
41-
return bundle.getString(attributeLiteralValue);
38+
protected String localizeAttribute(String i18nKey, String defaultValue) {
39+
if (bundle != null && bundle.containsKey(i18nKey)) {
40+
return bundle.getString(i18nKey);
4241
}
43-
return StringHelper.defaultIfBlank(attributeLiteralValue, NOT_SPECIFIED);
42+
// If we don't need to localize, just return the literal value or default value
43+
return StringHelper.defaultIfBlank(i18nKey, defaultValue);
4444
}
4545
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ public McpServerFeatures.SyncPromptSpecification create(Method method) {
4545
McpPrompt promptMethod = methodCache.getMcpPromptAnnotation();
4646
final String name =
4747
StringHelper.defaultIfBlank(promptMethod.name(), methodCache.getMethodName());
48-
final String title = resolveComponentAttributeValue(promptMethod.title());
49-
final String description = resolveComponentAttributeValue(promptMethod.description());
48+
final String title = localizeAttribute(promptMethod.title(), name);
49+
final String description = localizeAttribute(promptMethod.description(), name);
5050

5151
List<McpSchema.PromptArgument> promptArgs = createPromptArguments(methodCache.getParameters());
5252
McpSchema.Prompt prompt = new McpSchema.Prompt(name, title, description, promptArgs);
@@ -94,8 +94,8 @@ private List<McpSchema.PromptArgument> createPromptArguments(Parameter[] methodP
9494
if (param.isAnnotationPresent(McpPromptParam.class)) {
9595
McpPromptParam promptParam = param.getAnnotation(McpPromptParam.class);
9696
final String name = promptParam.name();
97-
final String title = resolveComponentAttributeValue(promptParam.title());
98-
final String description = resolveComponentAttributeValue(promptParam.description());
97+
final String title = localizeAttribute(promptParam.title(), name);
98+
final String description = localizeAttribute(promptParam.description(), name);
9999
final boolean required = promptParam.required();
100100
promptArguments.add(new McpSchema.PromptArgument(name, title, description, required));
101101
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ public McpServerFeatures.SyncResourceSpecification create(Method method) {
3131

3232
McpResource res = methodCache.getMcpResourceAnnotation();
3333
final String name = StringHelper.defaultIfBlank(res.name(), methodCache.getMethodName());
34-
final String title = resolveComponentAttributeValue(res.title());
35-
final String description = resolveComponentAttributeValue(res.description());
34+
final String title = localizeAttribute(res.title(), name);
35+
final String description = localizeAttribute(res.description(), name);
3636

3737
McpSchema.Resource resource =
3838
McpSchema.Resource.builder()

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ public McpServerFeatures.SyncToolSpecification create(Method method) {
5353

5454
McpTool toolMethod = methodCache.getMcpToolAnnotation();
5555
final String name = StringHelper.defaultIfBlank(toolMethod.name(), methodCache.getMethodName());
56-
final String title = resolveComponentAttributeValue(toolMethod.title());
57-
final String description = resolveComponentAttributeValue(toolMethod.description());
56+
final String title = localizeAttribute(toolMethod.title(), name);
57+
final String description = localizeAttribute(toolMethod.description(), name);
5858

5959
McpSchema.JsonSchema inputSchema = createJsonSchema(methodCache.getParameters());
6060
Map<String, Object> outputSchema = createJsonSchemaDefinition(methodCache.getReturnType());
@@ -131,7 +131,7 @@ private McpSchema.JsonSchema createJsonSchema(Parameter[] methodParams) {
131131
definitions.put(definitionClassName, definition);
132132
} else {
133133
property.put("type", JavaTypeToJsonSchemaMapper.getJsonSchemaType(definitionClass));
134-
property.put("description", resolveComponentAttributeValue(toolParam.description()));
134+
property.put("description", localizeAttribute(toolParam.description(), parameterName));
135135
}
136136
properties.put(parameterName, property);
137137

@@ -176,10 +176,10 @@ private Map<String, Object> createJsonSchemaDefinition(Class<?> definitionClass)
176176
}
177177

178178
Map<String, Object> fieldProperties = new HashMap<>();
179+
final String fieldName = StringHelper.defaultIfBlank(property.name(), field.getName());
179180
fieldProperties.put("type", JavaTypeToJsonSchemaMapper.getJsonSchemaType(field.getType()));
180-
fieldProperties.put("description", resolveComponentAttributeValue(property.description()));
181+
fieldProperties.put("description", localizeAttribute(property.description(), fieldName));
181182

182-
final String fieldName = StringHelper.defaultIfBlank(property.name(), field.getName());
183183
properties.put(fieldName, fieldProperties);
184184

185185
if (property.required()) {

0 commit comments

Comments
 (0)