Skip to main content
Glama

get_field_options

Retrieve available options for Link or Select fields in Frappe Framework. For Link fields, returns documents from the linked DocType; for Select fields, returns predefined options.

Instructions

    Get available options for a Link or Select field.
    
    For Link fields, returns documents from the linked DocType.
    For Select fields, returns the predefined options.
    
    Args:
        doctype: DocType name
        fieldname: Field name
        limit: Maximum number of options to return (default: 20)
    

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
doctypeYes
fieldnameYes
limitNo

Implementation Reference

  • The main handler function implementing the get_field_options tool. It fetches the DocType schema, identifies the field type (Link or Select), and retrieves either linked documents or static options.
    async def get_field_options(
        doctype: str,
        fieldname: str,
        limit: Optional[int] = 20
    ) -> str:
        """
        Get available options for a Link or Select field.
        
        For Link fields, returns documents from the linked DocType.
        For Select fields, returns the predefined options.
        
        Args:
            doctype: DocType name
            fieldname: Field name
            limit: Maximum number of options to return (default: 20)
        """
        try:
            client = get_client()
            
            # First get the field definition to understand its type
            schema_response = await client.get(f"api/resource/DocType/{doctype}")
            
            if "data" not in schema_response:
                return f"Could not get schema for DocType: {doctype}"
            
            fields = schema_response["data"].get("fields", [])
            target_field = None
            
            for field in fields:
                if field.get("fieldname") == fieldname:
                    target_field = field
                    break
            
            if not target_field:
                return f"Field '{fieldname}' not found in DocType '{doctype}'"
            
            fieldtype = target_field.get("fieldtype")
            options = target_field.get("options", "")
            
            if fieldtype == "Link":
                # Get documents from linked DocType
                if not options:
                    return f"Link field '{fieldname}' has no linked DocType defined"
                
                params = {
                    "fields": json.dumps(["name", "title"]),
                    "limit": str(limit)
                }
                
                response = await client.get(f"api/resource/{options}", params=params)
                
                if "data" in response:
                    documents = response["data"]
                    return f"Available {options} documents for field '{fieldname}':\n\n" + json.dumps(documents, indent=2)
                else:
                    return json.dumps(response, indent=2)
            
            elif fieldtype == "Select":
                # Parse select options
                if not options:
                    return f"Select field '{fieldname}' has no options defined"
                
                select_options = [opt.strip() for opt in options.split("\n") if opt.strip()]
                return f"Select options for field '{fieldname}':\n\n" + json.dumps(select_options, indent=2)
            
            else:
                return f"Field '{fieldname}' is of type '{fieldtype}' which doesn't have predefined options"
                
        except Exception as error:
            return _format_error_response(error, "get_field_options")
  • src/server.py:41-41 (registration)
    Registers the schema tools module, which includes the get_field_options tool, with the MCP server instance.
    schema.register_tools(mcp)
  • Utility function used by get_field_options (and other tools) to format and return detailed error responses.
    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/server.py:38-42 (registration)
    Block where all tools are registered during server creation, specifically including schema tools containing get_field_options.
    # Register all tool modules
    helpers.register_tools(mcp)
    documents.register_tools(mcp)
    schema.register_tools(mcp)
    reports.register_tools(mcp)

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/appliedrelevance/frappe-mcp-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server