|
| 1 | +mod pdk; |
| 2 | + |
| 3 | +use std::collections::BTreeMap; |
| 4 | + |
| 5 | +use extism_pdk::*; |
| 6 | +use json::Value; |
| 7 | +use pdk::types::{ |
| 8 | + CallToolRequest, CallToolResult, Content, ContentType, ListToolsResult, ToolDescription, |
| 9 | +}; |
| 10 | +use serde_json::json; |
| 11 | + |
| 12 | +pub(crate) fn call(input: CallToolRequest) -> Result<CallToolResult, Error> { |
| 13 | + match input.params.name.as_str() { |
| 14 | + "serper_web_search" => serper_web_search(input), |
| 15 | + _ => Ok(CallToolResult { |
| 16 | + is_error: Some(true), |
| 17 | + content: vec![Content { |
| 18 | + annotations: None, |
| 19 | + text: Some(format!("Unknown tool: {}", input.params.name)), |
| 20 | + mime_type: None, |
| 21 | + r#type: ContentType::Text, |
| 22 | + data: None, |
| 23 | + }], |
| 24 | + }), |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +fn serper_web_search(input: CallToolRequest) -> Result<CallToolResult, Error> { |
| 29 | + let args = input.params.arguments.unwrap_or_default(); |
| 30 | + let query = match args.get("q") { |
| 31 | + Some(Value::String(q)) => q.clone(), |
| 32 | + _ => { |
| 33 | + return Ok(CallToolResult { |
| 34 | + is_error: Some(true), |
| 35 | + content: vec![Content { |
| 36 | + annotations: None, |
| 37 | + text: Some("Please provide a 'q' argument for the search query".into()), |
| 38 | + mime_type: None, |
| 39 | + r#type: ContentType::Text, |
| 40 | + data: None, |
| 41 | + }], |
| 42 | + }); |
| 43 | + } |
| 44 | + }; |
| 45 | + |
| 46 | + let api_key = config::get("SERPER_API_KEY")? |
| 47 | + .ok_or_else(|| Error::msg("SERPER_API_KEY configuration is required but not set"))?; |
| 48 | + |
| 49 | + let mut headers = BTreeMap::new(); |
| 50 | + headers.insert("X-API-KEY".to_string(), api_key); |
| 51 | + headers.insert("Content-Type".to_string(), "application/json".to_string()); |
| 52 | + |
| 53 | + let req = HttpRequest { |
| 54 | + url: "https://google.serper.dev/search".to_string(), |
| 55 | + headers, |
| 56 | + method: Some("POST".to_string()), |
| 57 | + }; |
| 58 | + |
| 59 | + let body = json!({ "q": query }); |
| 60 | + let res = http::request(&req, Some(&body.to_string()))?; |
| 61 | + let response_body = res.body(); |
| 62 | + let response_text = String::from_utf8_lossy(response_body.as_slice()).to_string(); |
| 63 | + |
| 64 | + Ok(CallToolResult { |
| 65 | + is_error: None, |
| 66 | + content: vec![Content { |
| 67 | + annotations: None, |
| 68 | + text: Some(response_text), |
| 69 | + mime_type: Some("application/json".to_string()), |
| 70 | + r#type: ContentType::Text, |
| 71 | + data: None, |
| 72 | + }], |
| 73 | + }) |
| 74 | +} |
| 75 | + |
| 76 | +pub(crate) fn describe() -> Result<ListToolsResult, Error> { |
| 77 | + Ok(ListToolsResult{ |
| 78 | + tools: vec![ |
| 79 | + ToolDescription { |
| 80 | + name: "serper_web_search".into(), |
| 81 | + description: "Performs a Google web search using the Serper API and returns the raw JSON response for the given query string.".into(), |
| 82 | + input_schema: json!({ |
| 83 | + "type": "object", |
| 84 | + "properties": { |
| 85 | + "q": { |
| 86 | + "type": "string", |
| 87 | + "description": "The search query string", |
| 88 | + }, |
| 89 | + }, |
| 90 | + "required": ["q"], |
| 91 | + }) |
| 92 | + .as_object() |
| 93 | + .unwrap() |
| 94 | + .clone(), |
| 95 | + }, |
| 96 | + ], |
| 97 | + }) |
| 98 | +} |
0 commit comments