Skip to content

Commit 850fd44

Browse files
dhsifssxbingW
authored andcommitted
refactor: tools
1 parent b243e2b commit 850fd44

File tree

4 files changed

+39
-33
lines changed

4 files changed

+39
-33
lines changed

mcp_server/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ class Hello(BaseModel, ABCTool):
3333
# run is tool logic, must use classmethod
3434
@classmethod
3535
async def run(arguments: dict) -> str:
36-
return f"Hello {arguments['name']}"
36+
req = Hello.model_validate(arguments)
37+
return f"Hello {req.name}"
3738

3839
# tool description, must use classmethod
3940
@classmethod

mcp_server/tools/create_http_application.py

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,26 @@
55
@tools.register
66
class CreateHttpApplication(BaseModel, ABCTool):
77
domain: str = Field(default="",description="application domain, if empty, match all domain")
8-
port: int = Field(description="application listen port, must between 1 and 65535")
8+
port: int = Field(min=1, max=65535,description="application listen port, must between 1 and 65535")
99
upstream: str = Field(description="application proxy address, must be a valid url")
1010

1111
@classmethod
1212
async def run(self, arguments:dict) -> str:
13-
port = arguments["port"]
14-
upstream = arguments["upstream"]
15-
domain = arguments["domain"]
13+
try:
14+
req = CreateHttpApplication.model_validate(arguments)
15+
parsed_upstream = urlparse(req.upstream)
16+
if parsed_upstream.scheme != "https" and parsed_upstream.scheme != "http":
17+
return "invalid upstream scheme"
1618

17-
if port is None or port < 1 or port > 65535:
18-
return "invalid port"
19-
20-
parsed_upstream = urlparse(upstream)
21-
if parsed_upstream.scheme != "https" and parsed_upstream.scheme != "http":
22-
return "invalid upstream scheme"
23-
if parsed_upstream.hostname == "":
24-
return "invalid upstream host"
19+
if parsed_upstream.hostname == "":
20+
return "invalid upstream host"
21+
except Exception as e:
22+
return str(e)
2523

2624
return await post_slce_api("/api/open/site",{
27-
"server_names": [domain],
28-
"ports": [ str(port) ],
29-
"upstreams": [ upstream ],
25+
"server_names": [req.domain],
26+
"ports": [ str(req.port) ],
27+
"upstreams": [ req.upstream ],
3028
"type": 0,
3129
"static_default": 1,
3230
"health_check": True,

mcp_server/tools/create_ip_custom_rule.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,34 @@
11
from pydantic import BaseModel, Field
22
from utils.request import post_slce_api
33
from tools import Tool, ABCTool, tools
4+
import ipaddress
45

56
@tools.register
67
class CreateIPCustomRule(BaseModel, ABCTool):
78
ip: str = Field(description="request ip to allow or block")
8-
action: int = Field(description="1: block, 0: allow")
9+
action: int = Field(min=0, max=1,description="1: block, 0: allow")
910

1011
@classmethod
1112
async def run(self, arguments:dict) -> str:
12-
print(arguments)
13-
ip = arguments["ip"]
14-
action = arguments["action"]
13+
try:
14+
req = CreateIPCustomRule.model_validate(arguments)
15+
ipaddress.ip_address(req.ip)
16+
except Exception as e:
17+
return str(e)
18+
1519
name = ""
16-
match action:
20+
match req.action:
1721
case 0:
1822
name += "allow "
1923
case 1:
2024
name += "block "
2125
case _:
2226
return "invalid action"
2327

24-
if not ip or ip == "":
28+
if not req.ip or req.ip == "":
2529
return "ip is required"
2630

27-
name += f"ip: {ip}"
31+
name += f"ip: {req.ip}"
2832

2933
return await post_slce_api("/api/open/policy",{
3034
"name": name,
@@ -34,12 +38,12 @@ async def run(self, arguments:dict) -> str:
3438
{
3539
"k": "src_ip",
3640
"op": "eq",
37-
"v": [ip],
41+
"v": [req.ip],
3842
"sub_k": ""
3943
},
4044
]
4145
],
42-
"action": action
46+
"action": req.action
4347
})
4448

4549
@classmethod

mcp_server/tools/create_path_custom_rule.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,28 @@
55
@tools.register
66
class CreatePathCustomRule(BaseModel, ABCTool):
77
path: str = Field(description="request path to block or allow")
8-
action: int = Field(description="1: block, 0: allow")
8+
action: int = Field(min=0, max=1,description="1: block, 0: allow")
99

1010
@classmethod
1111
async def run(self, arguments:dict) -> str:
12-
path = arguments["path"]
13-
action = arguments["action"]
12+
try:
13+
req = CreatePathCustomRule.model_validate(arguments)
14+
except Exception as e:
15+
return str(e)
16+
1417
name = ""
15-
match action:
18+
match req.action:
1619
case 0:
1720
name += "allow "
1821
case 1:
1922
name += "block "
2023
case _:
2124
return "invalid action"
2225

23-
if not path or path == "":
26+
if not req.path or req.path == "":
2427
return "path is required"
2528

26-
name += f"path: {path}"
29+
name += f"path: {req.path}"
2730

2831
return await post_slce_api("/api/open/policy",{
2932
"name": name,
@@ -33,12 +36,12 @@ async def run(self, arguments:dict) -> str:
3336
{
3437
"k": "uri_no_query",
3538
"op": "eq",
36-
"v": [path],
39+
"v": [req.path],
3740
"sub_k": ""
3841
},
3942
]
4043
],
41-
"action": action
44+
"action": req.action
4245
})
4346

4447
@classmethod

0 commit comments

Comments
 (0)