@@ -38,8 +38,20 @@ class ServeCommand extends Command
38
38
* for reading STDIN and writing to STDOUT.
39
39
*/
40
40
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
41
52
{
42
53
$ transportOption = $ this ->option ('transport ' );
54
+
43
55
if ($ transportOption === null ) {
44
56
if ($ this ->input ->isInteractive ()) {
45
57
$ transportOption = select (
@@ -55,62 +67,71 @@ public function handle(Server $server): int
55
67
}
56
68
}
57
69
58
- $ host = $ this ->option ('host ' );
59
- $ port = $ this ->option ('port ' );
60
- $ pathPrefix = $ this ->option ('path-prefix ' );
70
+ return $ transportOption ;
71
+ }
61
72
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. ' );
65
77
66
- return Command::FAILURE ;
67
- }
78
+ return Command::FAILURE ;
79
+ }
68
80
69
- $ this ->info ('Starting MCP server with STDIO transport... ' );
81
+ $ this ->info ('Starting MCP server with STDIO transport... ' );
70
82
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 ()}" );
76
88
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
+ }
82
91
83
- return Command::FAILURE ;
84
- }
92
+ $ this ->info ("MCP Server (STDIO) stopped. " );
85
93
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
+ }
108
96
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 ;
110
103
}
111
104
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. " );
113
127
114
128
return Command::SUCCESS ;
115
129
}
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
+ }
116
137
}
0 commit comments