From 6ce828d6ca6b71dab2fa3cf337e4fb1ae369e3fc Mon Sep 17 00:00:00 2001 From: Rai Siqueira Date: Tue, 18 Nov 2025 17:12:50 -0300 Subject: [PATCH] feat: add support to define the port and the host to start the server Signed-off-by: Rai Siqueira --- README.md | 31 ++++++++++++++++++++++++--- pyproject.toml | 2 +- server.json | 4 ++-- src/django_ai_boost/__init__.py | 18 +++++++++++++++- src/django_ai_boost/server_fastmcp.py | 14 ++++++++++-- uv.lock | 2 +- 6 files changed, 61 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index a99576c..bd9a638 100644 --- a/README.md +++ b/README.md @@ -100,10 +100,18 @@ django-ai-boost # Or specify settings directly django-ai-boost --settings myproject.settings -# Run with SSE transport (default is stdio) +# Run with SSE transport (default is stdio, which doesn't use network ports) django-ai-boost --settings myproject.settings --transport sse + +# Run with SSE transport on a custom port (default port is 8000) +django-ai-boost --settings myproject.settings --transport sse --port 3000 + +# Run with SSE transport on custom host and port +django-ai-boost --settings myproject.settings --transport sse --host 0.0.0.0 --port 8080 ``` +**Note:** The stdio transport (default) communicates via standard input/output and does not use network ports. The `--port` and `--host` options only apply when using `--transport sse`. + ## AI Tools Setup ### Cursor @@ -272,11 +280,17 @@ Add to your Zed MCP configuration (`~/.config/zed/mcp.json`): For any MCP-compatible client, you can run the server manually: ```bash -# Standard I/O transport (default) +# Standard I/O transport (default, no network port) django-ai-boost --settings myproject.settings -# Server-Sent Events transport +# Server-Sent Events transport (default: 127.0.0.1:8000) django-ai-boost --settings myproject.settings --transport sse + +# SSE transport with custom port +django-ai-boost --settings myproject.settings --transport sse --port 3000 + +# SSE transport with custom host and port +django-ai-boost --settings myproject.settings --transport sse --host 0.0.0.0 --port 8080 ``` ## Available Tools and Prompts @@ -465,6 +479,17 @@ Or in your MCP client configuration: Ensure your Django database is properly configured and accessible. The MCP server needs the same database access as your Django application. +### Port Already in Use (SSE Transport) + +If you see an error like "Address already in use" when using SSE transport, the default port 8000 is likely occupied by another service (such as your Django development server). Use a different port: + +```bash +# Use a different port for the MCP server +django-ai-boost --settings myproject.settings --transport sse --port 8001 +``` + +**Note:** The stdio transport (default) does not use network ports and will not have this issue. + ## Requirements - Python 3.12+ diff --git a/pyproject.toml b/pyproject.toml index 32b4b0c..4e5eb58 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "django-ai-boost" -version = "0.3.1" +version = "0.4.0" description = "A Model Context Protocol (MCP) server for Django applications, inspired by Laravel Boost" readme = "README.md" authors = [ diff --git a/server.json b/server.json index 09cfa18..ed55449 100644 --- a/server.json +++ b/server.json @@ -6,12 +6,12 @@ "url": "https://github.com/vintasoftware/django-ai-boost", "source": "github" }, - "version": "0.3.1", + "version": "0.4.0", "packages": [ { "registryType": "pypi", "identifier": "django-ai-boost", - "version": "0.3.1", + "version": "0.4.0", "transport": { "type": "stdio" }, diff --git a/src/django_ai_boost/__init__.py b/src/django_ai_boost/__init__.py index 08b700f..c98d939 100644 --- a/src/django_ai_boost/__init__.py +++ b/src/django_ai_boost/__init__.py @@ -17,7 +17,23 @@ def main() -> None: default="stdio", help="Transport type (default: stdio)", ) + parser.add_argument( + "--host", + default="127.0.0.1", + help="Host to bind to for SSE transport (default: 127.0.0.1)", + ) + parser.add_argument( + "--port", + type=int, + default=8000, + help="Port to bind to for SSE transport (default: 8000)", + ) args = parser.parse_args() - run_server(settings_module=args.settings, transport=args.transport) + run_server( + settings_module=args.settings, + transport=args.transport, + host=args.host, + port=args.port, + ) diff --git a/src/django_ai_boost/server_fastmcp.py b/src/django_ai_boost/server_fastmcp.py index c173d9a..35ab0dc 100644 --- a/src/django_ai_boost/server_fastmcp.py +++ b/src/django_ai_boost/server_fastmcp.py @@ -574,19 +574,29 @@ async def search_django_docs(topic: str) -> str: return prompt -def run_server(settings_module: str | None = None, transport: str = "stdio"): +def run_server( + settings_module: str | None = None, + transport: str = "stdio", + host: str = "127.0.0.1", + port: int = 8000, +): """ Run the Django MCP server. Args: settings_module: Django settings module path transport: Transport type (stdio or sse) + host: Host to bind to for SSE transport (default: 127.0.0.1) + port: Port to bind to for SSE transport (default: 8000) """ # Initialize Django before starting the server initialize_django(settings_module) # Run the FastMCP server - mcp.run(transport=transport) + if transport == "sse": + mcp.run(transport=transport, host=host, port=port) + else: + mcp.run(transport=transport) if __name__ == "__main__": diff --git a/uv.lock b/uv.lock index d140a66..45548a5 100644 --- a/uv.lock +++ b/uv.lock @@ -271,7 +271,7 @@ wheels = [ [[package]] name = "django-ai-boost" -version = "0.3.0" +version = "0.3.1" source = { editable = "." } dependencies = [ { name = "django" },