mcp icon indicating copy to clipboard operation
mcp copied to clipboard

Add Logging Support

Open pushpak1300 opened this issue 1 month ago • 0 comments

This PR adds support for the MCP logging utility. sign logging utility Server can send client a log in structured way.

How It Works

  1. Default level: Info (filters out Debug by default)
  2. Client control: Clients call logging/setLevel to change verbosity
  3. Automatic filtering: Server only sends logs matching severity threshold
  4. Session isolation: Each connection maintains independent log level
  5. 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

pushpak1300 avatar Nov 27 '25 13:11 pushpak1300