- Implemented server and client script management tools in `frappe_mcp/tools/scripts.py` - Added translation and user permission management tools in `frappe_mcp/tools/translations.py` - Created user and role management tools in `frappe_mcp/tools/users.py` - Developed webhook and API key management tools in `frappe_mcp/tools/webhooks.py` - Introduced workflow management tools in `frappe_mcp/tools/workflow_tools.py` - Added `pyproject.toml` for project metadata and dependencies
175 lines
6.3 KiB
Python
175 lines
6.3 KiB
Python
"""Server Script and Client Script tools."""
|
|
|
|
import json
|
|
from mcp.types import Tool
|
|
from frappe_mcp.client.frappe_api import FrappeClient
|
|
|
|
|
|
def tools() -> list[Tool]:
|
|
return [
|
|
Tool(
|
|
name="frappe_create_server_script",
|
|
description=(
|
|
"Create a Frappe Server Script (Python). "
|
|
"script_type options: DocType Event, API, Permission Query, Scheduler Event."
|
|
),
|
|
inputSchema={
|
|
"type": "object",
|
|
"required": ["name", "script_type", "script"],
|
|
"properties": {
|
|
"name": {"type": "string", "description": "Unique script name"},
|
|
"script_type": {
|
|
"type": "string",
|
|
"enum": ["DocType Event", "API", "Permission Query", "Scheduler Event"],
|
|
},
|
|
"script": {"type": "string", "description": "Python code"},
|
|
"reference_doctype": {"type": "string", "description": "Required for DocType Event"},
|
|
"doctype_event": {
|
|
"type": "string",
|
|
"enum": [
|
|
"Before Insert", "After Insert", "Before Save", "After Save",
|
|
"Before Submit", "After Submit", "Before Cancel", "After Cancel",
|
|
"Before Delete", "After Delete", "Before Naming", "After Naming",
|
|
],
|
|
"description": "Required for DocType Event",
|
|
},
|
|
"api_method": {"type": "string", "description": "Method name for API type"},
|
|
"allow_guest": {"type": "integer", "default": 0},
|
|
"disabled": {"type": "integer", "default": 0},
|
|
},
|
|
},
|
|
),
|
|
Tool(
|
|
name="frappe_update_server_script",
|
|
description="Update an existing Server Script.",
|
|
inputSchema={
|
|
"type": "object",
|
|
"required": ["name"],
|
|
"properties": {
|
|
"name": {"type": "string"},
|
|
"script": {"type": "string"},
|
|
"disabled": {"type": "integer"},
|
|
"updates": {"type": "object"},
|
|
},
|
|
},
|
|
),
|
|
Tool(
|
|
name="frappe_list_server_scripts",
|
|
description="List all Server Scripts, optionally filtered by script_type.",
|
|
inputSchema={
|
|
"type": "object",
|
|
"properties": {
|
|
"script_type": {"type": "string"},
|
|
"reference_doctype": {"type": "string"},
|
|
},
|
|
},
|
|
),
|
|
Tool(
|
|
name="frappe_create_client_script",
|
|
description="Create a Frappe Client Script (JavaScript) for a DocType.",
|
|
inputSchema={
|
|
"type": "object",
|
|
"required": ["dt", "script"],
|
|
"properties": {
|
|
"dt": {"type": "string", "description": "DocType this script applies to"},
|
|
"script": {"type": "string", "description": "JavaScript code"},
|
|
"view": {
|
|
"type": "string",
|
|
"enum": ["Form", "List", "Tree", "Calendar", "Gantt"],
|
|
"default": "Form",
|
|
},
|
|
"enabled": {"type": "integer", "default": 1},
|
|
},
|
|
},
|
|
),
|
|
Tool(
|
|
name="frappe_list_client_scripts",
|
|
description="List all Client Scripts, optionally filtered by DocType.",
|
|
inputSchema={
|
|
"type": "object",
|
|
"properties": {
|
|
"dt": {"type": "string"},
|
|
},
|
|
},
|
|
),
|
|
]
|
|
|
|
|
|
def handlers() -> dict:
|
|
return {
|
|
"frappe_create_server_script": _create_server_script,
|
|
"frappe_update_server_script": _update_server_script,
|
|
"frappe_list_server_scripts": _list_server_scripts,
|
|
"frappe_create_client_script": _create_client_script,
|
|
"frappe_list_client_scripts": _list_client_scripts,
|
|
}
|
|
|
|
|
|
async def _create_server_script(args: dict) -> str:
|
|
client = FrappeClient()
|
|
payload = {
|
|
"name": args["name"],
|
|
"script_type": args["script_type"],
|
|
"script": args["script"],
|
|
"reference_doctype": args.get("reference_doctype", ""),
|
|
"doctype_event": args.get("doctype_event", ""),
|
|
"api_method": args.get("api_method", ""),
|
|
"allow_guest": args.get("allow_guest", 0),
|
|
"disabled": args.get("disabled", 0),
|
|
}
|
|
result = await client.create_doc("Server Script", payload)
|
|
return json.dumps(result, indent=2)
|
|
|
|
|
|
async def _update_server_script(args: dict) -> str:
|
|
client = FrappeClient()
|
|
updates = args.get("updates", {})
|
|
if "script" in args:
|
|
updates["script"] = args["script"]
|
|
if "disabled" in args:
|
|
updates["disabled"] = args["disabled"]
|
|
result = await client.update_doc("Server Script", args["name"], updates)
|
|
return json.dumps(result, indent=2)
|
|
|
|
|
|
async def _list_server_scripts(args: dict) -> str:
|
|
client = FrappeClient()
|
|
filters = []
|
|
if script_type := args.get("script_type"):
|
|
filters.append(["script_type", "=", script_type])
|
|
if ref := args.get("reference_doctype"):
|
|
filters.append(["reference_doctype", "=", ref])
|
|
result = await client.get_list(
|
|
"Server Script",
|
|
fields=["name", "script_type", "reference_doctype", "doctype_event", "disabled"],
|
|
filters=filters if filters else None,
|
|
limit=50,
|
|
)
|
|
return json.dumps(result, indent=2)
|
|
|
|
|
|
async def _create_client_script(args: dict) -> str:
|
|
client = FrappeClient()
|
|
payload = {
|
|
"dt": args["dt"],
|
|
"script": args["script"],
|
|
"view": args.get("view", "Form"),
|
|
"enabled": args.get("enabled", 1),
|
|
}
|
|
result = await client.create_doc("Client Script", payload)
|
|
return json.dumps(result, indent=2)
|
|
|
|
|
|
async def _list_client_scripts(args: dict) -> str:
|
|
client = FrappeClient()
|
|
filters = []
|
|
if dt := args.get("dt"):
|
|
filters.append(["dt", "=", dt])
|
|
result = await client.get_list(
|
|
"Client Script",
|
|
fields=["name", "dt", "view", "enabled"],
|
|
filters=filters if filters else None,
|
|
limit=50,
|
|
)
|
|
return json.dumps(result, indent=2)
|