Skip to content
Open
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
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ dependencies = [

[project.optional-dependencies]
mcp = [
"mcp>=1.20,<2",
"mcp>=1.20,<3",
]
all = [
"mcp>=1.20,<2",
"mcp>=1.20,<3",
]
dev = [
"pytest>=8.0",
Expand Down
21 changes: 12 additions & 9 deletions src/nxtflow/mcp_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import os
from collections.abc import Sequence

from mcp.server.fastmcp import FastMCP
from mcp.server.mcpserver import MCPServer
from starlette.requests import Request
from starlette.responses import JSONResponse

Expand All @@ -18,13 +18,10 @@


def create_server(
host: str = "127.0.0.1",
port: int = 8000,
stateless_http: bool = False,
config: NxtflowConfig | None = None,
) -> FastMCP:
"""Create and configure the nxtflow FastMCP server with host/port bound at init time."""
mcp = FastMCP("nxtflow", host=host, port=port, stateless_http=stateless_http)
) -> MCPServer:
"""Create and configure the nxtflow MCPServer."""
mcp = MCPServer("nxtflow")
cfg = config or load_config()

def _client() -> TelemetryBackend:
Expand Down Expand Up @@ -282,8 +279,14 @@ def parse_args(argv: Sequence[str] | None = None) -> argparse.Namespace:

def main(argv: Sequence[str] | None = None) -> None:
args = parse_args(argv)
mcp = create_server(host=args.host, port=args.port, stateless_http=args.stateless_http)
mcp.run(transport=args.transport)
mcp = create_server()

if args.transport == "stdio":
mcp.run(transport=args.transport)
elif args.transport == "streamable-http":
mcp.run(transport=args.transport, host=args.host, port=args.port, stateless_http=args.stateless_http)
elif args.transport == "sse":
mcp.run(transport=args.transport, host=args.host, port=args.port)


if __name__ == "__main__":
Expand Down
10 changes: 5 additions & 5 deletions tests/test_mcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@


def test_mcp_registers_all_10_tools_and_healthz() -> None:
mcp = create_server(host="0.0.0.0", port=8000, stateless_http=True, config=NxtflowConfig())
mcp = create_server(config=NxtflowConfig())
tools = asyncio.run(mcp.list_tools())
names = {t.name for t in tools}
assert names == EXPECTED_TOOLS
Expand Down Expand Up @@ -59,7 +59,7 @@ def test_mcp_all_10_tools_and_input_bounds() -> None:

async def call_tool(name: str, args: dict) -> dict | list:
res = await mcp.call_tool(name, args)
content_blocks = res[0] if isinstance(res, tuple) else res
content_blocks = res.content if hasattr(res, "content") else (res[0] if isinstance(res, tuple) else res)
return json.loads(content_blocks[0].text)

with patch("nxtflow.mcp_server.LokiClient", return_value=mock_client):
Expand Down Expand Up @@ -117,7 +117,7 @@ def test_mcp_30d_summary_and_non_dict_error() -> None:

async def call_tool(name: str, args: dict) -> dict | list:
res = await mcp.call_tool(name, args)
content_blocks = res[0] if isinstance(res, tuple) else res
content_blocks = res.content if hasattr(res, "content") else (res[0] if isinstance(res, tuple) else res)
return json.loads(content_blocks[0].text)

with patch("nxtflow.mcp_server.LokiClient", return_value=mock_client):
Expand Down Expand Up @@ -154,7 +154,7 @@ async def _invoke_all() -> None:
]
for tool_name, params in calls:
res = await mcp.call_tool(tool_name, params)
blocks = res[0] if isinstance(res, tuple) else res
blocks = res.content if hasattr(res, "content") else (res[0] if isinstance(res, tuple) else res)
text = blocks[0].text
payload = json.loads(text)
assert "error" in payload, f"Expected error JSON for {tool_name}, got {payload}"
Expand Down Expand Up @@ -224,7 +224,7 @@ def test_mcp_routes_victorialogs_and_clickhouse_backends() -> None:

async def call_status(server: object = mcp) -> dict:
res = await server.call_tool("nxtflow_status", {"range_str": "5m"}) # type: ignore[attr-defined]
blocks = res[0] if isinstance(res, tuple) else res
blocks = res.content if hasattr(res, "content") else (res[0] if isinstance(res, tuple) else res)
return json.loads(blocks[0].text)

with patch("nxtflow.mcp_server.create_backend_client", return_value=mock_client):
Expand Down