From 40414a2905bcef8ff2788c634ef2728583cf2808 Mon Sep 17 00:00:00 2001 From: rankaiyx Date: Sat, 2 Aug 2025 14:31:40 +0800 Subject: [PATCH 1/8] Update serve.py to add api-key --- python/mlc_llm/cli/serve.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/python/mlc_llm/cli/serve.py b/python/mlc_llm/cli/serve.py index bf1f5e5a36..c5afe97ee8 100644 --- a/python/mlc_llm/cli/serve.py +++ b/python/mlc_llm/cli/serve.py @@ -194,6 +194,12 @@ def main(argv): default=["*"], help="allowed headers" + ' (default: "%(default)s")', ) + parser.add_argument( + "--api-key", + type=str, + default=None, + help="API key for authentication. If not provided, authentication is disabled." + ) parsed = parser.parse_args(argv) additional_models = [] @@ -236,4 +242,5 @@ def main(argv): allow_origins=parsed.allow_origins, allow_methods=parsed.allow_methods, allow_headers=parsed.allow_headers, + api_key=parsed.api_key, ) From cbfd8af72cd270c0926bc7f9660ca617325977a6 Mon Sep 17 00:00:00 2001 From: rankaiyx Date: Sat, 2 Aug 2025 14:33:38 +0800 Subject: [PATCH 2/8] Update serve.py to add api-key --- python/mlc_llm/interface/serve.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/python/mlc_llm/interface/serve.py b/python/mlc_llm/interface/serve.py index c00ed1adc5..ae571aebfd 100644 --- a/python/mlc_llm/interface/serve.py +++ b/python/mlc_llm/interface/serve.py @@ -51,6 +51,7 @@ def serve( allow_origins: Any, allow_methods: Any, allow_headers: Any, + api_key: Optional[str] = None, ): # pylint: disable=too-many-arguments, too-many-locals """Serve the model with the specified configuration.""" # Create engine and start the background loop @@ -84,6 +85,7 @@ def serve( with ServerContext() as server_context: server_context.add_model(model, async_engine) + server_context.api_key = api_key app = fastapi.FastAPI() app.add_middleware( From 1fe334c81240d918f58391e6ba341f8200ede477 Mon Sep 17 00:00:00 2001 From: rankaiyx Date: Sat, 2 Aug 2025 14:35:17 +0800 Subject: [PATCH 3/8] Update server_context.py to add api-key --- python/mlc_llm/serve/server/server_context.py | 1 + 1 file changed, 1 insertion(+) diff --git a/python/mlc_llm/serve/server/server_context.py b/python/mlc_llm/serve/server/server_context.py index 2f4bf26626..ea38d967ab 100644 --- a/python/mlc_llm/serve/server/server_context.py +++ b/python/mlc_llm/serve/server/server_context.py @@ -15,6 +15,7 @@ class ServerContext: def __init__(self) -> None: self._models: Dict[str, AsyncMLCEngine] = {} + self.api_key: Optional[str] = None # New API key property def __enter__(self): if ServerContext.server_context is not None: From 51a6ea51cc16fbc6d52429756b80fd0799bba145 Mon Sep 17 00:00:00 2001 From: rankaiyx Date: Sat, 2 Aug 2025 14:37:47 +0800 Subject: [PATCH 4/8] Update openai_entrypoints.py to add api-key --- .../mlc_llm/serve/entrypoints/openai_entrypoints.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/python/mlc_llm/serve/entrypoints/openai_entrypoints.py b/python/mlc_llm/serve/entrypoints/openai_entrypoints.py index 18a415e413..a88a9c77ed 100644 --- a/python/mlc_llm/serve/entrypoints/openai_entrypoints.py +++ b/python/mlc_llm/serve/entrypoints/openai_entrypoints.py @@ -18,7 +18,18 @@ from mlc_llm.serve import engine_base, engine_utils from mlc_llm.serve.server import ServerContext -app = fastapi.APIRouter() +def verify_api_key(request: fastapi.Request): + """Function to verify API key""" + server_context = ServerContext.current() + # Only perform verification when API key is configured + if server_context is not None and server_context.api_key is not None: + provided_key = request.headers.get("Authorization", "").replace("Bearer ", "") + if provided_key != server_context.api_key: + raise fastapi.HTTPException(status_code=401, detail="Invalid API Key") + # Skip verification if no API key is configured + +app = fastapi.APIRouter(dependencies=[fastapi.Depends(verify_api_key)]) + ################ v1/models ################ From f5dce87ef8427bfcc126e2013db00b8469cea119 Mon Sep 17 00:00:00 2001 From: rankaiyx Date: Sat, 2 Aug 2025 15:10:24 +0800 Subject: [PATCH 5/8] Update serve.py for Formatting issue --- python/mlc_llm/cli/serve.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/mlc_llm/cli/serve.py b/python/mlc_llm/cli/serve.py index c5afe97ee8..4495154fbd 100644 --- a/python/mlc_llm/cli/serve.py +++ b/python/mlc_llm/cli/serve.py @@ -198,7 +198,7 @@ def main(argv): "--api-key", type=str, default=None, - help="API key for authentication. If not provided, authentication is disabled." + help="API key for authentication. If not provided, authentication is disabled.", ) parsed = parser.parse_args(argv) From 8c09364cb7c1194af974b6db652971797b3898e4 Mon Sep 17 00:00:00 2001 From: rankaiyx Date: Sat, 2 Aug 2025 15:11:03 +0800 Subject: [PATCH 6/8] Update openai_entrypoints.py for Formatting issue --- python/mlc_llm/serve/entrypoints/openai_entrypoints.py | 1 + 1 file changed, 1 insertion(+) diff --git a/python/mlc_llm/serve/entrypoints/openai_entrypoints.py b/python/mlc_llm/serve/entrypoints/openai_entrypoints.py index a88a9c77ed..507c80b315 100644 --- a/python/mlc_llm/serve/entrypoints/openai_entrypoints.py +++ b/python/mlc_llm/serve/entrypoints/openai_entrypoints.py @@ -28,6 +28,7 @@ def verify_api_key(request: fastapi.Request): raise fastapi.HTTPException(status_code=401, detail="Invalid API Key") # Skip verification if no API key is configured + app = fastapi.APIRouter(dependencies=[fastapi.Depends(verify_api_key)]) ################ v1/models ################ From 6410b894cc1e4c9f098cff9fac1607e360191e8d Mon Sep 17 00:00:00 2001 From: rankaiyx Date: Sat, 2 Aug 2025 15:14:20 +0800 Subject: [PATCH 7/8] Update openai_entrypoints.py for Formatting issue --- python/mlc_llm/serve/entrypoints/openai_entrypoints.py | 1 + 1 file changed, 1 insertion(+) diff --git a/python/mlc_llm/serve/entrypoints/openai_entrypoints.py b/python/mlc_llm/serve/entrypoints/openai_entrypoints.py index 507c80b315..c4054f2d7b 100644 --- a/python/mlc_llm/serve/entrypoints/openai_entrypoints.py +++ b/python/mlc_llm/serve/entrypoints/openai_entrypoints.py @@ -18,6 +18,7 @@ from mlc_llm.serve import engine_base, engine_utils from mlc_llm.serve.server import ServerContext + def verify_api_key(request: fastapi.Request): """Function to verify API key""" server_context = ServerContext.current() From 873db0b7b00727919a0f6fdc96b97d1035ab9e3b Mon Sep 17 00:00:00 2001 From: rankaiyx Date: Wed, 27 Aug 2025 08:27:02 +0800 Subject: [PATCH 8/8] Update server_context.py --- python/mlc_llm/serve/server/server_context.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/mlc_llm/serve/server/server_context.py b/python/mlc_llm/serve/server/server_context.py index ea38d967ab..6256cf079d 100644 --- a/python/mlc_llm/serve/server/server_context.py +++ b/python/mlc_llm/serve/server/server_context.py @@ -15,7 +15,7 @@ class ServerContext: def __init__(self) -> None: self._models: Dict[str, AsyncMLCEngine] = {} - self.api_key: Optional[str] = None # New API key property + self.api_key: Optional[str] = None def __enter__(self): if ServerContext.server_context is not None: