Skip to content

Commit e19b859

Browse files
committed
feat: add mcp client
1 parent 64fd88d commit e19b859

File tree

3 files changed

+55
-3
lines changed

3 files changed

+55
-3
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* Copyright 2023 Sven Loesekann
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
package ch.xxx.aidoclibchat.domain.model.dto;
14+
15+
public record McpRequestDto(String question) { }
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Copyright 2023 Sven Loesekann
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
package ch.xxx.aidoclibchat.domain.model.dto;
14+
15+
import java.util.List;
16+
17+
import org.springframework.ai.chat.messages.AssistantMessage.ToolCall;
18+
19+
public record McpResponseDto(String answer, List<ToolCall> toolCalls) { }

backend/src/main/java/ch/xxx/aidoclibchat/usecase/service/LocalMcpClient.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,31 @@
1313
package ch.xxx.aidoclibchat.usecase.service;
1414

1515
import java.util.List;
16+
import java.util.Optional;
1617

18+
import org.springframework.ai.chat.client.ChatClient;
19+
import org.springframework.ai.mcp.SyncMcpToolCallbackProvider;
20+
import org.springframework.stereotype.Service;
21+
22+
import ch.xxx.aidoclibchat.domain.model.dto.McpRequestDto;
23+
import ch.xxx.aidoclibchat.domain.model.dto.McpResponseDto;
1724
import io.modelcontextprotocol.client.McpSyncClient;
1825

26+
@Service
1927
public class LocalMcpClient {
20-
private final List<McpSyncClient> mcpSyncClients;
28+
private final List<McpSyncClient> mcpSyncClients;
29+
private final ChatClient chatClient;
2130

22-
public LocalMcpClient(List<McpSyncClient> mcpSyncClients) {
23-
this.mcpSyncClients = mcpSyncClients;
31+
public LocalMcpClient(List<McpSyncClient> mcpSyncClients, ChatClient.Builder chatClientBuilder) {
32+
this.mcpSyncClients = mcpSyncClients;
33+
this.chatClient = chatClientBuilder.build();
2434
}
35+
36+
public McpResponseDto createResponse(McpRequestDto requestDto) {
37+
var result = this.chatClient.prompt(requestDto.question()).toolCallbacks(new SyncMcpToolCallbackProvider(mcpSyncClients)).call();
38+
var resultText = Optional.ofNullable(result.chatResponse()).stream().map(value -> value.getResult().getOutput().getText()).findFirst().orElse("");
39+
var toolCalls = Optional.ofNullable(result.chatResponse()).stream().map(value -> value.getResult().getOutput().getToolCalls()).findFirst().orElse(List.of());
40+
var responseDto = new McpResponseDto(resultText, toolCalls);
41+
return responseDto;
42+
}
2543
}

0 commit comments

Comments
 (0)