*/ private $tools = array(); /** * Register a tool. An invalid definition is logged and skipped rather than thrown: one bad * registration must not take the whole endpoint (initialize, tools/list) down with it. * * @return bool Whether the tool was registered. */ public function register( $name, array $def ) { if ( ! is_string( $name ) || ! preg_match( '/^[a-z][a-z0-9_]{0,63}\z/', $name ) ) { Log::line( 'skipped tool with invalid name ' . wp_json_encode( $name ) ); return false; } if ( empty( $def['handler'] ) || ! is_callable( $def['handler'] ) ) { Log::line( "skipped tool $name (no callable handler)" ); return false; } if ( isset( $this->tools[ $name ] ) ) { Log::line( "skipped duplicate registration of tool $name (the first one stays)" ); return false; } if ( isset( $def['annotations'] ) && ! is_array( $def['annotations'] ) ) { Log::line( "skipped tool $name (annotations must be an array)" ); return false; } $read_only = ! empty( $def['read_only'] ); if ( ! $read_only && ! self::adi_gates( $name ) ) { // ADI would run this without asking the user: a write must never look like a read. Log::line( "skipped tool $name (a write tool needs a write verb in its name so ADI asks for approval)" ); return false; } $schema = isset( $def['input_schema'] ) ? $def['input_schema'] : array( 'type' => 'object' ); if ( ! is_array( $schema ) || ! isset( $schema['type'] ) || 'object' !== $schema['type'] ) { Log::line( "skipped tool $name (input_schema must be an array with type 'object')" ); return false; } // An empty PHP array serialises as [] — MCP clients need an object here. if ( empty( $schema['properties'] ) ) { $schema['properties'] = new \stdClass(); } $this->tools[ $name ] = array( 'name' => $name, 'description' => isset( $def['description'] ) ? (string) $def['description'] : '', 'inputSchema' => $schema, // read_only is authoritative: caller annotations can add hints but never flip these two. 'annotations' => array_merge( array( 'openWorldHint' => false ), isset( $def['annotations'] ) ? $def['annotations'] : array(), array( 'readOnlyHint' => $read_only, 'destructiveHint' => ! $read_only, ) ), 'handler' => $def['handler'], ); return true; } /** * Would ADI Desktop ask the user before running a tool with this name? Mirrors * is_mutating_tool(): any write-verb segment gates; otherwise a read verb or read prefix * passes ungated; no recognisable verb fails closed (gated). */ public static function adi_gates( $name ) { $write = array( 'create', 'update', 'delete', 'remove', 'set', 'send', 'submit', 'write', 'add', 'post', 'put', 'move', 'rename', 'install', 'exec', 'execute', 'run', 'start', 'stop', 'cancel', 'approve', 'pay', 'transfer', 'upload', 'publish', 'replace', 'restore' ); $read = array( 'get', 'list', 'read', 'search', 'query', 'fetch', 'status', 'show', 'describe', 'count', 'check', 'view' ); $segs = explode( '_', $name ); if ( array_intersect( $segs, $write ) ) { return true; } foreach ( array( 'get_', 'list_', 'read_', 'check_', 'search_', 'query_', 'fetch_' ) as $prefix ) { if ( 0 === strpos( $name, $prefix ) ) { return false; } } return ! array_intersect( $segs, $read ); } public function has( $name ) { return isset( $this->tools[ $name ] ); } public function get( $name ) { return isset( $this->tools[ $name ] ) ? $this->tools[ $name ] : null; } /** The `tools/list` payload: every tool without its handler. */ public function list_for_mcp() { $out = array(); foreach ( $this->tools as $tool ) { unset( $tool['handler'] ); $out[] = $tool; } return $out; } /** * Validate arguments against the small JSON Schema subset our tools use: type, required, * properties, enum, items, minimum/maximum, additionalProperties:false. Returns an error * message, or null when the arguments are valid. */ public static function validate( $value, $schema, $path = 'arguments' ) { $schema = (array) $schema; if ( isset( $schema['type'] ) && ! self::type_matches( $value, $schema['type'] ) ) { $want = is_array( $schema['type'] ) ? implode( '|', $schema['type'] ) : $schema['type']; return "$path must be $want"; } if ( isset( $schema['enum'] ) && ! in_array( $value, $schema['enum'], true ) ) { return "$path must be one of: " . implode( ', ', array_map( 'json_encode', $schema['enum'] ) ); } if ( is_int( $value ) || is_float( $value ) ) { if ( isset( $schema['minimum'] ) && $value < $schema['minimum'] ) { return "$path must be >= {$schema['minimum']}"; } if ( isset( $schema['maximum'] ) && $value > $schema['maximum'] ) { return "$path must be <= {$schema['maximum']}"; } } if ( is_array( $value ) && self::is_assoc_or_empty_object( $value, $schema ) ) { foreach ( isset( $schema['required'] ) ? $schema['required'] : array() as $key ) { if ( ! array_key_exists( $key, $value ) ) { return "$path.$key is required"; } } $props = isset( $schema['properties'] ) ? (array) $schema['properties'] : array(); foreach ( $value as $key => $item ) { if ( isset( $props[ $key ] ) ) { $err = self::validate( $item, $props[ $key ], "$path.$key" ); if ( $err ) { return $err; } } elseif ( isset( $schema['additionalProperties'] ) && false === $schema['additionalProperties'] ) { return "$path.$key is not a known argument"; } } } elseif ( is_array( $value ) && isset( $schema['items'] ) ) { foreach ( $value as $i => $item ) { $err = self::validate( $item, $schema['items'], "{$path}[$i]" ); if ( $err ) { return $err; } } } return null; } private static function is_assoc_or_empty_object( array $value, array $schema ) { if ( array() === $value ) { return isset( $schema['type'] ) && 'object' === $schema['type']; } return array_keys( $value ) !== range( 0, count( $value ) - 1 ); } private static function type_matches( $value, $type ) { if ( is_array( $type ) ) { foreach ( $type as $t ) { if ( self::type_matches( $value, $t ) ) { return true; } } return false; } switch ( $type ) { case 'object': return is_array( $value ) && ( array() === $value || array_keys( $value ) !== range( 0, count( $value ) - 1 ) ); case 'array': return is_array( $value ) && ( array() === $value || array_keys( $value ) === range( 0, count( $value ) - 1 ) ); case 'string': return is_string( $value ); case 'integer': return is_int( $value ); case 'number': return is_int( $value ) || is_float( $value ); case 'boolean': return is_bool( $value ); case 'null': return null === $value; } return true; } }