|
4 | 4 | import java.util.List; |
5 | 5 | import java.util.Set; |
6 | 6 | import java.util.function.Function; |
7 | | -import java.util.function.Supplier; |
8 | 7 |
|
9 | 8 | import dev.langchain4j.mcp.McpToolProvider; |
10 | 9 | import dev.langchain4j.mcp.client.DefaultMcpClient; |
|
24 | 23 | @Recorder |
25 | 24 | public class McpRecorder { |
26 | 25 |
|
27 | | - public Supplier<McpClient> mcpClientSupplier(String key, McpBuildTimeConfiguration buildTimeConfiguration, |
| 26 | + public Function<SyntheticCreationalContext<McpClient>, McpClient> mcpClientSupplier(String clientName, |
| 27 | + McpBuildTimeConfiguration buildTimeConfiguration, |
28 | 28 | McpRuntimeConfiguration mcpRuntimeConfiguration) { |
29 | | - return new Supplier<McpClient>() { |
| 29 | + return new Function<>() { |
30 | 30 | @Override |
31 | | - public McpClient get() { |
32 | | - McpTransport transport = null; |
33 | | - McpClientBuildTimeConfig buildTimeConfig = buildTimeConfiguration.clients().get(key); |
34 | | - McpClientRuntimeConfig runtimeConfig = mcpRuntimeConfiguration.clients().get(key); |
35 | | - switch (buildTimeConfig.transportType()) { |
36 | | - case STDIO: |
| 31 | + public McpClient apply(SyntheticCreationalContext<McpClient> context) { |
| 32 | + McpTransport transport; |
| 33 | + McpClientBuildTimeConfig buildTimeConfig = buildTimeConfiguration.clients().get(clientName); |
| 34 | + McpClientRuntimeConfig runtimeConfig = mcpRuntimeConfiguration.clients().get(clientName); |
| 35 | + transport = switch (buildTimeConfig.transportType()) { |
| 36 | + case STDIO -> { |
37 | 37 | List<String> command = runtimeConfig.command().orElseThrow(() -> new ConfigurationException( |
38 | | - "MCP client configuration named " + key + " is missing the 'command' property")); |
39 | | - transport = new StdioMcpTransport.Builder() |
| 38 | + "MCP client configuration named " + clientName + " is missing the 'command' property")); |
| 39 | + yield new StdioMcpTransport.Builder() |
40 | 40 | .command(command) |
41 | 41 | .logEvents(runtimeConfig.logResponses().orElse(false)) |
42 | 42 | .environment(runtimeConfig.environment()) |
43 | 43 | .build(); |
44 | | - break; |
45 | | - case HTTP: |
46 | | - transport = new QuarkusHttpMcpTransport.Builder() |
47 | | - .sseUrl(runtimeConfig.url().orElseThrow(() -> new ConfigurationException( |
48 | | - "MCP client configuration named " + key + " is missing the 'url' property"))) |
49 | | - .logRequests(runtimeConfig.logRequests().orElse(false)) |
50 | | - .logResponses(runtimeConfig.logResponses().orElse(false)) |
51 | | - .build(); |
52 | | - break; |
53 | | - default: |
54 | | - throw new IllegalArgumentException("Unknown transport type: " + buildTimeConfig.transportType()); |
55 | | - } |
56 | | - return new DefaultMcpClient.Builder() |
| 44 | + } |
| 45 | + case HTTP -> new QuarkusHttpMcpTransport.Builder() |
| 46 | + .sseUrl(runtimeConfig.url().orElseThrow(() -> new ConfigurationException( |
| 47 | + "MCP client configuration named " + clientName + " is missing the 'url' property"))) |
| 48 | + .logRequests(runtimeConfig.logRequests().orElse(false)) |
| 49 | + .logResponses(runtimeConfig.logResponses().orElse(false)) |
| 50 | + .build(); |
| 51 | + }; |
| 52 | + var result = new DefaultMcpClient.Builder() |
57 | 53 | .transport(transport) |
58 | 54 | .toolExecutionTimeout(runtimeConfig.toolExecutionTimeout()) |
59 | 55 | .resourcesTimeout(runtimeConfig.resourcesTimeout()) |
60 | 56 | // TODO: it should be possible to choose a log handler class via configuration |
61 | | - .logHandler(new QuarkusDefaultMcpLogHandler(key)) |
| 57 | + .logHandler(new QuarkusDefaultMcpLogHandler(clientName)) |
62 | 58 | .build(); |
| 59 | + return result; |
63 | 60 | } |
64 | 61 | }; |
65 | 62 | } |
|
0 commit comments