|
| 1 | +from typing import Any |
| 2 | + |
| 3 | +import httpx |
| 4 | +import pytest |
| 5 | + |
| 6 | +import mcp.types as types |
| 7 | +from mcp.client.session import ClientSession |
| 8 | +from mcp.client.streamable_http import streamable_http_client |
| 9 | +from mcp.server import Server |
| 10 | + |
| 11 | + |
| 12 | +@pytest.fixture |
| 13 | +def server() -> Server: |
| 14 | + server_app = Server("test_server") |
| 15 | + |
| 16 | + @server_app.call_tool() |
| 17 | + async def my_tool(name: str, arguments: dict[str, Any]) -> list[types.TextContent]: |
| 18 | + return [types.TextContent(type="text", text="Tool result")] |
| 19 | + |
| 20 | + @server_app.list_tools() |
| 21 | + async def list_tools(): |
| 22 | + return [ |
| 23 | + types.Tool( |
| 24 | + name="my_tool", |
| 25 | + description="Test tool", |
| 26 | + input_schema={ |
| 27 | + "type": "object", |
| 28 | + "properties": {}, |
| 29 | + }, |
| 30 | + ) |
| 31 | + ] |
| 32 | + |
| 33 | + return server_app |
| 34 | + |
| 35 | + |
| 36 | +HOST = "testserver" |
| 37 | + |
| 38 | + |
| 39 | +@pytest.mark.anyio |
| 40 | +async def test_streamable_http_server_cleanup(server: Server): |
| 41 | + mcp_app = server.streamable_http_app(host=HOST) |
| 42 | + async with ( |
| 43 | + mcp_app.router.lifespan_context(mcp_app), |
| 44 | + httpx.ASGITransport(mcp_app) as transport, |
| 45 | + httpx.AsyncClient(transport=transport) as client, |
| 46 | + streamable_http_client(f"http://{HOST}/mcp", http_client=client) as (read_stream, write_stream), |
| 47 | + ClientSession(read_stream, write_stream) as session, |
| 48 | + ): |
| 49 | + await session.initialize() |
| 50 | + await session.call_tool("my_tool", arguments={}) |
0 commit comments