File tree Expand file tree Collapse file tree 3 files changed +55
-3
lines changed
backend/src/main/java/ch/xxx/aidoclibchat Expand file tree Collapse file tree 3 files changed +55
-3
lines changed Original file line number Diff line number Diff line change 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 ) { }
Original file line number Diff line number Diff line change 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 ) { }
Original file line number Diff line number Diff line change 1313package ch .xxx .aidoclibchat .usecase .service ;
1414
1515import 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 ;
1724import io .modelcontextprotocol .client .McpSyncClient ;
1825
26+ @ Service
1927public 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}
You can’t perform that action at this time.
0 commit comments