-
Notifications
You must be signed in to change notification settings - Fork 13.1k
Expand file tree
/
Copy pathtest_stdio_protocols.py
More file actions
165 lines (142 loc) · 4.92 KB
/
Copy pathtest_stdio_protocols.py
File metadata and controls
165 lines (142 loc) · 4.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/env python3 -m pytest
"""Drive the stdio server through both MCP handshake eras.
The server must answer clients that open with the legacy `initialize`
handshake as well as clients that open with the 2026-07-28 `server/discover`
request, since both are in use across MCP hosts.
"""
import json
import os
import subprocess
import sys
import tempfile
import threading
import time
import pytest
MODERN_ENVELOPE = {
"io.modelcontextprotocol/clientCapabilities": {},
"io.modelcontextprotocol/clientInfo": {"name": "test", "version": "0"},
"io.modelcontextprotocol/protocolVersion": "2026-07-28",
}
EXPECTED_MARKDOWN = "# Hello\n\nA **markitdown** fixture."
@pytest.fixture
def fixture_uri():
with tempfile.TemporaryDirectory() as tmpdir:
path = os.path.join(tmpdir, "sample.md")
with open(path, "w") as fh:
fh.write(EXPECTED_MARKDOWN + "\n")
yield "file://" + path
def run_server(requests, expected_responses):
"""Feed newline-delimited requests to the stdio server, return the responses.
Stdin is held open until the expected responses arrive: closing it early
tears the server down before an in-flight request can be answered.
"""
process = subprocess.Popen(
[sys.executable, "-m", "markitdown_mcp"],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL,
text=True,
)
responses = []
try:
for request in requests:
process.stdin.write(json.dumps(request) + "\n")
process.stdin.flush()
reader = threading.Thread(
target=lambda: [
responses.append(json.loads(line))
for line in process.stdout
if line.strip()
],
daemon=True,
)
reader.start()
deadline = time.monotonic() + 60
while len(responses) < expected_responses and time.monotonic() < deadline:
time.sleep(0.05)
finally:
process.stdin.close()
process.terminate()
process.wait(timeout=10)
return sorted(responses, key=lambda response: response["id"])
def test_legacy_initialize_handshake(fixture_uri):
"""A client opening with `initialize` gets the pre-2026 protocol."""
responses = run_server(
[
{
"jsonrpc": "2.0",
"id": 1,
"method": "initialize",
"params": {
"protocolVersion": "2025-06-18",
"capabilities": {},
"clientInfo": {"name": "test", "version": "0"},
},
},
{"jsonrpc": "2.0", "method": "notifications/initialized"},
{
"jsonrpc": "2.0",
"id": 2,
"method": "tools/call",
"params": {
"name": "convert_to_markdown",
"arguments": {"uri": fixture_uri},
},
},
],
expected_responses=2,
)
assert responses[0]["result"]["serverInfo"]["name"] == "markitdown"
call = responses[1]["result"]
assert call["isError"] is False
assert EXPECTED_MARKDOWN in call["content"][0]["text"]
def test_modern_discover_handshake(fixture_uri):
"""A client opening with `server/discover` gets the 2026-07-28 protocol."""
responses = run_server(
[
{
"jsonrpc": "2.0",
"id": 1,
"method": "server/discover",
"params": {"_meta": MODERN_ENVELOPE},
},
{
"jsonrpc": "2.0",
"id": 2,
"method": "tools/call",
"params": {
"name": "convert_to_markdown",
"arguments": {"uri": fixture_uri},
"_meta": MODERN_ENVELOPE,
},
},
],
expected_responses=2,
)
discover = responses[0]["result"]
assert "2026-07-28" in discover["supportedVersions"]
call = responses[1]["result"]
assert call["isError"] is False
assert EXPECTED_MARKDOWN in call["content"][0]["text"]
def test_unknown_method_does_not_kill_the_server(fixture_uri):
"""An unrecognized method is answered, and the session stays usable."""
responses = run_server(
[
{"jsonrpc": "2.0", "id": 1, "method": "totally/bogus", "params": {}},
{
"jsonrpc": "2.0",
"id": 2,
"method": "initialize",
"params": {
"protocolVersion": "2025-06-18",
"capabilities": {},
"clientInfo": {"name": "test", "version": "0"},
},
},
],
expected_responses=2,
)
assert "error" in responses[0]
assert responses[1]["result"]["serverInfo"]["name"] == "markitdown"
if __name__ == "__main__":
sys.exit(pytest.main([__file__, "-v"]))