66import io .modelcontextprotocol .spec .McpSchema .CallToolRequest ;
77import io .modelcontextprotocol .spec .McpSchema .CallToolResult ;
88import io .modelcontextprotocol .spec .McpSchema .ListToolsResult ;
9- import java .time .Duration ;
109import java .util .List ;
1110import java .util .Map ;
1211import lombok .RequiredArgsConstructor ;
1918public class BraveSearchMcpService {
2019
2120 private static final String BRAVE_WEB_TOOL = "brave_web_search" ;
22- private static final Duration INIT_TIMEOUT = Duration . ofSeconds ( 60 );
21+
2322 private final List <McpSyncClient > mcpClients ;
23+
2424 private final ObjectMapper objectMapper ;
2525
2626 public JsonNode search (String query , int count , int offset ) {
@@ -40,36 +40,23 @@ public JsonNode search(String query, int count, int offset) {
4040 var root = objectMapper .createObjectNode ();
4141 root .set ("results" , content );
4242 return root ;
43-
4443 }
45- }
4644
47- private void ensureInitialized (McpSyncClient client ) {
48- if (!client .isInitialized ()) {
49- synchronized (client ) { // 다중 스레드 초기화 경합 방지
50- if (!client .isInitialized ()) {
51- log .debug ("MCP 클라이언트 초기화 시작…" );
52- client .initialize (); // 매개변수 없는 버전
53- log .debug ("MCP 클라이언트 초기화 완료" );
54- }
55- }
56- }
45+ return content != null ? content : objectMapper .createObjectNode ();
5746 }
5847
5948 private McpSyncClient resolveBraveClient () {
6049 for (McpSyncClient client : mcpClients ) {
61- try {
62- ensureInitialized (client ); // 초기화
63- ListToolsResult tools = client .listTools ();
64- if (tools != null && tools .tools () != null &&
65- tools .tools ().stream ()
66- .anyMatch (t -> BRAVE_WEB_TOOL .equalsIgnoreCase (t .name ()))) {
50+ ListToolsResult tools = client .listTools ();
51+ if (tools != null && tools .tools () != null ) {
52+ boolean found = tools .tools ().stream ()
53+ .anyMatch (tool -> BRAVE_WEB_TOOL .equalsIgnoreCase (tool .name ()));
54+ if (found ) {
6755 return client ;
6856 }
69- } catch (Exception e ) {
70- log .debug ("Brave MCP 클라이언트 후보 실패: {}" , e .toString ());
7157 }
7258 }
73- throw new IllegalStateException ("Brave MCP 서버에서 '" + BRAVE_WEB_TOOL + "' 툴을 찾을 수 없습니다." );
59+
60+ throw new IllegalStateException ("Brave MCP 서버에서 brave_web_search 툴을 찾을 수 없습니다." );
7461 }
7562}
0 commit comments