Skip to content

Commit ca20a25

Browse files
committed
feat: Added the annotations attribute to McpSchema.Tool
Signed-off-by: Sun Yuhan <sunyuhan1998@users.noreply.github.com>
1 parent 2f94434 commit ca20a25

File tree

2 files changed

+58
-4
lines changed

2 files changed

+58
-4
lines changed

mcp/src/main/java/io/modelcontextprotocol/spec/McpSchema.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,17 @@ public record JsonSchema( // @formatter:off
738738
@JsonProperty("definitions") Map<String, Object> definitions) {
739739
} // @formatter:on
740740

741+
@JsonInclude(JsonInclude.Include.NON_ABSENT)
742+
@JsonIgnoreProperties(ignoreUnknown = true)
743+
public record ToolAnnotations( // @formatter:off
744+
@JsonProperty("title") String title,
745+
@JsonProperty("readOnlyHint") Boolean readOnlyHint,
746+
@JsonProperty("destructiveHint") Boolean destructiveHint,
747+
@JsonProperty("idempotentHint") Boolean idempotentHint,
748+
@JsonProperty("openWorldHint") Boolean openWorldHint,
749+
@JsonProperty("returnDirect") Boolean returnDirect) {
750+
} // @formatter:on
751+
741752
/**
742753
* Represents a tool that the server provides. Tools enable servers to expose
743754
* executable functionality to the system. Through these tools, you can interact with
@@ -749,17 +760,23 @@ public record JsonSchema( // @formatter:off
749760
* used by clients to improve the LLM's understanding of available tools.
750761
* @param inputSchema A JSON Schema object that describes the expected structure of
751762
* the arguments when calling this tool. This allows clients to validate tool
763+
* @param annotations Additional properties describing a Tool to clients.
752764
* arguments before sending them to the server.
753765
*/
754766
@JsonInclude(JsonInclude.Include.NON_ABSENT)
755767
@JsonIgnoreProperties(ignoreUnknown = true)
756768
public record Tool( // @formatter:off
757-
@JsonProperty("name") String name,
758-
@JsonProperty("description") String description,
759-
@JsonProperty("inputSchema") JsonSchema inputSchema) {
769+
@JsonProperty("name") String name,
770+
@JsonProperty("description") String description,
771+
@JsonProperty("inputSchema") JsonSchema inputSchema,
772+
@JsonProperty("annotations") ToolAnnotations annotations) {
760773

761774
public Tool(String name, String description, String schema) {
762-
this(name, description, parseSchema(schema));
775+
this(name, description, parseSchema(schema), null);
776+
}
777+
778+
public Tool(String name, String description, String schema, ToolAnnotations annotations) {
779+
this(name, description, parseSchema(schema), annotations);
763780
}
764781

765782
} // @formatter:on

mcp/src/test/java/io/modelcontextprotocol/spec/McpSchemaTests.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,43 @@ void testToolWithComplexSchema() throws Exception {
606606
assertThat(deserializedTool.inputSchema().defs()).containsKey("Address");
607607
}
608608

609+
@Test
610+
void testToolWithAnnotations() throws Exception {
611+
String schemaJson = """
612+
{
613+
"type": "object",
614+
"properties": {
615+
"name": {
616+
"type": "string"
617+
},
618+
"value": {
619+
"type": "number"
620+
}
621+
},
622+
"required": ["name"]
623+
}
624+
""";
625+
McpSchema.ToolAnnotations annotations = new McpSchema.ToolAnnotations(
626+
"A test tool",
627+
false,
628+
false,
629+
false,
630+
false,
631+
false
632+
);
633+
634+
McpSchema.Tool tool = new McpSchema.Tool("test-tool", "A test tool", schemaJson, annotations);
635+
636+
String value = mapper.writeValueAsString(tool);
637+
assertThatJson(value).when(Option.IGNORING_ARRAY_ORDER)
638+
.when(Option.IGNORING_EXTRA_ARRAY_ITEMS)
639+
.isObject()
640+
.isEqualTo(
641+
json("""
642+
{"name":"test-tool","description":"A test tool","inputSchema":{"type":"object","properties":{"name":{"type":"string"},"value":{"type":"number"}},"required":["name"]},"annotations":{"title":"A test tool","readOnlyHint":false,"destructiveHint":false,"idempotentHint":false,"openWorldHint":false,"returnDirect":false}}"""));
643+
}
644+
645+
609646
@Test
610647
void testCallToolRequest() throws Exception {
611648
Map<String, Object> arguments = new HashMap<>();

0 commit comments

Comments
 (0)