modelcontextprotocol / modelcontextprotocol/python-sdk

Don’t use shell=True in mcp dev subprocess on Windows (command injection risk)

Aperta Adatta ai principianti
#1,257 3 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug fix proposed P1 ready for work
Lingua principale
Python
Stelle
24.3k
Fork
4k
Merge medio
1g 1h
PR unite (30g)
31

Descrizione

Initial Checks
Description

What’s happening:

When you run the mcp dev command on Windows, it starts another program using a method (subprocess.run with shell=True) that lets the Windows command prompt (cmd.exe) handle the command. This is risky because if any part of the command includes special characters (like &, |, %, etc.), Windows might run something you didn’t expect — even another program, if the file path or arguments are weirdly named or crafted.

Why this is a real problem:

  • This isn’t just a theory — it’s a well-known risk with shell=True in Python. If anyone (or any script) can control part of the file path or arguments, they might be able to run extra commands on your computer.
  • The Python documentation says to avoid shell=True when possible for exactly this reason.
  • The fix is easy: use shell=False and make sure the right Windows executable is picked (like npx.cmd).
  • This keeps things safe and works the same on all systems.

What should happen instead:

  • The command should be run without shell=True on Windows, just like it is on Linux/Mac.
  • File paths and arguments should always be passed as a list, not a single string.

How this could be abused:

  • If someone manages to sneak a file or argument with a shell special character into your project, running mcp dev could run extra commands (for example, opening Calculator if the file had &calc in its name).

Please fix:

  • Remove shell=True from the subprocess.run call in src/mcp/cli/cli.py (Windows part).
  • Make sure the command and its arguments are always passed as a list.
  • Make sure it works on Windows by using the right executable (like npx.cmd).

Thanks!

Example Code
# Example of risky situation on Windows:
# If a file is named "server&calc.py" and you run:
#   mcp dev path\to\server&calc.py
# Windows might run Calculator because of the &

# Please see src/mcp/cli/cli.py (mcp dev command) for the subprocess.run([npx_cmd, ...], shell=True, ...)
Python & MCP Python SDK
Python 3.11, Windows 11, latest MCP Python SDK (main branch, August 2025)

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia in src/mcp/cli/cli.py, nella chiamata a subprocess.run di mcp dev per Windows. Controlla come vengono assemblati il comando e gli argomenti, quindi verifica che l’eseguibile venga selezionato correttamente per Windows e che gli argomenti vengano passati come lista senza shell=True. Il lavoro è completato quando mcp dev viene eseguito su Windows e i metacaratteri della shell nei percorsi o negli argomenti non vengono interpretati come comandi aggiuntivi.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
cli, security
Tipo di issue
Bug
Difficoltà
2/5
Tempo stimato
1-3 ore
Stato di attività
Tranquilla
Chiarezza
Specificata chiaramente
Idoneità per principianti
72/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.