get_doctype_schema
Retrieve the complete schema for a Frappe DocType, including field definitions, validations, and linked DocTypes to understand document structure before creating or updating records.
Instructions
Get the complete schema for a DocType including field definitions, validations, and linked DocTypes.
Use this to understand the structure of a DocType before creating or updating documents.
Args:
doctype: DocType name
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| doctype | Yes |
Implementation Reference
- src/tools/schema.py:39-93 (handler)The handler function for the 'get_doctype_schema' tool. It retrieves the DocType schema from the Frappe API, formats the fields summary, and returns JSON. Includes error handling.@mcp.tool() async def get_doctype_schema(doctype: str) -> str: """ Get the complete schema for a DocType including field definitions, validations, and linked DocTypes. Use this to understand the structure of a DocType before creating or updating documents. Args: doctype: DocType name """ try: client = get_client() # Get DocType schema response = await client.get(f"api/resource/DocType/{doctype}") if "data" in response: schema_data = response["data"] # Format the response to highlight key information fields = schema_data.get("fields", []) field_summary = [] for field in fields: field_info = { "label": field.get("label"), "fieldname": field.get("fieldname"), "fieldtype": field.get("fieldtype"), "reqd": field.get("reqd", 0) == 1, "options": field.get("options"), "default": field.get("default") } if field.get("description"): field_info["description"] = field.get("description") field_summary.append(field_info) formatted_response = { "doctype": doctype, "module": schema_data.get("module"), "naming_rule": schema_data.get("autoname"), "is_submittable": schema_data.get("is_submittable", 0) == 1, "is_tree": schema_data.get("is_tree", 0) == 1, "track_changes": schema_data.get("track_changes", 0) == 1, "allow_rename": schema_data.get("allow_rename", 0) == 1, "fields": field_summary, "permissions": schema_data.get("permissions", []) } return json.dumps(formatted_response, indent=2) else: return json.dumps(response, indent=2) except Exception as error: return _format_error_response(error, "get_doctype_schema")
- src/server.py:38-42 (registration)Central registration point where schema.register_tools(mcp) is called to register all tools from the schema module, including 'get_doctype_schema'.# Register all tool modules helpers.register_tools(mcp) documents.register_tools(mcp) schema.register_tools(mcp) reports.register_tools(mcp)
- src/tools/schema.py:15-34 (helper)Helper function used by get_doctype_schema to format and return error responses with authentication and API error handling.def _format_error_response(error: Exception, operation: str) -> str: """Format error response with detailed information.""" credentials_check = validate_api_credentials() # Check for missing credentials first if not credentials_check["valid"]: error_msg = f"Authentication failed: {credentials_check['message']}. " error_msg += "API key/secret is the only supported authentication method." return error_msg # Handle FrappeApiError if isinstance(error, FrappeApiError): error_msg = f"Frappe API error: {error}" if error.status_code in (401, 403): error_msg += " Please check your API key and secret." return error_msg # Default error handling return f"Error in {operation}: {str(error)}"
- src/tools/schema.py:36-36 (registration)The register_tools function in schema.py that defines and registers the get_doctype_schema tool using @mcp.tool() decorator.def register_tools(mcp: Any) -> None: