"""Naming Series tools — manage document naming patterns in Frappe.""" import json from mcp.types import Tool from frappe_mcp.client.frappe_api import FrappeClient def tools() -> list[Tool]: return [ Tool( name="frappe_get_naming_series", description="Get all naming series options for a DocType.", inputSchema={ "type": "object", "required": ["doctype"], "properties": { "doctype": {"type": "string"}, }, }, ), Tool( name="frappe_set_naming_series", description=( "Set or update naming series options for a DocType. " "Series format: 'PREFIX-.YYYY.-.MM.-.####' where #### is zero-padded counter." ), inputSchema={ "type": "object", "required": ["doctype", "series"], "properties": { "doctype": {"type": "string"}, "series": { "type": "string", "description": "Newline-separated list of series patterns e.g. 'INV-.YYYY.-.####\\nINV-TEST-.####'", }, }, }, ), Tool( name="frappe_get_series_counter", description="Get the current counter value for a naming series prefix.", inputSchema={ "type": "object", "required": ["prefix"], "properties": { "prefix": {"type": "string", "description": "Prefix e.g. 'INV-2024-'"}, }, }, ), Tool( name="frappe_update_series_counter", description="Manually set the counter for a naming series prefix.", inputSchema={ "type": "object", "required": ["prefix", "value"], "properties": { "prefix": {"type": "string"}, "value": {"type": "integer", "description": "New counter value"}, }, }, ), ] def handlers() -> dict: return { "frappe_get_naming_series": _get_naming_series, "frappe_set_naming_series": _set_naming_series, "frappe_get_series_counter": _get_series_counter, "frappe_update_series_counter": _update_series_counter, } async def _get_naming_series(args: dict) -> str: client = FrappeClient() result = await client.call_method( "frappe.core.doctype.naming_series.naming_series.get_options", arg=args["doctype"], ) return json.dumps(result, indent=2) async def _set_naming_series(args: dict) -> str: client = FrappeClient() result = await client.call_method( "frappe.core.doctype.naming_series.naming_series.update_series", args={ "df": {"options": args["series"]}, "doctype": args["doctype"], }, ) return json.dumps(result, indent=2) async def _get_series_counter(args: dict) -> str: client = FrappeClient() result = await client.call_method( "frappe.core.doctype.naming_series.naming_series.get_current", prefix=args["prefix"], ) return json.dumps({"prefix": args["prefix"], "current": result}, indent=2) async def _update_series_counter(args: dict) -> str: client = FrappeClient() result = await client.call_method( "frappe.core.doctype.naming_series.naming_series.update_counter", prefix=args["prefix"], value=args["value"], ) return json.dumps(result, indent=2)