Skip to content

Commit 552f17d

Browse files
committed
add sm mcp tool
1 parent 114417e commit 552f17d

File tree

1 file changed

+56
-5
lines changed

1 file changed

+56
-5
lines changed

core/src/main/java/com/taobao/arthas/core/mcp/tool/function/klass100/SearchMethodTool.java

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,69 @@
55
import com.taobao.arthas.mcp.server.tool.annotation.Tool;
66
import com.taobao.arthas.mcp.server.tool.annotation.ToolParam;
77

8+
/**
9+
* 方法搜索工具,对应 Arthas 的 sm 命令
10+
* 用于搜索 JVM 中已加载类的方法,支持通配符和正则表达式匹配
11+
*/
812
public class SearchMethodTool extends AbstractArthasTool {
913

1014
@Tool(
11-
name = "getClassMethods",
12-
description = "获取指定类的所有方法信息"
15+
name = "sm",
16+
description = "搜索 JVM 中已加载类的方法。支持通配符(*)和正则表达式匹配,可查看方法的详细信息(返回类型、参数类型、异常类型、注解等)"
1317
)
1418
public String sm(
15-
@ToolParam(description = "要查询的类名,支持全限定名") String className,
19+
@ToolParam(description = "类名模式,支持全限定名。可使用通配符如 *StringUtils 或 org.apache.commons.lang.*,类名分隔符支持 '.' 或 '/'")
20+
String classPattern,
21+
22+
@ToolParam(description = "方法名模式。可使用通配符如 get* 或 *Name。不指定时匹配所有方法", required = false)
23+
String methodPattern,
24+
25+
@ToolParam(description = "是否显示方法的详细信息,包括返回类型、参数类型、异常类型、注解、类加载器等。默认为 true", required = false)
26+
Boolean detail,
27+
28+
@ToolParam(description = "是否使用正则表达式匹配类名和方法名。默认为 false(使用通配符匹配)", required = false)
29+
Boolean regex,
30+
31+
@ToolParam(description = "指定 ClassLoader 的 hashcode(16进制),用于在多个 ClassLoader 加载同名类时精确定位", required = false)
32+
String classLoaderHash,
33+
34+
@ToolParam(description = "指定 ClassLoader 的完整类名,如 sun.misc.Launcher$AppClassLoader,可替代 hashcode", required = false)
35+
String classLoaderClass,
36+
37+
@ToolParam(description = "最大匹配类数量限制。默认为 100,防止返回过多结果", required = false)
38+
Integer limit,
39+
1640
ToolContext toolContext) {
41+
1742
StringBuilder cmd = buildCommand("sm");
18-
addParameter(cmd, className);
43+
44+
// 默认显示详细信息
45+
boolean showDetail = (detail == null || detail);
46+
addFlag(cmd, "-d", showDetail);
47+
48+
// 使用正则表达式匹配
49+
addFlag(cmd, "-E", regex);
50+
51+
// 指定类加载器(两种方式,优先使用 hashcode)
52+
if (classLoaderHash != null && !classLoaderHash.trim().isEmpty()) {
53+
addParameter(cmd, "-c", classLoaderHash);
54+
} else if (classLoaderClass != null && !classLoaderClass.trim().isEmpty()) {
55+
addParameter(cmd, "--classLoaderClass", classLoaderClass);
56+
}
57+
58+
// 最大匹配类数量限制
59+
if (limit != null && limit > 0) {
60+
addParameter(cmd, "-n", String.valueOf(limit));
61+
}
62+
63+
// 类名模式(必需参数)
64+
addParameter(cmd, classPattern);
65+
66+
// 方法名模式(可选参数)
67+
if (methodPattern != null && !methodPattern.trim().isEmpty()) {
68+
addParameter(cmd, methodPattern);
69+
}
70+
1971
return executeSync(toolContext, cmd.toString());
2072
}
21-
2273
}

0 commit comments

Comments
 (0)