Skip to content

Addition of ToolAnnotations annotation and repeatable #187

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 5 commits into from
May 20, 2025
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 @@ -22,5 +22,6 @@

String description() default "";

ToolAnnotation[] value();
ToolAnnotation[] annotations() default {};

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* (draft as of 5/18/2025) located <a href=
* "https://github.com/modelcontextprotocol/modelcontextprotocol/blob/main/schema/draft/schema.json#L2164">here</a>
*/
@Repeatable(Tool.class)
@Repeatable(ToolAnnotations.class)
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface ToolAnnotation {
Expand All @@ -24,6 +24,5 @@
boolean readOnlyHint() default false;

String title() default "";

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@
*
* SPDX-License-Identifier: EPL-2.0
*****************************************************************************/
package org.eclipse.ecf.ai.mcp.tools.service;
package org.eclipse.ecf.ai.mcp.tools.annotation;

import java.util.List;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

public interface ToolGroup {

List<ToolDescription> getToolDescriptions(String interfaceClassName);
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface ToolAnnotations {
ToolAnnotation[] value() default {};
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@

import java.util.List;

public class AbstractToolGroup implements ToolGroup {
import org.eclipse.ecf.ai.mcp.tools.util.ToolDescription;

@Override
public List<ToolDescription> getToolDescriptions(String interfaceClassName) {
public interface ToolGroupService {

default List<ToolDescription> getToolDescriptions(String interfaceClassName) {
return ToolDescription.fromService(this, interfaceClassName);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*
* SPDX-License-Identifier: EPL-2.0
*****************************************************************************/
package org.eclipse.ecf.ai.mcp.tools.service;
package org.eclipse.ecf.ai.mcp.tools.util;

import java.util.Arrays;
import java.util.Collections;
Expand All @@ -25,10 +25,13 @@ public record ToolAnnotationDescription(boolean destructiveHint, boolean idempot
boolean readOnlyHint, String title) {

public static List<ToolAnnotationDescription> fromAnnotations(ToolAnnotation[] annotations) {
return (annotations != null) ? Arrays.asList(annotations).stream().map(a -> {
return new ToolAnnotationDescription(a.destructiveHint(), a.idempotentHint(), a.openWorldHint(),
a.readOnlyHint(), a.title());
}).collect(Collectors.toList()) : Collections.emptyList();

if (annotations != null) {
return Arrays.asList(annotations).stream().map(a -> {
return new ToolAnnotationDescription(a.destructiveHint(), a.idempotentHint(), a.openWorldHint(),
a.readOnlyHint(), a.title());
}).collect(Collectors.toList());
} else {
return Collections.emptyList();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
*
* SPDX-License-Identifier: EPL-2.0
*****************************************************************************/
package org.eclipse.ecf.ai.mcp.tools.service;
package org.eclipse.ecf.ai.mcp.tools.util;

import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
Expand All @@ -17,19 +18,26 @@
import java.util.stream.Collectors;

import org.eclipse.ecf.ai.mcp.tools.annotation.Tool;
import org.eclipse.ecf.ai.mcp.tools.annotation.ToolAnnotations;

public record ToolDescription(String name, String description, List<ToolParamDescription> toolParamDescriptions,
ToolResultDescription resultDesc, List<ToolAnnotationDescription> annotations) {
ToolResultDescription resultDescriptions, List<ToolAnnotationDescription> toolAnnotationDescriptions) {

public static List<ToolDescription> fromClass(Class<?> clazz) {
return Arrays.asList(clazz.getMethods()).stream().map(m -> {
Tool ma = m.getAnnotation(Tool.class);
return (ma != null)
? new ToolDescription(m.getName(), ma.description(),
ToolParamDescription.fromParameters(m.getParameters()),
ToolResultDescription.fromMethod(m),
ToolAnnotationDescription.fromAnnotations(ma.value()))
: null;
// skip static methods
if (!Modifier.isStatic(m.getModifiers())) {
// Look for Tool annotation
Tool ma = m.getAnnotation(Tool.class);
if (ma != null) {
// Look for ToolAnnotations method annotation
ToolAnnotations tas = m.getAnnotation(ToolAnnotations.class);
return new ToolDescription(m.getName(), ma.description(),
ToolParamDescription.fromParameters(m.getParameters()), ToolResultDescription.fromMethod(m),
ToolAnnotationDescription.fromAnnotations(tas != null ? tas.value() : null));
}
}
return null;
}).filter(Objects::nonNull).collect(Collectors.toList());

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*
* SPDX-License-Identifier: EPL-2.0
*****************************************************************************/
package org.eclipse.ecf.ai.mcp.tools.service;
package org.eclipse.ecf.ai.mcp.tools.util;

import java.lang.reflect.Parameter;
import java.util.Arrays;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*
* SPDX-License-Identifier: EPL-2.0
*****************************************************************************/
package org.eclipse.ecf.ai.mcp.tools.service;
package org.eclipse.ecf.ai.mcp.tools.util;

import java.lang.reflect.Method;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package org.eclipse.ecf.ai.mcp.tools.util;