@@ -8,25 +8,31 @@ import {
88 DEFAULT_HOSTNAME ,
99 DEFAULT_PORT ,
1010} from "$/shared/constants.ts" ;
11- import type { AppConfig } from "$/shared/types.ts" ;
11+ import type { AppConfig , Prettify } from "$/shared/types.ts" ;
1212import { validateConfig } from "$/shared/validation.ts" ;
1313
1414export type CliCommand = Awaited < ReturnType < typeof createCommand > > ;
1515
16- export type CliOptions =
16+ export type CliOptions = Prettify <
1717 & Omit < CliCommand [ "options" ] , "header" | "host" | "origin" | "noHttp" | "noStdio" >
1818 & {
19+ http : boolean ;
20+ stdio : boolean ;
1921 headers : string [ ] ;
2022 allowedOrigins : string [ ] ;
2123 allowedHosts : string [ ] ;
22- } ;
24+ }
25+ > ;
2326
2427/** Merges two arrays of strings, removing duplicates */
2528function mergeArrays ( a ?: string [ ] , b ?: string [ ] ) : string [ ] {
2629 return [ ...new Set ( [ ...a ?? [ ] , ...b ?? [ ] ] ) ] ;
2730}
2831
29- async function createCommand ( ) {
32+ /** Prefix for environment variables */
33+ const prefix = "MCP_" ;
34+
35+ function createCommand ( ) {
3036 return new Command ( )
3137 . throwErrors ( )
3238 . name ( APP_USAGE )
@@ -51,63 +57,56 @@ async function createCommand() {
5157 . option ( "--no-stdio" , "Disable the STDIO server." , {
5258 conflicts : [ "no-http" ] ,
5359 } )
54- . env ( "NO_STDIO=<value:boolean>" , "Disable the STDIO server." , {
55- prefix : "MCP_" ,
56- } )
60+ . env ( "NO_STDIO=<value:boolean>" , "Disable the STDIO server." , { prefix } )
5761 // HTTP server
5862 . option ( "--no-http" , "Disable the HTTP server." , {
5963 conflicts : [ "no-stdio" ] ,
6064 } )
61- . env ( "NO_HTTP=<value:boolean>" , "Disable the HTTP server." , {
62- prefix : "MCP_" ,
63- } )
65+ . env ( "NO_HTTP=<value:boolean>" , "Disable the HTTP server." , { prefix } )
6466 // Port
6567 . option ( "-p, --port <port:integer>" , "Set the port." , {
6668 default : DEFAULT_PORT ,
6769 conflicts : [ "no-http" ] ,
6870 } )
69- . env ( "PORT=<value:integer>" , "Set the port." , { prefix : "MCP_" } )
71+ . env ( "PORT=<value:integer>" , "Set the port." , { prefix } )
7072 // Hostname
7173 . option ( "-n, --hostname <hostname:string>" , "Set the hostname." , {
7274 default : DEFAULT_HOSTNAME ,
7375 conflicts : [ "no-http" ] ,
7476 } )
75- . env ( "HOSTNAME=<value:string>" , "Set the hostname." , { prefix : "MCP_" } )
77+ . action ( function ( options ) {
78+ if ( options . port < 1 || options . port > 65535 ) {
79+ throw new ValidationError ( "Port must be between 1 and 65535" ) ;
80+ }
81+ } )
82+ . env ( "HOSTNAME=<value:string>" , "Set the hostname." , { prefix } )
7683 // Headers
7784 . option ( "-H, --header <header:string>" , "Set a custom header." , {
7885 collect : true ,
7986 conflicts : [ "no-http" ] ,
8087 } )
81- . env ( "HEADERS=<value:string[]>" , "Set custom headers." , {
82- prefix : "MCP_" ,
83- } )
88+ . env ( "HEADERS=<value:string[]>" , "Set custom headers." , { prefix } )
8489 // DNS rebinding
8590 . option ( "--dnsRebinding" , "Enable DNS rebinding protection." , {
8691 default : false ,
8792 conflicts : [ "no-http" ] ,
8893 depends : [ "origin" , "host" ] ,
8994 } )
90- . env ( "DNS_REBINDING=<value:boolean>" , "Enable DNS rebinding protection." , {
91- prefix : "MCP_" ,
92- } )
95+ . env ( "DNS_REBINDING=<value:boolean>" , "Enable DNS rebinding protection." , { prefix } )
9396 // Allowed origins
9497 . option ( "--origin <origin:string>" , "Allow an origin for DNS rebinding." , {
9598 collect : true ,
9699 conflicts : [ "no-http" ] ,
97100 depends : [ "dnsRebinding" ] ,
98101 } )
99- . env ( "ALLOWED_ORIGINS=<value:string[]>" , "Allowed origins for DNS rebinding." , {
100- prefix : "MCP_" ,
101- } )
102+ . env ( "ALLOWED_ORIGINS=<value:string[]>" , "Allowed origins for DNS rebinding." , { prefix } )
102103 // Allowed hosts
103104 . option ( "--host <host:string>" , "Allow a host for DNS rebinding." , {
104105 collect : true ,
105106 conflicts : [ "no-http" ] ,
106107 depends : [ "dnsRebinding" ] ,
107108 } )
108- . env ( "ALLOWED_HOSTS=<value:string[]>" , "Allowed hosts for DNS rebinding." , {
109- prefix : "MCP_" ,
110- } )
109+ . env ( "ALLOWED_HOSTS=<value:string[]>" , "Allowed hosts for DNS rebinding." , { prefix } )
111110 . parse ( Deno . args ) ;
112111}
113112
0 commit comments