Source code for tooluniverse.europe_pmc_tool

import requests
from .base_tool import BaseTool
from .tool_registry import register_tool
from .http_utils import request_with_retry


[docs] @register_tool("EuropePMCTool") class EuropePMCTool(BaseTool): """ Tool to search for articles on Europe PMC including abstracts. """
[docs] def __init__( self, tool_config, base_url="https://www.ebi.ac.uk/europepmc/webservices/rest/search", ): super().__init__(tool_config) self.base_url = base_url
[docs] def run(self, arguments): query = arguments.get("query") limit = arguments.get("limit", 5) if not query: return {"error": "`query` parameter is required."} return self._search(query, limit)
[docs] @register_tool("EuropePMCRESTTool") class EuropePMCRESTTool(BaseTool): """ Generic REST tool for Europe PMC API endpoints. Supports citations, references, and other article-related endpoints. """
[docs] def __init__(self, tool_config): super().__init__(tool_config) self.base_url = "https://www.ebi.ac.uk/europepmc/webservices/rest" self.session = requests.Session() self.session.headers.update({"Accept": "application/json"}) self.timeout = 30
[docs] def _build_url(self, arguments): """Build URL from endpoint template and arguments.""" endpoint = self.tool_config["fields"]["endpoint"] url = endpoint for key, value in arguments.items(): placeholder = f"{{{key}}}" if placeholder in url: url = url.replace(placeholder, str(value)) return url
[docs] def run(self, arguments): """Execute the Europe PMC REST API request.""" try: url = self._build_url(arguments) # Extract query parameters (those not in URL path) params = {"format": "json"} endpoint_template = self.tool_config["fields"]["endpoint"] # Add parameters that are not path parameters for key, value in arguments.items(): placeholder = f"{{{key}}}" if placeholder not in endpoint_template and value is not None: params[key] = value response = request_with_retry( self.session, "GET", url, params=params, timeout=self.timeout, max_attempts=3, ) if response.status_code == 200: data = response.json() return {"status": "success", "data": data, "url": response.url} else: return { "status": "error", "error": f"Europe PMC API returned status {response.status_code}", "url": response.url, "status_code": response.status_code, "detail": response.text[:200] if response.text else None, } except Exception as e: return { "status": "error", "error": f"Europe PMC API request failed: {str(e)}", "url": url if "url" in locals() else None, }