Merge pull request #49 from MHugonKaliop/feat/add-prefix-from-discussion-24

This commit is contained in:
Timothy Jaeryang Baek 2025-04-04 22:17:09 -07:00 committed by GitHub
commit e52410714f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 17 additions and 1 deletions

View File

@ -47,6 +47,9 @@ def main(
ssl_keyfile: Annotated[
Optional[str], typer.Option("--ssl-keyfile", "-k", help="SSL keyfile")
] = None,
path_prefix: Annotated[
Optional[str], typer.Option("--path_prefix", "-x", help="URL prefix")
] = None,
):
server_command = None
if not config:
@ -81,6 +84,17 @@ def main(
for key, value in env_dict.items():
os.environ[key] = value
# Whatever the prefix is, make sure it starts and ends with a /
if path_prefix is None:
# Set default value
path_prefix = "/"
# if prefix doesn't end with a /, add it
if not path_prefix.endswith("/"):
path_prefix = f"{path_prefix}/"
# if prefix doesn't start with a /, add it
if not path_prefix.startswith("/"):
path_prefix = f"/{path_prefix}"
# Run your async run function from mcpo.main
asyncio.run(
run(
@ -95,6 +109,7 @@ def main(
server_command=server_command,
ssl_certfile=ssl_certfile,
ssl_keyfile=ssl_keyfile,
path_prefix=path_prefix,
)
)

View File

@ -155,6 +155,7 @@ async def run(
version = kwargs.get("version") or "1.0"
ssl_certfile = kwargs.get("ssl_certfile")
ssl_keyfile = kwargs.get("ssl_keyfile")
path_prefix = kwargs.get("path_prefix") or "/"
main_app = FastAPI(
title=name, description=description, version=version, ssl_certfile=ssl_certfile, ssl_keyfile=ssl_keyfile, lifespan=lifespan
@ -203,7 +204,7 @@ async def run(
sub_app.state.env = {**os.environ, **server_cfg.get("env", {})}
sub_app.state.api_dependency = api_dependency
main_app.mount(f"/{server_name}", sub_app)
main_app.mount(f"{path_prefix}{server_name}", sub_app)
main_app.description += f"\n - [{server_name}](http://{host}:{port}/{server_name}/docs)"
else:
raise ValueError("You must provide either server_command or config.")