deepseek-1.x-dev/src/Plugin/McpTool/SearchContentTool.php
src/Plugin/McpTool/SearchContentTool.php
<?php
namespace Drupal\deepseek\Plugin\McpTool;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\deepseek\Attribute\McpTool;
use Drupal\deepseek\McpInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* {@inheritdoc}
*/
#[McpTool(
id: "search_content",
name: new TranslatableMarkup("Search Content"),
description: new TranslatableMarkup("Search for content on the article, page Drupal site"),
parameters: [
"query" => [
"type" => "string",
"description" => "Search keywords",
],
"content_type" => [
"type" => "string",
"description" => "Content type (article, page, etc.)",
],
],
required: ['query']
)]
class SearchContentTool implements McpInterface, ContainerFactoryPluginInterface {
/**
* {@inheritDoc}
*/
public function __construct(
protected EntityTypeManagerInterface $entityTypeManager,
) {
}
/**
* {@inheritDoc}
*/
public static function create(ContainerInterface $container, array $configuration = [], $plugin_id = '', $plugin_definition = NULL): static {
return new static(
$container->get('entity_type.manager')
);
}
/**
* {@inheritdoc}
*/
public function execute(array $params): array {
$limit = 10;
$search_text = $params['query'] ?? '';
$content_type = $params['content_type'] ?? NULL;
$query = $this->entityTypeManager->getStorage('node')->getQuery()
->condition('type', $content_type)
->condition('status', 1)
->accessCheck(TRUE)
->range(0, $limit);
if (!empty($search_text)) {
$group = $query->orConditionGroup()
->condition('title', '%' . $search_text . '%', 'LIKE')
->condition('body.value', '%' . $search_text . '%', 'LIKE');
$query->condition($group);
}
$nids = $query->execute();
$nodes = [];
$node_storage = $this->entityTypeManager->getStorage('node');
foreach ($node_storage->loadMultiple($nids) as $node) {
$nodes[] = [
'total' => count($nids),
'title' => $node->getTitle(),
'body' => $node->hasField('body') ? $node->get('body')->value : '',
'created' => date('Y-m-d H:i', $node->getCreatedTime()),
];
}
return [
"total" => count($nids),
'results' => $nodes,
];
}
}
