php-agentic/Mcp/Tools/Agent/Session/SessionEnd.php

79 lines
1.9 KiB
PHP
Raw Normal View History

2026-01-27 00:28:29 +00:00
<?php
declare(strict_types=1);
namespace Core\Agentic\Mcp\Tools\Agent\Session;
use Core\Agentic\Models\AgentSession;
use Core\Agentic\Mcp\Tools\Agent\AgentTool;
/**
* End the current session.
*/
class SessionEnd extends AgentTool
{
protected string $category = 'session';
protected array $scopes = ['write'];
public function name(): string
{
return 'session_end';
}
public function description(): string
{
return 'End the current session';
}
public function inputSchema(): array
{
return [
'type' => 'object',
'properties' => [
'status' => [
'type' => 'string',
'description' => 'Final session status',
'enum' => ['completed', 'handed_off', 'paused', 'failed'],
],
'summary' => [
'type' => 'string',
'description' => 'Final summary',
],
],
'required' => ['status'],
];
}
public function handle(array $args, array $context = []): array
{
try {
$status = $this->require($args, 'status');
} catch (\InvalidArgumentException $e) {
return $this->error($e->getMessage());
}
$sessionId = $context['session_id'] ?? null;
if (! $sessionId) {
return $this->error('No active session');
}
$session = AgentSession::where('session_id', $sessionId)->first();
if (! $session) {
return $this->error('Session not found');
}
$session->end($status, $this->optional($args, 'summary'));
return $this->success([
'session' => [
'session_id' => $session->session_id,
'status' => $session->status,
'duration' => $session->getDurationFormatted(),
],
]);
}
}