Skip to content

Commit b93d49d

Browse files
committed
fix: MCP tool having outdated ENV vars
Don't pass env vars to tool executor from long running parent process, force child to get own env
1 parent 663b962 commit b93d49d

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

src/Mcp/ToolExecutor.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace Laravel\Boost\Mcp;
66

7+
use Dotenv\Dotenv;
8+
use Illuminate\Support\Env;
79
use Laravel\Mcp\Server\Tools\ToolResult;
810
use Symfony\Component\Process\Exception\ProcessFailedException;
911
use Symfony\Component\Process\Exception\ProcessTimedOutException;
@@ -32,8 +34,19 @@ protected function executeInProcess(string $toolClass, array $arguments): ToolRe
3234
{
3335
$command = $this->buildCommand($toolClass, $arguments);
3436

35-
$process = new Process($command);
36-
$process->setTimeout($this->getTimeout());
37+
// We need to 'unset' env vars that will be passed from the parent process to the child process, stopping the child process from reading .env and getting updated values
38+
$env = (Dotenv::create(
39+
Env::getRepository(),
40+
app()->environmentPath(),
41+
app()->environmentFile()
42+
))->safeLoad();
43+
$cleanEnv = array_fill_keys(array_keys($env), false);
44+
45+
$process = new Process(
46+
command: $command,
47+
env: $cleanEnv,
48+
timeout: $this->getTimeout()
49+
);
3750

3851
try {
3952
$process->mustRun();
@@ -45,9 +58,7 @@ protected function executeInProcess(string $toolClass, array $arguments): ToolRe
4558
return ToolResult::error('Invalid JSON output from tool process: '.json_last_error_msg());
4659
}
4760

48-
// Reconstruct ToolResult from the JSON output
4961
return $this->reconstructToolResult($decoded);
50-
5162
} catch (ProcessTimedOutException $e) {
5263
$process->stop();
5364

0 commit comments

Comments
 (0)