@@ -36,9 +36,10 @@ public static Object invokeMethod(Class<?> clazz, Method method) throws Exceptio
36
36
return method .invoke (object );
37
37
}
38
38
39
- public static Object invokeMethod (Class <?> clazz , Method method , Map <String , Object > parameters ) throws Exception {
39
+ public static Object invokeMethod (Class <?> clazz , Method method , List < McpSchema . PromptArgument > arguments , Map <String , Object > parameters ) throws Exception {
40
40
Object object = clazz .getDeclaredConstructor ().newInstance ();
41
- return method .invoke (object , parameters .values ().toArray ());
41
+ Map <String , Object > typedParameters = asTypedParameters (method , arguments , parameters );
42
+ return method .invoke (object , typedParameters .values ().toArray ());
42
43
}
43
44
44
45
public static Object invokeMethod (Class <?> clazz , Method method , McpSchema .JsonSchema schema , Map <String , Object > parameters ) throws Exception {
@@ -47,27 +48,55 @@ public static Object invokeMethod(Class<?> clazz, Method method, McpSchema.JsonS
47
48
return method .invoke (object , typedParameters .values ().toArray ());
48
49
}
49
50
51
+ private static Map <String , Object > asTypedParameters (Method method , List <McpSchema .PromptArgument > arguments , Map <String , Object > parameters ) {
52
+ Class <?>[] parameterTypes = method .getParameterTypes ();
53
+ Map <String , Object > typedParameters = new LinkedHashMap <>(parameters .size ());
54
+
55
+ for (int i = 0 , size = arguments .size (); i < size ; i ++) {
56
+ final String parameterName = arguments .get (i ).name ();
57
+ final Object parameterValue = parameters .get (parameterName );
58
+ // Fill in a default value when the parameter is not specified
59
+ // to ensure that the parameter type is correct when calling method.invoke()
60
+ Class <?> parameterType = parameterTypes [i ];
61
+ if (String .class == parameterType ) {
62
+ typedParameters .put (parameterName , parameterValue == null ? StringHelper .EMPTY : parameterValue .toString ());
63
+ } else if (int .class == parameterType || Integer .class == parameterType ) {
64
+ typedParameters .put (parameterName , parameterValue == null ? 0 : Integer .parseInt (parameterValue .toString ()));
65
+ } else if (long .class == parameterType || Long .class == parameterType ) {
66
+ typedParameters .put (parameterName , parameterValue == null ? 0 : Long .parseLong (parameterValue .toString ()));
67
+ } else if (float .class == parameterType || Float .class == parameterType ) {
68
+ typedParameters .put (parameterName , parameterValue == null ? 0.0 : Float .parseFloat (parameterValue .toString ()));
69
+ } else if (double .class == parameterType || Double .class == parameterType ) {
70
+ typedParameters .put (parameterName , parameterValue == null ? 0.0 : Double .parseDouble (parameterValue .toString ()));
71
+ } else if (boolean .class == parameterType || Boolean .class == parameterType ) {
72
+ typedParameters .put (parameterName , parameterValue != null && Boolean .parseBoolean (parameterValue .toString ()));
73
+ } else {
74
+ typedParameters .put (parameterName , parameterValue );
75
+ }
76
+ }
77
+
78
+ return typedParameters ;
79
+ }
80
+
50
81
@ SuppressWarnings ("unchecked" )
51
82
private static Map <String , Object > asTypedParameters (McpSchema .JsonSchema schema , Map <String , Object > parameters ) {
52
83
Map <String , Object > properties = schema .properties ();
53
84
Map <String , Object > typedParameters = new LinkedHashMap <>(properties .size ());
54
85
55
86
properties .forEach ((parameterName , parameterProperties ) -> {
56
87
Object parameterValue = parameters .get (parameterName );
57
- if (parameterValue == null ) {
58
- Map <String , Object > map = (Map <String , Object >) parameterProperties ;
59
- final String jsonSchemaType = map .getOrDefault ("type" , StringHelper .EMPTY ).toString ();
60
- if (jsonSchemaType .isEmpty ()) {
61
- typedParameters .put (parameterName , null );
62
- } else if (isTypeOf (String .class , jsonSchemaType )) {
63
- typedParameters .put (parameterName , StringHelper .EMPTY );
64
- } else if (isTypeOf (Integer .class , jsonSchemaType )) {
65
- typedParameters .put (parameterName , 0 );
66
- } else if (isTypeOf (Number .class , jsonSchemaType )) {
67
- typedParameters .put (parameterName , 0.0 );
68
- } else if (isTypeOf (Boolean .class , jsonSchemaType )) {
69
- typedParameters .put (parameterName , false );
70
- }
88
+ // Fill in a default value when the parameter is not specified
89
+ // to ensure that the parameter type is correct when calling method.invoke()
90
+ Map <String , Object > map = (Map <String , Object >) parameterProperties ;
91
+ final String jsonSchemaType = map .getOrDefault ("type" , StringHelper .EMPTY ).toString ();
92
+ if (String .class .getSimpleName ().equalsIgnoreCase (jsonSchemaType )) {
93
+ typedParameters .put (parameterName , parameterValue == null ? StringHelper .EMPTY : parameterValue .toString ());
94
+ } else if (Integer .class .getSimpleName ().equalsIgnoreCase (jsonSchemaType )) {
95
+ typedParameters .put (parameterName , parameterValue == null ? 0 : Integer .parseInt (parameterValue .toString ()));
96
+ } else if (Number .class .getSimpleName ().equalsIgnoreCase (jsonSchemaType )) {
97
+ typedParameters .put (parameterName , parameterValue == null ? 0.0 : Double .parseDouble (parameterValue .toString ()));
98
+ } else if (Boolean .class .getSimpleName ().equalsIgnoreCase (jsonSchemaType )) {
99
+ typedParameters .put (parameterName , parameterValue != null && Boolean .parseBoolean (parameterValue .toString ()));
71
100
} else {
72
101
typedParameters .put (parameterName , parameterValue );
73
102
}
@@ -76,8 +105,4 @@ private static Map<String, Object> asTypedParameters(McpSchema.JsonSchema schema
76
105
return typedParameters ;
77
106
}
78
107
79
- private static boolean isTypeOf (Class <?> clazz , String jsonSchemaType ) {
80
- return clazz .getName ().equalsIgnoreCase (jsonSchemaType ) || clazz .getSimpleName ().equalsIgnoreCase (jsonSchemaType );
81
- }
82
-
83
108
}
0 commit comments