教学类网站怎么做台湾永久免费加密一
序
本文主要研究一下langchain4j对Model Context Protocol (MCP) 的支持
MCP
MCP协议规定了两种传输方式:
- HTTP:客户端请求一个SSE(Server-Sent Events)通道以从服务器接收事件,然后通过HTTP POST请求发送命令。这种方式适用于需要跨网络通信的场景,通常用于分布式系统或需要高并发的场景。
- stdio:客户端可以将MCP服务器作为本地子进程运行,并通过标准输入/输出直接与其通信。这种方式适用于本地集成和命令行工具,适合简单的本地批处理任务。
如果需要让ChatModel或AI service运行由MCP服务器提供的工具,则需要创建一个MCP tool provider
McpToolProvider
McpTransport
McpTransport transport = new StdioMcpTransport.Builder().command(List.of("/usr/bin/npm", "exec", "@modelcontextprotocol/server-everything@0.6.2")).logEvents(true) // only if you want to see the traffic in the log.build();
对于HTTP方式则如下:
McpTransport transport = new HttpMcpTransport.Builder().sseUrl("http://localhost:3001/sse").logRequests(true) // if you want to see the traffic in the log.logResponses(true).build();
McpClient
McpClient mcpClient = new DefaultMcpClient.Builder().transport(transport).logMessageHandler(new MyLogMessageHandler()).build();
基于McpTransport创建McpClient,可以指定logMessageHandler来打印相关信息,可以通过client.listResources()、client.listResourceTemplates()来返回MCP的resources
provider
ToolProvider toolProvider = McpToolProvider.builder().mcpClients(List.of(mcpClient)).build();
基于McpClient来创建McpToolProvider
AiService
Bot bot = AiServices.builder(Bot.class).chatLanguageModel(model).toolProvider(toolProvider).build();
AiServices.builder提供了方法来设置toolProvider
示例
public static void main(String[] args) throws Exception {ChatLanguageModel model = OpenAiChatModel.builder().apiKey(System.getenv("OPENAI_API_KEY")).modelName("gpt-4o-mini").logRequests(true).logResponses(true).build();McpTransport transport = new StdioMcpTransport.Builder().command(List.of("/usr/local/bin/docker", "run", "-e", "GITHUB_PERSONAL_ACCESS_TOKEN", "-i", "mcp/github")).logEvents(true).build();McpClient mcpClient = new DefaultMcpClient.Builder().transport(transport).build();ToolProvider toolProvider = McpToolProvider.builder().mcpClients(List.of(mcpClient)).build();Bot bot = AiServices.builder(Bot.class).chatLanguageModel(model).toolProvider(toolProvider).build();try {String response = bot.chat("Summarize the last 3 commits of the LangChain4j GitHub repository");System.out.println("RESPONSE: " + response);} finally {mcpClient.close();}
}
这里使用stdio的方式来连接docker部署的GitHub MCP server,通过chat告诉LLM去总结LangChain4j这个github仓库最近的3个commits
返回结果示例如下:
Here are the summaries of the last three commits in the LangChain4j GitHub repository:1. **Commit [36951f9](https://github.com/langchain4j/langchain4j/commit/36951f9649c1beacd8b9fc2d910a2e23223e0d93)** (Date: 2025-02-05)- **Author:** Dmytro Liubarskyi- **Message:** Updated to `upload-pages-artifact@v3`.- **Details:** This commit updates the GitHub Action used for uploading pages artifacts to version 3.2. **Commit [6fcd19f](https://github.com/langchain4j/langchain4j/commit/6fcd19f50c8393729a0878d6125b0bb1967ac055)** (Date: 2025-02-05)- **Author:** Dmytro Liubarskyi- **Message:** Updated to `checkout@v4`, `deploy-pages@v4`, and `upload-pages-artifact@v4`.- **Details:** This commit updates multiple GitHub Actions to their version 4.3. **Commit [2e74049](https://github.com/langchain4j/langchain4j/commit/2e740495d2aa0f16ef1c05cfcc76f91aef6f6599)** (Date: 2025-02-05)- **Author:** Dmytro Liubarskyi- **Message:** Updated to `setup-node@v4` and `configure-pages@v4`.- **Details:** This commit updates the `setup-node` and `configure-pages` GitHub Actions to version 4.All commits were made by the same author, Dmytro Liubarskyi, on the same day, focusing on updating various GitHub Actions to newer versions.
源码
ToolProvider
langchain4j/src/main/java/dev/langchain4j/service/tool/ToolProvider.java
@FunctionalInterface
public interface ToolProvider {/*** Provides tools for the request to the LLM.** @param request {@link ToolProviderRequest} contains {@link UserMessage} and chat memory id (see {@link MemoryId}).* @return {@link ToolProviderResult} contains tools that should be included in the request to the LLM.*/ToolProviderResult provideTools(ToolProviderRequest request);
}
langchain4j定义了ToolProvider接口,每次调用AI服务时,它都会被调用,并为该次调用提供相应的工具,其provideTools返回ToolProviderResult
McpToolProvider
public class McpToolProvider implements ToolProvider {private final List<McpClient> mcpClients;private final boolean failIfOneServerFails;private static final Logger log = LoggerFactory.getLogger(McpToolProvider.class);private McpToolProvider(Builder builder) {this.mcpClients = new ArrayList<>(builder.mcpClients);this.failIfOneServerFails = Utils.getOrDefault(builder.failIfOneServerFails, false);}@Overridepublic ToolProviderResult provideTools(final ToolProviderRequest request) {ToolProviderResult.Builder builder = ToolProviderResult.builder();for (McpClient mcpClient : mcpClients) {try {List<ToolSpecification> toolSpecifications = mcpClient.listTools();for (ToolSpecification toolSpecification : toolSpecifications) {builder.add(toolSpecification, (executionRequest, memoryId) -> mcpClient.executeTool(executionRequest));}} catch (Exception e) {if (failIfOneServerFails) {throw new RuntimeException("Failed to retrieve tools from MCP server", e);} else {log.warn("Failed to retrieve tools from MCP server", e);}}}return builder.build();}//......
}
McpToolProvider要求构造器传入McpToolProvider.Builder,provideTools会遍历mcpClients,之后遍历mcpClient.listTools(),构建每个tool对应的executor(
mcpClient.executeTool(executionRequest)
)
小结
langchain4j提供了langchain4j-mcp模块来支持MCP协议,它通过McpToolProvider来实现ToolProvider接口,以tool的方式来对接mcp。
doc
- Model Context Protocol (MCP)
- modelcontextprotocol.io