From b22d3af8959b6fdc656d97e0d64901223bbb8f11 Mon Sep 17 00:00:00 2001 From: GuDaStudio Date: Tue, 16 Dec 2025 14:03:23 +0800 Subject: [PATCH] =?UTF-8?q?v0.3.0=EF=BC=9A=E4=B8=BAgemini=E5=87=BD?= =?UTF-8?q?=E6=95=B0=E6=B7=BB=E5=8A=A0cd=E5=8F=82=E6=95=B0=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E5=B7=A5=E4=BD=9C=E7=9B=AE=E5=BD=95=E8=AE=BE=E7=BD=AE?= =?UTF-8?q?=E3=80=82run=5Fshell=5Fcommand=E5=87=BD=E6=95=B0=E6=96=B0?= =?UTF-8?q?=E5=A2=9Ecwd=E5=8F=82=E6=95=B0=EF=BC=9Bsubprocess.Popen?= =?UTF-8?q?=E8=B0=83=E7=94=A8=E4=B8=AD=E4=BC=A0=E5=85=A5cwd=EF=BC=9Bgemini?= =?UTF-8?q?=E5=87=BD=E6=95=B0=E6=96=B0=E5=A2=9Ecd=E5=8F=82=E6=95=B0?= =?UTF-8?q?=E5=B9=B6=E6=A0=A1=E9=AA=8C=E7=9B=AE=E5=BD=95=E5=AD=98=E5=9C=A8?= =?UTF-8?q?=E6=80=A7=EF=BC=9B=E8=A1=A5=E5=85=85time=E6=A8=A1=E5=9D=97?= =?UTF-8?q?=E5=AF=BC=E5=85=A5=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/geminimcp/server.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/geminimcp/server.py b/src/geminimcp/server.py index b2c7123..da1a7f2 100644 --- a/src/geminimcp/server.py +++ b/src/geminimcp/server.py @@ -7,6 +7,7 @@ import os import queue import subprocess import threading +import time import uuid from pathlib import Path from typing import Annotated, Any, Dict, Generator, List, Literal, Optional @@ -18,11 +19,12 @@ import shutil mcp = FastMCP("Gemini MCP Server-from guda.studio") -def run_shell_command(cmd: list[str]) -> Generator[str, None, None]: +def run_shell_command(cmd: list[str], cwd: str | None = None) -> Generator[str, None, None]: """Execute a command and stream its output line-by-line. Args: cmd: Command and arguments as a list (e.g., ["gemini", "-o", "stream-json", "--", "prompt"]) + cwd: Working directory for the command Yields: Output lines from the command @@ -44,6 +46,7 @@ def run_shell_command(cmd: list[str]) -> Generator[str, None, None]: stderr=subprocess.STDOUT, universal_newlines=True, encoding='utf-8', + cwd=cwd, ) output_queue: queue.Queue[str | None] = queue.Queue() @@ -146,6 +149,7 @@ def windows_escape(prompt): ) async def gemini( PROMPT: Annotated[str, "Instruction for the task to send to gemini."], + cd: Annotated[Path, "Set the workspace root for gemini before executing the task."], sandbox: Annotated[ bool, Field(description="Run in sandbox mode. Defaults to `False`."), @@ -164,6 +168,11 @@ async def gemini( ] = "", ) -> Dict[str, Any]: """Execute a gemini CLI session and return the results.""" + + if not cd.exists(): + success = False + err_message = f"The workspace root directory `{cd.absolute().as_posix()}` does not exist. Please check the path and try again." + return {"success": success, "error": err_message} if os.name == "nt": PROMPT = windows_escape(PROMPT) @@ -187,7 +196,7 @@ async def gemini( err_message = "" thread_id: Optional[str] = None - for line in run_shell_command(cmd): + for line in run_shell_command(cmd, cwd=cd.absolute().as_posix()): try: line_dict = json.loads(line.strip()) all_messages.append(line_dict)