Skip to content

Commit 1171aca

Browse files
feat: implement Laravel-native session handlers
- Add FileSessionHandler with directory auto-creation and proper gc() - Add DatabaseSessionHandler with base64 encoding and session tracking - Update McpServiceProvider to construct handlers directly instead of using server builder - Add session configuration options for file path and database settings - Include database migration for mcp_sessions table - Support all Laravel session drivers: array, file, cache, database, redis, memcached, dynamodb
1 parent 9f99a94 commit 1171aca

File tree

7 files changed

+451
-63
lines changed

7 files changed

+451
-63
lines changed

config/mcp.php

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -53,25 +53,25 @@
5353
| MCP Cache Configuration
5454
|--------------------------------------------------------------------------
5555
|
56-
| Configure how the MCP server caches discovered elements and transport
57-
| state using Laravel's cache system. You can specify which store to use
58-
| and how long items should be cached.
56+
| Configure how the MCP server caches discovered elements using Laravel's cache system.
57+
| You can specify which store to use and how long items should be cached.
5958
|
6059
*/
6160
'cache' => [
6261
'store' => env('MCP_CACHE_STORE', config('cache.default')),
63-
'ttl' => env('MCP_CACHE_TTL', 3600),
6462
],
6563

6664
/*
6765
|--------------------------------------------------------------------------
6866
| MCP Transport Configuration
6967
|--------------------------------------------------------------------------
7068
|
71-
| Configure the available transports for MCP communication. Three types are
72-
| supported: stdio for CLI clients, http_dedicated for a standalone server,
73-
| and http_integrated for serving through Laravel's routing system.
69+
| Configure the available transports for MCP communication.
7470
|
71+
| Supported Transports:
72+
| - `stdio`: for CLI clients.
73+
| - `http_dedicated`: for a standalone server running on a process.
74+
| - `http_integrated`: for serving through Laravel's routing system.
7575
*/
7676
'transports' => [
7777
'stdio' => [
@@ -86,20 +86,19 @@
8686
'path_prefix' => env('MCP_HTTP_DEDICATED_PATH_PREFIX', 'mcp'),
8787
'ssl_context_options' => [],
8888
'enable_json_response' => (bool) env('MCP_HTTP_DEDICATED_JSON_RESPONSE', true),
89-
'event_store' => env('MCP_HTTP_DEDICATED_EVENT_STORE'), // FQCN or null
89+
'event_store' => null, // FQCN or null
9090
],
9191

9292
'http_integrated' => [
9393
'enabled' => (bool) env('MCP_HTTP_INTEGRATED_ENABLED', true),
9494
'legacy' => (bool) env('MCP_HTTP_INTEGRATED_LEGACY', false),
9595
'route_prefix' => env('MCP_HTTP_INTEGRATED_ROUTE_PREFIX', 'mcp'),
96-
'middleware' => explode(',', env('MCP_HTTP_INTEGRATED_MIDDLEWARE', 'web')),
96+
'middleware' => explode(',', env('MCP_HTTP_INTEGRATED_MIDDLEWARE', 'api')),
9797
'domain' => env('MCP_HTTP_INTEGRATED_DOMAIN'),
9898
'sse_poll_interval' => (int) env('MCP_HTTP_INTEGRATED_SSE_POLL_SECONDS', 1),
9999
'cors_origin' => env('MCP_HTTP_INTEGRATED_CORS_ORIGIN', '*'),
100100
'enable_json_response' => (bool) env('MCP_HTTP_INTEGRATED_JSON_RESPONSE', true),
101-
'json_response_timeout' => (int) env('MCP_HTTP_INTEGRATED_JSON_TIMEOUT', 30),
102-
'event_store' => env('MCP_HTTP_INTEGRATED_EVENT_STORE'), // FQCN or null
101+
'event_store' => null, // FQCN or null
103102
],
104103
],
105104

@@ -109,13 +108,26 @@
109108
|--------------------------------------------------------------------------
110109
|
111110
| Configure how the MCP server manages client sessions. Sessions store
112-
| client state, message queues, and subscriptions.
111+
| client state, message queues, and subscriptions. Supports Laravel's
112+
| native session drivers for seamless integration.
113113
|
114114
*/
115115
'session' => [
116-
'driver' => env('MCP_SESSION_DRIVER', 'cache'), // 'array' or 'cache'
117-
'ttl' => (int) env('MCP_SESSION_TTL', 3600), // Session lifetime in seconds
118-
'lottery' => [2, 100], // 2% chance of garbage collection
116+
'driver' => env('MCP_SESSION_DRIVER', 'cache'), // 'file', 'cache', 'database', 'redis', 'memcached', 'dynamodb', 'array'
117+
'ttl' => (int) env('MCP_SESSION_TTL', 3600),
118+
119+
// For cache-based drivers (redis, memcached, etc.)
120+
'store' => env('MCP_SESSION_CACHE_STORE', config('cache.default')),
121+
122+
// For file driver
123+
'path' => env('MCP_SESSION_FILE_PATH', storage_path('framework/mcp_sessions')),
124+
125+
// For database driver
126+
'connection' => env('MCP_SESSION_DB_CONNECTION', config('database.default')),
127+
'table' => env('MCP_SESSION_DB_TABLE', 'mcp_sessions'),
128+
129+
// Session garbage collection probability. 2% chance that garbage collection will run on any given session operation.
130+
'lottery' => [2, 100],
119131
],
120132

121133
/*
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*/
12+
public function up(): void
13+
{
14+
Schema::create('mcp_sessions', function (Blueprint $table) {
15+
$table->string('id')->primary();
16+
$table->longText('payload');
17+
$table->integer('last_activity')->index();
18+
});
19+
}
20+
21+
/**
22+
* Reverse the migrations.
23+
*/
24+
public function down(): void
25+
{
26+
Schema::dropIfExists('mcp_sessions');
27+
}
28+
};

samples/basic/config/mcp.php

Lines changed: 52 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@
2929
*/
3030
'discovery' => [
3131
'base_path' => base_path(),
32-
'directories' => [
33-
env('MCP_DISCOVERY_PATH', 'app/Mcp'),
34-
],
32+
'directories' => array_filter(explode(',', env('MCP_DISCOVERY_DIRECTORIES', 'app/Mcp'))),
3533
'exclude_dirs' => [
3634
'vendor',
3735
'tests',
@@ -46,38 +44,38 @@
4644
'.git',
4745
],
4846
'definitions_file' => base_path('routes/mcp.php'),
49-
'auto_discover' => env('MCP_AUTO_DISCOVER', true),
50-
'save_to_cache' => env('MCP_DISCOVERY_SAVE_TO_CACHE', true),
47+
'auto_discover' => (bool) env('MCP_AUTO_DISCOVER', true),
48+
'save_to_cache' => (bool) env('MCP_DISCOVERY_SAVE_TO_CACHE', true),
5149
],
5250

5351
/*
5452
|--------------------------------------------------------------------------
5553
| MCP Cache Configuration
5654
|--------------------------------------------------------------------------
5755
|
58-
| Configure how the MCP server caches discovered elements and transport
59-
| state using Laravel's cache system. You can specify which store to use
60-
| and how long items should be cached.
56+
| Configure how the MCP server caches discovered elements using Laravel's cache system.
57+
| You can specify which store to use and how long items should be cached.
6158
|
6259
*/
6360
'cache' => [
6461
'store' => env('MCP_CACHE_STORE', config('cache.default')),
65-
'ttl' => env('MCP_CACHE_TTL', 3600),
6662
],
6763

6864
/*
6965
|--------------------------------------------------------------------------
7066
| MCP Transport Configuration
7167
|--------------------------------------------------------------------------
7268
|
73-
| Configure the available transports for MCP communication. Three types are
74-
| supported: stdio for CLI clients, http_dedicated for a standalone server,
75-
| and http_integrated for serving through Laravel's routing system.
69+
| Configure the available transports for MCP communication.
7670
|
71+
| Supported Transports:
72+
| - `stdio`: for CLI clients.
73+
| - `http_dedicated`: for a standalone server running on a process.
74+
| - `http_integrated`: for serving through Laravel's routing system.
7775
*/
7876
'transports' => [
7977
'stdio' => [
80-
'enabled' => env('MCP_STDIO_ENABLED', true),
78+
'enabled' => (bool) env('MCP_STDIO_ENABLED', true),
8179
],
8280

8381
'http_dedicated' => [
@@ -88,23 +86,50 @@
8886
'path_prefix' => env('MCP_HTTP_DEDICATED_PATH_PREFIX', 'mcp'),
8987
'ssl_context_options' => [],
9088
'enable_json_response' => (bool) env('MCP_HTTP_DEDICATED_JSON_RESPONSE', true),
91-
'event_store' => env('MCP_HTTP_DEDICATED_EVENT_STORE'), // FQCN or null
89+
'event_store' => null, // FQCN or null
9290
],
9391

9492
'http_integrated' => [
9593
'enabled' => (bool) env('MCP_HTTP_INTEGRATED_ENABLED', true),
9694
'legacy' => (bool) env('MCP_HTTP_INTEGRATED_LEGACY', false),
9795
'route_prefix' => env('MCP_HTTP_INTEGRATED_ROUTE_PREFIX', 'mcp'),
98-
'middleware' => explode(',', env('MCP_HTTP_INTEGRATED_MIDDLEWARE', 'web')),
96+
'middleware' => explode(',', env('MCP_HTTP_INTEGRATED_MIDDLEWARE', 'api')),
9997
'domain' => env('MCP_HTTP_INTEGRATED_DOMAIN'),
10098
'sse_poll_interval' => (int) env('MCP_HTTP_INTEGRATED_SSE_POLL_SECONDS', 1),
10199
'cors_origin' => env('MCP_HTTP_INTEGRATED_CORS_ORIGIN', '*'),
102-
'enable_json_response' => (bool) env('MCP_HTTP_INTEGRATED_JSON_RESPONSE', false),
103-
'json_response_timeout' => (int) env('MCP_HTTP_INTEGRATED_JSON_TIMEOUT', 30),
104-
'event_store' => env('MCP_HTTP_INTEGRATED_EVENT_STORE'), // FQCN or null
100+
'enable_json_response' => (bool) env('MCP_HTTP_INTEGRATED_JSON_RESPONSE', true),
101+
'event_store' => null, // FQCN or null
105102
],
106103
],
107104

