Skip to content

Add integration test for aconnect_sse using FastAPI and httpx (Fixes #109) #624

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
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
Binary file added tests/test_sse_client_server.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
50 changes: 50 additions & 0 deletions tests/test_sse_client_server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import asyncio
from typing import AsyncGenerator, List

from fastapi import FastAPI
from starlette.responses import StreamingResponse
import uvicorn
from threading import Thread
import httpx
from mcp.client.sse import aconnect_sse

app = FastAPI()

@app.get("/sse")
async def sse_endpoint():
async def event_stream():
for i in range(3):
yield f"data: Hello {i+1}\n\n"
await asyncio.sleep(0.1)
return StreamingResponse(event_stream(), media_type="text/event-stream")

def run_mock_server():
uvicorn.run(app, host="127.0.0.1", port=8012, log_level="warning")

async def main():
server_thread = Thread(target=run_mock_server, daemon=True)
server_thread.start()
await asyncio.sleep(1)

messages = []

async with httpx.AsyncClient() as client:
async with aconnect_sse(client, "GET", "http://127.0.0.1:8012/sse") as event_source:
async for event in event_source.aiter_sse():
if event.data:
print(" Event received:", event.data)
messages.append(event.data)
if len(messages) == 3:
break

assert messages == ["Hello 1", "Hello 2", "Hello 3"]
print("\n Test passed! SSE connection via aconnect_sse worked correctly.")

await main()


# In[ ]:




Loading