Add RequestContext and logging (#176)

* Implemented MCP logging specification with auto-injection

* Add RequestContext

* fix tests and style

* Better deprecation

* fix: minor

* Remove ClientAwareInterface

* fix cs

* fix docs

---------

Co-authored-by: Adam Jamiu <jamiuadam120@gmail.com>
This commit is contained in:
Tobias Nyholm 2025-12-16 00:12:02 +01:00 • committed by GitHub
commit a1e0f61b6b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -0,0 +1,92 @@
<?php
/*
* This file is part of the official PHP MCP SDK.
*
* A collaboration between Symfony and the PHP Foundation.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Mcp\Tests\Unit\Capability\Logger;
use Mcp\Capability\Logger\ClientLogger;
use Mcp\Schema\Enum\LoggingLevel;
use Mcp\Server\ClientGateway;
use Mcp\Server\Session\Session;
use PHPUnit\Framework\TestCase;
/**
* Test for simplified ClientLogger PSR-3 compliance.
*/
final class ClientLoggerTest extends TestCase
{
public function testLog()
{
$session = $this->getMockBuilder(Session::class)
->disableOriginalConstructor()
->onlyMethods(['get'])
->getMock();
$session->expects($this->once())->method('get')->willReturn('info');
$clientGateway = $this->getMockBuilder(ClientGateway::class)
->disableOriginalConstructor()
->onlyMethods(['log'])
->getMock();
$clientGateway->expects($this->once())->method('log')->with(LoggingLevel::Notice, 'test');
$logger = new ClientLogger($clientGateway, $session);
$logger->notice('test');
}
public function testLogFilter()
{
$session = $this->getMockBuilder(Session::class)
->disableOriginalConstructor()
->onlyMethods(['get'])
->getMock();
$session->expects($this->once())->method('get')->willReturn('info');
$clientGateway = $this->getMockBuilder(ClientGateway::class)
->disableOriginalConstructor()
->onlyMethods(['log'])
->getMock();
$clientGateway->expects($this->never())->method('log');
$logger = new ClientLogger($clientGateway, $session);
$logger->debug('test');
}
public function testLogFilterSameLevel()
{
$session = $this->getMockBuilder(Session::class)
->disableOriginalConstructor()
->onlyMethods(['get'])
->getMock();
$session->expects($this->once())->method('get')->willReturn('info');
$clientGateway = $this->getMockBuilder(ClientGateway::class)
->disableOriginalConstructor()
->onlyMethods(['log'])
->getMock();
$clientGateway->expects($this->once())->method('log');
$logger = new ClientLogger($clientGateway, $session);
$logger->info('test');
}
public function testLogWithInvalidLevel()
{
$session = $this->getMockBuilder(Session::class)
->disableOriginalConstructor()
->onlyMethods(['get'])
->getMock();
$session->expects($this->any())->method('get')->willReturn('info');
$clientGateway = $this->getMockBuilder(ClientGateway::class)
->disableOriginalConstructor()
->onlyMethods(['log'])
->getMock();
$clientGateway->expects($this->never())->method('log');
$logger = new ClientLogger($clientGateway, $session);
$logger->log('foo', 'test');
}
}