clipwright/tests/test_mcp_stdio.py
juancarlos.cavero 925583efdd feat(mcp): stdio transport for hosts that only launch a subprocess
Some MCP hosts never speak HTTP to a server: they run it as a command and
talk over stdin/stdout. Glama's Dockerfile deployments wrap exactly that
(and a local self-host user can now skip the web server entirely), so the
directory could not introspect our tools at all — mcp.openshorts.app 401s
without a key, by design.

mcp_stdio.py is transport only: mcp_server keeps the protocol, and tool
calls go through the same call_tool -> ASGITransport path, so stdio and
HTTP cannot answer differently. Two things it has to get right, both
covered by the test: stdout is swapped for stderr before app is imported,
because the pipeline prints everywhere and one stray line corrupts the
JSON-RPC stream; and the app's lifespan is entered explicitly, which
ASGITransport does not do on its own.

glama.json declares the maintainer, one of the listing gaps the directory
reports.

Claude-Session: https://claude.ai/code/session_01PkWCDoMsdmSxGXXh4jqF1m
2026-09-07 11:50:39 +02:00

54 lines
2 KiB
Python

"""Stdio transport (mcp_stdio.py), exercised as the subprocess a host launches.
It has to be a real subprocess: the thing most likely to break this transport
is the pipeline's own chatter (the app prints on import and on startup), and
that only shows up when stdout is a pipe. Every line the host reads must be
JSON-RPC and nothing else.
"""
import json
import os
import subprocess
import sys
import pytest
from mcp_server import TOOLS
REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
MESSAGES = [
{"jsonrpc": "2.0", "id": 1, "method": "initialize",
"params": {"protocolVersion": "2025-06-18", "capabilities": {},
"clientInfo": {"name": "pytest", "version": "1"}}},
{"jsonrpc": "2.0", "method": "notifications/initialized"},
{"jsonrpc": "2.0", "id": 2, "method": "tools/list"},
]
@pytest.fixture(scope="module")
def responses():
env = dict(os.environ, BILLING_ENABLED="0", PYTHONPATH=REPO)
env.pop("PROXY_URL", None)
proc = subprocess.run(
[sys.executable, "-u", os.path.join(REPO, "mcp_stdio.py")],
input="".join(json.dumps(m) + "\n" for m in MESSAGES),
capture_output=True, text=True, cwd=REPO, env=env, timeout=300,
)
assert proc.returncode == 0, proc.stderr[-2000:]
return [json.loads(line) for line in proc.stdout.splitlines() if line.strip()]
class TestStdioTransport:
def test_stdout_carries_only_protocol_lines(self, responses):
# The notification gets no reply, and no print() leaked into the stream.
assert [r["id"] for r in responses] == [1, 2]
assert all(r["jsonrpc"] == "2.0" for r in responses)
def test_initialize_reports_the_same_server(self, responses):
result = responses[0]["result"]
assert result["serverInfo"]["name"] == "openshorts"
assert result["protocolVersion"] == "2025-06-18"
def test_tools_match_the_http_transport(self, responses):
names = [t["name"] for t in responses[1]["result"]["tools"]]
assert names == [t["name"] for t in TOOLS]