As discussed (discussions/24), this new parameter 'prefix' adds an optional prefix to the routes of the services

This commit is contained in:
Your Full Name 2025-04-04 17:54:19 +02:00
parent f1b5fbd52c
commit 6c003f8bef
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,
prefix: Annotated[
Optional[str], typer.Option("--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 prefix is None:
# Set default value
prefix = "/"
# if prefix doesn't end with a /, add it
if not prefix.endswith("/"):
prefix = f"{prefix}/"
# if prefix doesn't start with a /, add it
if not prefix.startswith("/"):
prefix = f"/{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,
prefix=prefix,
)
)

View File

@ -159,6 +159,7 @@ async def run(
version = kwargs.get("version") or "1.0"
ssl_certfile = kwargs.get("ssl_certfile")
ssl_keyfile= kwargs.get("ssl_keyfile")
prefix = kwargs.get("prefix") or "/"
main_app = FastAPI(
title=name, description=description, version=version, ssl_certfile=ssl_certfile, ssl_keyfile=ssl_keyfile, lifespan=lifespan
@ -209,7 +210,7 @@ async def run(
sub_app.state.api_dependency = api_dependency
main_app.mount(f"/{server_name}", sub_app)
main_app.mount(f"/mcpo/{server_name}", sub_app)
else:
raise ValueError("You must provide either server_command or config.")