ai-joe-here commited on
Commit
1e45926
·
1 Parent(s): 6837525

1) Fixing this for windows in finding npm/npx for running servers in dev mode.

Browse files

2) Fixing this for allowing server.py files to include module imports. Before they weren't resolving properly.

Files changed (1) hide show
  1. src/fastmcp/cli/cli.py +40 -5
src/fastmcp/cli/cli.py CHANGED
@@ -11,8 +11,8 @@ import typer
11
  from typing_extensions import Annotated
12
  import dotenv
13
 
14
- from ..utilities.logging import get_logger
15
- from . import claude
16
 
17
  logger = get_logger("cli")
18
 
@@ -24,6 +24,22 @@ app = typer.Typer(
24
  )
25
 
26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  def _parse_env_var(env_var: str) -> Tuple[str, str]:
28
  """Parse environment variable string in format KEY=VALUE."""
29
  if "=" not in env_var:
@@ -99,6 +115,11 @@ def _import_server(file: Path, server_object: Optional[str] = None):
99
  Returns:
100
  The server object
101
  """
 
 
 
 
 
102
  # Import the module
103
  spec = importlib.util.spec_from_file_location("server_module", file)
104
  if not spec or not spec.loader:
@@ -205,10 +226,22 @@ def dev(
205
  with_packages = list(set(with_packages + server.dependencies))
206
 
207
  uv_cmd = _build_uv_command(file_spec, with_editable, with_packages)
208
- # Run the MCP Inspector command
 
 
 
 
 
 
 
 
 
 
 
209
  process = subprocess.run(
210
- ["npx", "@modelcontextprotocol/inspector"] + uv_cmd,
211
  check=True,
 
212
  )
213
  sys.exit(process.returncode)
214
  except subprocess.CalledProcessError as e:
@@ -223,7 +256,9 @@ def dev(
223
  sys.exit(e.returncode)
224
  except FileNotFoundError:
225
  logger.error(
226
- "npx not found. Please install Node.js and npm.",
 
 
227
  extra={"file": str(file)},
228
  )
229
  sys.exit(1)
 
11
  from typing_extensions import Annotated
12
  import dotenv
13
 
14
+ from fastmcp.cli import claude
15
+ from fastmcp.utilities.logging import get_logger
16
 
17
  logger = get_logger("cli")
18
 
 
24
  )
25
 
26
 
27
+ def _get_npx_command():
28
+ """Get the correct npx command for the current platform."""
29
+ if sys.platform == "win32":
30
+ # Try both npx.cmd and npx.exe on Windows
31
+ for cmd in ["npx.cmd", "npx.exe", "npx"]:
32
+ try:
33
+ subprocess.run(
34
+ [cmd, "--version"], check=True, capture_output=True, shell=True
35
+ )
36
+ return cmd
37
+ except subprocess.CalledProcessError:
38
+ continue
39
+ return None
40
+ return "npx" # On Unix-like systems, just use npx
41
+
42
+
43
  def _parse_env_var(env_var: str) -> Tuple[str, str]:
44
  """Parse environment variable string in format KEY=VALUE."""
45
  if "=" not in env_var:
 
115
  Returns:
116
  The server object
117
  """
118
+ # Add parent directory to Python path so imports can be resolved
119
+ file_dir = str(file.parent)
120
+ if file_dir not in sys.path:
121
+ sys.path.insert(0, file_dir)
122
+
123
  # Import the module
124
  spec = importlib.util.spec_from_file_location("server_module", file)
125
  if not spec or not spec.loader:
 
226
  with_packages = list(set(with_packages + server.dependencies))
227
 
228
  uv_cmd = _build_uv_command(file_spec, with_editable, with_packages)
229
+
230
+ # Get the correct npx command
231
+ npx_cmd = _get_npx_command()
232
+ if not npx_cmd:
233
+ logger.error(
234
+ "npx not found. Please ensure Node.js and npm are properly installed "
235
+ "and added to your system PATH."
236
+ )
237
+ sys.exit(1)
238
+
239
+ # Run the MCP Inspector command with shell=True on Windows
240
+ shell = sys.platform == "win32"
241
  process = subprocess.run(
242
+ [npx_cmd, "@modelcontextprotocol/inspector"] + uv_cmd,
243
  check=True,
244
+ shell=shell,
245
  )
246
  sys.exit(process.returncode)
247
  except subprocess.CalledProcessError as e:
 
256
  sys.exit(e.returncode)
257
  except FileNotFoundError:
258
  logger.error(
259
+ "npx not found. Please ensure Node.js and npm are properly installed "
260
+ "and added to your system PATH. You may need to restart your terminal "
261
+ "after installation.",
262
  extra={"file": str(file)},
263
  )
264
  sys.exit(1)