get_doctype_schema
Retrieve complete DocType schema with field definitions, validations, and linked types to understand document structure before creation or updates.
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:40-92 (handler)The main handler function for the 'get_doctype_schema' tool. It fetches the DocType schema from the Frappe API, processes the fields, formats key information into a JSON structure, and returns it. Handles errors using _format_error_response.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)Registers the schema tools module (containing get_doctype_schema) by calling schema.register_tools(mcp) among other tool modules during server initialization.# 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-33 (helper)Helper function _format_error_response used by get_doctype_schema to format and return error messages, including authentication checks.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-93 (registration)The register_tools function in schema.py that defines and registers the get_doctype_schema tool using the @mcp.tool() decorator.def register_tools(mcp: Any) -> None: """Register schema tools with the MCP server.""" @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/tools/schema.py:40-48 (schema)Input schema defined by function signature (doctype: str) and docstring describing the tool's purpose and args.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 """