initial commit
This commit is contained in:
commit
2a878b27f9
3 changed files with 80 additions and 0 deletions
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
# Created by venv; see https://docs.python.org/3/library/venv.html
|
||||||
|
.venv
|
||||||
|
.env
|
||||||
|
|
||||||
74
mcp_server.py
Normal file
74
mcp_server.py
Normal file
|
|
@ -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=["*"]
|
||||||
|
)
|
||||||
2
startmcp.sh
Executable file
2
startmcp.sh
Executable file
|
|
@ -0,0 +1,2 @@
|
||||||
|
source .venv/bin/activate
|
||||||
|
python mcp_server.py
|
||||||
Loading…
Add table
Add a link
Reference in a new issue