Skip to content

Commit e1b8b1d

Browse files
Add test for aconnect_sse to resolve modelcontextprotocol#109
1 parent 2cbd155 commit e1b8b1d

File tree

1 file changed

+118
-0
lines changed

1 file changed

+118
-0
lines changed

tests/test_sse_client_server.py

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
#!/usr/bin/env python
2+
# coding: utf-8
3+
4+
# In[1]:
5+
6+
7+
get_ipython().run_line_magic('cd', 'C:/Users/vinis/Documents/python-sdk')
8+
9+
10+
# In[2]:
11+
12+
13+
import os
14+
print(os.getcwd())
15+
16+
17+
# In[3]:
18+
19+
20+
import sys, os
21+
sys.path.append(os.path.abspath("src"))
22+
23+
24+
# In[4]:
25+
26+
27+
pip install fastapi uvicorn httpx httpx-sse sse-starlette anyio
28+
29+
30+
# In[5]:
31+
32+
33+
pip install -e .
34+
35+
36+
# In[6]:
37+
38+
39+
import os
40+
41+
for root, dirs, files in os.walk("C:/Users/vinis/Documents/python-sdk"):
42+
for file in files:
43+
if file == "sse.py":
44+
print(os.path.join(root, file))
45+
46+
47+
# In[7]:
48+
49+
50+
import sys, os
51+
sys.path.append(os.path.abspath("src"))
52+
53+
import mcp.client.sse as sse_module
54+
55+
print("Top-level definitions in mcp.client.sse:")
56+
print(dir(sse_module))
57+
58+
59+
# In[8]:
60+
61+
62+
import inspect
63+
from mcp.client.sse import aconnect_sse
64+
65+
print(inspect.signature(aconnect_sse))
66+
67+
68+
# In[9]:
69+
70+
71+
import asyncio
72+
from fastapi import FastAPI
73+
from starlette.responses import StreamingResponse
74+
import uvicorn
75+
from threading import Thread
76+
import httpx
77+
from mcp.client.sse import aconnect_sse
78+
79+
app = FastAPI()
80+
81+
@app.get("/sse")
82+
async def sse_endpoint():
83+
async def event_stream():
84+
for i in range(3):
85+
yield f"data: Hello {i+1}\n\n"
86+
await asyncio.sleep(0.1)
87+
return StreamingResponse(event_stream(), media_type="text/event-stream")
88+
89+
def run_mock_server():
90+
uvicorn.run(app, host="127.0.0.1", port=8012, log_level="warning")
91+
92+
async def main():
93+
server_thread = Thread(target=run_mock_server, daemon=True)
94+
server_thread.start()
95+
await asyncio.sleep(1)
96+
97+
messages = []
98+
99+
async with httpx.AsyncClient() as client:
100+
async with aconnect_sse(client, "GET", "http://127.0.0.1:8012/sse") as event_source:
101+
async for event in event_source.aiter_sse():
102+
if event.data:
103+
print(" Event received:", event.data)
104+
messages.append(event.data)
105+
if len(messages) == 3:
106+
break
107+
108+
assert messages == ["Hello 1", "Hello 2", "Hello 3"]
109+
print("\n Test passed! SSE connection via aconnect_sse worked correctly.")
110+
111+
await main()
112+
113+
114+
# In[ ]:
115+
116+
117+
118+

0 commit comments

Comments
 (0)