Skip to content

Commit c4e88e7

Browse files
committed
fix(#8): Mcp tools params type is double or Double will error
1 parent 2b95c3d commit c4e88e7

File tree

5 files changed

+264
-100
lines changed

5 files changed

+264
-100
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package com.github.codeboyzhou.mcp.declarative.enums;
2+
3+
/**
4+
* This enum is used to map Java types to JSON schema data types.
5+
*
6+
* @author codeboyzhou
7+
*/
8+
public enum JavaTypeToJsonSchemaMapper {
9+
10+
/** Java {@code String} is mapped to JSON schema data type {@code string}. */
11+
STRING(String.class, "string"),
12+
13+
/** Java {@code Number} is mapped to JSON schema data type {@code number}. */
14+
NUMBER(Number.class, "number"),
15+
16+
/** Java {@code long} is mapped to JSON schema data type {@code number}. */
17+
LONG(long.class, "number"),
18+
19+
/** Java {@code Long} is mapped to JSON schema data type {@code number}. */
20+
LONG_CLASS(Long.class, "number"),
21+
22+
/** Java {@code float} is mapped to JSON schema data type {@code number}. */
23+
FLOAT(float.class, "number"),
24+
25+
/** Java {@code Float} is mapped to JSON schema data type {@code number}. */
26+
FLOAT_CLASS(Float.class, "number"),
27+
28+
/** Java {@code double} is mapped to JSON schema data type {@code number}. */
29+
DOUBLE(double.class, "number"),
30+
31+
/** Java {@code Double} is mapped to JSON schema data type {@code number}. */
32+
DOUBLE_CLASS(Double.class, "number"),
33+
34+
/** Java {@code int} is mapped to JSON schema data type {@code integer}. */
35+
INT(int.class, "integer"),
36+
37+
/** Java {@code Integer} is mapped to JSON schema data type {@code integer}. */
38+
INTEGER(Integer.class, "integer"),
39+
40+
/** Java {@code boolean} is mapped to JSON schema data type {@code boolean}. */
41+
BOOLEAN(boolean.class, "boolean"),
42+
43+
/** Java {@code Boolean} is mapped to JSON schema data type {@code boolean}. */
44+
BOOLEAN_CLASS(Boolean.class, "boolean"),
45+
46+
/** Java {@code Object} is mapped to JSON schema data type {@code object}. */
47+
OBJECT(Object.class, "object"),
48+
;
49+
50+
/** The Java class that is mapped to the JSON schema data type. */
51+
private final Class<?> javaType;
52+
53+
/** The JSON schema data type. */
54+
private final String jsonSchemaType;
55+
56+
/**
57+
* Creates a new instance of {@code JsonSchemaDataType} with the specified Java class and JSON
58+
* schema data type.
59+
*
60+
* @param javaType the Java class that is mapped to the JSON schema data type
61+
* @param jsonSchemaType the JSON schema data type
62+
*/
63+
JavaTypeToJsonSchemaMapper(Class<?> javaType, String jsonSchemaType) {
64+
this.javaType = javaType;
65+
this.jsonSchemaType = jsonSchemaType;
66+
}
67+
68+
/**
69+
* Returns the JSON schema data type.
70+
*
71+
* @return the JSON schema data type
72+
*/
73+
public String getJsonSchemaType() {
74+
return jsonSchemaType;
75+
}
76+
77+
/**
78+
* Returns the JSON schema data type for the specified Java class. If the Java class is not
79+
* supported or mapped to a JSON schema data type, {@code "string"} will be returned.
80+
*
81+
* @param javaType the Java class
82+
* @return the JSON schema data type
83+
*/
84+
public static String getJsonSchemaType(Class<?> javaType) {
85+
JavaTypeToJsonSchemaMapper[] values = values();
86+
for (JavaTypeToJsonSchemaMapper mapper : values) {
87+
if (mapper.javaType.equals(javaType)) {
88+
return mapper.jsonSchemaType;
89+
}
90+
}
91+
return STRING.jsonSchemaType;
92+
}
93+
}

src/main/java/com/github/codeboyzhou/mcp/declarative/enums/JsonSchemaDataType.java

Lines changed: 0 additions & 69 deletions
This file was deleted.

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
@@ -4,7 +4,7 @@
44
import com.github.codeboyzhou.mcp.declarative.annotation.McpJsonSchemaDefinitionProperty;
55
import com.github.codeboyzhou.mcp.declarative.annotation.McpTool;
66
import com.github.codeboyzhou.mcp.declarative.annotation.McpToolParam;
7-
import com.github.codeboyzhou.mcp.declarative.enums.JsonSchemaDataType;
7+
import com.github.codeboyzhou.mcp.declarative.enums.JavaTypeToJsonSchemaMapper;
88
import com.github.codeboyzhou.mcp.declarative.reflect.InvocationResult;
99
import com.github.codeboyzhou.mcp.declarative.reflect.MethodCache;
1010
import com.github.codeboyzhou.mcp.declarative.server.converter.McpToolParameterConverter;
@@ -116,7 +116,7 @@ private McpSchema.JsonSchema createJsonSchema(Parameter[] methodParams) {
116116
Map<String, Object> definition = createJsonSchemaDefinition(definitionClass);
117117
definitions.put(definitionClassName, definition);
118118
} else {
119-
property.put("type", definitionClass.getSimpleName().toLowerCase());
119+
property.put("type", JavaTypeToJsonSchemaMapper.getJsonSchemaType(definitionClass));
120120
property.put("description", resolveComponentAttributeValue(toolParam.description()));
121121
}
122122
properties.put(parameterName, property);
@@ -129,7 +129,7 @@ private McpSchema.JsonSchema createJsonSchema(Parameter[] methodParams) {
129129

130130
final boolean hasAdditionalProperties = false;
131131
return new McpSchema.JsonSchema(
132-
JsonSchemaDataType.OBJECT.getType(),
132+
JavaTypeToJsonSchemaMapper.OBJECT.getJsonSchemaType(),
133133
properties,
134134
required,
135135
hasAdditionalProperties,
@@ -145,7 +145,7 @@ private McpSchema.JsonSchema createJsonSchema(Parameter[] methodParams) {
145145
*/
146146
private Map<String, Object> createJsonSchemaDefinition(Class<?> definitionClass) {
147147
Map<String, Object> definitionJsonSchema = new HashMap<>();
148-
definitionJsonSchema.put("type", JsonSchemaDataType.OBJECT.getType());
148+
definitionJsonSchema.put("type", JavaTypeToJsonSchemaMapper.OBJECT.getJsonSchemaType());
149149

150150
Map<String, Object> properties = new LinkedHashMap<>();
151151
List<String> required = new ArrayList<>();
@@ -164,7 +164,7 @@ private Map<String, Object> createJsonSchemaDefinition(Class<?> definitionClass)
164164
}
165165

166166
Map<String, Object> fieldProperties = new HashMap<>();
167-
fieldProperties.put("type", JsonSchemaDataType.fromJavaType(field.getType()).getType());
167+
fieldProperties.put("type", JavaTypeToJsonSchemaMapper.getJsonSchemaType(field.getType()));
168168
fieldProperties.put("description", resolveComponentAttributeValue(property.description()));
169169

170170
final String fieldName = StringHelper.defaultIfBlank(property.name(), field.getName());

0 commit comments

Comments
 (0)