Add user agent option
All checks were successful
CI / Build and push Docker image (push) Successful in 1m33s

This commit is contained in:
Tom Foster 2025-07-13 16:04:26 +01:00
parent 35bf0314e9
commit c5aa6a8008
3 changed files with 11 additions and 1 deletions

View file

@ -26,3 +26,4 @@ app:
default_location: "London, UK"
web:
enabled: true
user_agent: "Mozilla/5.0 (X11; Linux x86_64; rv:128.0) Gecko/20100101 Firefox/128.0esr"

View file

@ -31,6 +31,7 @@ class AppConfig:
searxng_base_url: The base URL of the SearXNG instance.
default_timezone: The default timezone for the Time tool.
default_location: The default location for the Weather tool.
web_user_agent: The user agent string for the Web tool HTTP requests.
"""
_instance = None
@ -41,6 +42,7 @@ class AppConfig:
searxng_base_url: str
default_timezone: str
default_location: str
web_user_agent: str
def __new__(cls) -> Self:
"""Ensures that only one instance of the AppConfig is created.
@ -69,6 +71,7 @@ class AppConfig:
self.searxng_base_url = "http://localhost:8080" # Default value
self.default_timezone = "UTC" # Default value
self.default_location = "London, UK" # Default value
self.web_user_agent = "Mozilla/5.0 (compatible; OpenAPI-MCP-Server/1.0)" # Default value
config_path = Path("config.yaml")
print(f"DEBUG: _initialize - config_path: {config_path}")
print(f"DEBUG: _initialize - config_path.exists(): {config_path.exists()}")
@ -103,6 +106,9 @@ class AppConfig:
self.default_location = weather_tool_config.get(
"default_location", self.default_location
)
# Extract web user agent
web_tool_config = self.tools.get("web", {})
self.web_user_agent = web_tool_config.get("user_agent", self.web_user_agent)
def get_app_config() -> AppConfig:

View file

@ -16,6 +16,7 @@ import trafilatura
from fastapi import APIRouter, HTTPException, Query
from pydantic import HttpUrl # noqa: TC002
from openapi_mcp_server.core.config import get_app_config
from openapi_mcp_server.tools.base import BaseTool
from .models import WebRawResponse, WebRequest, WebResponse
@ -103,7 +104,9 @@ class WebTool(BaseTool):
raise HTTPException(status_code=422, detail="URL query parameter is required.")
try:
response = requests.get(str(url), timeout=30)
config = get_app_config()
headers = {"User-Agent": config.web_user_agent}
response = requests.get(str(url), headers=headers, timeout=30)
response.raise_for_status()
headers = dict(response.headers)