fix: Add authentication support for Dedicated HTTP configuration#44
Open
arifulhoque7 wants to merge 1 commit intophp-mcp:mainfrom
Open
fix: Add authentication support for Dedicated HTTP configuration#44arifulhoque7 wants to merge 1 commit intophp-mcp:mainfrom
arifulhoque7 wants to merge 1 commit intophp-mcp:mainfrom
Conversation
This commit resolves the authentication issue where Auth::user() returns null
in MCP tool handlers when using the Dedicated HTTP transport mode.
## Problem Solved
- HTTP Integrated mode worked fine (uses Laravel's routing + middleware)
- HTTP Dedicated mode failed authentication (bypassed middleware stack)
- Tools couldn't access authenticated user context
## Solution Implemented
- McpAuthenticationMiddleware: Captures auth context from HTTP requests
- McpContext: Thread-safe storage for authentication data
- McpAuth facade: Laravel-style access to auth context in tools
- Enhanced transport controllers with automatic auth middleware
- Updated service provider with proper dependency registration
## Key Features
✅ Works with both HTTP transport modes (Integrated & Dedicated)
✅ Supports all authentication guards (Sanctum, API, Web)
✅ Automatic Bearer token authentication
✅ Zero breaking changes to existing code
✅ Comprehensive test coverage
✅ Proper security with context isolation
## Usage
```php
use PhpMcp\Laravel\Facades\McpAuth;
Mcp::tool('get_me', function () {
// Works in both transport modes
return McpAuth::user() ?? Auth::user();
});
```
## Files Added
- src/Http/Middleware/McpAuthenticationMiddleware.php
- src/Support/McpContext.php
- src/Facades/McpAuth.php
- tests/Feature/AuthenticationTest.php
- examples/AuthenticatedTools.php
## Files Modified
- src/Http/Controllers/*TransportController.php (auth integration)
- src/McpServiceProvider.php (service registration)
- config/mcp.php (default auth middleware)
- samples/basic/routes/mcp.php (usage example)
Fixes authentication issues in Dedicated HTTP configuration while maintaining
full backward compatibility and following Laravel coding standards.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Authentication was not working with the Dedicated HTTP configuration (
mcp:serve --transport=http). TheAuth::user()method returnednullin tool handlers, while HTTP Integrated mode worked fine.Root Cause
The Dedicated HTTP transport runs independently of Laravel's HTTP kernel and middleware stack, so authentication context was not available to MCP tools.
Solution
This PR implements a comprehensive authentication bridge that:
Usage
Testing
Fixes #35