@@ -697,7 +697,7 @@ static bool common_params_parse_ex(int argc, char ** argv, common_params_context
697697 }
698698 };
699699
700- // parse the first time to get -hf option (used for remote preset)
700+ // parse all CLI args now, so that -hf is available below for remote preset resolution
701701 parse_cli_args ();
702702
703703 postprocess_cpu_params (params.cpuparams , nullptr );
@@ -748,6 +748,11 @@ static bool common_params_parse_ex(int argc, char ** argv, common_params_context
748748 params.kv_overrides .back ().key [0 ] = 0 ;
749749 }
750750
751+ if (!params.server_tools .empty () && !params.cors_origins_explicit ) {
752+ LOG_WRN (" server tools are enabled, using localhost as default CORS origin (change via --cors-origins)\n " );
753+ params.cors_origins = " localhost" ;
754+ }
755+
751756 // pad tensor_buft_overrides for llama_params_fit:
752757 const size_t ntbo = llama_max_tensor_buft_overrides ();
753758 while (params.tensor_buft_overrides .size () < ntbo) {
@@ -3047,6 +3052,42 @@ common_params_context common_params_parser_init(common_params & params, llama_ex
30473052 params.public_path = value;
30483053 }
30493054 ).set_examples ({LLAMA_EXAMPLE_SERVER }).set_env (" LLAMA_ARG_STATIC_PATH" ));
3055+ add_opt (common_arg (
3056+ {" --cors-origins" }, " ORIGINS" ,
3057+ string_format (
3058+ " comma-separated list of allowed origins for CORS (default: %s)\n "
3059+ " if set to special value 'localhost', reflect the Origin header only if it is localhost" ,
3060+ params.cors_origins .c_str ()),
3061+ [](common_params & params, const std::string & value) {
3062+ params.cors_origins = value;
3063+ params.cors_origins_explicit = true ;
3064+ }
3065+ ).set_examples ({LLAMA_EXAMPLE_SERVER }).set_env (" LLAMA_ARG_CORS_ORIGINS" ));
3066+ add_opt (common_arg (
3067+ {" --cors-methods" }, " METHODS" ,
3068+ string_format (" comma-separated list of allowed methods for CORS (default: %s)" , params.cors_methods .c_str ()),
3069+ [](common_params & params, const std::string & value) {
3070+ params.cors_methods = value;
3071+ }
3072+ ).set_examples ({LLAMA_EXAMPLE_SERVER }).set_env (" LLAMA_ARG_CORS_METHODS" ));
3073+ add_opt (common_arg (
3074+ {" --cors-headers" }, " HEADERS" ,
3075+ string_format (" comma-separated list of allowed headers for CORS (default: %s)" , params.cors_headers .c_str ()),
3076+ [](common_params & params, const std::string & value) {
3077+ params.cors_headers = value;
3078+ }
3079+ ).set_examples ({LLAMA_EXAMPLE_SERVER }).set_env (" LLAMA_ARG_CORS_HEADERS" ));
3080+ add_opt (common_arg (
3081+ {" --cors-credentials" },
3082+ {" --no-cors-credentials" },
3083+ string_format (
3084+ " whether to allow credentials for CORS (default: %s)\n "
3085+ " note: if this is enabled and --cors-origins is set to * (default), the Origin header will be echoed back, and credentials will always be allowed" ,
3086+ params.cors_credentials ? " enabled" : " disabled" ),
3087+ [](common_params & params, bool value) {
3088+ params.cors_credentials = value;
3089+ }
3090+ ).set_examples ({LLAMA_EXAMPLE_SERVER }).set_env (" LLAMA_ARG_CORS_CREDENTIALS" ));
30503091 add_opt (common_arg (
30513092 {" --api-prefix" }, " PREFIX" ,
30523093 string_format (" prefix path the server serves from, without the trailing slash (default: %s)" , params.api_prefix .c_str ()),
@@ -3080,15 +3121,17 @@ common_params_context common_params_parser_init(common_params & params, llama_ex
30803121 {" --tools" }, " TOOL1,TOOL2,..." ,
30813122 " experimental: whether to enable built-in tools for AI agents - do not enable in untrusted environments (default: no tools)\n "
30823123 " specify \" all\" to enable all tools\n "
3083- " available tools: read_file, file_glob_search, grep_search, exec_shell_command, write_file, edit_file, get_datetime" ,
3124+ " available tools: read_file, file_glob_search, grep_search, exec_shell_command, write_file, edit_file, get_datetime\n "
3125+ " note: for security reasons, this will limit --cors-origins to localhost by default" ,
30843126 [](common_params & params, const std::string & value) {
30853127 params.server_tools = parse_csv_row (value);
30863128 }
30873129 ).set_examples ({LLAMA_EXAMPLE_SERVER }).set_env (" LLAMA_ARG_TOOLS" ));
30883130 add_opt (common_arg (
30893131 {" -ag" , " --agent" },
30903132 {" -no-ag" , " --no-agent" },
3091- " whether to enable CORS proxy and all built-in tools - do not enable in untrusted environments (default: disabled)" ,
3133+ " whether to enable CORS proxy and all built-in tools - do not enable in untrusted environments (default: disabled)\n "
3134+ " note: for security reasons, this will limit --cors-origins to localhost by default" ,
30923135 [](common_params & params, bool value) {
30933136 if (value) {
30943137 params.server_tools = {" all" };
@@ -3097,6 +3140,7 @@ common_params_context common_params_parser_init(common_params & params, llama_ex
30973140 params.server_tools .clear ();
30983141 params.ui_mcp_proxy = false ;
30993142 }
3143+ // note: do not modify cors_origins here, as the options are not evaluated in order (user may explicitly set --cors-origins before --agent)
31003144 }
31013145 ).set_examples ({LLAMA_EXAMPLE_SERVER }).set_env (" LLAMA_ARG_AGENT" ));
31023146 add_opt (common_arg (
0 commit comments