From ebeb3b30dfbaaccbc2f2ad17504123e71a1675d9 Mon Sep 17 00:00:00 2001 From: Guro Date: Thu, 23 Jan 2025 15:54:45 -0500 Subject: [PATCH 1/3] add transport type --- src/mcp/cli/cli.py | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/src/mcp/cli/cli.py b/src/mcp/cli/cli.py index 1512e11..284da72 100644 --- a/src/mcp/cli/cli.py +++ b/src/mcp/cli/cli.py @@ -6,6 +6,7 @@ import subprocess import sys from pathlib import Path +from enum import Enum from typing import Annotated try: @@ -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": @@ -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"] @@ -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 @@ -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) @@ -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() @@ -284,7 +303,7 @@ def run( help="Python file to run, optionally with :object suffix", ), transport: Annotated[ - str | None, + Transport | None, typer.Option( "--transport", "-t", From 063ce2c2ce7cd452267b167f79d83add99388f01 Mon Sep 17 00:00:00 2001 From: Guro Date: Thu, 23 Jan 2025 16:53:29 -0500 Subject: [PATCH 2/3] add SSE_HOSTPORT env passing --- src/mcp/cli/cli.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/mcp/cli/cli.py b/src/mcp/cli/cli.py index 284da72..b797828 100644 --- a/src/mcp/cli/cli.py +++ b/src/mcp/cli/cli.py @@ -269,11 +269,16 @@ def dev( # Run the MCP Inspector command with shell=True on Windows shell = sys.platform == "win32" + + env = dict(os.environ.items()) + if transport == Transport.SSE: + env["SSE_HOSTPORT"] = "0.0.0.0:8000" + 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: From 901ee3b08a8d2118e6f0d6fbb9d7b94f094486be Mon Sep 17 00:00:00 2001 From: Guro Date: Thu, 23 Jan 2025 16:55:23 -0500 Subject: [PATCH 3/3] add missing comment --- src/mcp/cli/cli.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mcp/cli/cli.py b/src/mcp/cli/cli.py index b797828..8e3dbe1 100644 --- a/src/mcp/cli/cli.py +++ b/src/mcp/cli/cli.py @@ -270,7 +270,7 @@ def dev( # Run the MCP Inspector command with shell=True on Windows shell = sys.platform == "win32" - env = dict(os.environ.items()) + 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"