Skip to content

GH-3805: Add title attribute support to MCP @Tool annotation #3806

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 @@ -30,6 +30,7 @@
import io.modelcontextprotocol.server.McpSyncServerExchange;
import io.modelcontextprotocol.spec.McpSchema;
import io.modelcontextprotocol.spec.McpSchema.Role;
import org.springframework.ai.tool.definition.ToolDefinition;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Schedulers;

Expand Down Expand Up @@ -167,8 +168,9 @@ public static McpServerFeatures.SyncToolSpecification toSyncToolSpecification(To
public static McpServerFeatures.SyncToolSpecification toSyncToolSpecification(ToolCallback toolCallback,
MimeType mimeType) {

var tool = new McpSchema.Tool(toolCallback.getToolDefinition().name(),
toolCallback.getToolDefinition().description(), toolCallback.getToolDefinition().inputSchema());
ToolDefinition toolDefinition = toolCallback.getToolDefinition();
var tool = new McpSchema.Tool(toolDefinition.name(), toolDefinition.title(), toolDefinition.description(),
ModelOptionsUtils.jsonToObject(toolDefinition.inputSchema(), McpSchema.JsonSchema.class), null);

return new McpServerFeatures.SyncToolSpecification(tool, (exchange, request) -> {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@
*/
String name() default "";

/**
* The title of the tool. If not provided, the method name will be used.
*/
String title() default "";

/**
* The description of the tool. If not provided, the method name will be used.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package org.springframework.ai.tool.definition;

import org.springframework.ai.util.ParsingUtils;
import org.springframework.ai.tool.support.ToolUtils;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

Expand All @@ -26,10 +26,12 @@
* @author Thomas Vitale
* @since 1.0.0
*/
public record DefaultToolDefinition(String name, String description, String inputSchema) implements ToolDefinition {
public record DefaultToolDefinition(String name, String title, String description,
String inputSchema) implements ToolDefinition {

public DefaultToolDefinition {
Assert.hasText(name, "name cannot be null or empty");
Assert.hasText(title, "title cannot be null or empty");
Assert.hasText(description, "description cannot be null or empty");
Assert.hasText(inputSchema, "inputSchema cannot be null or empty");
}
Expand All @@ -42,6 +44,8 @@ public static final class Builder {

private String name;

private String title;

private String description;

private String inputSchema;
Expand All @@ -54,6 +58,11 @@ public Builder name(String name) {
return this;
}

public Builder title(String title) {
this.title = title;
return this;
}

public Builder description(String description) {
this.description = description;
return this;
Expand All @@ -65,11 +74,13 @@ public Builder inputSchema(String inputSchema) {
}

public ToolDefinition build() {
if (!StringUtils.hasText(this.title)) {
this.title = ToolUtils.getToolTitleFromName(this.name);
}
if (!StringUtils.hasText(this.description)) {
Assert.hasText(this.name, "toolName cannot be null or empty");
this.description = ParsingUtils.reConcatenateCamelCase(this.name, " ");
this.description = ToolUtils.getToolDescriptionFromName(this.name);
}
return new DefaultToolDefinition(this.name, this.description, this.inputSchema);
return new DefaultToolDefinition(this.name, this.title, this.description, this.inputSchema);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ public interface ToolDefinition {
*/
String name();

/**
* The human-readable title for the tool.
*/
String title();

/**
* The tool description, used by the AI model to determine what the tool does.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public static DefaultToolDefinition.Builder builder(Method method) {
Assert.notNull(method, "method cannot be null");
return DefaultToolDefinition.builder()
.name(ToolUtils.getToolName(method))
.title(ToolUtils.getToolTitle(method))
.description(ToolUtils.getToolDescription(method))
.inputSchema(JsonSchemaGenerator.generateForMethodInput(method));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,20 @@ public static String getToolName(Method method) {
return StringUtils.hasText(tool.name()) ? tool.name() : method.getName();
}

public static String getToolTitleFromName(String toolName) {
Assert.hasText(toolName, "toolName cannot be null or empty");
return ParsingUtils.reConcatenateCamelCase(toolName, " ");
}

public static String getToolTitle(Method method) {
Assert.notNull(method, "method cannot be null");
var tool = AnnotatedElementUtils.findMergedAnnotation(method, Tool.class);
if (tool == null) {
return ParsingUtils.reConcatenateCamelCase(method.getName(), " ");
}
return StringUtils.hasText(tool.title()) ? tool.title() : method.getName();
}

public static String getToolDescriptionFromName(String toolName) {
Assert.hasText(toolName, "toolName cannot be null or empty");
return ParsingUtils.reConcatenateCamelCase(toolName, " ");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ class DefaultToolExecutionExceptionProcessorTests {

private final IllegalStateException toolException = new IllegalStateException("Inner exception");

private final DefaultToolDefinition toolDefinition = new DefaultToolDefinition("toolName", "toolDescription",
"inputSchema");
private final DefaultToolDefinition toolDefinition = new DefaultToolDefinition("toolName", "toolTitle",
"toolDescription", "inputSchema");

private final ToolExecutionException toolExecutionException = new ToolExecutionException(toolDefinition,
toolException);
Expand Down
Loading