Coding standards
Security in ResourceSpace
Developer reference
Database
Mcp browse functions
Mcp report functions
Mcp management functions
Core feature functions
Embed functions

mcp_management_read()

Description

Dispatch only named management reads. Each handler enforces the equivalent web permissions.

Parameters

ColumnTypeDefaultDescription
$name string Registered read-only tool name.
$args: array
$args array Validated tool arguments.

Return

array Structured MCP result or permission/availability error.

Location

include/mcp_management_functions.php lines 347 to 610

Definition

 
function mcp_management_read(string $name, array $args): array
{
    switch (
$name) {
        case 
'audit_resources':
            if (!
checkperm('s')) {
                return 
mcp_error_result('Search permission is required.', 'permission_denied');
            }
            return 
mcp_resource_audit($args);
        case 
'get_processing_jobs':
            require_once 
__DIR__ . '/job_functions.php';
            if (!
$GLOBALS['offline_job_queue']) {
                return 
mcp_error_result('Offline jobs are disabled.', 'feature_unavailable');
            }
            
$jobs = job_queue_get_jobs(user: checkperm('a') ? 0 : $GLOBALS['userref'], returnsql: true);
            return 
mcp_query_page("SELECT ref, type, status, start_date FROM ({$jobs->sql}) jobs ORDER BY ref DESC", $jobs->parameters, $args);
        case 
'get_collection_searches':
            
$collection = get_collection($args['collection']);
            if (
                !
$collection || checkperm('b') || !collection_readable($args['collection'])
                || ((int) 
$collection['user'] !== (int) $GLOBALS['userref'] && !checkperm('a'))
            ) {
                throw new 
DomainException('Collection unavailable.');
            }
            return 
mcp_list_page('rows', array_map(
                static fn(
$row) => array_intersect_key(
                    
$row,
                    
array_flip(['ref', 'search', 'restypes', 'archive', 'created', 'last_updated', 'result_limit'])
                ),
                
get_saved_searches($args['collection'])
            ), 
$args);
        case 
'get_notifications':
            return 
mcp_query_page('SELECT um.ref, um.seen, m.created, m.expires FROM user_message um JOIN message m ON m.ref = um.message
                WHERE um.user = ? AND m.expires > NOW() ORDER BY m.created DESC, um.ref DESC'
, ['i', $GLOBALS['userref']], $args);
        case 
'get_brand_guidelines':
            if (!
in_array('brand_guidelines', $GLOBALS['plugins'], true)) {
                return 
mcp_error_result('Brand Guidelines unavailable.', 'feature_unavailable');
            }
            if (!
\Montala\ResourceSpace\Plugins\BrandGuidelines\acl_can_view_brand_guidelines()) {
                return 
mcp_error_result('Brand Guidelines unavailable.', 'permission_denied');
            }
            
$pages = [];
            foreach (
\Montala\ResourceSpace\Plugins\BrandGuidelines\get_all_pages() as $page) {
                
$pages[] = ['ref' => (int) $page['ref'], 'name' => $page['name'], 'parent' => $page['parent'],
                    
'url' => generateURL($GLOBALS['baseurl'] . '/plugins/brand_guidelines/pages/guidelines.php', ['spage' => $page['ref']])];
            }
            return 
mcp_list_page('rows', $pages, $args);
        case 
'get_workflow_transitions':
            if (!
core_feature_enabled('rse_workflow')) {
                return 
mcp_error_result('Workflow actions unavailable.', 'feature_unavailable');
            }
            require_once 
dirname(__DIR__) . '/include/rse_workflow/rse_workflow_functions.php';
            
$resource = mcp_visible_resource($args['ref']);
            
$actions = [];
            foreach (
rse_workflow_get_actions() as $action) {
                if (
rse_workflow_validate_action($action, $resource) && !checkperm('z' . $action['statusto'])) {
                    
$actions[] = ['ref' => (int) $action['ref'], 'name' => $action['name'], 'destination' => (int) $action['statusto']];
                }
            }
            return 
mcp_list_page('rows', $actions, $args);
        case 
'get_search_statistics':
            return 
mcp_activity_statistics($args + ['metric' => 'keyword_usage']);
        case 
'get_plugin_fields':
            if (!
checkperm('s')) {
                return 
mcp_error_result('Search permission is required.', 'permission_denied');
            }
            
$fields = mcp_field_catalogue($args['resource_type']);
            
$mappings = [];
            foreach (
$GLOBALS['plugins'] as $plugin) {
                
$manifest = get_plugin_yaml($plugin, true, false);
                if (
                    empty(
$manifest['mcp_field_settings'])
                    || (!empty(
$manifest['mcp_hide_permission']) && checkperm($manifest['mcp_hide_permission']))
                ) {
                    continue;
                }
                foreach (
explode(',', $manifest['mcp_field_settings']) as $setting) {
                    
$setting = trim($setting);
                    if (!
str_starts_with($setting, $plugin . '_') || !str_ends_with($setting, '_field')) {
                        continue;
                    }
                    
$id = $GLOBALS[$setting] ?? null;
                    if (
is_positive_int_loose($id) && isset($fields[$id])) {
                        
$mappings[] = ['plugin' => $plugin, 'field' => (int) $id, 'title' => $fields[$id]['title']];
                    }
                }
            }
            return 
mcp_list_page('rows', $mappings, $args);
        case 
'get_metadata_completeness':
            if (!
checkperm('s') || !$args['metadata_fields'] || count($args['metadata_fields']) > 10) {
                throw new 
DomainException('Choose one to ten visible metadata fields with search access.');
            }
            
$population = mcp_report_population($args, false);
            
$fields = [];
            foreach (
array_unique($args['metadata_fields']) as $id) {
                
$fields[] = mcp_field_report('get_field_statistics', ['field' => $id] + $args, $population)['structuredContent'];
            }
            
usort($fields, static fn($left, $right) =>
                [
$left['percentage_populated'] ?? 101, $left['field']] <=> [$right['percentage_populated'] ?? 101, $right['field']]);
            return 
mcp_list_page('rows', $fields, []);
        case 
'find_similar_resources':
            return 
mcp_similar_resources($args);
        case 
'get_collection_history':
            
$collection = get_collection($args['collection']);
            if (!
$collection || checkperm('b') || !collection_readable($args['collection']) || !can_view_collection_log($collection)) {
                throw new 
DomainException('Collection history unavailable.');
            }
            
$range = mcp_event_dates($args, 'c.date');
            
$log = collection_log_query(new PreparedStatementQuery(
                
"c.collection = ? AND COALESCE(c.resource, 0) = 0 AND {$range->sql}",
                [
'i', $args['collection'], ...$range->parameters]
            ));
            return 
mcp_query_page("SELECT ref, date, type FROM ({$log->sql}) history ORDER BY ref DESC", $log->parameters, $args);
        case 
'get_resource_representations':
            
$resource = mcp_visible_resource($args['ref']);
            
$rows = [];
            
$page = $args['page'] ?? 1;
            foreach ([
'thumbnail' => 'thm', 'screen' => 'pre', 'large' => 'scr'] as $size => $native) {
                
$preview = mcp_stored_preview($resource, [$native], $size !== 'thumbnail', $page);
                if (
$preview !== null) {
                    
$rows[] = ['size' => $size, 'page' => $page, 'width' => $preview['width'], 'height' => $preview['height'],
                        
'ref' => $args['ref'], 'url' => generateURL(
                            
$GLOBALS['baseurl'] . '/api/mcp/file.php',
                            [
'ref' => $args['ref'], 'preview' => $size, 'page' => $page]
                        )];
                }
            }
            
$media = $page === 1 ? mcp_downloadable_media_preview($resource) : null;
            if (
$media !== null) {
                
$rows[] = ['size' => 'media', 'ref' => $args['ref'], 'extension' => $media['ext'],
                    
'url' => generateURL($GLOBALS['baseurl'] . '/api/mcp/file.php', ['ref' => $args['ref'], 'rendition' => 'media'])];
            }
            return 
mcp_list_page('rows', $rows, []);
        case 
'get_resources':
            if (!
$args['refs']) {
                throw new 
DomainException('Supply at least one resource reference.');
            }
            
$storage = ($args['inspection'] ?? 'metadata') === 'storage';
            if (
$storage && !checkperm('a')) {
                return 
mcp_error_result('Administrator access is required for storage inspection.', 'permission_denied');
            }
            
$rows = [];
            
get_resource_data_batch($args['refs']);
            foreach (
array_unique($args['refs']) as $id) {
                try {
                    
$item = mcp_get_resource($id, $args['offset'] ?? 0, $args['limit'] ?? 10, $args['metadata_fields'] ?? null);
                    if (
$storage) {
                        
$asset = get_resource_data($id);
                        
$item['integrity_failure_recorded'] = (bool) $asset['integrity_fail'];
                        if (
in_array($asset['resource_type'], $GLOBALS['data_only_resource_types']) || $asset['no_file']) {
                            
$item['original_status'] = 'no_file_expected';
                        } else {
                            
$file = validate_resource_files([$asset], ['is_readable' => true]);
                            
$item['original_status'] = $file[$id] ? 'readable' : 'unavailable';
                        }
                        if (!empty(
$asset['file_checksum'])) {
                            
$item['recorded_checksum'] = $asset['file_checksum'];
                        }
                    }
                    
$rows[] = $item;
                } catch (
DomainException $error) {
                    
$rows[] = ['ref' => $id, 'status' => 'unavailable'];
                }
            }
            return 
mcp_result(['resources' => $rows]);
        case 
'get_current_user':
            return 
mcp_result(['ref' => (int) $GLOBALS['userref'], 'display_name' => $GLOBALS['userfullname'],
                
'group' => (int) $GLOBALS['usergroup'], 'permissions' => array_values($GLOBALS['userpermissions'])]);
        case 
'search_users':
        case 
'get_user_statistics':
            return 
mcp_user_directory($name === 'get_user_statistics', $args);
        case 
'get_user_groups':
            if (!
checkperm('a')) {
                return 
mcp_error_result('Group administration permission is required.', 'permission_denied');
            }
            
$rows = [];
            foreach (
get_usergroups(true) as $group) {
                if (!isset(
$args['group']) || $args['group'] === (int) $group['ref']) {
                    
$rows[] = ['ref' => (int) $group['ref'], 'name' => $group['name'], 'parent' => $group['parent'],
                        
'permissions' => trim_array(explode(',', (string) $group['permissions']))];
                }
            }
            return 
mcp_list_page('rows', $rows, $args);
        case 
'get_workflow_states':
            
$rows = [];
            foreach (
get_workflow_state_names() as $id => $label) {
                if (!
checkperm('z' . $id)) {
                    
$rows[] = ['id' => $id, 'label' => $label];
                }
            }
            return 
mcp_list_page('rows', $rows, $args);
        case 
'get_resource_requests':
            if (!
checkperm('R')) {
                return 
mcp_error_result('Resource request management permission is required.', 'permission_denied');
            }
            require_once 
__DIR__ . '/request_functions.php';
            
$requests = get_requests(returnsql: true);
            
$range = mcp_event_dates($args, 'created');
            
$sql = "SELECT ref, created, status, expires FROM ({$requests->sql}) requests WHERE {$range->sql}";
            
$params = array_merge($requests->parameters, $range->parameters);
            if (isset(
$args['record'])) {
                
$sql .= ' AND ref = ?';
                
array_push($params, 'i', $args['record']);
            }
            if (isset(
$args['status'])) {
                
$sql .= ' AND status = ?';
                
array_push($params, 'i', array_search($args['status'], ['pending', 'approved', 'declined'], true));
            }
            return 
mcp_query_page($sql . ' ORDER BY created DESC, ref DESC', $params, $args);
        case 
'list_reports':
            if (!
checkperm('t')) {
                return 
mcp_error_result('Report access is required.', 'permission_denied');
            }
            require_once 
__DIR__ . '/reporting_functions.php';
            return 
mcp_list_page('rows', array_map(static fn($row) => array_intersect_key(
                
$row,
                
array_flip(['ref', 'name', 'contains_date', 'support_non_correlated_sql'])
            ), 
get_reports()), $args);
        case 
'get_activity_statistics':
            return 
mcp_activity_statistics($args);
        case 
'get_storage_statistics':
            if (!
checkperm('a') || !checkperm('t')) {
                return 
mcp_error_result('Installation-wide reporting permission is required.', 'permission_denied');
            }
            
$group = ($args['group_by'] ?? 'resource_type') === 'resource_type' ? 'resource_type' : 'file_extension';
            
$sql = "SELECT {$group}, COUNT(*) AS resources, SUM(file_size) AS original_bytes, SUM(disk_usage) AS total_bytes,
                COUNT(file_size) AS measured_originals, COUNT(disk_usage) AS measured_totals FROM resource WHERE ref > 0"
;
            
$params = [];
            if (isset(
$args['resource_type'])) {
                
$sql .= ' AND resource_type = ?';
                
$params = ['i', $args['resource_type']];
            }
            return 
mcp_query_page($sql . " GROUP BY {$group} ORDER BY total_bytes DESC, {$group}", $params, $args, ['unit' => 'bytes']);
        case 
'get_licences':
        case 
'get_consent':
            return 
mcp_rights_records($name === 'get_consent', $args);
        case 
'get_external_shares':
            return 
mcp_share_inventory($args);
        case 
'get_resource_annotations':
            require_once 
__DIR__ . '/annotation_functions.php';
            
$row = mcp_visible_resource($args['ref']);
            
$fields = canSeeAnnotationsFields();
            if (!
$fields || !resource_type_applicable_for_annotations($row['resource_type'])) {
                return 
mcp_error_result('Annotations unavailable.', 'feature_unavailable');
            }
            
$fields = array_intersect($fields, [0, ...array_keys(mcp_search_fields((int) $row['resource_type']))]);
            if (
get_resource_access($row) === RESOURCE_ACCESS_RESTRICTED) {
                
$fields = array_filter($fields, static fn($id) => !$id || !get_resource_type_field($id)['hide_when_restricted']);
            }
            if (!
$fields) {
                return 
mcp_list_page('rows', [], $args);
            }
            
$sql = 'SELECT ref, page, resource_type_field, x, y, width, height FROM annotation WHERE resource = ?
                AND resource_type_field IN (' 
. ps_param_insert(count($fields)) . ')
                AND NOT EXISTS (SELECT 1 FROM comment c WHERE c.annotation = annotation.ref AND c.hide = 1)'
;
            
$params = ['i', $args['ref'], ...ps_param_fill($fields, 'i')];
            if (isset(
$args['page'])) {
                
$sql .= ' AND page = ?';
                
array_push($params, 'i', $args['page']);
            }
            return 
mcp_query_page($sql . ' ORDER BY page, ref', $params, $args);
        default:
            throw new 
DomainException('Unknown read operation.');
    }
}

This article was last updated 4th December 2026 17:35 Europe/London time based on the source file dated 4th December 2026 14:10 Europe/London time.