Jarvis-Brain 0.0.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,8 @@
1
+ Metadata-Version: 2.4
2
+ Name: Jarvis_Brain
3
+ Version: 0.0.1
4
+ Summary: Jarvis brain
5
+ Requires-Python: >=3.10
6
+ Requires-Dist: drissionpage
7
+ Requires-Dist: fastmcp
8
+ Requires-Dist: htmlmin
@@ -0,0 +1,6 @@
1
+ mcp_tools/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
+ mcp_tools/dp_tools.py,sha256=hnxybSDa8KWEFYfrfFqMNbnO9f3VQNUhZFK-L1F34IM,5505
3
+ jarvis_brain-0.0.1.dist-info/METADATA,sha256=ipfhv5W4pSRBTQNwBHTTECNp0wKFoxJF9pjXy8-RUPk,176
4
+ jarvis_brain-0.0.1.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
5
+ jarvis_brain-0.0.1.dist-info/entry_points.txt,sha256=_6gPuZq_ipnObk2O6CtFIRWmUQrUIDGc_NcdNNhuOr0,54
6
+ jarvis_brain-0.0.1.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.28.0
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ jarvis-dp = mcp_tools.dp_tools:main
mcp_tools/__init__.py ADDED
File without changes
mcp_tools/dp_tools.py ADDED
@@ -0,0 +1,170 @@
1
+ import json
2
+ import os
3
+ import random
4
+ from typing import Any
5
+
6
+ import htmlmin
7
+ from DrissionPage import ChromiumPage, ChromiumOptions
8
+ from DrissionPage._elements.none_element import NoneElement
9
+ from fastmcp import FastMCP
10
+ from lxml import html, etree
11
+
12
+ mcp = FastMCP("JARVIS_DP_TOOLS")
13
+
14
+ html_source_code_local_save_path = os.path.join(os.getcwd(), "html-source-code")
15
+
16
+
17
+ # 压缩html
18
+ def compress_html(content):
19
+ doc = html.fromstring(content)
20
+ # 删除 style 和 script 标签
21
+ for element in doc.xpath('//style | //script'):
22
+ element.getparent().remove(element)
23
+
24
+ # 删除 link 标签
25
+ for link in doc.xpath('//link[@rel="stylesheet"]'):
26
+ link.getparent().remove(link)
27
+
28
+ # 删除 style 属性
29
+ for element in doc.xpath('//*[@style]'):
30
+ element.attrib.pop('style')
31
+
32
+ # 删除所有 on* 事件属性
33
+ for element in doc.xpath('//*'):
34
+ for attr in list(element.attrib.keys()):
35
+ if attr.startswith('on'):
36
+ element.attrib.pop(attr)
37
+
38
+ result = etree.tostring(doc, encoding='unicode')
39
+ result = htmlmin.minify((result))
40
+ print(f"html压缩比=> {len(content) / len(result) * 100:.2f}%")
41
+ return result
42
+
43
+
44
+ # 随机一个浏览器池中不存在的端口,创建一个浏览器,返回随机端口,和浏览器对象。
45
+ def create_browser():
46
+ global browser_pool
47
+ random_port = random.randint(9222, 9934)
48
+ while random_port in browser_pool:
49
+ random_port = random.randint(9222, 9934)
50
+ co = ChromiumOptions().set_local_port(random_port)
51
+ browser_pool[random_port] = ChromiumPage(co)
52
+ return random_port, browser_pool[random_port]
53
+
54
+
55
+ # 根据传入的端口查找对应的浏览器对象
56
+ def get_page(port):
57
+ return browser_pool.get(port, None)
58
+
59
+
60
+ # @tool("visit_url", "使用Drissionpage打开url访问某个网站", {"url": str})
61
+ @mcp.tool(name="visit_url", description="使用Drissionpage打开url访问某个网站")
62
+ async def visit_url(url: str) -> dict[str, Any]:
63
+ port, _browser = create_browser()
64
+ tab = _browser.get_tab()
65
+ tab.get(url)
66
+ tab_id = tab.tab_id
67
+ return {
68
+ "content": [{
69
+ "type": "text",
70
+ "text": json.dumps({
71
+ "message": f"已在[{port}]端口创建浏览器对象,并已打开链接:{url}",
72
+ "tab_id": tab_id,
73
+ "browser_port": port,
74
+ }, ensure_ascii=False)
75
+ }]
76
+ }
77
+
78
+
79
+ @mcp.tool(name="get_html", description="使用Drissionpage获取某一个tab页的html")
80
+ async def get_html(browser_port: int, tab_id: str) -> dict[str, Any]:
81
+ _browser = get_page(browser_port)
82
+ tab = _browser.get_tab(tab_id)
83
+ file_name = tab.title + f"_{tab_id}.html"
84
+ abs_path = os.path.join(html_source_code_local_save_path, file_name)
85
+ with open(abs_path, "w", encoding="utf-8") as f:
86
+ f.write(compress_html(tab.html))
87
+ return {
88
+ "content": [{
89
+ "type": "text",
90
+ "text": json.dumps({
91
+ "message": f"已保存tab页:【{tab_id}】的html源码",
92
+ "tab_id": tab_id,
93
+ "html_local_path": abs_path
94
+ }, ensure_ascii=False)
95
+ }]
96
+ }
97
+
98
+
99
+ @mcp.tool(name="get_new_tab", description="使用Drissionpage创建一个新的tab页,在新的tab页中打开url")
100
+ async def get_new_tab(browser_port: int, url: str) -> dict[str, Any]:
101
+ _browser = get_page(browser_port)
102
+ tab = _browser.new_tab(url)
103
+ _browser.activate_tab(tab)
104
+ tab_id = tab.tab_id
105
+ return {
106
+ "content": [{
107
+ "type": "text",
108
+ "text": json.dumps({
109
+ "message": f"已创建新的tab页,并打开链接:{url}",
110
+ "tab_id": tab_id,
111
+ }, ensure_ascii=False)
112
+ }]
113
+ }
114
+
115
+
116
+ @mcp.tool(name="switch_tab", description="根据传入的tab_id切换到对应的tab页", )
117
+ async def switch_tab(browser_port: int, tab_id: str) -> dict[str, Any]:
118
+ _browser = get_page(browser_port)
119
+ _browser.activate_tab(tab_id)
120
+ return {
121
+ "content": [{
122
+ "type": "text",
123
+ "text": json.dumps({
124
+ "message": f"已将tab页:【{tab_id}】切换至最前端",
125
+ }, ensure_ascii=False)
126
+ }]
127
+ }
128
+
129
+
130
+ @mcp.tool(name="close_tab", description="根据传入的tab_id关闭tab页", )
131
+ async def close_tab(browser_port, tab_id) -> dict[str, Any]:
132
+ _browser = get_page(browser_port)
133
+ _browser.close_tabs(tab_id)
134
+ return {
135
+ "content": [{
136
+ "type": "text",
137
+ "text": json.dumps({
138
+ "message": f"已将tab页:【{tab_id}】关闭",
139
+ }, ensure_ascii=False)
140
+ }]
141
+ }
142
+
143
+
144
+ @mcp.tool(name="check_selector", description="查找tab页中是否包含元素")
145
+ async def check_selector(browser_port: int, tab_id: str, css_selector: str) -> dict[str, Any]:
146
+ _browser = get_page(browser_port)
147
+ target_tab = _browser.get_tab(tab_id)
148
+ css_selector = css_selector
149
+ if "css:" not in css_selector:
150
+ css_selector = "css:" + css_selector
151
+ target_ele = target_tab.ele(css_selector)
152
+ return {
153
+ "content": [{
154
+ "type": "text",
155
+ "text": json.dumps({
156
+ "message": f"已完成tab页:【{tab_id}】对:【{css_selector}】的检查",
157
+ "tab_id": tab_id,
158
+ "selector": css_selector,
159
+ "selector_ele_exist": not isinstance(target_ele, NoneElement),
160
+ }, ensure_ascii=False)
161
+ }]
162
+ }
163
+
164
+
165
+ def main():
166
+ mcp.run()
167
+
168
+
169
+ if __name__ == '__main__':
170
+ main()