This repository was archived by the owner on Apr 8, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzread_mcp_server.py
More file actions
executable file
·1684 lines (1377 loc) · 53.2 KB
/
zread_mcp_server.py
File metadata and controls
executable file
·1684 lines (1377 loc) · 53.2 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
# /// script
# requires-python = ">=3.10"
# dependencies = [
# "requests>=2.28.0",
# "fastmcp>=2.0.0",
# ]
# ///
"""
Zread.ai MCP 服务
提供代码仓库文档查询、AI 智能问答、仓库发现等功能
合并自:
- nextjs_rsc_extractor.py: 核心功能模块
- mcp_server.py: MCP 服务封装
使用方法:
# 基础模式(无需 Token)
python zread_mcp_server.py
# 完整模式(需要 Token)
export ZREAD_TOKEN='your-token'
python zread_mcp_server.py
# 或使用命令行参数
python zread_mcp_server.py --token 'your-token'
# HTTP 模式
python zread_mcp_server.py --transport http
传输协议:
- stdio (默认): 用于 Claude Desktop 等客户端
- sse: HTTP Server-Sent Events 模式
- http: Streamable HTTP 模式
功能说明:
- 无需 Token: 文档查询、仓库发现、热门仓库、状态检查
- 需要 Token: AI 智能问答、文件获取
获取 Token:
1. 访问 https://zread.ai 并登录账号
2. 按 F12 打开浏览器控制台
3. 粘贴: prompt('复制token', JSON.parse(localStorage.getItem('CGX_AUTH_STORAGE')).state.token)
4. 在弹出的对话框中复制 Token
"""
# 标准库
import json
import os
import re
import sys
from typing import Any, Dict, List, Optional, Tuple
# 第三方库
import requests
from fastmcp import FastMCP
# ==========================================
# 全局配置
# ==========================================
# 硬编码 token(可选,优先从环境变量读取)
# 使用 --no-token 参数可在无 token 模式下运行,只提供不需要 token 的功能
_DEFAULT_TOKEN = os.environ.get("ZREAD_TOKEN", "")
# 固定域名
BASE_URL = "https://zread.ai"
# User-Agent
USER_AGENT = (
"Mozilla/5.0 (compatible; zread-mcp/1.0.0; +https://github.com/efjdkev/zread-mcp)"
)
# 默认请求头
DEFAULT_HEADERS = {
"User-Agent": USER_AGENT,
}
# ==========================================
# 核心功能函数
# ==========================================
def _get_token(token: Optional[str] = None) -> str:
"""获取 token,优先级:传入参数 > 环境变量 > 硬编码"""
if token:
return token
if _DEFAULT_TOKEN:
return _DEFAULT_TOKEN
raise ValueError("Token 未设置。请传入 token 参数,或设置 ZREAD_TOKEN 环境变量")
def set_default_token(token: str) -> None:
"""设置默认 token(运行时修改)"""
global _DEFAULT_TOKEN
_DEFAULT_TOKEN = token
def _parse_repo_url(url_or_path: str) -> Tuple[str, str, str]:
"""
解析多种格式的仓库 URL 或路径
:param url_or_path: 可以是以下格式:
- https://zread.ai/owner/repo
- https://github.com/owner/repo
- owner/repo
:return: (owner, repo, 完整zread_url)
"""
url_or_path = url_or_path.strip()
# 移除协议头
if url_or_path.startswith("https://"):
url_or_path = url_or_path[8:]
elif url_or_path.startswith("http://"):
url_or_path = url_or_path[7:]
# 移除域名前缀(如果有)
if url_or_path.startswith("zread.ai/"):
url_or_path = url_or_path[9:]
elif url_or_path.startswith("github.com/"):
url_or_path = url_or_path[11:]
# 现在应该是 owner/repo 格式
parts = url_or_path.split("/")
if len(parts) >= 2:
owner = parts[0]
repo = parts[1]
zread_url = f"{BASE_URL}/{owner}/{repo}"
return owner, repo, zread_url
raise ValueError(
f"无法解析仓库路径: {url_or_path},请使用格式: owner/repo 或完整 URL"
)
def fetch_repo_metadata(repo_url_or_path: str) -> Optional[Dict[str, Any]]:
"""
步骤一:获取仓库元数据
:param repo_url_or_path: 支持多种格式:
- https://zread.ai/owner/repo
- https://github.com/owner/repo
- owner/repo
:return: dict 包含 wiki.info 和简化后的 pages 列表,失败返回 None
"""
_, _, zread_url = _parse_repo_url(repo_url_or_path)
response = requests.get(zread_url, headers=DEFAULT_HEADERS)
html = response.text
# HTML markers for extracting wiki data
_START_MARKER = '{\\"wiki\\":{\\"info\\":{\\"wiki_id\\":\\"'
_END_MARKER = ']\\n"])</script><script>self.__next_f.push'
start_pos = html.find(_START_MARKER)
if start_pos == -1:
return None
end_pos = html.find(_END_MARKER, start_pos)
if end_pos == -1:
return None
try:
json_str = html[start_pos:end_pos].replace('\\"', '"').replace("\\\\", "\\")
wiki_obj = json.loads(json_str)
def find_wiki_node(node):
if isinstance(node, dict):
if "wiki" in node and "info" in node["wiki"]:
return node["wiki"]
for v in node.values():
res = find_wiki_node(v)
if res:
return res
elif isinstance(node, list):
for item in node:
res = find_wiki_node(item)
if res:
return res
return None
wiki_node = find_wiki_node(wiki_obj)
if not wiki_node:
return None
simplified_pages = []
for page in wiki_node.get("pages", []):
section = page.get("section", "")
group = page.get("group", "")
topic = page.get("topic", "")
parts = [p for p in [section, group, topic] if p]
title = "/".join(parts)
simplified_pages.append(
{
"page_id": page.get("page_id"),
"slug": page.get("slug"),
"title": title,
"topic": topic,
"group": group,
"section": section,
"order": page.get("order"),
}
)
return {"wiki_info": wiki_node.get("info", {}), "pages": simplified_pages}
except json.JSONDecodeError as e:
print(f"解析 JSON 失败: {e}")
return None
except (KeyError, TypeError) as e:
print(f"解析数据结构失败: {e}")
return None
def fetch_markdown(repo_url_or_path: str, slug: str, lang: str = "zh") -> Optional[str]:
"""
获取 Markdown 正文
:param repo_url_or_path: 支持多种格式: owner/repo 或完整 URL
:param slug: 页面 slug
:param lang: 语言,默认 'zh'
:return: Markdown 字符串 或 None
"""
_, _, zread_url = _parse_repo_url(repo_url_or_path)
url = f"{zread_url}/{slug}"
response = requests.get(
url, cookies={"X-Locale": lang}, headers={**DEFAULT_HEADERS, "RSC": "1"}
)
content = response.content
# 倒着搜索 ",---" 第一次出现的位置
marker = b",---"
end_pos = content.rfind(marker)
if end_pos == -1:
return None
# 往前找 \n(换行符)
line_start = content.rfind(b"\n", 0, end_pos)
if line_start == -1:
line_start = 0 # 如果没有找到换行符,从开头开始
else:
line_start += 1 # 跳过换行符本身
# 提取中间的字符(如 81:T42bf,)
header_line = content[line_start : end_pos + 1].decode("latin-1") # +1 包含逗号
# 用正则匹配出字节大小
head_pattern = re.compile(r"^([0-9a-f]+):T([0-9a-f]+),")
match = head_pattern.match(header_line)
if not match:
return "获取失败"
try:
byte_length = int(match.group(2), 16)
except ValueError:
return "获取失败"
# 计算内容开始位置(头部结束位置,即逗号后的位置)
header_end = line_start + match.end()
# 往后提取内容
return content[header_end : header_end + byte_length].decode("utf-8")
def search_wiki(repo_url_or_path: str, query: str, lang: str = "zh") -> str:
"""
搜索 Wiki 内容
:param repo_url_or_path: 支持多种格式: owner/repo 或完整 URL
:param query: 搜索词
:param lang: 语言,默认 'zh'
:return: 格式化结果字符串
"""
metadata = fetch_repo_metadata(repo_url_or_path)
if not metadata or not metadata.get("wiki_info", {}).get("wiki_id"):
return "no result"
wiki_id = metadata["wiki_info"]["wiki_id"]
search_url = f"{BASE_URL}/api/v1/wiki/{wiki_id}/search"
headers = {**DEFAULT_HEADERS, "x-locale": lang}
params = {"q": query}
try:
response = requests.get(search_url, headers=headers, params=params, timeout=30)
response.raise_for_status()
data = response.json()
if data.get("code") != 0 or not data.get("data"):
return "no result"
results = data["data"]
if not results:
return "no result"
formatted_results = []
for result in results:
lines = [f"# [{result.get('title', '')}]({result.get('slug', '')})"]
for match in result.get("matches", []):
text = match.get("highlight") or match.get("content", "")
text = re.sub(r"<[^>]+>", "", text).replace("\n", " ")
text = re.sub(r" {3,}", " ", text).strip()
if text:
lines.append(text)
formatted_results.append("\n".join(lines))
return "\n\n".join(formatted_results) if formatted_results else "no result"
except requests.RequestException as e:
print(f"搜索 Wiki 网络请求失败: {e}")
return "no result"
except json.JSONDecodeError as e:
print(f"搜索 Wiki 响应解析失败: {e}")
return "no result"
def create_talk(
repo_id: str, token: Optional[str] = None, lang: str = "zh"
) -> Optional[str]:
"""
创建 AI 对话
:param repo_id: 仓库 ID
:param token: 可选,Bearer Token
:param lang: 语言,默认 'zh'
:return: talk_id 或 None
"""
token = _get_token(token)
url = f"{BASE_URL}/api/v1/talk"
headers = {
**DEFAULT_HEADERS,
"Content-Type": "application/json",
"Authorization": f"Bearer {token}",
"x-locale": lang,
}
data = {"repo_id": repo_id, "query": "."}
try:
response = requests.post(url, headers=headers, json=data, timeout=30)
response.raise_for_status()
result = response.json()
if result.get("code") == 0 and result.get("data"):
return result["data"].get("talk_id")
else:
print(f"创建对话失败: {result.get('msg', '未知错误')}")
return None
except requests.RequestException as e:
print(f"创建对话网络请求失败: {e}")
return None
except json.JSONDecodeError as e:
print(f"创建对话响应解析失败: {e}")
return None
def send_message(
talk_id: str,
query: str,
wiki_id: str,
page_id: str,
repo_id: str,
token: Optional[str] = None,
model: str = "glm-4.7",
lang: str = "zh",
) -> Optional[str]:
"""
发送消息并获取 AI 回复
:param model: 'glm-4.7' (默认) 或 'claude-sonnet-4.5'
:return: AI 回复文本(收集所有 round_finish 事件的内容,直到遇到 finish 事件)
"""
token = _get_token(token)
url = f"{BASE_URL}/api/v1/talk/{talk_id}/message"
headers = {
**DEFAULT_HEADERS,
"Content-Type": "application/json",
"Authorization": f"Bearer {token}",
"x-locale": lang,
"Accept": "text/event-stream",
}
data = {
"parent_message_id": "",
"query": query,
"context": {
"wiki": {"page_id": page_id, "wiki_id": wiki_id},
"repo": {"repo_id": repo_id},
},
"model": model,
}
try:
response = requests.post(
url, headers=headers, json=data, stream=True, timeout=120
)
response.raise_for_status()
# 收集所有 round_finish 事件的内容
round_answers = []
current_event = None
for line in response.iter_lines(decode_unicode=True):
if not line:
continue
line = line.strip()
# 解析 event 行
if line.startswith("event:"):
current_event = line[6:].strip()
# 检查是否对话完成
if current_event == "finish":
break
# 解析 data 行
elif line.startswith("data:"):
data_str = line[5:].strip()
# 收集 round_finish 事件的内容
if current_event == "round_finish":
try:
event_data = json.loads(data_str)
text = event_data.get("text", "")
if text:
round_answers.append(text)
except json.JSONDecodeError:
continue
# 拼接所有 round_finish 的内容
if round_answers:
return "\n\n".join(round_answers)
return None
except requests.RequestException as e:
print(f"发送消息网络请求失败: {e}")
return None
def delete_talk(talk_id: str, token: Optional[str] = None) -> bool:
"""
删除对话
:param talk_id: 对话 ID
:param token: 可选,Bearer Token
:return: 是否成功
"""
token = _get_token(token)
url = f"{BASE_URL}/api/v1/talk/{talk_id}"
headers = {**DEFAULT_HEADERS, "Authorization": f"Bearer {token}"}
try:
response = requests.delete(url, headers=headers, timeout=30)
return response.status_code < 300
except requests.RequestException as e:
print(f"删除对话网络请求失败: {e}")
return False
def chat_with_ai(
repo_url_or_path: str,
query: str,
token: Optional[str] = None,
model: str = "glm-4.7",
lang: str = "zh",
) -> str:
"""
完整的 AI 对话流程
:param repo_url_or_path: 支持多种格式: owner/repo 或完整 URL
:param query: 用户问题
:param token: 可选,Bearer Token
:param model: 模型,默认 'glm-4.7'
:param lang: 语言,默认 'zh'
:return: AI 回复文本
"""
token = _get_token(token)
metadata = fetch_repo_metadata(repo_url_or_path)
if not metadata:
return "获取仓库元数据失败"
wiki_id = metadata["wiki_info"].get("wiki_id")
repo_id = metadata["wiki_info"].get("repo_id")
if not wiki_id or not repo_id:
return "缺少 wiki_id 或 repo_id"
if not metadata["pages"]:
return "仓库没有页面"
first_page = metadata["pages"][0]
page_id = first_page["page_id"]
talk_id = create_talk(repo_id, token=token, lang=lang)
if not talk_id:
return "创建对话失败"
try:
answer = send_message(
talk_id,
query,
wiki_id,
page_id,
repo_id,
token=token,
model=model,
lang=lang,
)
return answer if answer else "未获取到 AI 回复"
finally:
delete_talk(talk_id, token=token)
def recommend_repos(topic: str = "") -> Optional[Dict[str, Any]]:
"""
随机推荐仓库
:param topic: 可选的 topic 标签
:return: dict 包含 topics 和 repos,或 None
"""
url = f"{BASE_URL}/api/v1/repo/recommend"
params = {"topic": topic} if topic else {}
try:
response = requests.get(url, headers=DEFAULT_HEADERS, params=params, timeout=30)
response.raise_for_status()
result = response.json()
if result.get("code") == 0:
return result.get("data")
else:
print(f"推荐仓库失败: {result.get('msg', '未知错误')}")
return None
except requests.RequestException as e:
print(f"推荐仓库网络请求失败: {e}")
return None
except json.JSONDecodeError as e:
print(f"推荐仓库响应解析失败: {e}")
return None
def search_repos(query: str, lang: str = "zh") -> Optional[List[Dict[str, Any]]]:
"""
模糊搜索仓库
:param query: 搜索词
:param lang: 语言,默认 'zh'
:return: list 仓库列表,或 None
"""
url = f"{BASE_URL}/api/v1/repo"
params = {"q": query}
try:
response = requests.get(url, headers=DEFAULT_HEADERS, params=params, timeout=30)
response.raise_for_status()
result = response.json()
if result.get("code") == 0:
return result.get("data", [])
else:
print(f"搜索仓库失败: {result.get('msg', '未知错误')}")
return None
except requests.RequestException as e:
print(f"搜索仓库网络请求失败: {e}")
return None
except json.JSONDecodeError as e:
print(f"搜索仓库响应解析失败: {e}")
return None
def get_trending_repos() -> Optional[List[Dict[str, Any]]]:
"""
获取每周热榜(展平为一维数组)
:return: list 一维数组,包含所有热门仓库
"""
url = f"{BASE_URL}/api/v1/public/repo/trending"
try:
response = requests.get(url, timeout=30)
response.raise_for_status()
result = response.json()
if result.get("code") == 0:
all_repos = []
for item in result.get("data", []):
repos = item.get("repos", [])
all_repos.extend(repos)
return all_repos
else:
print(f"获取热榜失败: {result.get('msg', '未知错误')}")
return None
except requests.RequestException as e:
print(f"获取热榜网络请求失败: {e}")
return None
except json.JSONDecodeError as e:
print(f"获取热榜响应解析失败: {e}")
return None
def get_repo_info(owner_or_path: str) -> Optional[Dict[str, Any]]:
"""
查看仓库信息和状态
:param owner_or_path: 仓库路径 (owner/repo 格式)
:return: dict 仓库信息,或 None
"""
# 解析 owner/repo 格式
if "/" not in owner_or_path:
raise ValueError("请使用 owner/repo 格式,例如: openclaw/openclaw")
parts = owner_or_path.split("/")
owner = parts[0]
name = parts[1]
url = f"{BASE_URL}/api/v1/repo/github/{owner}/{name}"
try:
response = requests.get(url, headers=DEFAULT_HEADERS, timeout=30)
response.raise_for_status()
result = response.json()
if result.get("code") == 0:
return result.get("data")
else:
print(f"获取仓库信息失败: {result.get('msg', '未知错误')}")
return None
except requests.RequestException as e:
print(f"获取仓库信息网络请求失败: {e}")
return None
except json.JSONDecodeError as e:
print(f"获取仓库信息响应解析失败: {e}")
return None
def submit_repo(
name_or_path: str, notification_email: str = "example@zread.ai"
) -> Optional[Dict[str, Any]]:
"""
提交索引
:param name_or_path: 仓库 URL 或路径(支持 github.com/owner/repo 或 owner/repo)
:param notification_email: 可选的通知邮箱
:return: dict 提交结果,或 None
"""
url = f"{BASE_URL}/api/v1/public/repo/submit"
headers = {**DEFAULT_HEADERS, "Content-Type": "application/json"}
data = {"name_or_path": name_or_path}
if notification_email:
data["notification_email"] = notification_email
try:
response = requests.post(url, headers=headers, json=data, timeout=30)
response.raise_for_status()
result = response.json()
if result.get("code") == 0:
return result.get("data")
else:
print(f"提交索引失败: {result.get('msg', '未知错误')}")
return None
except requests.RequestException as e:
print(f"提交索引网络请求失败: {e}")
return None
except json.JSONDecodeError as e:
print(f"提交索引响应解析失败: {e}")
return None
def refresh_repo(repo_id: str, token: Optional[str] = None) -> bool:
"""
请求刷新索引
:param repo_id: 仓库 ID
:param token: 可选,Bearer Token
:return: 是否成功
"""
token = _get_token(token)
url = f"{BASE_URL}/api/v1/repo/{repo_id}/refresh"
headers = {**DEFAULT_HEADERS, "Authorization": f"Bearer {token}"}
try:
response = requests.post(url, headers=headers, timeout=30)
return response.status_code < 300
except requests.RequestException as e:
print(f"刷新索引网络请求失败: {e}")
return False
def fetch_repo_files(
repo_path: str,
file_path: str,
start_line: Optional[int] = None,
end_line: Optional[int] = None,
token: Optional[str] = None,
) -> Optional[str]:
"""
获取仓库内的文件内容
:param repo_path: 仓库路径,支持格式: owner/repo, https://zread.ai/owner/repo, https://github.com/owner/repo
:param file_path: 文件路径,如 "src/config.ts"
:param start_line: 可选,开始行号(包含),从 1 开始计数
:param end_line: 可选,结束行号(不包含)
:param token: 可选,Bearer Token
:return: 指定行范围的纯文本内容,失败返回 None
示例:
# 获取完整文件
content = fetch_repo_files("openclaw/openclaw", "src/config.ts")
# 获取前 50 行
content = fetch_repo_files("openclaw/openclaw", "src/config.ts", start_line=1, end_line=51)
# 从第 100 行到文件末尾
content = fetch_repo_files("openclaw/openclaw", "src/config.ts", start_line=100)
"""
# 通过 repo_path 获取 repo_id
owner, repo, _ = _parse_repo_url(repo_path)
repo_info = get_repo_info(f"{owner}/{repo}")
if not repo_info:
print(f"无法获取仓库信息: {repo_path}")
return None
repo_id = repo_info.get("repo_id")
if not repo_id:
print("仓库信息中缺少 repo_id")
return None
token = _get_token(token)
url = f"{BASE_URL}/api/v1/repo/{repo_id}/files"
headers = {
**DEFAULT_HEADERS,
"Content-Type": "application/json",
"Authorization": f"Bearer {token}",
}
data = {"files": [{"path": file_path}]}
try:
response = requests.post(url, headers=headers, json=data, timeout=30)
response.raise_for_status()
result = response.json()
if result.get("code") != 0:
print(f"获取文件失败: {result.get('msg', '未知错误')}")
return None
files_data = result.get("data", [])
if not files_data:
print("文件不存在或无法访问")
return None
file_info = files_data[0]
content = file_info.get("content", "")
# 如果没有指定行号范围,返回完整内容
if start_line is None and end_line is None:
return content
# 按行分割
lines = content.split("\n")
total_lines = len(lines)
# 处理行号参数(转换为 0-based 索引)
start_idx = 0
end_idx = total_lines
if start_line is not None:
# start_line 是 1-based,转换为 0-based
start_idx = max(0, start_line - 1)
if end_line is not None:
# end_line 是 1-based(不包含),转换为 0-based 的索引(包含)
end_idx = min(total_lines, end_line - 1)
# 确保范围有效
if start_idx >= end_idx:
return ""
# 提取指定范围的行
selected_lines = lines[start_idx:end_idx]
return "\n".join(selected_lines)
except requests.RequestException as e:
print(f"获取文件内容网络请求失败: {e}")
return None
except json.JSONDecodeError as e:
print(f"获取文件内容响应解析失败: {e}")
return None
except (KeyError, IndexError) as e:
print(f"获取文件内容数据解析失败: {e}")
return None
# ==========================================
# 测试代码
# ==========================================
def run_tests():
"""运行所有测试"""
import time
print("\n" + "=" * 70)
print("开始测试所有功能")
print("=" * 70)
# 测试仓库路径
TEST_REPO = "openclaw/openclaw"
# 1. 测试 URL 解析
print("\n[测试 1/13] URL 解析 (_parse_repo_url)")
try:
test_urls = [
"https://zread.ai/openclaw/openclaw",
"https://github.com/openclaw/openclaw",
"openclaw/openclaw",
]
for url in test_urls:
owner, repo, zread_url = _parse_repo_url(url)
assert owner == "openclaw" and repo == "openclaw", f"解析失败: {url}"
print(" ✓ 通过 - 所有 URL 格式解析正确")
except Exception as e:
print(f" ✗ 失败 - {e}")
# 2. 测试获取元数据
print("\n[测试 2/13] 获取元数据 (fetch_repo_metadata)")
try:
metadata = fetch_repo_metadata(TEST_REPO)
if metadata and metadata.get("wiki_info"):
print(f" ✓ 通过 - 获取到 {len(metadata.get('pages', []))} 个页面")
print(f" wiki_id: {metadata['wiki_info'].get('wiki_id', 'N/A')[:20]}...")
else:
print(" ✗ 失败 - 无法获取元数据(请检查 start_marker 和 end_marker)")
except Exception as e:
print(f" ✗ 失败 - {e}")
# 3. 测试获取 Markdown
print("\n[测试 3/13] 获取 Markdown (fetch_markdown)")
try:
md = fetch_markdown(TEST_REPO, "1-overview")
if md and len(md) > 100:
print(f" ✓ 通过 - 获取到 {len(md)} 字符")
print(f" 预览: {md[:50].replace(chr(10), ' ')}...")
else:
print(" ✗ 失败 - 未获取到内容")
except Exception as e:
print(f" ✗ 失败 - {e}")
# 4. 测试搜索 Wiki
print("\n[测试 4/13] 搜索 Wiki (search_wiki)")
try:
result = search_wiki(TEST_REPO, "gateway")
if result and result != "no result":
print(f" ✓ 通过 - 搜索到结果")
print(f" 预览: {result[:100].replace(chr(10), ' ')}...")
else:
print(" ! 警告 - 未搜索到结果(可能是网络或索引问题)")
except Exception as e:
print(f" ✗ 失败 - {e}")
# 5. 测试推荐仓库
print("\n[测试 5/13] 推荐仓库 (recommend_repos)")
try:
result = recommend_repos()
if result and result.get("repos"):
print(f" ✓ 通过 - 获取到 {len(result.get('repos', []))} 个推荐仓库")
print(f" Topics: {', '.join(result.get('topics', [])[:5])}...")
else:
print(" ! 警告 - 未获取到推荐")
except Exception as e:
print(f" ✗ 失败 - {e}")
# 6. 测试搜索仓库
print("\n[测试 6/13] 搜索仓库 (search_repos)")
try:
result = search_repos("openclaw")
if result and len(result) > 0:
print(f" ✓ 通过 - 搜索到 {len(result)} 个仓库")
print(
f" 第一个: {result[0].get('owner', 'N/A')}/{result[0].get('name', 'N/A')}"
)
else:
print(" ! 警告 - 未搜索到结果")
except Exception as e:
print(f" ✗ 失败 - {e}")
# 7. 测试热榜
print("\n[测试 7/13] 每周热榜 (get_trending_repos)")
try:
result = get_trending_repos()
if result and len(result) > 0:
print(f" ✓ 通过 - 获取到 {len(result)} 个热门仓库")
print(
f" 第一个: {result[0].get('owner', 'N/A')}/{result[0].get('name', 'N/A')}"
)
else:
print(" ! 警告 - 未获取到热榜")
except Exception as e:
print(f" ✗ 失败 - {e}")
# 8. 测试获取仓库信息
print("\n[测试 8/13] 获取仓库信息 (get_repo_info)")
try:
result = get_repo_info("openclaw/openclaw")
if result:
print(f" ✓ 通过 - 获取到仓库信息")
print(f" Status: {result.get('status', 'N/A')}")
print(f" Stars: {result.get('star_count', 'N/A')}")
else:
print(" ! 警告 - 未获取到信息")
except Exception as e:
print(f" ✗ 失败 - {e}")
# 9. 测试提交索引
print("\n[测试 9/13] 提交索引 (submit_repo)")
try:
# 测试已存在的仓库
result = submit_repo("https://github.com/openclaw/openclaw")
if result:
print(f" ✓ 通过 - 提交成功")
print(f" Status: {result.get('status', 'N/A')}")
else:
print(" ! 警告 - 提交返回空结果")
except Exception as e:
print(f" ✗ 失败 - {e}")
# 10. 检查 Token 相关功能
print("\n[测试 10/13] Token 状态检查")
if _DEFAULT_TOKEN:
print(f" ✓ Token 已设置 ({_DEFAULT_TOKEN[:20]}...)")
# 11. 测试创建对话
print("\n[测试 11/13] 创建对话 (create_talk)")
try:
# 先获取 repo_id
repo_info = get_repo_info("openclaw/openclaw")
if repo_info and repo_info.get("repo_id"):
talk_id = create_talk(repo_info["repo_id"])
if talk_id:
print(f" ✓ 通过 - 创建对话成功")
print(f" talk_id: {talk_id[:30]}...")
# 12. 测试删除对话
print("\n[测试 12/13] 删除对话 (delete_talk)")
success = delete_talk(talk_id)
if success:
print(" ✓ 通过 - 删除对话成功")
else:
print(" ! 警告 - 删除对话可能失败")
else:
print(" ! 警告 - 创建对话返回空")
else:
print(" ! 跳过 - 无法获取 repo_id")
except Exception as e:
print(f" ✗ 失败 - {e}")
# 13. 测试完整 AI 对话流程
print("\n[测试 13/13] 完整 AI 对话 (chat_with_ai)")
try:
answer = chat_with_ai(
TEST_REPO, "你好,简要介绍一下这个项目", model="glm-4.7"
)
if answer and len(answer) > 10:
print(f" ✓ 通过 - 获取到 AI 回复")
print(f" 回复: {answer[:80].replace(chr(10), ' ')}...")
else:
print(" ! 警告 - AI 回复为空或太短")
except Exception as e:
print(f" ✗ 失败 - {e}")
else:
print(" ! 跳过 - Token 未设置,跳过 AI 相关测试")
print(
" 设置方式: export ZREAD_TOKEN='your-token' 或 set_default_token('token')"
)
print("\n" + "=" * 70)
print("测试完成")
print("=" * 70)
# ==========================================
# MCP 服务封装
# ==========================================
# 创建 MCP 服务
mcp = FastMCP("zread-ai")
def _parse_repo_path(repo_path: str) -> Tuple[str, str]:
"""
解析多种格式的仓库路径
支持: owner/repo, https://zread.ai/owner/repo, https://github.com/owner/repo
返回: (owner, repo)
"""
# 复用 _parse_repo_url,只取前两个返回值
owner, repo, _ = _parse_repo_url(repo_path)
return owner, repo
def _chat_with_repo_ai(
repo_path: str, question: str, model: str = "glm-4.7", lang: str = "zh"
) -> str:
"""
与仓库 AI 助手对话(内部完整流程)
流程: 提交索引 → 刷新 → 创建会话 → 提问 → 删除会话
"""
# 提交仓库索引
owner, repo = _parse_repo_path(repo_path)
submit_result = submit_repo(f"{owner}/{repo}")
if not submit_result:
return "❌ 仓库索引提交失败,请稍后重试"
if submit_result.get("status") != "success":
return f"⏳ 仓库正在索引中,当前状态: {submit_result.get('status', 'unknown')},请稍后再试"
repo_id = submit_result.get("repo_id")
wiki_id = submit_result.get("wiki_id")
if not repo_id or not wiki_id: