Skip to content

Commit d383539

Browse files
committed
Merge branch 'feat/support-custom-env' into develop
2 parents 947935b + 5d79a1d commit d383539

File tree

2 files changed

+57
-4
lines changed

2 files changed

+57
-4
lines changed

src/mcp_shell_server/shell_executor.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,7 @@ async def execute(
392392
directory: str,
393393
stdin: Optional[str] = None,
394394
timeout: Optional[int] = None,
395+
envs: Optional[Dict[str, str]] = None,
395396
) -> Dict[str, Any]:
396397
start_time = time.time()
397398
process = None # Initialize process variable
@@ -449,7 +450,9 @@ async def execute(
449450
if current_cmd:
450451
commands.append(current_cmd)
451452

452-
return await self._execute_pipeline(commands, directory, timeout)
453+
return await self._execute_pipeline(
454+
commands, directory, timeout, envs
455+
)
453456
except ValueError as e:
454457
return {
455458
"error": str(e),
@@ -535,7 +538,7 @@ async def execute(
535538
stdin=asyncio.subprocess.PIPE if stdin else None,
536539
stdout=stdout_handle,
537540
stderr=asyncio.subprocess.PIPE,
538-
env=os.environ, # Use all environment variables
541+
env={**os.environ, **(envs or {})}, # Merge environment variables
539542
cwd=directory,
540543
)
541544

@@ -613,6 +616,7 @@ async def _execute_pipeline(
613616
commands: List[List[str]],
614617
directory: Optional[str] = None,
615618
timeout: Optional[int] = None,
619+
envs: Optional[Dict[str, str]] = None,
616620
) -> Dict[str, Any]:
617621
start_time = time.time()
618622
processes: List[asyncio.subprocess.Process] = []
@@ -663,7 +667,7 @@ async def _execute_pipeline(
663667
stdin=asyncio.subprocess.PIPE if prev_stdout is not None else None,
664668
stdout=asyncio.subprocess.PIPE,
665669
stderr=asyncio.subprocess.PIPE,
666-
env=os.environ, # Use all environment variables
670+
env={**os.environ, **(envs or {})}, # Merge environment variables
667671
cwd=directory,
668672
)
669673

tests/test_shell_executor.py

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -621,4 +621,53 @@ async def test_output_redirection_with_append(executor, temp_test_dir, monkeypat
621621
lines = result["stdout"].strip().split("\n")
622622
assert len(lines) == 2
623623
assert lines[0] == "hello"
624-
assert lines[1] == "world"
624+
625+
626+
@pytest.mark.asyncio
627+
async def test_execute_with_custom_env(executor, temp_test_dir, monkeypatch):
628+
"""Test command execution with custom environment variables"""
629+
clear_env(monkeypatch)
630+
monkeypatch.setenv("ALLOW_COMMANDS", "env,printenv")
631+
632+
custom_env = {"TEST_VAR1": "test_value1", "TEST_VAR2": "test_value2"}
633+
634+
# Test env command
635+
result = await executor.execute(["env"], directory=temp_test_dir, envs=custom_env)
636+
assert "TEST_VAR1=test_value1" in result["stdout"]
637+
assert "TEST_VAR2=test_value2" in result["stdout"]
638+
639+
# Test specific variable
640+
result = await executor.execute(
641+
["printenv", "TEST_VAR1"], directory=temp_test_dir, envs=custom_env
642+
)
643+
assert result["stdout"].strip() == "test_value1"
644+
645+
646+
@pytest.mark.asyncio
647+
async def test_execute_env_override(executor, temp_test_dir, monkeypatch):
648+
"""Test that custom environment variables override system variables"""
649+
clear_env(monkeypatch)
650+
monkeypatch.setenv("ALLOW_COMMANDS", "env")
651+
monkeypatch.setenv("TEST_VAR", "original_value")
652+
653+
# Override system environment variable
654+
result = await executor.execute(
655+
["env"], directory=temp_test_dir, envs={"TEST_VAR": "new_value"}
656+
)
657+
658+
assert "TEST_VAR=new_value" in result["stdout"]
659+
assert "TEST_VAR=original_value" not in result["stdout"]
660+
661+
662+
@pytest.mark.asyncio
663+
async def test_execute_with_empty_env(executor, temp_test_dir, monkeypatch):
664+
"""Test command execution with empty environment variables"""
665+
clear_env(monkeypatch)
666+
monkeypatch.setenv("ALLOW_COMMANDS", "env")
667+
668+
result = await executor.execute(["env"], directory=temp_test_dir, envs={})
669+
670+
# Command should still work with system environment
671+
assert result["error"] is None
672+
assert result["status"] == 0
673+
assert len(result["stdout"]) > 0

0 commit comments

Comments
 (0)