Skip to content

Commit 171739a

Browse files
authored
Add SSE support for running MCP servers and enhance add command funct… (#29)
* Add SSE support for running MCP servers and enhance add command functionality * Refactor add_command in CLI to simplify configuration handling and remove client-specific logic
1 parent be0eb5a commit 171739a

File tree

2 files changed

+139
-0
lines changed

2 files changed

+139
-0
lines changed

README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,55 @@ Configure your MCP servers in `.mcphub.json`:
175175
### Transport Support
176176

177177
- **stdio Transport**: Run MCP servers as local subprocesses
178+
- **SSE Transport**: Run MCP servers with Server-Sent Events (SSE) support using supergateway
178179
- **Automatic Path Management**: Manages server paths and working directories
179180
- **Environment Variable Handling**: Configurable environment variables per server
180181

182+
#### Running Servers with SSE Support
183+
184+
You can run MCP servers with SSE support using the `mcphub run` command:
185+
186+
```bash
187+
# Basic usage with default settings
188+
mcphub run your-server-name --sse
189+
190+
# Advanced usage with custom settings
191+
mcphub run your-server-name --sse \
192+
--port 8000 \
193+
--base-url http://localhost:8000 \
194+
--sse-path /sse \
195+
--message-path /message
196+
```
197+
198+
SSE support is useful when you need to:
199+
- Connect to MCP servers from web applications
200+
- Use real-time communication with MCP servers
201+
- Integrate with clients that support SSE
202+
203+
The SSE server provides two endpoints:
204+
- `/sse`: SSE endpoint for real-time updates
205+
- `/message`: HTTP endpoint for sending messages
206+
207+
Example configuration in `.mcphub.json`:
208+
```json
209+
{
210+
"mcpServers": {
211+
"sequential-thinking-mcp": {
212+
"package_name": "smithery-ai/server-sequential-thinking",
213+
"command": "npx",
214+
"args": [
215+
"-y",
216+
"@smithery/cli@latest",
217+
"run",
218+
"@smithery-ai/server-sequential-thinking",
219+
"--key",
220+
"your-api-key"
221+
]
222+
}
223+
}
224+
}
225+
```
226+
181227
### Framework Integration
182228

183229
Provides adapters for popular AI frameworks:

src/mcphub/cli/commands.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import argparse
33
import json
44
import sys
5+
import subprocess
56
from pathlib import Path
67
from typing import Dict, Any, List, Optional
78

@@ -90,6 +91,60 @@ def list_command(args):
9091
else:
9192
print(" No preconfigured servers available")
9293

94+
def run_command(args):
95+
"""Run an MCP server with optional SSE support."""
96+
server_name = args.mcp_name
97+
config = load_config()
98+
99+
if server_name not in config.get("mcpServers", {}):
100+
print(f"Error: MCP server '{server_name}' not found in configuration")
101+
sys.exit(1)
102+
103+
server_config = config["mcpServers"][server_name]
104+
105+
# Build the command
106+
cmd = []
107+
108+
# Add SSE support if requested
109+
if args.sse:
110+
# Construct the stdio command based on server configuration
111+
stdio_cmd = []
112+
if "command" in server_config:
113+
stdio_cmd.append(server_config["command"])
114+
if "args" in server_config:
115+
stdio_cmd.extend(server_config["args"])
116+
117+
# If no command specified, use package_name with npx
118+
if not stdio_cmd and "package_name" in server_config:
119+
stdio_cmd = ["npx", "-y", server_config["package_name"]]
120+
121+
# Join the stdio command parts
122+
stdio_str = " ".join(stdio_cmd)
123+
124+
cmd.extend([
125+
"npx", "-y", "supergateway",
126+
"--stdio", stdio_str,
127+
"--port", str(args.port),
128+
"--baseUrl", args.base_url,
129+
"--ssePath", args.sse_path,
130+
"--messagePath", args.message_path
131+
])
132+
else:
133+
# Use the server's configured command
134+
if "command" in server_config:
135+
cmd.append(server_config["command"])
136+
if "args" in server_config:
137+
cmd.extend(server_config["args"])
138+
139+
try:
140+
print(f"Running command: {' '.join(cmd)}")
141+
subprocess.run(cmd)
142+
except KeyboardInterrupt:
143+
print("\nServer stopped")
144+
except Exception as e:
145+
print(f"Error running server: {e}")
146+
sys.exit(1)
147+
93148
def parse_args(args=None):
94149
"""Parse command line arguments."""
95150
parser = argparse.ArgumentParser(
@@ -140,6 +195,42 @@ def parse_args(args=None):
140195
help="Show all available preconfigured servers"
141196
)
142197

198+
# Run command
199+
run_parser = subparsers.add_parser(
200+
"run",
201+
help="Run an MCP server with optional SSE support"
202+
)
203+
run_parser.add_argument(
204+
"mcp_name",
205+
help="Name of the MCP server to run"
206+
)
207+
run_parser.add_argument(
208+
"--sse",
209+
action="store_true",
210+
help="Enable SSE support using supergateway"
211+
)
212+
run_parser.add_argument(
213+
"--port",
214+
type=int,
215+
default=8000,
216+
help="Port to run the server on (default: 8000)"
217+
)
218+
run_parser.add_argument(
219+
"--base-url",
220+
default="http://localhost:8000",
221+
help="Base URL for the server (default: http://localhost:8000)"
222+
)
223+
run_parser.add_argument(
224+
"--sse-path",
225+
default="/sse",
226+
help="Path for SSE endpoint (default: /sse)"
227+
)
228+
run_parser.add_argument(
229+
"--message-path",
230+
default="/message",
231+
help="Path for message endpoint (default: /message)"
232+
)
233+
143234
return parser.parse_args(args)
144235

145236
def main():
@@ -154,6 +245,8 @@ def main():
154245
remove_command(args)
155246
elif args.command == "list":
156247
list_command(args)
248+
elif args.command == "run":
249+
run_command(args)
157250
else:
158251
# Show help if no command is provided
159252
parse_args(["-h"])

0 commit comments

Comments
 (0)