Skip to content

Commit 507e602

Browse files
committed
Move SSE route registration to service provider and remove manual registering of routes
1 parent 3a23d71 commit 507e602

File tree

4 files changed

+185
-57
lines changed

4 files changed

+185
-57
lines changed

README.md

Lines changed: 113 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,121 @@
1-
# Laravel MCP Server
1+
<h1 align="center">Laravel MCP Server by OP.GG</h1>
22

3-
A base Laravel package for building Model Context Protocol (MCP) servers.
3+
<p align="center">
4+
A powerful Laravel package to build a Model Context Protocol Server seamlessly
5+
</p>
6+
7+
<p align="center">
8+
<a href="https://github.com/opgginc/laravel-mcp-server/actions"><img src="https://github.com/opgginc/laravel-mcp-server/actions/workflows/tests.yml/badge.svg" alt="Build Status"></a>
9+
<a href="https://packagist.org/packages/opgginc/laravel-mcp-server"><img src="https://img.shields.io/packagist/dt/opgginc/laravel-mcp-server" alt="Total Downloads"></a>
10+
<a href="https://packagist.org/packages/opgginc/laravel-mcp-server"><img src="https://img.shields.io/packagist/v/opgginc/laravel-mcp-server" alt="Latest Stable Version"></a>
11+
<a href="https://packagist.org/packages/opgginc/laravel-mcp-server"><img src="https://img.shields.io/packagist/l/opgginc/laravel-mcp-server" alt="License"></a>
12+
</p>
13+
14+
<p align="center">
15+
<a href="https://op.gg/open-source/laravel-mcp-server">Official Website</a>
16+
</p>
17+
18+
## Overview
19+
20+
Laravel MCP Server is a powerful package designed to streamline the implementation of Model Context Protocol (MCP) servers in Laravel applications. **Unlike most Laravel MCP packages that use Standard Input/Output (stdio) transport**, this package **utilizes Server-Sent Events (SSE)** transport, providing a more secure and controlled integration method.
21+
22+
### Why SSE instead of STDIO?
23+
24+
While stdio is straightforward and widely used in MCP implementations, it has significant security implications for enterprise environments:
25+
26+
- **Security Risk**: STDIO transport potentially exposes internal system details and API specifications
27+
- **Data Protection**: Organizations need to protect proprietary API endpoints and internal system architecture
28+
- **Control**: SSE offers better control over the communication channel between LLM clients and your application
29+
30+
By implementing the MCP server with SSE transport, enterprises can:
31+
32+
- Expose only the necessary tools and resources while keeping proprietary API details private
33+
- Maintain control over authentication and authorization processes
34+
35+
Key benefits:
36+
37+
- Seamless and rapid implementation of SSE in existing Laravel projects
38+
- Support for the latest Laravel and PHP versions
39+
- Efficient server communication and real-time data processing
40+
- Enhanced security for enterprise environments
41+
42+
## Key Features
43+
44+
- Real-time communication support through Server-Sent Events (SSE) integration
45+
- Implementation of tools and resources compliant with Model Context Protocol specifications
46+
- Adapter-based design architecture with Pub/Sub messaging pattern (starting with Redis, more adapters planned)
47+
- Simple routing and middleware configuration
448

549
## Requirements
650

7-
- PHP ^8.4
8-
- Laravel 10.x, 11.x, 12.x
51+
- PHP >=8.2
52+
- Laravel >=10.x
953

1054
## Installation
1155

