Skip to content

As discussed (discussions/24), this new parameter 'prefix' adds an op… #49

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/mcpo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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(
Expand All @@ -95,6 +109,7 @@ def main(
server_command=server_command,
ssl_certfile=ssl_certfile,
ssl_keyfile=ssl_keyfile,
path_prefix=path_prefix,
)
)

Expand Down
3 changes: 2 additions & 1 deletion src/mcpo/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.")
Expand Down