Skip to content

Commit e5a974f

Browse files
committed
refactor(server): Streamline parameter handling in McpServerPromptFactory and McpServerToolFactory
1 parent 47cad0f commit e5a974f

File tree

5 files changed

+45
-59
lines changed

5 files changed

+45
-59
lines changed

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

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import java.util.ArrayList;
1414
import java.util.List;
1515
import java.util.Map;
16-
import java.util.stream.Stream;
1716
import org.slf4j.Logger;
1817
import org.slf4j.LoggerFactory;
1918

@@ -51,40 +50,36 @@ public McpServerFeatures.SyncPromptSpecification create(Class<?> clazz, Method m
5150
}
5251

5352
private List<McpSchema.PromptArgument> createPromptArguments(Method method) {
54-
Stream<Parameter> parameters = Stream.of(method.getParameters());
55-
List<Parameter> params =
56-
parameters.filter(p -> p.isAnnotationPresent(McpPromptParam.class)).toList();
57-
List<McpSchema.PromptArgument> promptArguments = new ArrayList<>(params.size());
58-
for (Parameter param : params) {
59-
McpPromptParam promptParam = param.getAnnotation(McpPromptParam.class);
60-
final String name = promptParam.name();
61-
final String title = resolveComponentAttributeValue(promptParam.title());
62-
final String description = resolveComponentAttributeValue(promptParam.description());
63-
final boolean required = promptParam.required();
64-
McpSchema.PromptArgument promptArgument =
65-
new McpSchema.PromptArgument(name, title, description, required);
66-
promptArguments.add(promptArgument);
53+
Parameter[] methodParams = method.getParameters();
54+
List<McpSchema.PromptArgument> promptArguments = new ArrayList<>(methodParams.length);
55+
for (Parameter param : methodParams) {
56+
if (param.isAnnotationPresent(McpPromptParam.class)) {
57+
McpPromptParam promptParam = param.getAnnotation(McpPromptParam.class);
58+
final String name = promptParam.name();
59+
final String title = resolveComponentAttributeValue(promptParam.title());
60+
final String description = resolveComponentAttributeValue(promptParam.description());
61+
final boolean required = promptParam.required();
62+
promptArguments.add(new McpSchema.PromptArgument(name, title, description, required));
63+
}
6764
}
6865
return promptArguments;
6966
}
7067

71-
private List<Object> asTypedParameterValues(Method method, Map<String, Object> parameters) {
68+
private List<Object> asTypedParameterValues(Method method, Map<String, Object> arguments) {
7269
Parameter[] methodParams = method.getParameters();
7370
List<Object> typedValues = new ArrayList<>(methodParams.length);
74-
7571
for (Parameter param : methodParams) {
7672
Object rawValue = null;
7773
if (param.isAnnotationPresent(McpPromptParam.class)) {
7874
McpPromptParam promptParam = param.getAnnotation(McpPromptParam.class);
79-
rawValue = parameters.get(promptParam.name());
75+
rawValue = arguments.get(promptParam.name());
8076
}
8177
// Fill in a default value when the parameter is not specified or unannotated
8278
// to ensure that the parameter type is correct when calling method.invoke()
8379
Class<?> targetType = param.getType();
84-
Object typed = Types.convert(rawValue, targetType);
85-
typedValues.add(typed);
80+
Object typedValue = Types.convert(rawValue, targetType);
81+
typedValues.add(typedValue);
8682
}
87-
8883
return typedValues;
8984
}
9085
}

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

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import java.util.List;
2121
import java.util.Map;
2222
import java.util.Set;
23-
import java.util.stream.Stream;
2423
import org.reflections.Reflections;
2524
import org.slf4j.Logger;
2625
import org.slf4j.LoggerFactory;
@@ -71,29 +70,28 @@ private McpSchema.JsonSchema createJsonSchema(Method method) {
7170
Map<String, Object> definitions = new LinkedHashMap<>();
7271
List<String> required = new ArrayList<>();
7372

74-
Stream<Parameter> parameters = Stream.of(method.getParameters());
75-
List<Parameter> params =
76-
parameters.filter(p -> p.isAnnotationPresent(McpToolParam.class)).toList();
77-
78-
for (Parameter param : params) {
79-
McpToolParam toolParam = param.getAnnotation(McpToolParam.class);
80-
final String parameterName = toolParam.name();
81-
Class<?> parameterType = param.getType();
82-
Map<String, String> property = new HashMap<>();
83-
84-
if (parameterType.isAnnotationPresent(McpJsonSchemaDefinition.class)) {
85-
final String parameterTypeSimpleName = parameterType.getSimpleName();
86-
property.put("$ref", "#/definitions/" + parameterTypeSimpleName);
87-
Map<String, Object> definition = createJsonSchemaDefinition(parameterType);
88-
definitions.put(parameterTypeSimpleName, definition);
89-
} else {
90-
property.put("type", parameterType.getSimpleName().toLowerCase());
91-
property.put("description", resolveComponentAttributeValue(toolParam.description()));
92-
}
93-
properties.put(parameterName, property);
94-
95-
if (toolParam.required()) {
96-
required.add(parameterName);
73+
Parameter[] methodParams = method.getParameters();
74+
for (Parameter param : methodParams) {
75+
if (param.isAnnotationPresent(McpToolParam.class)) {
76+
McpToolParam toolParam = param.getAnnotation(McpToolParam.class);
77+
final String parameterName = toolParam.name();
78+
Class<?> definitionClass = param.getType();
79+
Map<String, String> property = new HashMap<>();
80+
81+
if (definitionClass.isAnnotationPresent(McpJsonSchemaDefinition.class)) {
82+
final String definitionClassName = definitionClass.getSimpleName();
83+
property.put("$ref", "#/definitions/" + definitionClassName);
84+
Map<String, Object> definition = createJsonSchemaDefinition(definitionClass);
85+
definitions.put(definitionClassName, definition);
86+
} else {
87+
property.put("type", definitionClass.getSimpleName().toLowerCase());
88+
property.put("description", resolveComponentAttributeValue(toolParam.description()));
89+
}
90+
properties.put(parameterName, property);
91+
92+
if (toolParam.required()) {
93+
required.add(parameterName);
94+
}
9795
}
9896
}
9997

@@ -146,23 +144,21 @@ private Map<String, Object> createJsonSchemaDefinition(Class<?> definitionClass)
146144
return definitionJsonSchema;
147145
}
148146

149-
private List<Object> asTypedParameterValues(Method method, Map<String, Object> parameters) {
147+
private List<Object> asTypedParameterValues(Method method, Map<String, Object> arguments) {
150148
Parameter[] methodParams = method.getParameters();
151149
List<Object> typedValues = new ArrayList<>(methodParams.length);
152-
153150
for (Parameter param : methodParams) {
154151
Object rawValue = null;
155152
if (param.isAnnotationPresent(McpToolParam.class)) {
156153
McpToolParam toolParam = param.getAnnotation(McpToolParam.class);
157-
rawValue = parameters.get(toolParam.name());
154+
rawValue = arguments.get(toolParam.name());
158155
}
159156
// Fill in a default value when the parameter is not specified or unannotated
160157
// to ensure that the parameter type is correct when calling method.invoke()
161158
Class<?> targetType = param.getType();
162-
Object typed = Types.convert(rawValue, targetType);
163-
typedValues.add(typed);
159+
Object typedValue = Types.convert(rawValue, targetType);
160+
typedValues.add(typedValue);
164161
}
165-
166162
return typedValues;
167163
}
168164
}

src/test/java/com/github/codeboyzhou/mcp/declarative/McpServersTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import com.github.codeboyzhou.mcp.declarative.server.factory.McpSseServerInfo;
1414
import com.github.codeboyzhou.mcp.declarative.server.factory.McpStreamableServerInfo;
1515
import com.github.codeboyzhou.mcp.declarative.test.TestSimpleMcpStdioServer;
16-
import com.github.codeboyzhou.mcp.declarative.util.ObjectMappers;
1716
import io.modelcontextprotocol.client.McpClient;
1817
import io.modelcontextprotocol.client.McpSyncClient;
1918
import io.modelcontextprotocol.client.transport.HttpClientSseClientTransport;
@@ -206,7 +205,7 @@ private void verifyPromptsCalled(McpSyncClient client) {
206205
McpSchema.GetPromptRequest request1 = new McpSchema.GetPromptRequest(name1, args1);
207206
McpSchema.GetPromptResult result1 = client.getPrompt(request1);
208207
McpSchema.TextContent content = (McpSchema.TextContent) result1.messages().get(0).content();
209-
assertEquals(ObjectMappers.toJson(args1), content.text());
208+
assertEquals(args1.get("param1") + args1.get("param2").toString(), content.text());
210209
assertEquals("prompt1_description", result1.description());
211210
}
212211

@@ -225,7 +224,7 @@ private void verifyToolsCalled(McpSyncClient client) {
225224
McpSchema.CallToolRequest request1 = new McpSchema.CallToolRequest(name1, args1);
226225
McpSchema.CallToolResult result1 = client.callTool(request1);
227226
McpSchema.TextContent content = (McpSchema.TextContent) result1.content().get(0);
228-
assertEquals(ObjectMappers.toJson(args1), content.text());
227+
assertEquals(args1.get("param1") + args1.get("param2").toString(), content.text());
229228
assertFalse(result1.isError());
230229
}
231230
}

src/test/java/com/github/codeboyzhou/mcp/declarative/test/TestMcpPrompts.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
import com.github.codeboyzhou.mcp.declarative.annotation.McpPrompt;
44
import com.github.codeboyzhou.mcp.declarative.annotation.McpPromptParam;
5-
import com.github.codeboyzhou.mcp.declarative.util.ObjectMappers;
6-
import java.util.Map;
75
import org.slf4j.Logger;
86
import org.slf4j.LoggerFactory;
97

@@ -19,6 +17,6 @@ public String prompt1(
1917
String param2,
2018
String param3) {
2119
log.debug("prompt1 called with params: {}, {}, {}", param1, param2, param3);
22-
return ObjectMappers.toJson(Map.of("param1", param1, "param2", param2));
20+
return param1 + param2;
2321
}
2422
}

src/test/java/com/github/codeboyzhou/mcp/declarative/test/TestMcpTools.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
import com.github.codeboyzhou.mcp.declarative.annotation.McpTool;
44
import com.github.codeboyzhou.mcp.declarative.annotation.McpToolParam;
5-
import com.github.codeboyzhou.mcp.declarative.util.ObjectMappers;
6-
import java.util.Map;
75
import org.slf4j.Logger;
86
import org.slf4j.LoggerFactory;
97

@@ -18,6 +16,6 @@ public String tool1(
1816
String param2,
1917
String param3) {
2018
log.debug("tool1 called with params: {}, {}, {}", param1, param2, param3);
21-
return ObjectMappers.toJson(Map.of("param1", param1, "param2", param2));
19+
return param1 + param2;
2220
}
2321
}

0 commit comments

Comments
 (0)