Skip to content

Fix the parameters order problems when invoke in MCP Inspector v0.12.0 #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
@Override
public void registerTo(McpSyncServer server) {
for (Class<?> promptClass : promptClasses) {
Set<Method> methods = ReflectionHelper.getMethodsAnnotatedWith(promptClass, McpPrompt.class);
List<Method> methods = ReflectionHelper.getMethodsAnnotatedWith(promptClass, McpPrompt.class);

Check warning on line 32 in src/main/java/com/github/codeboyzhou/mcp/declarative/server/McpSyncServerPromptRegister.java

View check run for this annotation

Codecov / codecov/patch

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

Added line #L32 was not covered by tests
for (Method method : methods) {
McpServerFeatures.SyncPromptSpecification prompt = createComponentFrom(promptClass, method);
server.addPrompt(prompt);
Expand Down Expand Up @@ -59,7 +59,7 @@
}

private List<McpSchema.PromptArgument> createPromptArguments(Method method) {
Set<Parameter> parameters = ReflectionHelper.getParametersAnnotatedWith(method, McpPromptParam.class);
List<Parameter> parameters = ReflectionHelper.getParametersAnnotatedWith(method, McpPromptParam.class);

Check warning on line 62 in src/main/java/com/github/codeboyzhou/mcp/declarative/server/McpSyncServerPromptRegister.java

View check run for this annotation

Codecov / codecov/patch

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

Added line #L62 was not covered by tests
List<McpSchema.PromptArgument> promptArguments = new ArrayList<>(parameters.size());
for (Parameter parameter : parameters) {
McpPromptParam promptParam = parameter.getAnnotation(McpPromptParam.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public McpSyncServerResourceRegister(Set<Class<?>> resourceClasses) {
@Override
public void registerTo(McpSyncServer server) {
for (Class<?> resourceClass : resourceClasses) {
Set<Method> methods = ReflectionHelper.getMethodsAnnotatedWith(resourceClass, McpResource.class);
List<Method> methods = ReflectionHelper.getMethodsAnnotatedWith(resourceClass, McpResource.class);
for (Method method : methods) {
McpServerFeatures.SyncResourceSpecification resource = createComponentFrom(resourceClass, method);
server.addResource(resource);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand All @@ -33,7 +33,7 @@ public McpSyncServerToolRegister(Set<Class<?>> toolClasses) {
@Override
public void registerTo(McpSyncServer server) {
for (Class<?> toolClass : toolClasses) {
Set<Method> methods = ReflectionHelper.getMethodsAnnotatedWith(toolClass, McpTool.class);
List<Method> methods = ReflectionHelper.getMethodsAnnotatedWith(toolClass, McpTool.class);
for (Method method : methods) {
McpServerFeatures.SyncToolSpecification tool = createComponentFrom(toolClass, method);
server.addTool(tool);
Expand Down Expand Up @@ -63,10 +63,11 @@ public McpServerFeatures.SyncToolSpecification createComponentFrom(Class<?> claz
}

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

Set<Parameter> parameters = ReflectionHelper.getParametersAnnotatedWith(method, McpToolParam.class);
List<Parameter> parameters = ReflectionHelper.getParametersAnnotatedWith(method, McpToolParam.class);
for (Parameter parameter : parameters) {
McpToolParam toolParam = parameter.getAnnotation(McpToolParam.class);
final String parameterName = toolParam.name();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,34 @@
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static java.util.stream.Collectors.toSet;
import java.util.stream.Stream;

public final class ReflectionHelper {

public static Set<Method> getMethodsAnnotatedWith(Class<?> clazz, Class<? extends Annotation> annotation) {
/**
* has to use list as result to make order correct
* @param clazz
* @param annotation
* @return
*/
public static List<Method> getMethodsAnnotatedWith(Class<?> clazz, Class<? extends Annotation> annotation) {
Method[] methods = clazz.getMethods();
return Set.of(methods).stream().filter(m -> m.isAnnotationPresent(annotation)).collect(toSet());
return Stream.of(methods).filter(m -> m.isAnnotationPresent(annotation)).toList();
}

public static Set<Parameter> getParametersAnnotatedWith(Method method, Class<? extends Annotation> annotation) {
/**
* has to use list as result to make order correct
* @param method
* @param annotation
* @return
*/
public static List<Parameter> getParametersAnnotatedWith(Method method, Class<? extends Annotation> annotation) {
Parameter[] parameters = method.getParameters();
return Set.of(parameters).stream().filter(p -> p.isAnnotationPresent(annotation)).collect(toSet());
return Stream.of(parameters).filter(p -> p.isAnnotationPresent(annotation)).toList();
}

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