105+
/*
106+
|--------------------------------------------------------------------------
107+
| Session Management Configuration
108+
|--------------------------------------------------------------------------
109+
|
110+
| Configure how the MCP server manages client sessions. Sessions store
111+
| client state, message queues, and subscriptions. Supports Laravel's
112+
| native session drivers for seamless integration.
113+
|
114+
*/
115+
'session' => [
116+
'driver' => env('MCP_SESSION_DRIVER', 'cache'), // 'file', 'cache', 'database', 'redis', 'memcached', 'dynamodb', 'array'
117+
'ttl' => (int) env('MCP_SESSION_TTL', 3600),
118+
119+
// For cache-based drivers (redis, memcached, etc.)
120+
'store' => env('MCP_SESSION_CACHE_STORE', config('cache.default')),
121+
122+
// For file driver
123+
'path' => env('MCP_SESSION_FILE_PATH', storage_path('framework/mcp_sessions')),
124+
125+
// For database driver
126+
'connection' => env('MCP_SESSION_DB_CONNECTION', config('database.default')),
127+
'table' => env('MCP_SESSION_DB_TABLE', 'mcp_sessions'),
128+
129+
// Session garbage collection probability. 2% chance that garbage collection will run on any given session operation.
130+
'lottery' => [2, 100],
131+
],
132+
108133
/*
109134
|--------------------------------------------------------------------------
110135
| Pagination Limit
@@ -128,24 +153,24 @@
128153
*/
129154
'capabilities' => [
130155
'tools' => [
131-
'enabled' => env('MCP_CAP_TOOLS_ENABLED', true),
132-
'listChanged' => env('MCP_CAP_TOOLS_LIST_CHANGED', true),
156+
'enabled' => (bool) env('MCP_CAP_TOOLS_ENABLED', true),
157+
'listChanged' => (bool) env('MCP_CAP_TOOLS_LIST_CHANGED', true),
133158
],
134159

135160
'resources' => [
136-
'enabled' => env('MCP_CAP_RESOURCES_ENABLED', true),
137-
'subscribe' => env('MCP_CAP_RESOURCES_SUBSCRIBE', true),
138-
'listChanged' => env('MCP_CAP_RESOURCES_LIST_CHANGED', true),
161+
'enabled' => (bool) env('MCP_CAP_RESOURCES_ENABLED', true),
162+
'subscribe' => (bool) env('MCP_CAP_RESOURCES_SUBSCRIBE', true),
163+
'listChanged' => (bool) env('MCP_CAP_RESOURCES_LIST_CHANGED', true),
139164
],
140165

141166
'prompts' => [
142-
'enabled' => env('MCP_CAP_PROMPTS_ENABLED', true),
143-
'listChanged' => env('MCP_CAP_PROMPTS_LIST_CHANGED', true),
167+
'enabled' => (bool) env('MCP_CAP_PROMPTS_ENABLED', true),
168+
'listChanged' => (bool) env('MCP_CAP_PROMPTS_LIST_CHANGED', true),
144169
],
145170

146171
'logging' => [
147-
'enabled' => env('MCP_CAP_LOGGING_ENABLED', true),
148-
'setLevel' => env('MCP_CAP_LOGGING_SET_LEVEL', false),
172+
'enabled' => (bool) env('MCP_CAP_LOGGING_ENABLED', true),
173+
'setLevel' => (bool) env('MCP_CAP_LOGGING_SET_LEVEL', false),
149174
],
150175
],
151176

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*/
12+
public function up(): void
13+
{
14+
Schema::create('mcp_sessions', function (Blueprint $table) {
15+
$table->string('id')->primary();
16+
$table->longText('payload');
17+
$table->integer('last_activity')->index();
18+
});
19+
}
20+
21+
/**
22+
* Reverse the migrations.
23+
*/
24+
public function down(): void
25+
{
26+
Schema::dropIfExists('mcp_sessions');
27+
}
28+
};

0 commit comments

Comments
 (0)