|
| 1 | +package org.tinystruct.mcp.websearch; |
| 2 | + |
| 3 | +import org.tinystruct.ApplicationException; |
| 4 | +import org.tinystruct.data.component.Builder; |
| 5 | +import org.tinystruct.mcp.MCPClient; |
| 6 | +import org.tinystruct.mcp.MCPException; |
| 7 | +import org.tinystruct.mcp.MCPTool; |
| 8 | +import org.tinystruct.net.URLRequest; |
| 9 | +import org.tinystruct.net.handlers.HTTPHandler; |
| 10 | +import org.tinystruct.system.annotation.Action; |
| 11 | +import org.tinystruct.system.annotation.Argument; |
| 12 | + |
| 13 | +import java.net.MalformedURLException; |
| 14 | +import java.net.URI; |
| 15 | +import java.net.URL; |
| 16 | +import java.net.URLEncoder; |
| 17 | +import java.nio.charset.StandardCharsets; |
| 18 | +import java.util.logging.Level; |
| 19 | +import java.util.logging.Logger; |
| 20 | + |
| 21 | +/** |
| 22 | + * WebSearch tool using DuckDuckGo Instant Answer API (JSON) |
| 23 | + */ |
| 24 | +public class WebSearchTool extends MCPTool { |
| 25 | + private static final Logger LOGGER = Logger.getLogger(WebSearchTool.class.getName()); |
| 26 | + |
| 27 | + public WebSearchTool() { |
| 28 | + super("websearch", "Tool for performing web searches", null, null, true); |
| 29 | + } |
| 30 | + |
| 31 | + public WebSearchTool(MCPClient client) { |
| 32 | + super("websearch", "Tool for performing web searches", null, client, true); |
| 33 | + } |
| 34 | + |
| 35 | + @Action(value = "websearch/search", description = "Perform a web search using DuckDuckGo Instant Answer API", arguments = { |
| 36 | + @Argument(key = "query", description = "The search query", type = "string") |
| 37 | + }) |
| 38 | + public Builder search(String query) throws MCPException { |
| 39 | + try { |
| 40 | + return searchInternal(query); |
| 41 | + } catch (Exception e) { |
| 42 | + LOGGER.log(Level.SEVERE, "Error performing web search: " + e.getMessage(), e); |
| 43 | + throw new MCPException("Error performing web search: " + e.getMessage()); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + private Builder searchInternal(String query) throws Exception { |
| 48 | + String encodedQuery = URLEncoder.encode(query, StandardCharsets.UTF_8); |
| 49 | + String apiUrl = "https://api.duckduckgo.com/?q=" + encodedQuery + "&format=json"; |
| 50 | + |
| 51 | + URL url = URI.create(apiUrl).toURL(); |
| 52 | + URLRequest request = new URLRequest(url); |
| 53 | + request.setMethod("GET"); |
| 54 | + |
| 55 | + HTTPHandler handler = new HTTPHandler(); |
| 56 | + final StringBuilder content = new StringBuilder(); |
| 57 | + |
| 58 | + try { |
| 59 | + handler.handleRequest(request, chunk -> { |
| 60 | + content.append(chunk); |
| 61 | + }); |
| 62 | + } catch (ApplicationException e) { |
| 63 | + throw new MCPException("HTTP request failed: " + e.getMessage()); |
| 64 | + } |
| 65 | + |
| 66 | + Builder json = new Builder(); |
| 67 | + json.parse(content.toString()); |
| 68 | + |
| 69 | + Builder result = new Builder(); |
| 70 | + result.put("status", "success"); |
| 71 | + result.put("query", query); |
| 72 | + result.put("results", json); |
| 73 | + |
| 74 | + LOGGER.info("Successfully fetched search results for query: " + query); |
| 75 | + return result; |
| 76 | + } |
| 77 | +} |
0 commit comments