Skip to content

Commit 003683e

Browse files
committed
feat: align with MCP Python SDK best practices
- FastMCP initialization with host/port from environment variables - Add stateless_http=True and json_response=True for production - Use MCP_HOST/MCP_PORT env vars instead of CLI args - Simplify docker-compose.yml to use environment variables - Update Dockerfile with default MCP_HOST=0.0.0.0, MCP_PORT=8000
1 parent b9138a5 commit 003683e

File tree

3 files changed

+36
-15
lines changed

3 files changed

+36
-15
lines changed

Dockerfile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ WORKDIR /app
55
# Environment variables
66
ENV UV_COMPILE_BYTECODE=1 \
77
PYTHONUNBUFFERED=1 \
8-
PYTHONDONTWRITEBYTECODE=1
8+
PYTHONDONTWRITEBYTECODE=1 \
9+
# MCP server defaults for HTTP transports
10+
MCP_HOST=0.0.0.0 \
11+
MCP_PORT=8000
912

1013
# Install uv
1114
COPY --from=ghcr.io/astral-sh/uv:latest /uv /uvx /bin/
@@ -31,5 +34,5 @@ ENTRYPOINT ["uv", "run", "src/server.py"]
3134

3235
# Default arguments (can be overridden)
3336
# For stdio (default MCP transport): no args needed
34-
# For HTTP: --transport streamable-http --host 0.0.0.0 --port 8000
37+
# For HTTP: --transport streamable-http
3538
CMD ["--transport", "stdio"]

docker-compose.yml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ services:
1515
# Usage: docker-compose up mcp-trino-http
1616
mcp-trino-http:
1717
build: .
18-
command: ["--transport", "streamable-http", "--host", "0.0.0.0", "--port", "8000"]
18+
command: ["--transport", "streamable-http"]
1919
ports:
2020
- "8000:8000"
2121
environment:
@@ -24,6 +24,8 @@ services:
2424
- TRINO_USER=trino
2525
- TRINO_CATALOG=tpch
2626
- TRINO_SCHEMA=tiny
27+
- MCP_HOST=0.0.0.0
28+
- MCP_PORT=8000
2729
depends_on:
2830
trino:
2931
condition: service_healthy
@@ -35,7 +37,7 @@ services:
3537
# Usage: docker-compose --profile sse up mcp-trino-sse
3638
mcp-trino-sse:
3739
build: .
38-
command: ["--transport", "sse", "--host", "0.0.0.0", "--port", "8000"]
40+
command: ["--transport", "sse"]
3941
ports:
4042
- "8001:8000"
4143
environment:
@@ -44,6 +46,8 @@ services:
4446
- TRINO_USER=trino
4547
- TRINO_CATALOG=tpch
4648
- TRINO_SCHEMA=tiny
49+
- MCP_HOST=0.0.0.0
50+
- MCP_PORT=8000
4751
depends_on:
4852
trino:
4953
condition: service_healthy

src/server.py

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
functionality through resources and tools, with special support for Iceberg tables.
55
"""
66

7+
import os
8+
79
from mcp.server.fastmcp import FastMCP
810
from mcp.server.fastmcp.prompts import base
911
from pydantic import Field
@@ -16,11 +18,16 @@
1618
client = TrinoClient(config)
1719

1820

19-
# Initialize the MCP server with context
21+
# Initialize the MCP server with recommended settings for all transports
2022
mcp = FastMCP(
2123
name="Trino Explorer",
2224
instructions="This Model Context Protocol (MCP) server provides access to Trino Query Engine.",
23-
dependencies=["trino", "python-dotenv", "loguru"],
25+
# HTTP transport settings (used by streamable-http and sse)
26+
host=os.getenv("MCP_HOST", "127.0.0.1"),
27+
port=int(os.getenv("MCP_PORT", "8000")),
28+
# Recommended for production HTTP deployments
29+
stateless_http=True,
30+
json_response=True,
2431
)
2532

2633

@@ -561,17 +568,21 @@ def maintain_iceberg(table: str, catalog: str, schema_name: str) -> list[base.Me
561568
562569
# Run with SSE transport
563570
python server.py --transport sse --host 127.0.0.1 --port 8001
571+
572+
Environment variables:
573+
MCP_HOST Default host for HTTP transports (default: 127.0.0.1)
574+
MCP_PORT Default port for HTTP transports (default: 8000)
564575
""",
565576
)
566577
parser.add_argument(
567578
"--host",
568-
default="127.0.0.1",
579+
default=None,
569580
help="Host to bind to (default: 127.0.0.1, use 0.0.0.0 for all interfaces)",
570581
)
571582
parser.add_argument(
572583
"--port",
573584
type=int,
574-
default=8000,
585+
default=None,
575586
help="Port to listen on (default: 8000)",
576587
)
577588
parser.add_argument(
@@ -582,22 +593,25 @@ def maintain_iceberg(table: str, catalog: str, schema_name: str) -> list[base.Me
582593
)
583594
args = parser.parse_args()
584595

596+
# Update settings if CLI args provided (override env vars)
597+
if args.host:
598+
mcp.settings.host = args.host
599+
if args.port:
600+
mcp.settings.port = args.port
601+
585602
logger.info(f"Starting Trino MCP server with {args.transport} transport")
586603

587604
if args.transport == "stdio":
588605
logger.info("Using stdio transport for local MCP communication")
589606
mcp.run(transport="stdio")
590607
elif args.transport == "streamable-http":
591-
logger.info(f"Starting Streamable HTTP server on http://{args.host}:{args.port}/mcp")
592-
mcp.run(transport="streamable-http", host=args.host, port=args.port)
608+
logger.info(f"Starting Streamable HTTP server on http://{mcp.settings.host}:{mcp.settings.port}/mcp")
609+
mcp.run(transport="streamable-http")
593610
elif args.transport == "sse":
594-
logger.info(f"Starting SSE server on http://{args.host}:{args.port}/sse")
595-
mcp.run(transport="sse", host=args.host, port=args.port)
611+
logger.info(f"Starting SSE server on http://{mcp.settings.host}:{mcp.settings.port}/sse")
612+
mcp.run(transport="sse")
596613

597614

598615
def main():
599616
"""Entry point for the MCP Trino server."""
600-
import sys
601-
602-
sys.argv = sys.argv[:1] # Reset args for mcp.run
603617
mcp.run(transport="stdio")

0 commit comments

Comments
 (0)