Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

draft: add transport parameter to dev command so that server starts in correct transport mode and passes URL to the inspector #168

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 29 additions & 5 deletions src/mcp/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import subprocess
import sys
from pathlib import Path
from enum import Enum
from typing import Annotated

try:
Expand Down Expand Up @@ -36,6 +37,11 @@
)


class Transport(str, Enum):
STDIO = "stdio"
SSE = "sse"


def _get_npx_command():
"""Get the correct npx command for the current platform."""
if sys.platform == "win32":
Expand Down Expand Up @@ -67,6 +73,7 @@ def _build_uv_command(
file_spec: str,
with_editable: Path | None = None,
with_packages: list[str] | None = None,
transport: Transport | None = None,
) -> list[str]:
"""Build the uv run command that runs a MCP server through mcp run."""
cmd = ["uv"]
Expand All @@ -81,8 +88,12 @@ def _build_uv_command(
if pkg:
cmd.extend(["--with", pkg])

# Add mcp run command
cmd.extend(["mcp", "run", file_spec])
# Add mcp run command with optional transport
cmd.extend(["mcp", "run"])
if transport:
cmd.extend(["--transport", transport])

cmd.append(file_spec)
return cmd


Expand Down Expand Up @@ -217,6 +228,14 @@ def dev(
help="Additional packages to install",
),
] = [],
transport: Annotated[
Transport | None,
typer.Option(
"--transport",
"-t",
help="Transport protocol to use (stdio or sse)",
),
] = None,
) -> None:
"""Run a MCP server with the MCP Inspector."""
file, server_object = _parse_file_path(file_spec)
Expand All @@ -237,7 +256,7 @@ def dev(
if hasattr(server, "dependencies"):
with_packages = list(set(with_packages + server.dependencies))

uv_cmd = _build_uv_command(file_spec, with_editable, with_packages)
uv_cmd = _build_uv_command(file_spec, with_editable, with_packages, transport)

# Get the correct npx command
npx_cmd = _get_npx_command()
Expand All @@ -250,11 +269,16 @@ def dev(

# Run the MCP Inspector command with shell=True on Windows
shell = sys.platform == "win32"

env = dict(os.environ.items()) # Convert to list of tuples for env update
if transport == Transport.SSE:
env["SSE_HOSTPORT"] = "0.0.0.0:8000"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't listen by default on every connection, but only localhost, which we should designate as localhost:8000 and use the system mechanism to translate it to socketaddresses, so we cover both ipv4 and ipv6


process = subprocess.run(
[npx_cmd, "@modelcontextprotocol/inspector"] + uv_cmd,
check=True,
shell=shell,
env=dict(os.environ.items()), # Convert to list of tuples for env update
env=env,
)
sys.exit(process.returncode)
except subprocess.CalledProcessError as e:
Expand Down Expand Up @@ -284,7 +308,7 @@ def run(
help="Python file to run, optionally with :object suffix",
),
transport: Annotated[
str | None,
Transport | None,
typer.Option(
"--transport",
"-t",
Expand Down