Additive-only — no existing files modified. Services (php/Mcp/Services/): - CircuitBreaker (3-state, Cache::add trial lock) - DataRedactor (28 sensitive + 16 PII keys, partial-redact algorithm) - McpHealthService (YAML registry + JSON-RPC stdio ping protocolVersion 2024-11-05) - McpMetricsService (p50/p95/p99 linear interpolation) - McpWebhookDispatcher (mcp.tool.executed → WebhookEndpoints) - OpenApiGenerator (OpenAPI 3.0.3) - ToolRateLimiter (Cache::put first, Cache::increment after — no reset) - AgentSessionService (php/Mod/Mcp/Services/ namespace per spec) Transport (php/Mcp/Transport/): - McpContext (transport-agnostic callbacks) - Contracts/McpToolHandler interface Resources (php/Mcp/Resources/): - AppConfig, ContentResource, DatabaseSchema Config: php/resources/mcp/registry.yaml. Pest Feature tests _Good/_Bad/_Ugly per AX-10 for each new class. Co-authored-by: Codex <noreply@openai.com> Closes tasks.lthn.sh/view.php?id=842
54 lines
2 KiB
PHP
54 lines
2 KiB
PHP
<?php
|
|
|
|
// SPDX-License-Identifier: EUPL-1.2
|
|
|
|
declare(strict_types=1);
|
|
|
|
require_once dirname(__DIR__).'/Support/bootstrap.php';
|
|
|
|
mcpRequire('Mcp/Services/ToolRateLimiter.php');
|
|
|
|
use Core\Mcp\Services\ToolRateLimiter;
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
beforeEach(function (): void {
|
|
Cache::flush();
|
|
config()->set('mcp.rate_limiting.enabled', true);
|
|
config()->set('mcp.rate_limiting.decay_seconds', 60);
|
|
config()->set('mcp.rate_limiting.calls_per_minute', 2);
|
|
config()->set('mcp.rate_limiting.per_tool', ['send_email' => 1]);
|
|
});
|
|
|
|
test('ToolRateLimiter_check_Good_reports_remaining_calls_before_the_limit_is_hit', function (): void {
|
|
$limiter = new ToolRateLimiter;
|
|
|
|
$status = $limiter->check('sess-1', 'list_posts');
|
|
$limiter->hit('sess-1', 'list_posts');
|
|
$afterHit = $limiter->getStatus('sess-1', 'list_posts');
|
|
|
|
expect($status['limited'])->toBeFalse()
|
|
->and($status['remaining'])->toBe(1)
|
|
->and($afterHit['remaining'])->toBe(1);
|
|
});
|
|
|
|
test('ToolRateLimiter_check_Bad_applies_tool_specific_limits_and_returns_retry_after_when_limited', function (): void {
|
|
$limiter = new ToolRateLimiter;
|
|
|
|
$limiter->hit('sess-2', 'send_email');
|
|
$result = $limiter->check('sess-2', 'send_email');
|
|
|
|
expect($result['limited'])->toBeTrue()
|
|
->and($result['remaining'])->toBe(0)
|
|
->and($result['retry_after'])->toBeInt();
|
|
});
|
|
|
|
test('ToolRateLimiter_hit_Ugly_uses_put_for_the_first_call_and_increment_for_subsequent_calls', function (): void {
|
|
Cache::shouldReceive('get')->once()->with('mcp_rate_limit:sess-3:list_posts', 0)->andReturn(0);
|
|
Cache::shouldReceive('put')->once()->with('mcp_rate_limit:sess-3:list_posts', 1, 60);
|
|
Cache::shouldReceive('get')->once()->with('mcp_rate_limit:sess-3:list_posts', 0)->andReturn(1);
|
|
Cache::shouldReceive('increment')->once()->with('mcp_rate_limit:sess-3:list_posts');
|
|
|
|
$limiter = new ToolRateLimiter;
|
|
$limiter->hit('sess-3', 'list_posts');
|
|
$limiter->hit('sess-3', 'list_posts');
|
|
});
|