-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevdocs.py
More file actions
106 lines (92 loc) · 4.54 KB
/
Copy pathdevdocs.py
File metadata and controls
106 lines (92 loc) · 4.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
from __future__ import annotations
import httpx
from config import DEVDOCS_API, get_http_client
async def devdocs_list_docs() -> dict:
try:
c = get_http_client()
r = await c.get(f"{DEVDOCS_API}/docs.json",
headers={"User-Agent": "mcp-codesearch/1.0"})
if r.status_code != 200:
return {"success": False, "error": f"DevDocs: {r.status_code}"}
docs = r.json()
return {"success": True, "total": len(docs), "docs": [d.get("name", d.get("slug", "")) for d in docs]}
except (httpx.HTTPError, ValueError, KeyError) as e:
return {"success": False, "error": str(e)}
async def devdocs_fetch(slug: str) -> dict:
try:
c = get_http_client()
r = await c.get(f"{DEVDOCS_API}/{slug}/index.json",
headers={"User-Agent": "mcp-codesearch/1.0"})
if r.status_code != 200:
return {"success": False, "error": f"DevDocs: HTTP {r.status_code}"}
data = r.json()
entries = []
for entry in (data.get("entries", data.get("types", [data])) if isinstance(data, dict) else data):
if isinstance(entry, dict):
entries.append({
"name": entry.get("name", ""),
"path": entry.get("path", ""),
"type": entry.get("type", ""),
})
return {"success": True, "slug": slug, "entries": entries[:30], "total": len(entries)}
except (httpx.HTTPError, ValueError, KeyError) as e:
return {"success": False, "error": str(e)}
async def devdocs_fetch_content(slug: str, path: str) -> dict:
try:
c = get_http_client()
r = await c.get(f"{DEVDOCS_API}/{slug}/db.json",
headers={"User-Agent": "mcp-codesearch/1.0"})
if r.status_code != 200:
return {"success": False, "error": f"DevDocs: HTTP {r.status_code}"}
db = r.json()
content = db.get(path, "")
if not content:
keys = [k for k in db.keys() if path.lower() in k.lower()]
if keys:
content = db[keys[0]]
return {"success": True, "slug": slug, "path": path, "content": content[:8000] if content else ""}
except (httpx.HTTPError, ValueError, KeyError) as e:
return {"success": False, "error": str(e)}
async def devdocs_search(slug: str, query: str) -> dict:
try:
c = get_http_client()
r = await c.get(f"{DEVDOCS_API}/{slug}/index.json",
headers={"User-Agent": "mcp-codesearch/1.0"})
if r.status_code != 200:
return {"success": False, "error": f"DevDocs: HTTP {r.status_code}"}
data = r.json()
ql = query.lower()
matched = []
entries = data.get("entries", []) if isinstance(data, dict) else data
for entry in entries if isinstance(entries, list) else []:
if isinstance(entry, dict) and (ql in entry.get("name", "").lower() or ql in entry.get("path", "").lower()):
matched.append({
"name": entry.get("name", ""),
"path": entry.get("path", ""),
"type": entry.get("type", ""),
"url": f"https://devdocs.io/{slug}/{entry.get('path', '')}",
})
return {"success": True, "slug": slug, "results": matched[:15], "total": len(matched)}
except (httpx.HTTPError, ValueError, KeyError) as e:
return {"success": False, "error": str(e)}
async def devdocs_toc(doc: str, version: str = "") -> dict:
"""Get table of contents for a DevDocs docset."""
try:
c = get_http_client()
url = f"{DEVDOCS_API}/{doc}/{version}.json" if version else f"{DEVDOCS_API}/{doc}.json"
r = await c.get(url, headers={"User-Agent": "mcp-codesearch/1.0"})
if r.status_code != 200:
return {"success": False, "error": f"DevDocs TOC: HTTP {r.status_code}"}
return {"success": True, "doc": doc, "version": version, "toc": r.json()}
except (httpx.HTTPError, ValueError) as e:
return {"success": False, "error": str(e)}
async def devdocs_meta(slug: str) -> dict:
try:
c = get_http_client()
r = await c.get(f"{DEVDOCS_API}/{slug}/meta.json",
headers={"User-Agent": "mcp-codesearch/1.0"})
if r.status_code != 200:
return {"success": False, "error": f"DevDocs meta: HTTP {r.status_code}"}
return {"success": True, "slug": slug, "meta": r.json()}
except (httpx.HTTPError, ValueError, KeyError) as e:
return {"success": False, "error": str(e)}