-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtool_groups.py
More file actions
56 lines (43 loc) · 2.21 KB
/
tool_groups.py
File metadata and controls
56 lines (43 loc) · 2.21 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
# examples/python/tool_groups.py
# Demonstrates tool groups with per-action schemas, validation, and strategies.
# Run: pmcp dev examples/python/tool_groups.py
from typing import Literal, Optional
from protomcp import tool_group, action, ToolResult
@tool_group("db", description="Database operations", strategy="union")
class DatabaseTools:
"""Union strategy: all actions exposed as a single tool with discriminated input."""
@action("query", description="Run a read-only SQL query", requires=["sql"])
def query(self, sql: str, limit: int = 100) -> ToolResult:
return ToolResult(result=f"Executed: {sql} (limit {limit})")
@action(
"insert",
description="Insert a record into a table",
requires=["table", "data"],
enum_fields={"table": ["users", "events", "logs"]},
)
def insert(self, table: str, data: str) -> ToolResult:
return ToolResult(result=f"Inserted into {table}: {data}")
@action("migrate", description="Run a schema migration", requires=["version"])
def migrate(self, version: str, dry_run: bool = False) -> ToolResult:
mode = "dry run" if dry_run else "applied"
return ToolResult(result=f"Migration {version} {mode}")
@tool_group("files", description="File operations", strategy="separate")
class FileTools:
"""Separate strategy: each action becomes its own tool (files.read, files.write, etc.)."""
@action("read", description="Read a file by path", requires=["path"])
def read(self, path: str) -> ToolResult:
return ToolResult(result=f"Contents of {path}")
@action("write", description="Write content to a file", requires=["path", "content"])
def write(self, path: str, content: str) -> ToolResult:
return ToolResult(result=f"Wrote {len(content)} bytes to {path}")
@action(
"search",
description="Search files by pattern",
requires=["pattern"],
enum_fields={"scope": ["workspace", "project", "global"]},
)
def search(self, pattern: str, scope: Literal["workspace", "project", "global"] = "workspace") -> ToolResult:
return ToolResult(result=f"Searching '{pattern}' in {scope}")
if __name__ == "__main__":
from protomcp.runner import run
run()