Files
Leantime/app/Domain/Projects/Tools/DeleteProjectTool.php

62 lines
2.0 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace Leantime\Domain\Projects\Tools;
use Laravel\Mcp\Server\Tool;
use Laravel\Mcp\Server\Tools\ToolInputSchema;
use Laravel\Mcp\Server\Tools\ToolResult;
use Leantime\Domain\Projects\Services\Projects;
/**
* 删除项目(高风险写操作)。
*
* 会删除项目本身 + 所有用户关系。调用方AI应先 getProject 列出项目信息
* 并征得用户明确确认。
*/
class DeleteProjectTool extends Tool
{
public function __construct(
private Projects $projectService,
) {}
public function name(): string
{
return 'deleteProject';
}
public function description(): string
{
return '删除一个项目(高风险操作,不可恢复)。会删除项目及其用户关联。调用前必须先 getProject 列出项目信息并征得用户明确确认。';
}
public function schema(ToolInputSchema $schema): ToolInputSchema
{
return $schema
->integer('id')->description('要删除的项目 ID。')->required()
->boolean('confirmed')->description('用户是否已明确确认删除。必须为 true 才执行。')->required();
}
public function handle(array $arguments): ToolResult
{
$id = (int) ($arguments['id'] ?? 0);
$confirmed = (bool) ($arguments['confirmed'] ?? false);
if (! $confirmed) {
return ToolResult::error('未确认删除。请先 getProject 列出项目信息,征得用户确认后再传 confirmed=true。');
}
$project = $this->projectService->getProject($id);
if (! isset($project['id'])) {
return ToolResult::error("项目不存在或无权访问:{$id}");
}
$name = $project['name'] ?? ('项目 #'.$id);
if ($this->projectService->deleteProject($id)) {
return ToolResult::text("项目已删除:{$name}ID {$id})。");
}
return ToolResult::error('删除失败(可能无权限或项目不存在)。');
}
}