Skip to content

Conversation

@pushpak1300
Copy link
Member

@pushpak1300 pushpak1300 commented Nov 27, 2025

This PR adds support for the MCP logging utility, allowing servers to send structured log messages to clients.

Logging is opt in. MCP servers will not enable this by default. To turn it on, the user must explicitly add the capability in their MCP server class:

protected function boot(): void
{
    $this->addCapability(self::CAPABILITY_LOGGING);
}

Once enabled, client can send logging/setLevel request. User can log things but filtering won't happen without this capability.

How It Works

  1. Default level: Info (Debug is filtered out unless enabled)
  2. Client control: Clients call logging/setLevel to adjust verbosity
  3. Automatic filtering: Server only emits logs at or above the current threshold
  4. Session isolation: Each connection maintains its own log level
  5. Cache-backed state: Session data is stored via Laravel cache, which works for both STDIO and HTTP transports (unlike Laravel sessions)

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

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
@pushpak1300 pushpak1300 marked this pull request as ready for review December 3, 2025 13:18
Copy link
Collaborator

@joetannenbaum joetannenbaum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good overall, just the one nit comment about the cases as opposed to hardcoding, I'll let you make the call there.

Is this the first time we're doing something like $this->addCapability('logging'); as a public facing user API? Is that string essentially arbitrary or are there pre-defined use cases? Either way, for built-in capabilities, do we want to avoid the magic string and provide a helper method such as enableLogging or something similar?

@pushpak1300
Copy link
Member Author

Is this the first time we're doing something like $this->addCapability('logging'); as a public facing user API? Is that string essentially arbitrary or are there pre-defined use cases? Either way, for built-in capabilities, do we want to avoid the magic string and provide a helper method such as enableLogging or something similar?

Yes, this is the first time we’re exposing $this->addCapability('logging') in the public API. The string is predefined, it comes directly from the MCP spec. I agree we should avoid raw strings. Adding a constant makes sense, but I don’t think a helper like enableLogging is necessary since the capability API already exists.

And keeping this opt-in feels right, since most MCP users won’t need logging unless they know exactly what they’re doing.

@taylorotwell taylorotwell marked this pull request as draft December 7, 2025 15:55
@taylorotwell
Copy link
Member

Kinda want to wait for more demand for this since it's a beefy PR to maintain.

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

4 participants