Skip to content

McpToolset Do I have to pass the Optional<Object>toolFilter parameter? #712

@fuzhengwei

Description

@fuzhengwei

problem

McpToolset Do I have to pass the OptionaltoolFilter parameter?

An error occurs when execution is not passed in?

In addition, I believe that the tool design of LlmAgent should be compatible with the MCP configuration under io.modelcontextprotocol, and can be directly used by McpSyncClient

case

public class ToolsTest {

    public static void main(String[] args) {

        SseServerParameters sseServerParameters = SseServerParameters.builder()
                .url("http://appbuilder.baidu.com/v2/ai_search/mcp/")
                .sseEndpoint("sse?api_key=bce-v3/ALTAK-3zODLb9qHozIftQlGwez5/2696e92781f5bf1ba1870****")
                .build();

        StdioServerParameters stdioServerParameters = StdioServerParameters.builder()
                .command("npx")
                .args(List.of(
                        "-y",
                        "modelcontextprotocol/server-filesystem@2025.3.28",
                        "/Users/fuzhengwei/coding/gitcode/KnowledgePlanet/ai-agent-scaffold/ai-agent-scaffold"
                ))
                .env(new HashMap<>())
                .build();

        ServerParameters npx = ServerParameters.builder("npx")
                .args(List.of(
                        "-y",
                        "modelcontextprotocol/server-filesystem@2025.3.28",
                        "/Users/fuzhengwei/coding/gitcode/KnowledgePlanet/ai-agent-scaffold/ai-agent-scaffold"
                ))
                .env(new HashMap<>()).build();

        ArrayList<McpToolset> tools = new ArrayList<>() {{
            add(new McpToolset(sseServerParameters, Optional.of(Arrays.asList("ai_search"))));
            add(new McpToolset(stdioServerParameters.toServerParameters(), Optional.of(Arrays.asList(
                    "read_text_file",
                    "read_media_file",
                    "read_multiple_files",
                    "write_file",
                    "edit_file",
                    "create_directory",
                    "list_directory",
                    "list_directory_with_sizes",
                    "move_file",
                    "search_files",
                    "directory_tree",
                    "get_file_info",
                    "list_allowed_directories"))));
        }};

        OpenAiChatModel openAiChatModel = OpenAiChatModel.builder()
                .baseUrl("https://apis.****/v1/")
                .apiKey("sk-QidtEz0VSkcngFFc1*****")
                .modelName("gpt-4o")
                .build();

        LlmAgent agent = LlmAgent.builder()
                .name("test")
                .description("ai agent")
                .model(new LangChain4j(openAiChatModel))
//                .instruction("""
//                        你是一个编程架构师,具备网络检索和分析能力。
//                        """)
//                .tools(tools)
//                .tools(new McpToolset(sseServerParameters, Optional.of(Arrays.asList("ai_search"))))
                .tools(new McpToolset(stdioServerParameters.toServerParameters(), Optional.of(Arrays.asList(
                        "read_text_file",
                        "read_media_file",
                        "read_multiple_files",
                        "write_file",
                        "edit_file",
                        "create_directory",
                        "list_directory",
                        "list_directory_with_sizes",
                        "move_file",
                        "search_files",
                        "directory_tree",
                        "get_file_info",
                        "list_allowed_directories"))))
//                .tools(new McpToolset(stdioServerParameters.toServerParameters()))
                .build();

        InMemoryRunner runner = new InMemoryRunner(agent);

        Session session = runner
                .sessionService()
                .createSession("test", "xiaofuge")
                .blockingGet();

        Flowable<Event> eventsOne = runner.runAsync("xiaofuge", session.id(), Content.fromParts(Part.fromText("你有哪些工具能力")));

        System.out.print("\nAgent > ");
        eventsOne.blockingForEach(event -> System.out.println(event.stringifyContent()));

        try (Scanner scanner = new Scanner(System.in, StandardCharsets.UTF_8)) {
            while (true) {
                System.out.print("\nYou > ");
                String userInput = scanner.nextLine();

                if ("quit".equalsIgnoreCase(userInput)) {
                    break;
                }

                Content userMsg = Content.fromParts(Part.fromText(userInput));
                Flowable<Event> events = runner.runAsync("xiaofuge", session.id(), userMsg);

                System.out.print("\nAgent > ");
                events.blockingForEach(event -> System.out.println(event.stringifyContent()));
            }
        }

    }

}

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions