PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.6.4
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.6.4
1.6.4 1.6.3 1.6.2 1.6.1 1.6.0 1.5.4 1.5.5 1.5.3 1.5.2 1.5.1 1.5.0 1.4.2 1.4.1 1.4.0 1.3.28 1.3.27 1.3.26 1.3.25 1.3.23 1.3.22 1.3.21 1.3.20 1.3.19 trunk 1.2.0 All 47 releases
fluent-cart / app / Modules / MCP / Tools / SearchTools.php

SearchTools.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.6.4, at app/Modules/MCP/Tools/SearchTools.php

124 lines 5.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentCart\App\Modules\MCP\Tools;
4
5 use FluentCart\App\App;
6 use FluentCart\App\Modules\MCP\Support\AdvancedSearch;
7 use FluentCart\App\Modules\MCP\Support\MCPHelper;
8 use FluentCart\App\Modules\MCP\Support\PermissionGate;
9 use FluentCart\Framework\Support\Arr;
10
11 /**
12 * Advanced-search discovery.
13 *
14 * The admin UI's advanced filter catalogs are large (30+ properties across
15 * entities, each with operators, options and value formats) — inlining them
16 * into every list tool's input_schema would bloat every session's context.
17 * Instead the list tools carry ONE lean advanced_filters parameter, and this
18 * tool serves the full per-entity reference on demand: the agent fetches the
19 * schema for the one entity it is about to search, exactly like the existing
20 * get-store-context → list-reference-data progressive-disclosure split.
21 */
22 class SearchTools
23 {
24 public static function definitions()
25 {
26 $entities = AdvancedSearch::entityNames();
27
28 return [
29 'fluent-cart/get-search-schema' => [
30 'label' => __('Get Advanced Search Schema', 'fluent-cart'),
31 'description' => sprintf(
32 /* translators: %1$s: searchable entity names */
33 __('The reference for the advanced_filters parameter — the same condition-group search the admin "advanced filter" UI offers. Returns every filterable property of one entity (operators, value types, enum options), the payload format, and a worked example. Call this once before passing advanced_filters to a list tool. Entities: %1$s. Requires FluentCart Pro.', 'fluent-cart'),
34 implode(', ', $entities)
35 ),
36 'input_schema' => [
37 'type' => 'object',
38 'properties' => [
39 'entity' => [
40 'type' => 'string',
41 'enum' => $entities,
42 'description' => 'The entity whose search schema to return.',
43 ],
44 ],
45 'required' => ['entity'],
46 ],
47 'execute_callback' => [self::class, 'getSearchSchema'],
48 'permission_callback' => function () {
49 return PermissionGate::canAny(PermissionGate::readRoleCaps());
50 },
51 'annotations' => ['readonly' => true],
52 ],
53 ];
54 }
55
56 public static function getSearchSchema($params = [])
57 {
58 $entity = (isset($params['entity']) && is_scalar($params['entity'])) ? (string) $params['entity'] : '';
59 $entities = AdvancedSearch::entities();
60
61 if (!isset($entities[$entity])) {
62 return MCPHelper::error(
63 'unknown_entity',
64 sprintf(
65 /* translators: 1: entity name, 2: valid entity names */
66 __('Unknown entity "%1$s". Valid entities: %2$s.', 'fluent-cart'),
67 $entity,
68 implode(', ', array_keys($entities))
69 ),
70 ['entities' => array_keys($entities)]
71 );
72 }
73
74 // Schema visibility follows data visibility: if the caller can't read
75 // the entity's rows, don't hand it the entity's search surface either.
76 $permission = Arr::get($entities[$entity], 'permission');
77 if ($permission && !PermissionGate::can($permission)) {
78 return MCPHelper::error(
79 'forbidden',
80 sprintf(
81 /* translators: 1: entity name, 2: required permission */
82 __('Your role cannot read %1$s (requires %2$s).', 'fluent-cart'),
83 $entity,
84 $permission
85 ),
86 ['required_permission' => $permission]
87 );
88 }
89
90 // Fail here, not after the agent has built a filter that list tools
91 // will reject anyway.
92 if (!App::isProActive()) {
93 return MCPHelper::error(
94 'pro_required',
95 __('Advanced search requires FluentCart Pro. The named filters on the list tools remain available.', 'fluent-cart'),
96 ['entity' => $entity]
97 );
98 }
99
100 $schema = AdvancedSearch::schemaFor($entity);
101 if (is_wp_error($schema)) {
102 return $schema;
103 }
104
105 $count = isset($schema['properties']) ? count($schema['properties']) : 0;
106
107 return MCPHelper::envelope(
108 sprintf(
109 /* translators: 1: number of filterable properties, 2: entity name, 3: list tool name */
110 _n(
111 '%1$d filterable property for %2$s — pass conditions via advanced_filters on %3$s.',
112 '%1$d filterable properties for %2$s — pass conditions via advanced_filters on %3$s.',
113 $count,
114 'fluent-cart'
115 ),
116 $count,
117 $entity,
118 Arr::get($entities[$entity], 'list_tool', 'the list tool')
119 ),
120 $schema
121 );
122 }
123 }
124