Skip to content

Commit 31c380b

Browse files
committed
mcp tools support userid header part2
1 parent 67795b4 commit 31c380b

File tree

3 files changed

+17
-92
lines changed

3 files changed

+17
-92
lines changed

core/src/main/java/com/taobao/arthas/core/command/CommandExecutorImpl.java

Lines changed: 8 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,11 @@ public Session getCurrentSession(String sessionId, boolean oneTimeIsAllowed) {
8282
* @param timeout 超时时间
8383
* @param sessionId session ID,如果为null则创建临时session
8484
* @param authSubject 认证主体,如果不为null则应用到session
85+
* @param userId 用户 ID,用于统计上报
8586
* @return 执行结果
8687
*/
8788
@Override
88-
public Map<String, Object> executeSync(String commandLine, long timeout, String sessionId, Object authSubject) {
89+
public Map<String, Object> executeSync(String commandLine, long timeout, String sessionId, Object authSubject, String userId) {
8990
Session session = null;
9091
boolean oneTimeAccess = false;
9192

@@ -98,6 +99,12 @@ public Map<String, Object> executeSync(String commandLine, long timeout, String
9899
session.getSessionId(), authSubject.getClass().getSimpleName());
99100
}
100101

102+
// 设置 userId 到 session,用于统计上报
103+
if (userId != null && !userId.trim().isEmpty()) {
104+
session.setUserId(userId);
105+
logger.debug("Set userId to session: {} (userId: {})", session.getSessionId(), userId);
106+
}
107+
101108
if (session.get(ONETIME_SESSION_KEY) != null) {
102109
oneTimeAccess = true;
103110
}
@@ -349,84 +356,6 @@ public void setSessionUserId(String sessionId, String userId) {
349356
}
350357
}
351358

352-
/**
353-
* 同步执行命令,支持指定 userId
354-
*/
355-
@Override
356-
public Map<String, Object> executeSync(String commandLine, long timeout, String sessionId, Object authSubject, String userId) {
357-
Session session = null;
358-
boolean oneTimeAccess = false;
359-
360-
try {
361-
session = getCurrentSession(sessionId, true);
362-
363-
if (authSubject != null) {
364-
session.put(SUBJECT_KEY, authSubject);
365-
logger.debug("Applied auth subject to session: {} (authSubject: {})",
366-
session.getSessionId(), authSubject.getClass().getSimpleName());
367-
}
368-
369-
// 设置 userId 到 session
370-
if (userId != null && !userId.trim().isEmpty()) {
371-
session.setUserId(userId);
372-
logger.debug("Set userId to session: {} (userId: {})", session.getSessionId(), userId);
373-
}
374-
375-
if (session.get(ONETIME_SESSION_KEY) != null) {
376-
oneTimeAccess = true;
377-
}
378-
379-
PackingResultDistributorImpl resultDistributor = new PackingResultDistributorImpl(session);
380-
Job job = this.createJob(commandLine, session, resultDistributor);
381-
382-
if (job == null) {
383-
logger.error("Failed to create job for command: {}", commandLine);
384-
return createErrorResult(commandLine, "Failed to create job");
385-
}
386-
387-
job.run();
388-
boolean finished = waitForJob(job, (int) timeout);
389-
if (!finished) {
390-
logger.warn("Command timeout after {} ms: {}", timeout, commandLine);
391-
job.interrupt();
392-
return createTimeoutResult(commandLine, timeout);
393-
}
394-
395-
Map<String, Object> result = new TreeMap<>();
396-
result.put("command", commandLine);
397-
result.put("success", true);
398-
result.put("sessionId", session.getSessionId());
399-
result.put("executionTime", System.currentTimeMillis());
400-
401-
List<ResultModel> results = resultDistributor.getResults();
402-
if (results != null && !results.isEmpty()) {
403-
result.put("results", results);
404-
result.put("resultCount", results.size());
405-
} else {
406-
result.put("results", results);
407-
result.put("resultCount", 0);
408-
}
409-
410-
return result;
411-
412-
} catch (SessionNotFoundException e) {
413-
logger.error("Session error for command: {}", commandLine, e);
414-
return createErrorResult(commandLine, e.getMessage());
415-
} catch (Exception e) {
416-
logger.error("Error executing command: {}", commandLine, e);
417-
return createErrorResult(commandLine, "Error executing command: " + e.getMessage());
418-
} finally {
419-
if (oneTimeAccess && session != null) {
420-
try {
421-
sessionManager.removeSession(session.getSessionId());
422-
logger.debug("Destroyed one-time session {}", session.getSessionId());
423-
} catch (Exception e) {
424-
logger.warn("Error removing one-time session", e);
425-
}
426-
}
427-
}
428-
}
429-
430359
private boolean waitForJob(Job job, int timeout) {
431360
long startTime = System.currentTimeMillis();
432361
while (true) {

labs/arthas-mcp-server/src/main/java/com/taobao/arthas/mcp/server/CommandExecutor.java

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,40 +10,36 @@
1010
public interface CommandExecutor {
1111

1212
default Map<String, Object> executeSync(String commandLine, long timeout) {
13-
return executeSync(commandLine, timeout, null, null);
13+
return executeSync(commandLine, timeout, null, null, null);
1414
}
1515

1616
default Map<String, Object> executeSync(String commandLine, Object authSubject) {
17-
return executeSync(commandLine, 30000L, null, authSubject);
17+
return executeSync(commandLine, 30000L, null, authSubject, null);
1818
}
1919

2020
/**
2121
* 同步执行命令,支持指定 userId
2222
*
2323
* @param commandLine 命令行
2424
* @param authSubject 认证主体
25-
* @param userId 用户 ID
25+
* @param userId 用户 ID,用于统计上报
2626
* @return 执行结果
2727
*/
2828
default Map<String, Object> executeSync(String commandLine, Object authSubject, String userId) {
2929
return executeSync(commandLine, 30000L, null, authSubject, userId);
3030
}
3131

32-
Map<String, Object> executeSync(String commandLine, long timeout, String sessionId, Object authSubject);
33-
3432
/**
35-
* 同步执行命令,支持指定 userId
33+
* 同步执行命令
3634
*
3735
* @param commandLine 命令行
3836
* @param timeout 超时时间
39-
* @param sessionId session ID
40-
* @param authSubject 认证主体
41-
* @param userId 用户 ID
37+
* @param sessionId session ID,如果为null则创建临时session
38+
* @param authSubject 认证主体,如果不为null则应用到session
39+
* @param userId 用户 ID,用于统计上报
4240
* @return 执行结果
4341
*/
44-
default Map<String, Object> executeSync(String commandLine, long timeout, String sessionId, Object authSubject, String userId) {
45-
return executeSync(commandLine, timeout, sessionId, authSubject);
46-
}
42+
Map<String, Object> executeSync(String commandLine, long timeout, String sessionId, Object authSubject, String userId);
4743

4844
Map<String, Object> executeAsync(String commandLine, String sessionId);
4945

labs/arthas-mcp-server/src/main/java/com/taobao/arthas/mcp/server/session/ArthasCommandContext.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public Map<String, Object> executeSync(String commandLine, long timeout) {
5252
}
5353

5454
public Map<String, Object> executeSync(String commandStr, Object authSubject) {
55-
return commandExecutor.executeSync(commandStr, DEFAULT_SYNC_TIMEOUT, null, authSubject);
55+
return commandExecutor.executeSync(commandStr, DEFAULT_SYNC_TIMEOUT, null, authSubject, null);
5656
}
5757

5858
/**

0 commit comments

Comments
 (0)