12-
```bash
13-
composer require opgginc/laravel-mcp-server
56+
1. Install the package via Composer:
57+
58+
```bash
59+
composer require opgginc/laravel-mcp-server
60+
```
61+
62+
2. Publish the configuration file:
63+
```bash
64+
php artisan vendor:publish --provider="OPGG\LaravelMcpServer\LaravelMcpServerServiceProvider"
65+
```
66+
67+
## Basic Usage
68+
69+
### Adding Custom Tools
70+
71+
To develop your own tools, create a new class and register it in `config/mcp-server.php`:
72+
73+
```php
74+
use OPGG\LaravelMcpServer\Services\ToolService\BaseTool;
75+
76+
class MyCustomTool extends BaseTool
77+
{
78+
// Tool implementation
79+
}
1480
```
81+
82+
## Advanced Features
83+
84+
### Pub/Sub Architecture with SSE Adapters
85+
86+
The package implements a publish/subscribe (pub/sub) messaging pattern through its adapter system:
87+
88+
1. **Publisher (Server)**: When clients send requests to the `/message` endpoint, the server processes these requests and publishes responses through the configured adapter.
89+
90+
2. **Message Broker (Adapter)**: The adapter (e.g., Redis) maintains message queues for each client, identified by unique client IDs. This provides a reliable asynchronous communication layer.
91+
92+
3. **Subscriber (SSE Connection)**: Long-lived SSE connections subscribe to messages for their respective clients and deliver them in real-time.
93+
94+
This architecture enables:
95+
- Scalable real-time communication
96+
- Reliable message delivery even during temporary disconnections
97+
- Efficient handling of multiple concurrent client connections
98+
- Potential for distributed server deployments
99+
100+
### Redis Adapter Configuration
101+
102+
The default Redis adapter can be configured as follows:
103+
104+
```php
105+
'sse_adapter' => 'redis',
106+
'adapters' => [
107+
'redis' => [
108+
'prefix' => 'mcp_sse_', // Prefix for Redis keys
109+
'connection' => 'default', // Redis connection from database.php
110+
'ttl' => 100, // Message TTL in seconds
111+
],
112+
],
113+
```
114+
115+
## Contributing
116+
117+
Bug reports and feature improvement suggestions can be submitted through the GitHub repository. For detailed information on how to contribute, please refer to the CONTRIBUTING.md file.
118+
119+
## License
120+
121+
This project is distributed under the MIT license.

config/mcp-server.php

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,46 @@
44
use OPGG\LaravelMcpServer\Services\ToolService\Examples\VersionCheckTool;
55

66
return [
7+
/*
8+
|--------------------------------------------------------------------------
9+
| Server Information
10+
|--------------------------------------------------------------------------
11+
|
12+
| Configuration for the MCPServer instance. These values are used when
13+
| registering the MCPServer as a singleton in the service container.
14+
|
15+
*/
16+
'server' => [
17+
'name' => 'OP.GG MCP Server',
18+
'version' => '0.1.0',
19+
],
20+
721
/*
822
|--------------------------------------------------------------------------
923
| MCP Server
1024
| Specify the MCP path.
25+
|
26+
| GET /{default_path}/sse
27+
| POST /{default_path}/message (This endpoint requires `sessionId` from `/sse`)
28+
|
1129
|--------------------------------------------------------------------------
1230
*/
1331
'default_path' => 'mcp',
1432

33+
/*
34+
|--------------------------------------------------------------------------
35+
| SSE Route Middleware
36+
|--------------------------------------------------------------------------
37+
|
38+
| Middleware to apply to the SSE route (/{default_path}/sse). Use this to protect
39+
| your SSE endpoint with authentication or other middleware as needed.
40+
| This will only be applied to the SSE endpoint, not to the message endpoint.
41+
|
42+
*/
43+
'middlewares' => [
44+
// 'auth:api'
45+
],
46+
1547
/*
1648
|--------------------------------------------------------------------------
1749
| Server-Sent Events Provider
@@ -32,6 +64,15 @@
3264
| Configuration for the different SSE adapters available in the MCP server.
3365
| Each adapter has its own configuration options.
3466
|
67+
| Adapters function as a pub/sub message broker between clients and the server.
68+
| When a client sends a message to the server endpoint, the server processes it
69+
| and publishes a response through the adapter. SSE connections subscribe to
70+
| these messages and deliver them to the client in real-time.
71+
|
72+
| The Redis adapter uses Redis lists as message queues, with each client having
73+
| its own queue identified by a unique client ID. This enables efficient and
74+
| scalable real-time communication in distributed environments.
75+
|
3576
*/
3677
'sse_adapter' => 'redis',
3778
'adapters' => [
@@ -43,20 +84,6 @@
4384
// Add more adapter configurations as needed
4485
],
4586

46-
/*
47-
|--------------------------------------------------------------------------
48-
| Server Information
49-
|--------------------------------------------------------------------------
50-
|
51-
| Configuration for the MCPServer instance. These values are used when
52-
| registering the MCPServer as a singleton in the service container.
53-
|
54-
*/
55-
'server' => [
56-
'name' => 'Laravel MCP Server',
57-
'version' => '1.0.0',
58-
],
59-
6087
/*
6188
|--------------------------------------------------------------------------
6289
| Tools List

src/LaravelMcpServer.php

Lines changed: 0 additions & 37 deletions
This file was deleted.

src/LaravelMcpServerServiceProvider.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@
22

33
namespace OPGG\LaravelMcpServer;
44

5+
use Illuminate\Support\Facades\Config;
6+
use Illuminate\Support\Facades\Route;
7+
use OPGG\LaravelMcpServer\Http\Controllers\MessageController;
8+
use OPGG\LaravelMcpServer\Http\Controllers\SseController;
59
use OPGG\LaravelMcpServer\Providers\SseServiceProvider;
10+
use OPGG\LaravelMcpServer\Server\MCPServer;
11+
use RuntimeException;
612
use Spatie\LaravelPackageTools\Package;
713
use Spatie\LaravelPackageTools\PackageServiceProvider;
814

@@ -25,4 +31,29 @@ public function register(): void
2531
parent::register();
2632
$this->app->register(SseServiceProvider::class);
2733
}
34+
35+
public function boot(): void
36+
{
37+
parent::boot();
38+
39+
$this->registerRoutes();
40+
}
41+
42+
/**
43+
* Register the routes for the MCP Server
44+
*/
45+
protected function registerRoutes(): void
46+
{
47+
if (!app()->has(MCPServer::class)) {
48+
return;
49+
}
50+
51+
$path = Config::get('mcp-server.default_path');
52+
$middlewares = Config::get('mcp-server.middlewares', []);
53+
54+
Route::get("{$path}/sse", [SseController::class, 'handle'])
55+
->middleware($middlewares);
56+
57+
Route::post("{$path}/message", [MessageController::class, 'handle']);
58+
}
2859
}

0 commit comments

Comments
 (0)