commit 2a878b27f9736b0304ab46916e91511c47bc2ebb Author: krijay Date: Tue Oct 13 14:20:24 2026 +0200 initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..84ab5dd --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +# Created by venv; see https://docs.python.org/3/library/venv.html +.venv +.env + diff --git a/mcp_server.py b/mcp_server.py new file mode 100644 index 0000000..218ba63 --- /dev/null +++ b/mcp_server.py @@ -0,0 +1,74 @@ +import os +import asyncio +import httpx +import trafilatura +from fastmcp import FastMCP + +MOBILE_USER_AGENT = ( + "Mozilla/5.0 (iPhone; CPU iPhone OS 17_5 like Mac OS X) " + "AppleWebKit/605.1.15 (KHTML, like Gecko) " + "Version/17.5 Mobile/15E148 Safari/604.1" +) + +# Disable DNS rebinding protection to allow remote tunnel headers +mcp = FastMCP("Mobile Web MCP Server") + +@mcp.tool() +async def search_duckduckgo_lite(query: str) -> str: + """Searches DuckDuckGo Lite using a mobile User-Agent.""" + headers = { + "User-Agent": MOBILE_USER_AGENT, + "Content-Type": "application/x-www-form-urlencoded", + "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", + "Accept-Language": "en-US,en;q=0.9", + } + async with httpx.AsyncClient(follow_redirects=True) as client: + try: + response = await client.post( + "https://lite.duckduckgo.com/lite/", + data={"q": query}, + headers=headers, + timeout=15.0 + ) + response.raise_for_status() + text = response.text + cleaned_content = trafilatura.extract(text, output_format="markdown", include_links=True, include_images=False) + + if len(cleaned_content) > 6000: + return cleaned_content[:6000] + return cleaned_content or "Fail to extract" + except Exception as e: + return f"Failed to search: {str(e)}" + +@mcp.tool() +async def fetch_html(url: str) -> str: + """Fetches raw HTML content from a URL using a spoofed mobile User-Agent.""" + headers = { + "User-Agent": MOBILE_USER_AGENT, + "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", + "Accept-Language": "en-US,en;q=0.9", + } + async with httpx.AsyncClient(follow_redirects=True) as client: + try: + response = await client.get(url, headers=headers, timeout=15.0) + response.raise_for_status() + text = response.text + cleaned_content = trafilatura.extract(text, output_format="markdown", include_links=True, include_images=False, url=url) + + if len(cleaned_content) > 6000: + return cleaned_content[:6000] + return cleaned_content or "Fail to extract" + except Exception as e: + return f"Failed to fetch URL: {str(e)}" + +if __name__ == "__main__": + #print(asyncio.run(fetch_html("https://www.google.com"))) + #print(asyncio.run(search_duckduckgo_lite("bornhack"))) + mcp.run( + transport="http", + host="127.0.0.1", + port=8005, + path="/mcp", + allowed_origins=["*"], + allowed_hosts=["*"] + ) diff --git a/startmcp.sh b/startmcp.sh new file mode 100755 index 0000000..abf012a --- /dev/null +++ b/startmcp.sh @@ -0,0 +1,2 @@ +source .venv/bin/activate +python mcp_server.py