Skip to content

McpServiceProvider File And loadElements function are not found #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 6, 2025

Conversation

tsztodd
Copy link
Contributor

@tsztodd tsztodd commented May 6, 2025

No description provided.

@CodeWithKyrian CodeWithKyrian merged commit 44b8217 into php-mcp:main May 6, 2025
michabbb pushed a commit to michabbb/php-mcp-laravel that referenced this pull request Jun 10, 2025
…uth compliance

## Critical Transport Architecture Fix

This commit resolves two fundamental issues that prevented MCP HTTP transport
from working correctly with traditional PHP servers (PHP-FPM, Apache) while
maintaining compatibility with long-running processes (ReactPHP).

### 🔧 Problem 1: Client Registration Lost Between Requests

**Root Cause:**
Each HTTP request in PHP-FPM/Apache runs in a separate PHP process, causing:
- SSE stream (GET /sse) creates LaravelHttpTransport instance php-mcp#1
- POST requests (/message) create LaravelHttpTransport instance php-mcp#2
- Client registered in instance php-mcp#1 is not available in instance php-mcp#2
- Result: "Client not actively managed by this transport" errors

**Solution:**
Auto-registration in `LaravelHttpTransport::sendToClientAsync()`:
- Automatically emit `client_connected` event for unknown clients
- Ensures clients are active before processing messages
- Maintains backward compatibility with ReactPHP (no-op for already active clients)

### 🔧 Problem 2: OAuth Standard Compliance

**Root Cause:**
Original package used non-standard parameter naming that violates OAuth spec:
- Used: `clientId` (camelCase)
- OAuth Standard: `client_id` (snake_case)
- Real MCP clients (Claude Desktop) send `client_id` parameter

**Solution:**
- Updated SSE endpoint to accept `client_id` query parameter
- Consistent `client_id` usage in all log messages
- Maintains fallback to session ID if no `client_id` provided

### 🔧 Problem 3: Service Provider Architecture

**Root Cause:**
Multiple controller instantiations called `server->listen()` repeatedly, causing:
- Event handlers registered multiple times
- Transport state inconsistencies
- Memory leaks and performance issues

**Solution:**
- Moved `server->listen()` to McpServiceProvider singleton registration
- Removed redundant `server->listen()` calls from controller constructor
- Proper logger injection in service provider

## 🧪 Tested Scenarios

✅ **PHP-FPM/Apache (Separate Processes)**
- Each request gets clean transport instance
- Auto-registration ensures client connectivity
- No shared state issues

✅ **ReactPHP (Long-Running Process)**
- Existing clients remain active
- Auto-registration is no-op for active clients
- No performance impact

✅ **Claude Desktop Integration**
- Recognizes all MCP tools correctly
- Proper `client_id` parameter handling
- SSE stream maintains connection

## 🔍 Technical Details

**Modified Files:**
- `src/Transports/LaravelHttpTransport.php`: Auto-registration logic
- `src/Http/Controllers/McpController.php`: OAuth compliance + service provider cleanup
- `src/McpServiceProvider.php`: Centralized transport initialization

**Key Changes:**
1. Auto-registration in `sendToClientAsync()` when client not found
2. SSE endpoint accepts `client_id` query parameter
3. Service provider handles transport listening lifecycle
4. Consistent `client_id` logging throughout

## 🚀 Impact

**Before:** MCP HTTP transport only worked with ReactPHP
**After:** Works with all PHP server configurations

**Deployment:** Zero breaking changes - existing code continues to work
**Performance:** Minimal overhead (~1 isset() check per message)

This fix enables MCP HTTP transport to work reliably in production PHP
environments while maintaining the existing API and functionality.

Fixes issues with:
- Traditional PHP-FPM deployments
- Apache mod_php configurations
- Docker containers with nginx+php-fpm
- Shared hosting environments
- Any setup where each HTTP request runs in separate PHP process

Co-authored-by: Claude (Anthropic) <claude@anthropic.com>
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.

2 participants