|
| 1 | +from typing import Any |
| 2 | + |
| 3 | +import anyio |
| 4 | +import pytest |
| 5 | + |
| 6 | +from mcp.client.client_session_summarizing import ( |
| 7 | + DEFAULT_MAX_TOKENS, |
| 8 | + DEFAULT_SUMMARIZE_THRESHOLD, |
| 9 | + DEFAULT_SUMMARY_PROMPT, |
| 10 | + ClientSessionSummarizing, |
| 11 | +) |
| 12 | +from mcp.shared.context import RequestContext |
| 13 | +from mcp.types import ( |
| 14 | + CreateMessageRequestParams, |
| 15 | + CreateMessageResult, |
| 16 | + SamplingMessage, |
| 17 | + TextContent, |
| 18 | +) |
| 19 | + |
| 20 | + |
| 21 | +@pytest.mark.asyncio |
| 22 | +async def test_summarizing_session(): |
| 23 | + send_stream, receive_stream = anyio.create_memory_object_stream(10) |
| 24 | + try: |
| 25 | + session = ClientSessionSummarizing( |
| 26 | + read_stream=receive_stream, |
| 27 | + write_stream=send_stream, |
| 28 | + ) |
| 29 | + |
| 30 | + # Create real messages instead of simple strings |
| 31 | + messages = [SamplingMessage(role="user", content=TextContent(type="text", text="Hello")) for _ in range(3500)] |
| 32 | + session.history = messages # Simulate approaching token limit |
| 33 | + |
| 34 | + assert session.token_count() > session.max_tokens * session.summarize_threshold |
| 35 | + |
| 36 | + # Test that summarization works |
| 37 | + await session.summarize_context() |
| 38 | + |
| 39 | + # After summarization, history should contain only one message |
| 40 | + assert len(session.history) == 1 |
| 41 | + assert isinstance(session.history[0], SamplingMessage) |
| 42 | + assert session.history[0].role == "assistant" |
| 43 | + |
| 44 | + finally: |
| 45 | + await send_stream.aclose() |
| 46 | + await receive_stream.aclose() |
| 47 | + |
| 48 | + |
| 49 | +@pytest.mark.asyncio |
| 50 | +async def test_sampling_callback(): |
| 51 | + """Test sampling callback with ClientSessionSummarizing""" |
| 52 | + send_stream, receive_stream = anyio.create_memory_object_stream(10) |
| 53 | + try: |
| 54 | + session = ClientSessionSummarizing( |
| 55 | + read_stream=receive_stream, |
| 56 | + write_stream=send_stream, |
| 57 | + ) |
| 58 | + |
| 59 | + # Create request parameters |
| 60 | + params = CreateMessageRequestParams( |
| 61 | + messages=[SamplingMessage(role="user", content=TextContent(type="text", text="Hello world"))], maxTokens=100 |
| 62 | + ) |
| 63 | + |
| 64 | + # Create simple context for testing |
| 65 | + context: Any = RequestContext(session=session, request_id=1, meta=None, lifespan_context=None) |
| 66 | + |
| 67 | + # Call sampling callback |
| 68 | + result = await session._summarizing_sampling_callback(context, params) |
| 69 | + |
| 70 | + # Verify the result is correct |
| 71 | + assert isinstance(result, CreateMessageResult) |
| 72 | + assert result.role == "assistant" |
| 73 | + assert isinstance(result.content, TextContent) |
| 74 | + assert "Message processed with summarization" in result.content.text |
| 75 | + |
| 76 | + # Verify message was added to history |
| 77 | + assert len(session.history) == 1 |
| 78 | + assert session.history[0].role == "user" |
| 79 | + assert isinstance(session.history[0].content, TextContent) |
| 80 | + assert session.history[0].content.text == "Hello world" |
| 81 | + |
| 82 | + finally: |
| 83 | + await send_stream.aclose() |
| 84 | + await receive_stream.aclose() |
| 85 | + |
| 86 | + |
| 87 | +@pytest.mark.asyncio |
| 88 | +async def test_custom_summary_prompt(): |
| 89 | + """Test that user can define custom prompt""" |
| 90 | + send_stream, receive_stream = anyio.create_memory_object_stream(10) |
| 91 | + try: |
| 92 | + custom_prompt = "Custom summary prompt:\n\n" |
| 93 | + session = ClientSessionSummarizing( |
| 94 | + read_stream=receive_stream, |
| 95 | + write_stream=send_stream, |
| 96 | + summary_prompt=custom_prompt, |
| 97 | + ) |
| 98 | + |
| 99 | + # Verify user can define custom prompt |
| 100 | + assert session.summary_prompt == custom_prompt |
| 101 | + assert session.summary_prompt != DEFAULT_SUMMARY_PROMPT |
| 102 | + |
| 103 | + # Test that summarization uses custom prompt |
| 104 | + session.history = [SamplingMessage(role="user", content=TextContent(type="text", text="Test message"))] |
| 105 | + |
| 106 | + await session.summarize_context() |
| 107 | + |
| 108 | + # Verify summary contains custom prompt |
| 109 | + assert len(session.history) == 1 |
| 110 | + summary_content = session.history[0].content |
| 111 | + assert isinstance(summary_content, TextContent) |
| 112 | + assert custom_prompt in summary_content.text |
| 113 | + |
| 114 | + finally: |
| 115 | + await send_stream.aclose() |
| 116 | + await receive_stream.aclose() |
| 117 | + |
| 118 | + |
| 119 | +@pytest.mark.asyncio |
| 120 | +async def test_default_summary_prompt(): |
| 121 | + """Test that user gets default prompt if not specified""" |
| 122 | + send_stream, receive_stream = anyio.create_memory_object_stream(10) |
| 123 | + try: |
| 124 | + session = ClientSessionSummarizing( |
| 125 | + read_stream=receive_stream, |
| 126 | + write_stream=send_stream, |
| 127 | + ) |
| 128 | + |
| 129 | + # Verify user gets default prompt |
| 130 | + assert session.summary_prompt == DEFAULT_SUMMARY_PROMPT |
| 131 | + |
| 132 | + finally: |
| 133 | + await send_stream.aclose() |
| 134 | + await receive_stream.aclose() |
| 135 | + |
| 136 | + |
| 137 | +@pytest.mark.asyncio |
| 138 | +async def test_custom_max_tokens(): |
| 139 | + """Test that user can define custom max tokens""" |
| 140 | + send_stream, receive_stream = anyio.create_memory_object_stream(10) |
| 141 | + try: |
| 142 | + custom_max_tokens = 2000 |
| 143 | + session = ClientSessionSummarizing( |
| 144 | + read_stream=receive_stream, |
| 145 | + write_stream=send_stream, |
| 146 | + max_tokens=custom_max_tokens, |
| 147 | + ) |
| 148 | + |
| 149 | + # Verify user can define custom max tokens |
| 150 | + assert session.max_tokens == custom_max_tokens |
| 151 | + assert session.max_tokens != DEFAULT_MAX_TOKENS |
| 152 | + |
| 153 | + finally: |
| 154 | + await send_stream.aclose() |
| 155 | + await receive_stream.aclose() |
| 156 | + |
| 157 | + |
| 158 | +@pytest.mark.asyncio |
| 159 | +async def test_custom_summarize_threshold(): |
| 160 | + """Test that user can define custom summarize threshold""" |
| 161 | + send_stream, receive_stream = anyio.create_memory_object_stream(10) |
| 162 | + try: |
| 163 | + custom_threshold = 0.5 |
| 164 | + session = ClientSessionSummarizing( |
| 165 | + read_stream=receive_stream, |
| 166 | + write_stream=send_stream, |
| 167 | + summarize_threshold=custom_threshold, |
| 168 | + ) |
| 169 | + |
| 170 | + # Verify user can define custom threshold |
| 171 | + assert session.summarize_threshold == custom_threshold |
| 172 | + assert session.summarize_threshold != DEFAULT_SUMMARIZE_THRESHOLD |
| 173 | + |
| 174 | + finally: |
| 175 | + await send_stream.aclose() |
| 176 | + await receive_stream.aclose() |
| 177 | + |
| 178 | + |
| 179 | +@pytest.mark.asyncio |
| 180 | +async def test_default_parameters(): |
| 181 | + """Test that user gets default parameters if not specified""" |
| 182 | + send_stream, receive_stream = anyio.create_memory_object_stream(10) |
| 183 | + try: |
| 184 | + session = ClientSessionSummarizing( |
| 185 | + read_stream=receive_stream, |
| 186 | + write_stream=send_stream, |
| 187 | + ) |
| 188 | + |
| 189 | + # Verify user gets default parameters |
| 190 | + assert session.max_tokens == DEFAULT_MAX_TOKENS |
| 191 | + assert session.summarize_threshold == DEFAULT_SUMMARIZE_THRESHOLD |
| 192 | + assert session.summary_prompt == DEFAULT_SUMMARY_PROMPT |
| 193 | + |
| 194 | + finally: |
| 195 | + await send_stream.aclose() |
| 196 | + await receive_stream.aclose() |
0 commit comments