deepseek-1.x-dev/src/Plugin/McpTool/GetNodesTool.php
src/Plugin/McpTool/GetNodesTool.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: "get_nodes",
name: new TranslatableMarkup("Get nodes"),
description: new TranslatableMarkup("Returns a list of type nodes"),
parameters: [
'content_type' => [
'type' => 'string',
'description' => 'Content type (article, page, etc.)',
],
'limit' => [
'type' => 'integer',
'description' => 'limit of nodes return',
],
]
)]
class GetNodesTool 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 = $params['limit'] ?? 10;
$type = $params['type'] ?? 'page';
$nodes = [];
try {
$node_storage = $this->entityTypeManager->getStorage('node');
$query = $node_storage->getQuery()
->condition('type', $type)
->condition('status', 1)
->range(0, $limit)
->accessCheck(TRUE);
$nids = $query->execute();
foreach ($node_storage->loadMultiple($nids) as $node) {
$nodes[] = [
'nid' => $node->id(),
'title' => $node->getTitle(),
'body' => $node->hasField('body') ? $node->get('body')->value : $node->getTitle(),
'created' => date('Y-m-d H:i', $node->getCreatedTime()),
];
}
return [
'total' => count($nids),
'results' => $nodes,
'status' => 'success',
];
}
catch (\Exception $e) {
return [
'status' => 'error',
'error' => [
'code' => 400,
'message' => $e->getMessage(),
],
];
}
}
}
