22import argparse
33import json
44import sys
5+ import subprocess
56from pathlib import Path
67from 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 ("\n Server stopped" )
144+ except Exception as e :
145+ print (f"Error running server: { e } " )
146+ sys .exit (1 )
147+
93148def 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
145236def 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