|
| 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 | + "mvn_fetch_deps" => mvn_fetch_deps(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 mvn_fetch_deps(input: CallToolRequest) -> Result<CallToolResult, Error> { |
| 29 | + use quick_xml::Reader; |
| 30 | + use quick_xml::events::Event; |
| 31 | + use serde_json::json; |
| 32 | + |
| 33 | + let args = input.params.arguments.unwrap_or_default(); |
| 34 | + let group = match args.get("group") { |
| 35 | + Some(Value::String(s)) => s, |
| 36 | + _ => { |
| 37 | + return Ok(CallToolResult { |
| 38 | + is_error: Some(true), |
| 39 | + content: vec![Content { |
| 40 | + annotations: None, |
| 41 | + text: Some("Missing 'group' argument".into()), |
| 42 | + mime_type: None, |
| 43 | + r#type: ContentType::Text, |
| 44 | + data: None, |
| 45 | + }], |
| 46 | + }); |
| 47 | + } |
| 48 | + }; |
| 49 | + let artifact = match args.get("artifact") { |
| 50 | + Some(Value::String(s)) => s, |
| 51 | + _ => { |
| 52 | + return Ok(CallToolResult { |
| 53 | + is_error: Some(true), |
| 54 | + content: vec![Content { |
| 55 | + annotations: None, |
| 56 | + text: Some("Missing 'artifact' argument".into()), |
| 57 | + mime_type: None, |
| 58 | + r#type: ContentType::Text, |
| 59 | + data: None, |
| 60 | + }], |
| 61 | + }); |
| 62 | + } |
| 63 | + }; |
| 64 | + let version = match args.get("version") { |
| 65 | + Some(Value::String(s)) => s, |
| 66 | + _ => { |
| 67 | + return Ok(CallToolResult { |
| 68 | + is_error: Some(true), |
| 69 | + content: vec![Content { |
| 70 | + annotations: None, |
| 71 | + text: Some("Missing 'version' argument".into()), |
| 72 | + mime_type: None, |
| 73 | + r#type: ContentType::Text, |
| 74 | + data: None, |
| 75 | + }], |
| 76 | + }); |
| 77 | + } |
| 78 | + }; |
| 79 | + |
| 80 | + let group_path = group.replace('.', "/"); |
| 81 | + let pom_url = format!( |
| 82 | + "https://repo1.maven.org/maven2/{}/{}/{}/{}-{}.pom", |
| 83 | + group_path, artifact, version, artifact, version |
| 84 | + ); |
| 85 | + |
| 86 | + let mut req = HttpRequest { |
| 87 | + url: pom_url.clone(), |
| 88 | + headers: BTreeMap::new(), |
| 89 | + method: Some("GET".to_string()), |
| 90 | + }; |
| 91 | + req.headers |
| 92 | + .insert("User-Agent".to_string(), "maven-plugin/1.0".to_string()); |
| 93 | + |
| 94 | + let res = match http::request::<()>(&req, None) { |
| 95 | + Ok(r) => r, |
| 96 | + Err(e) => { |
| 97 | + return Ok(CallToolResult { |
| 98 | + is_error: Some(true), |
| 99 | + content: vec![Content { |
| 100 | + annotations: None, |
| 101 | + text: Some(format!("Failed to fetch POM: {}", e)), |
| 102 | + mime_type: None, |
| 103 | + r#type: ContentType::Text, |
| 104 | + data: None, |
| 105 | + }], |
| 106 | + }); |
| 107 | + } |
| 108 | + }; |
| 109 | + let body = res.body(); |
| 110 | + let xml = String::from_utf8_lossy(body.as_slice()); |
| 111 | + |
| 112 | + // Parse dependencies |
| 113 | + let mut reader = Reader::from_str(&xml); |
| 114 | + reader.trim_text(true); |
| 115 | + let mut buf = Vec::new(); |
| 116 | + let mut dependencies = Vec::new(); |
| 117 | + let mut in_dependencies = false; |
| 118 | + let mut current = serde_json::Map::new(); |
| 119 | + let mut current_tag = String::new(); |
| 120 | + |
| 121 | + loop { |
| 122 | + match reader.read_event_into(&mut buf) { |
| 123 | + Ok(Event::Start(ref e)) => { |
| 124 | + let tag = String::from_utf8_lossy(e.name().as_ref()).to_string(); |
| 125 | + if tag == "dependencies" { |
| 126 | + in_dependencies = true; |
| 127 | + } else if in_dependencies && tag == "dependency" { |
| 128 | + current = serde_json::Map::new(); |
| 129 | + } else if in_dependencies { |
| 130 | + current_tag = tag; |
| 131 | + } |
| 132 | + } |
| 133 | + Ok(Event::End(ref e)) => { |
| 134 | + let tag = String::from_utf8_lossy(e.name().as_ref()).to_string(); |
| 135 | + if tag == "dependencies" { |
| 136 | + in_dependencies = false; |
| 137 | + } else if in_dependencies && tag == "dependency" { |
| 138 | + dependencies.push(json!(current)); |
| 139 | + } else if in_dependencies { |
| 140 | + current_tag.clear(); |
| 141 | + } |
| 142 | + } |
| 143 | + Ok(Event::Text(e)) => { |
| 144 | + if in_dependencies && !current_tag.is_empty() { |
| 145 | + current.insert(current_tag.clone(), json!(e.unescape().unwrap_or_default())); |
| 146 | + } |
| 147 | + } |
| 148 | + Ok(Event::Eof) => break, |
| 149 | + Err(_) => break, |
| 150 | + _ => {} |
| 151 | + } |
| 152 | + buf.clear(); |
| 153 | + } |
| 154 | + |
| 155 | + Ok(CallToolResult { |
| 156 | + is_error: None, |
| 157 | + content: vec![Content { |
| 158 | + annotations: None, |
| 159 | + text: Some(json!({"dependencies": dependencies}).to_string()), |
| 160 | + mime_type: Some("application/json".to_string()), |
| 161 | + r#type: ContentType::Text, |
| 162 | + data: None, |
| 163 | + }], |
| 164 | + }) |
| 165 | +} |
| 166 | + |
| 167 | +pub(crate) fn describe() -> Result<ListToolsResult, Error> { |
| 168 | + Ok(ListToolsResult{ |
| 169 | + tools: vec![ |
| 170 | + ToolDescription { |
| 171 | + name: "mvn_fetch_deps".into(), |
| 172 | + description: "Fetches the dependencies of a Maven package by group, artifact, and version from Maven Central.".into(), |
| 173 | + input_schema: json!({ |
| 174 | + "type": "object", |
| 175 | + "properties": { |
| 176 | + "group": { |
| 177 | + "type": "string", |
| 178 | + "description": "The Maven groupId", |
| 179 | + }, |
| 180 | + "artifact": { |
| 181 | + "type": "string", |
| 182 | + "description": "The Maven artifactId", |
| 183 | + }, |
| 184 | + "version": { |
| 185 | + "type": "string", |
| 186 | + "description": "The Maven version", |
| 187 | + }, |
| 188 | + }, |
| 189 | + "required": ["group", "artifact", "version"], |
| 190 | + }) |
| 191 | + .as_object() |
| 192 | + .unwrap() |
| 193 | + .clone(), |
| 194 | + }, |
| 195 | + ], |
| 196 | + }) |
| 197 | +} |
0 commit comments