mcp
mcp copied to clipboard
Add Logging Support
This PR adds support for the MCP logging utility. sign logging utility Server can send client a log in structured way.
How It Works
- Default level: Info (filters out Debug by default)
- Client control: Clients call
logging/setLevelto change verbosity - Automatic filtering: Server only sends logs matching severity threshold
- Session isolation: Each connection maintains independent log level
- Cache-backed storage: Uses Laravel cache for session state (compatible with both STDIO and HTTP transports, unlike Laravel sessions which don't work with STDIO)
Usage
Basic Logging
use Laravel\Mcp\Enums\LogLevel;
class DatabaseTool extends Tool
{
public function handle(Request $request): Generator
{
yield Response::log(LogLevel::INFO, 'Connecting to database');
try {
$result = DB::query(...);
yield Response::log(LogLevel::DEBUG, ['query' => $sql, 'rows' => $result->count()]);
yield Response::text("Found {$result->count()} records");
} catch (\Exception $e) {
yield Response::log(LogLevel::ERROR, ['error' => $e->getMessage()], 'database');
yield Response::error('Query failed');
}
}
}
Testing Assertion
MyServer::tool(DatabaseTool::class)
->assertLogSent(LogLevel::INFO, 'Connecting')
->assertLogSent(LogLevel::ERROR)
->assertLogCount(2);
Configuration
// config/mcp.php
'session_ttl' => env('MCP_SESSION_TTL', 86400), // 24 hours