ability = $ability; $this->name = $name; $this->title = $title; $this->description = $description; $this->input_schema = $input_schema; $this->output_schema = $output_schema; $this->annotations = $annotations; $this->metadata = $metadata; } /** * Get the ability name. * * @return \WP_Ability|\WP_Error WP_Ability instance on success, WP_Error on failure. */ public function get_ability() { $ability = wp_get_ability( $this->ability ); if ( ! $ability ) { return new \WP_Error( 'ability_not_found', sprintf( /* translators: %s: ability name */ esc_html__( "WordPress ability '%s' does not exist.", 'mcp-adapter' ), esc_html( $this->ability ) ) ); } return $ability; } /** * Get the tool name. * * @return string */ public function get_name(): string { return $this->name; } /** * Get the tool title. * * @return string|null */ public function get_title(): ?string { return $this->title; } /** * Get the tool description. * * @return string */ public function get_description(): string { return $this->description; } /** * Get the input schema. * * @return array */ public function get_input_schema(): array { return $this->input_schema; } /** * Get the output schema. * * @return array|null */ public function get_output_schema(): ?array { return $this->output_schema; } /** * Get the annotations. * * @return array */ public function get_annotations(): array { return $this->annotations; } /** * Get internal metadata for server-side processing. * * @return array */ public function get_metadata(): array { return $this->metadata; } /** * Set the tool title. * * @param string|null $title The title to set. * * @return void */ public function set_title( ?string $title ): void { $this->title = $title; } /** * Set the tool description. * * @param string $description The description to set. * * @return void */ public function set_description( string $description ): void { $this->description = $description; } /** * Set the input schema. * * @param array $input_schema The input schema to set. * * @return void */ public function set_input_schema( array $input_schema ): void { $this->input_schema = $input_schema; } /** * Set the output schema. * * @param array|null $output_schema The output schema to set. * * @return void */ public function set_output_schema( ?array $output_schema ): void { $this->output_schema = $output_schema; } /** * Set the annotations. * * @param array $annotations The annotations to set. * * @return void */ public function set_annotations( array $annotations ): void { $this->annotations = $annotations; } /** * Set internal metadata. * * @param array $metadata Internal metadata values. * * @return void */ public function set_metadata( array $metadata ): void { $this->metadata = $metadata; } /** * Add an annotation. * * @param string $key The annotation key. * @param mixed $value The annotation value. * * @return void */ public function add_annotation( string $key, $value ): void { $this->annotations[ $key ] = $value; } /** * Remove an annotation. * * @param string $key The annotation key to remove. * * @return void */ public function remove_annotation( string $key ): void { unset( $this->annotations[ $key ] ); } /** * Get the MCP server instance this tool belongs to. * * @return \GoDaddy\WordPress\Plugins\AiroWp\Dependencies\WP\MCP\Core\McpServer */ public function get_mcp_server(): McpServer { if ( null === $this->mcp_server ) { throw new \RuntimeException( 'MCP server has not been set on this tool instance.' ); } return $this->mcp_server; } /** * Set the MCP server instance this tool belongs to. * * @param \GoDaddy\WordPress\Plugins\AiroWp\Dependencies\WP\MCP\Core\McpServer $mcp_server The MCP server instance. * * @return void */ public function set_mcp_server( McpServer $mcp_server ): void { $this->mcp_server = $mcp_server; } /** * Convert the tool to an array representation according to MCP specification. * * @return array */ public function to_array(): array { $input_schema_for_json = empty( $this->input_schema ) ? array( 'type' => 'object' ) : $this->input_schema; $tool_data = array( 'name' => $this->name, 'description' => $this->description, 'inputSchema' => $input_schema_for_json, ); if ( ! is_null( $this->title ) ) { $tool_data['title'] = $this->title; } if ( ! is_null( $this->output_schema ) ) { $tool_data['outputSchema'] = $this->output_schema; } if ( ! empty( $this->annotations ) ) { $tool_data['annotations'] = $this->annotations; } return $tool_data; } /** * Create an McpTool instance from an array. * * @param array $data Array containing tool data. * @param \GoDaddy\WordPress\Plugins\AiroWp\Dependencies\WP\MCP\Core\McpServer $mcp_server The MCP server instance. * * @return self|\WP_Error Returns a new McpTool instance or WP_Error if validation fails. */ public static function from_array( array $data, McpServer $mcp_server ) { $tool = new self( $data['ability'] ?? '', $data['name'] ?? '', $data['description'] ?? '', $data['inputSchema'] ?? array(), $data['title'] ?? null, $data['outputSchema'] ?? null, $data['annotations'] ?? array(), $data['_metadata'] ?? array() ); $tool->set_mcp_server( $mcp_server ); return $tool->validate( "McpTool::from_array::{$data['name']}" ); } /** * Validate the tool according to MCP specification requirements. * Uses the centralized McpToolValidator for consistent validation. * * @param string $context Optional context for error messages. * * @return self|\WP_Error Returns the validated tool instance or WP_Error if validation fails. */ public function validate( string $context = '' ) { if ( null === $this->mcp_server ) { return new \WP_Error( 'tool_missing_mcp_server', esc_html__( 'MCP server must be set before validating a tool.', 'mcp-adapter' ) ); } if ( ! $this->mcp_server->is_mcp_validation_enabled() ) { return $this; } $context_to_use = $context ?: "McpTool::{$this->name}"; $validation_result = McpToolValidator::validate_tool_instance( $this, $context_to_use ); if ( is_wp_error( $validation_result ) ) { return $validation_result; } return $this; } }