@@ -38,8 +38,20 @@ class ServeCommand extends Command
3838 * for reading STDIN and writing to STDOUT.
3939 */
4040 public function handle (Server $ server ): int
41+ {
42+ $ transportOption = $ this ->getTransportOption ();
43+
44+ return match ($ transportOption ) {
45+ 'stdio ' => $ this ->handleStdioTransport ($ server ),
46+ 'http ' => $ this ->handleHttpTransport ($ server ),
47+ default => $ this ->handleInvalidTransport ($ transportOption ),
48+ };
49+ }
50+
51+ private function getTransportOption (): string
4152 {
4253 $ transportOption = $ this ->option ('transport ' );
54+
4355 if ($ transportOption === null ) {
4456 if ($ this ->input ->isInteractive ()) {
4557 $ transportOption = select (
@@ -55,62 +67,71 @@ public function handle(Server $server): int
5567 }
5668 }
5769
58- $ host = $ this ->option ('host ' );
59- $ port = $ this ->option ('port ' );
60- $ pathPrefix = $ this ->option ('path-prefix ' );
70+ return $ transportOption ;
71+ }
6172
62- if ($ transportOption === 'stdio ' ) {
63- if (! config ('mcp.transports.stdio.enabled ' , true )) {
64- $ this ->error ('MCP STDIO transport is disabled in config/mcp.php. ' );
73+ private function handleStdioTransport (Server $ server ): int
74+ {
75+ if (! config ('mcp.transports.stdio.enabled ' , true )) {
76+ $ this ->error ('MCP STDIO transport is disabled in config/mcp.php. ' );
6577
66- return Command::FAILURE ;
67- }
78+ return Command::FAILURE ;
79+ }
6880
69- $ this ->info ('Starting MCP server with STDIO transport... ' );
81+ $ this ->info ('Starting MCP server with STDIO transport... ' );
7082
71- try {
72- $ transport = new StdioServerTransport ;
73- $ server ->listen ($ transport );
74- } catch (\Exception $ e ) {
75- $ this ->error ("Failed to start MCP server with STDIO transport: {$ e ->getMessage ()}" );
83+ try {
84+ $ transport = new StdioServerTransport ;
85+ $ server ->listen ($ transport );
86+ } catch (\Exception $ e ) {
87+ $ this ->error ("Failed to start MCP server with STDIO transport: {$ e ->getMessage ()}" );
7688
77- return Command::FAILURE ;
78- }
79- } elseif ($ transportOption === 'http ' ) {
80- if (! config ('mcp.transports.http_dedicated.enabled ' , true )) {
81- $ this ->error ('Dedicated MCP HTTP transport is disabled in config/mcp.php. ' );
89+ return Command::FAILURE ;
90+ }
8291
83- return Command::FAILURE ;
84- }
92+ $ this ->info ("MCP Server (STDIO) stopped. " );
8593
86- $ host = $ this ->option ('host ' ) ?? config ('mcp.transports.http_dedicated.host ' , '127.0.0.1 ' );
87- $ port = (int ) ($ this ->option ('port ' ) ?? config ('mcp.transports.http_dedicated.port ' , 8090 ));
88- $ pathPrefix = $ this ->option ('path-prefix ' ) ?? config ('mcp.transports.http_dedicated.path_prefix ' , 'mcp_server ' );
89- $ sslContextOptions = config ('mcp.transports.http_dedicated.ssl_context_options ' ); // For HTTPS
90-
91- $ this ->info ("Starting MCP server with dedicated HTTP transport on http:// {$ host }: {$ port } (prefix: / {$ pathPrefix })... " );
92- $ transport = new HttpServerTransport (
93- host: $ host ,
94- port: $ port ,
95- mcpPathPrefix: $ pathPrefix ,
96- sslContext: $ sslContextOptions
97- );
98-
99- try {
100- $ server ->listen ($ transport );
101- } catch (\Exception $ e ) {
102- $ this ->error ("Failed to start MCP server with dedicated HTTP transport: {$ e ->getMessage ()}" );
103-
104- return Command::FAILURE ;
105- }
106- } else {
107- $ this ->error ("Invalid transport specified: {$ transportOption }. Use 'stdio' or 'http'. " );
94+ return Command::SUCCESS ;
95+ }
10896
109- return Command::INVALID ;
97+ private function handleHttpTransport (Server $ server ): int
98+ {
99+ if (! config ('mcp.transports.http_dedicated.enabled ' , true )) {
100+ $ this ->error ('Dedicated MCP HTTP transport is disabled in config/mcp.php. ' );
101+
102+ return Command::FAILURE ;
110103 }
111104
112- $ this ->info ("MCP Server ( {$ transportOption }) stopped. " );
105+ $ host = $ this ->option ('host ' ) ?? config ('mcp.transports.http_dedicated.host ' , '127.0.0.1 ' );
106+ $ port = (int ) ($ this ->option ('port ' ) ?? config ('mcp.transports.http_dedicated.port ' , 8090 ));
107+ $ pathPrefix = $ this ->option ('path-prefix ' ) ?? config ('mcp.transports.http_dedicated.path_prefix ' , 'mcp_server ' );
108+ $ sslContextOptions = config ('mcp.transports.http_dedicated.ssl_context_options ' ); // For HTTPS
109+
110+ $ this ->info ("Starting MCP server with dedicated HTTP transport on http:// {$ host }: {$ port } (prefix: / {$ pathPrefix })... " );
111+ $ transport = new HttpServerTransport (
112+ host: $ host ,
113+ port: $ port ,
114+ mcpPathPrefix: $ pathPrefix ,
115+ sslContext: $ sslContextOptions
116+ );
117+
118+ try {
119+ $ server ->listen ($ transport );
120+ } catch (\Exception $ e ) {
121+ $ this ->error ("Failed to start MCP server with dedicated HTTP transport: {$ e ->getMessage ()}" );
122+
123+ return Command::FAILURE ;
124+ }
125+
126+ $ this ->info ("MCP Server (HTTP) stopped. " );
113127
114128 return Command::SUCCESS ;
115129 }
130+
131+ private function handleInvalidTransport (string $ transportOption ): int
132+ {
133+ $ this ->error ("Invalid transport specified: {$ transportOption }. Use 'stdio' or 'http'. " );
134+
135+ return Command::INVALID ;
136+ }
116137}
0 commit comments