2
2
import socket
3
3
import time
4
4
import anyio
5
+ from starlette .requests import Request
5
6
import uvicorn
6
7
import pytest
7
8
from pydantic import AnyUrl
8
9
import httpx
9
- from typing import AsyncGenerator
10
+ from typing import AsyncGenerator , Generator
10
11
from starlette .applications import Starlette
11
12
from starlette .routing import Mount , Route
12
13
@@ -56,7 +57,7 @@ async def handle_read_resource(uri: AnyUrl) -> str | bytes:
56
57
)
57
58
58
59
@self .list_tools ()
59
- async def handle_list_tools ():
60
+ async def handle_list_tools () -> list [ Tool ] :
60
61
return [
61
62
Tool (
62
63
name = "test_tool" ,
@@ -66,7 +67,7 @@ async def handle_list_tools():
66
67
]
67
68
68
69
@self .call_tool ()
69
- async def handle_call_tool (name : str , args : dict ):
70
+ async def handle_call_tool (name : str , args : dict ) -> list [ TextContent ] :
70
71
return [TextContent (type = "text" , text = f"Called { name } " )]
71
72
72
73
@@ -76,7 +77,7 @@ def make_server_app() -> Starlette:
76
77
sse = SseServerTransport ("/messages/" )
77
78
server = TestServer ()
78
79
79
- async def handle_sse (request ) :
80
+ async def handle_sse (request : Request ) -> None :
80
81
async with sse .connect_sse (
81
82
request .scope , request .receive , request ._send
82
83
) as streams :
@@ -94,14 +95,7 @@ async def handle_sse(request):
94
95
return app
95
96
96
97
97
- @pytest .fixture (autouse = True )
98
- def space_around_test ():
99
- time .sleep (0.1 )
100
- yield
101
- time .sleep (0.1 )
102
-
103
-
104
- def run_server (server_port : int ):
98
+ def run_server (server_port : int ) -> None :
105
99
app = make_server_app ()
106
100
server = uvicorn .Server (
107
101
config = uvicorn .Config (
@@ -118,7 +112,7 @@ def run_server(server_port: int):
118
112
119
113
120
114
@pytest .fixture ()
121
- def server (server_port : int ):
115
+ def server (server_port : int ) -> Generator [ None , None , None ] :
122
116
proc = multiprocessing .Process (
123
117
target = run_server , kwargs = {"server_port" : server_port }, daemon = True
124
118
)
@@ -161,11 +155,11 @@ async def http_client(server, server_url) -> AsyncGenerator[httpx.AsyncClient, N
161
155
162
156
# Tests
163
157
@pytest .mark .anyio
164
- async def test_raw_sse_connection (http_client : httpx .AsyncClient ):
158
+ async def test_raw_sse_connection (http_client : httpx .AsyncClient ) -> None :
165
159
"""Test the SSE connection establishment simply with an HTTP client."""
166
160
async with anyio .create_task_group () as tg :
167
161
168
- async def connection_test ():
162
+ async def connection_test () -> None :
169
163
async with http_client .stream ("GET" , "/sse" ) as response :
170
164
assert response .status_code == 200
171
165
assert (
@@ -189,7 +183,7 @@ async def connection_test():
189
183
190
184
191
185
@pytest .mark .anyio
192
- async def test_sse_client_basic_connection (server , server_url ) :
186
+ async def test_sse_client_basic_connection (server : None , server_url : str ) -> None :
193
187
async with sse_client (server_url + "/sse" ) as streams :
194
188
async with ClientSession (* streams ) as session :
195
189
# Test initialization
@@ -215,7 +209,7 @@ async def initialized_sse_client_session(
215
209
@pytest .mark .anyio
216
210
async def test_sse_client_happy_request_and_response (
217
211
initialized_sse_client_session : ClientSession ,
218
- ):
212
+ ) -> None :
219
213
session = initialized_sse_client_session
220
214
response = await session .read_resource (uri = AnyUrl ("foobar://should-work" ))
221
215
assert len (response .contents ) == 1
@@ -226,7 +220,7 @@ async def test_sse_client_happy_request_and_response(
226
220
@pytest .mark .anyio
227
221
async def test_sse_client_exception_handling (
228
222
initialized_sse_client_session : ClientSession ,
229
- ):
223
+ ) -> None :
230
224
session = initialized_sse_client_session
231
225
with pytest .raises (McpError , match = "OOPS! no resource with that URI was found" ):
232
226
await session .read_resource (uri = AnyUrl ("xxx://will-not-work" ))
0 commit comments