feat(mcp): tools/list按token作用域过滤(servlet层重写响应); ToolGuard.allowedToolNames批量查询; AkAuthFilter定序@Order(10)
This commit is contained in:
+88
@@ -0,0 +1,88 @@
|
||||
package me.iiv.agent.mcp;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class ToolsListResponseRewriterTest {
|
||||
|
||||
private static final ObjectMapper MAPPER = new ObjectMapper();
|
||||
|
||||
private static String sseEvent(String id, String json) {
|
||||
return "id:" + id + "\nevent:message\ndata:" + json + "\n\n";
|
||||
}
|
||||
|
||||
private static String toolsJson(String... names) {
|
||||
StringBuilder tools = new StringBuilder();
|
||||
for (String name : names) {
|
||||
if (tools.length() > 0) tools.append(',');
|
||||
tools.append("{\"name\":\"").append(name).append("\",\"description\":\"d\"}");
|
||||
}
|
||||
return "{\"jsonrpc\":\"2.0\",\"id\":2,\"result\":{\"tools\":[" + tools + "]}}";
|
||||
}
|
||||
|
||||
private static List<String> toolNames(String body) throws Exception {
|
||||
String data = null;
|
||||
for (String line : body.split("\\n")) {
|
||||
if (line.startsWith("data:")) {
|
||||
data = line.substring(5);
|
||||
}
|
||||
}
|
||||
JsonNode node = MAPPER.readTree(data == null ? body : data);
|
||||
List<String> names = new ArrayList<>();
|
||||
node.path("result").path("tools").forEach(tool -> names.add(tool.path("name").asText()));
|
||||
return names;
|
||||
}
|
||||
|
||||
@Test
|
||||
void filtersToolsInSseEvent() throws Exception {
|
||||
String body = sseEvent("evt-1", toolsJson("agent.list.query", "property.list.query", "sys.ping"));
|
||||
|
||||
String rewritten = ToolsListResponseRewriter.rewrite(body, Set.of("agent.list.query", "sys.ping"));
|
||||
|
||||
assertEquals(List.of("agent.list.query", "sys.ping"), toolNames(rewritten));
|
||||
assertTrue(rewritten.startsWith("id:evt-1\nevent:message\n"), "SSE 元信息行需保留");
|
||||
assertTrue(rewritten.endsWith("\n\n"), "事件末尾空行需保留");
|
||||
}
|
||||
|
||||
@Test
|
||||
void emptyAllowanceYieldsEmptyToolList() throws Exception {
|
||||
String body = sseEvent("evt-1", toolsJson("agent.list.query"));
|
||||
|
||||
String rewritten = ToolsListResponseRewriter.rewrite(body, Set.of());
|
||||
|
||||
assertEquals(List.of(), toolNames(rewritten));
|
||||
}
|
||||
|
||||
@Test
|
||||
void nonToolsEventsPassThroughUnchanged() {
|
||||
String initEvent = sseEvent("evt-0", "{\"jsonrpc\":\"2.0\",\"id\":1,\"result\":{\"protocolVersion\":\"2025-03-26\"}}");
|
||||
String body = initEvent + sseEvent("evt-1", toolsJson("a", "b"));
|
||||
|
||||
String rewritten = ToolsListResponseRewriter.rewrite(body, Set.of("a"));
|
||||
|
||||
assertTrue(rewritten.startsWith(initEvent), "非 tools/list 事件原样保留");
|
||||
}
|
||||
|
||||
@Test
|
||||
void plainJsonResponseIsAlsoFiltered() throws Exception {
|
||||
String body = toolsJson("a", "b");
|
||||
|
||||
String rewritten = ToolsListResponseRewriter.rewrite(body, Set.of("b"));
|
||||
|
||||
assertEquals(List.of("b"), toolNames(rewritten));
|
||||
}
|
||||
|
||||
@Test
|
||||
void unparseableBodyPassesThrough() {
|
||||
String garbage = "not-json-not-sse";
|
||||
assertEquals(garbage, ToolsListResponseRewriter.rewrite(garbage, Set.of("a")));
|
||||
}
|
||||
}
|
||||
@@ -14,7 +14,9 @@ import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
@@ -26,6 +28,15 @@ import static org.mockito.Mockito.when;
|
||||
@SuppressWarnings("unchecked")
|
||||
class ToolGuardTest {
|
||||
|
||||
@org.junit.jupiter.api.BeforeAll
|
||||
static void initTableInfo() {
|
||||
// allowedToolNames 的 .select(Tool::getName) 在构建 wrapper 时即解析 lambda 列,需 TableInfo
|
||||
com.baomidou.mybatisplus.core.metadata.TableInfoHelper.initTableInfo(
|
||||
new org.apache.ibatis.builder.MapperBuilderAssistant(
|
||||
new com.baomidou.mybatisplus.core.MybatisConfiguration(), ""),
|
||||
me.iiv.agent.entity.svc.Tool.class);
|
||||
}
|
||||
|
||||
@Mock
|
||||
TokenScopeMapper tokenScopeMapper;
|
||||
@Mock
|
||||
@@ -59,7 +70,7 @@ class ToolGuardTest {
|
||||
when(tokenScopeMapper.selectList(any(LambdaQueryWrapper.class))).thenReturn(List.of(tokenScope(3L)));
|
||||
when(agentScopeMapper.selectList(any(LambdaQueryWrapper.class))).thenReturn(List.of(scope(3L)));
|
||||
when(scopeToolMapper.selectList(any(LambdaQueryWrapper.class))).thenReturn(List.of(scopeTool(9L)));
|
||||
when(toolMapper.selectCount(any(LambdaQueryWrapper.class))).thenReturn(1L);
|
||||
when(toolMapper.selectList(any(LambdaQueryWrapper.class))).thenReturn(List.of(tool(9L, "agent.list.query")));
|
||||
ToolGuard guard = guard();
|
||||
|
||||
assertTrue(guard.isAllowed(7L, "agent.list.query"));
|
||||
@@ -70,12 +81,24 @@ class ToolGuardTest {
|
||||
when(tokenScopeMapper.selectList(any(LambdaQueryWrapper.class))).thenReturn(List.of(tokenScope(3L)));
|
||||
when(agentScopeMapper.selectList(any(LambdaQueryWrapper.class))).thenReturn(List.of(scope(3L)));
|
||||
when(scopeToolMapper.selectList(any(LambdaQueryWrapper.class))).thenReturn(List.of(scopeTool(9L)));
|
||||
when(toolMapper.selectCount(any(LambdaQueryWrapper.class))).thenReturn(0L);
|
||||
when(toolMapper.selectList(any(LambdaQueryWrapper.class))).thenReturn(List.of());
|
||||
ToolGuard guard = guard();
|
||||
|
||||
assertFalse(guard.isAllowed(7L, "agent.list.query"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void allowedToolNamesReturnsEnabledNamesOnly() {
|
||||
when(tokenScopeMapper.selectList(any(LambdaQueryWrapper.class))).thenReturn(List.of(tokenScope(3L)));
|
||||
when(agentScopeMapper.selectList(any(LambdaQueryWrapper.class))).thenReturn(List.of(scope(3L)));
|
||||
when(scopeToolMapper.selectList(any(LambdaQueryWrapper.class))).thenReturn(List.of(scopeTool(9L)));
|
||||
when(toolMapper.selectList(any(LambdaQueryWrapper.class)))
|
||||
.thenReturn(List.of(tool(9L, "agent.list.query"), tool(10L, "sys.ping")));
|
||||
ToolGuard guard = guard();
|
||||
|
||||
assertEquals(Set.of("agent.list.query", "sys.ping"), guard.allowedToolNames(7L));
|
||||
}
|
||||
|
||||
private ToolGuard guard() {
|
||||
return new ToolGuard(tokenScopeMapper, scopeToolMapper, toolMapper, agentScopeMapper);
|
||||
}
|
||||
@@ -97,4 +120,11 @@ class ToolGuardTest {
|
||||
scopeTool.setToolId(toolId);
|
||||
return scopeTool;
|
||||
}
|
||||
|
||||
private static me.iiv.agent.entity.svc.Tool tool(Long id, String name) {
|
||||
me.iiv.agent.entity.svc.Tool tool = new me.iiv.agent.entity.svc.Tool();
|
||||
tool.setId(id);
|
||||
tool.setName(name);
|
||||
return tool;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user