Skip to content

Conversation

@pushpak1300
Copy link
Member

@pushpak1300 pushpak1300 commented Nov 27, 2025

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 pushpak1300 linked an issue Dec 1, 2025 that may be closed by this pull request
@pushpak1300 pushpak1300 force-pushed the support_for_logging branch 2 times, most recently from e2b375c to 232f8c8 Compare December 1, 2025 15:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add Support For Logging

2 participants