Skip to content

Commit 05038e3

Browse files
committed
fix the parameters order problems when invode in MCP Inspector v0.12.0 tool
1 parent b2bc3e7 commit 05038e3

File tree

4 files changed

+28
-18
lines changed

4 files changed

+28
-18
lines changed

src/main/java/com/github/codeboyzhou/mcp/declarative/server/McpSyncServerPromptRegister.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public McpSyncServerPromptRegister(Set<Class<?>> promptClasses) {
2929
@Override
3030
public void registerTo(McpSyncServer server) {
3131
for (Class<?> promptClass : promptClasses) {
32-
Set<Method> methods = ReflectionHelper.getMethodsAnnotatedWith(promptClass, McpPrompt.class);
32+
List<Method> methods = ReflectionHelper.getMethodsAnnotatedWith(promptClass, McpPrompt.class);
3333
for (Method method : methods) {
3434
McpServerFeatures.SyncPromptSpecification prompt = createComponentFrom(promptClass, method);
3535
server.addPrompt(prompt);
@@ -59,7 +59,7 @@ public McpServerFeatures.SyncPromptSpecification createComponentFrom(Class<?> cl
5959
}
6060

6161
private List<McpSchema.PromptArgument> createPromptArguments(Method method) {
62-
Set<Parameter> parameters = ReflectionHelper.getParametersAnnotatedWith(method, McpPromptParam.class);
62+
List<Parameter> parameters = ReflectionHelper.getParametersAnnotatedWith(method, McpPromptParam.class);
6363
List<McpSchema.PromptArgument> promptArguments = new ArrayList<>(parameters.size());
6464
for (Parameter parameter : parameters) {
6565
McpPromptParam promptParam = parameter.getAnnotation(McpPromptParam.class);

src/main/java/com/github/codeboyzhou/mcp/declarative/server/McpSyncServerResourceRegister.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public McpSyncServerResourceRegister(Set<Class<?>> resourceClasses) {
2626
@Override
2727
public void registerTo(McpSyncServer server) {
2828
for (Class<?> resourceClass : resourceClasses) {
29-
Set<Method> methods = ReflectionHelper.getMethodsAnnotatedWith(resourceClass, McpResource.class);
29+
List<Method> methods = ReflectionHelper.getMethodsAnnotatedWith(resourceClass, McpResource.class);
3030
for (Method method : methods) {
3131
McpServerFeatures.SyncResourceSpecification resource = createComponentFrom(resourceClass, method);
3232
server.addResource(resource);

src/main/java/com/github/codeboyzhou/mcp/declarative/server/McpSyncServerToolRegister.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,7 @@
1111

1212
import java.lang.reflect.Method;
1313
import java.lang.reflect.Parameter;
14-
import java.util.ArrayList;
15-
import java.util.HashMap;
16-
import java.util.List;
17-
import java.util.Map;
18-
import java.util.Set;
14+
import java.util.*;
1915

2016
public class McpSyncServerToolRegister
2117
implements McpServerComponentRegister<McpSyncServer, McpServerFeatures.SyncToolSpecification> {
@@ -33,7 +29,7 @@ public McpSyncServerToolRegister(Set<Class<?>> toolClasses) {
3329
@Override
3430
public void registerTo(McpSyncServer server) {
3531
for (Class<?> toolClass : toolClasses) {
36-
Set<Method> methods = ReflectionHelper.getMethodsAnnotatedWith(toolClass, McpTool.class);
32+
List<Method> methods = ReflectionHelper.getMethodsAnnotatedWith(toolClass, McpTool.class);
3733
for (Method method : methods) {
3834
McpServerFeatures.SyncToolSpecification tool = createComponentFrom(toolClass, method);
3935
server.addTool(tool);
@@ -63,10 +59,11 @@ public McpServerFeatures.SyncToolSpecification createComponentFrom(Class<?> claz
6359
}
6460

6561
private McpSchema.JsonSchema createJsonSchema(Method method) {
66-
Map<String, Object> properties = new HashMap<>();
62+
//has to use linkedhashmap to make order correct
63+
Map<String, Object> properties = new LinkedHashMap<>();
6764
List<String> required = new ArrayList<>();
6865

69-
Set<Parameter> parameters = ReflectionHelper.getParametersAnnotatedWith(method, McpToolParam.class);
66+
List<Parameter> parameters = ReflectionHelper.getParametersAnnotatedWith(method, McpToolParam.class);
7067
for (Parameter parameter : parameters) {
7168
McpToolParam toolParam = parameter.getAnnotation(McpToolParam.class);
7269
final String parameterName = toolParam.name();

src/main/java/com/github/codeboyzhou/mcp/declarative/util/ReflectionHelper.java

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,35 @@
55
import java.lang.annotation.Annotation;
66
import java.lang.reflect.Method;
77
import java.lang.reflect.Parameter;
8+
import java.util.Arrays;
89
import java.util.LinkedHashMap;
10+
import java.util.List;
911
import java.util.Map;
10-
import java.util.Set;
11-
12-
import static java.util.stream.Collectors.toSet;
1312

1413
public final class ReflectionHelper {
1514

16-
public static Set<Method> getMethodsAnnotatedWith(Class<?> clazz, Class<? extends Annotation> annotation) {
15+
/**
16+
* has to use list as result to make order correct
17+
* @param clazz
18+
* @param annotation
19+
* @return
20+
*/
21+
public static List<Method> getMethodsAnnotatedWith(Class<?> clazz, Class<? extends Annotation> annotation) {
1722
Method[] methods = clazz.getMethods();
18-
return Set.of(methods).stream().filter(m -> m.isAnnotationPresent(annotation)).collect(toSet());
23+
List<Method> methodList = Arrays.asList(methods);
24+
return methodList.stream().filter(p -> p.isAnnotationPresent(annotation)).toList();
1925
}
2026

21-
public static Set<Parameter> getParametersAnnotatedWith(Method method, Class<? extends Annotation> annotation) {
27+
/**
28+
* has to use list as result to make order correct
29+
* @param method
30+
* @param annotation
31+
* @return
32+
*/
33+
public static List<Parameter> getParametersAnnotatedWith(Method method, Class<? extends Annotation> annotation) {
2234
Parameter[] parameters = method.getParameters();
23-
return Set.of(parameters).stream().filter(p -> p.isAnnotationPresent(annotation)).collect(toSet());
35+
List<Parameter> parameterList = Arrays.asList(parameters);
36+
return parameterList.stream().filter(p -> p.isAnnotationPresent(annotation)).toList();
2437
}
2538

2639
public static Object invokeMethod(Class<?> clazz, Method method) throws Exception {

0 commit comments

Comments
 (0)