|
| 1 | +"""Tests for client-side validation options.""" |
| 2 | + |
| 3 | +import logging |
| 4 | +from contextlib import contextmanager |
| 5 | +from unittest.mock import patch |
| 6 | + |
| 7 | +import pytest |
| 8 | + |
| 9 | +from mcp.client.session import ValidationOptions |
| 10 | +from mcp.server.lowlevel import Server |
| 11 | +from mcp.shared.memory import ( |
| 12 | + create_connected_server_and_client_session as client_session, |
| 13 | +) |
| 14 | +from mcp.types import Tool |
| 15 | + |
| 16 | + |
| 17 | +@contextmanager |
| 18 | +def bypass_server_output_validation(): |
| 19 | + """ |
| 20 | + Context manager that bypasses server-side output validation. |
| 21 | + This simulates a non-compliant server that doesn't validate its outputs. |
| 22 | + """ |
| 23 | + with patch("mcp.server.lowlevel.server.jsonschema.validate"): |
| 24 | + yield |
| 25 | + |
| 26 | + |
| 27 | +class TestValidationOptions: |
| 28 | + """Test validation options for MCP client sessions.""" |
| 29 | + |
| 30 | + @pytest.mark.anyio |
| 31 | + async def test_strict_validation_default(self): |
| 32 | + """Test that strict validation is enabled by default.""" |
| 33 | + server = Server("test-server") |
| 34 | + |
| 35 | + output_schema = { |
| 36 | + "type": "object", |
| 37 | + "properties": {"result": {"type": "integer"}}, |
| 38 | + "required": ["result"], |
| 39 | + } |
| 40 | + |
| 41 | + @server.list_tools() |
| 42 | + async def list_tools(): |
| 43 | + return [ |
| 44 | + Tool( |
| 45 | + name="test_tool", |
| 46 | + description="Test tool", |
| 47 | + inputSchema={"type": "object"}, |
| 48 | + outputSchema=output_schema, |
| 49 | + ) |
| 50 | + ] |
| 51 | + |
| 52 | + @server.call_tool() |
| 53 | + async def call_tool(name: str, arguments: dict): |
| 54 | + # Return unstructured content instead of structured content |
| 55 | + # This will trigger the validation error we want to test |
| 56 | + return "This is unstructured text content" |
| 57 | + |
| 58 | + with bypass_server_output_validation(): |
| 59 | + async with client_session(server) as client: |
| 60 | + # Should raise by default |
| 61 | + with pytest.raises(RuntimeError) as exc_info: |
| 62 | + await client.call_tool("test_tool", {}) |
| 63 | + assert "has an output schema but did not return structured content" in str(exc_info.value) |
| 64 | + |
| 65 | + @pytest.mark.anyio |
| 66 | + async def test_lenient_validation_missing_content(self, caplog): |
| 67 | + """Test lenient validation when structured content is missing.""" |
| 68 | + server = Server("test-server") |
| 69 | + |
| 70 | + output_schema = { |
| 71 | + "type": "object", |
| 72 | + "properties": {"result": {"type": "integer"}}, |
| 73 | + "required": ["result"], |
| 74 | + } |
| 75 | + |
| 76 | + @server.list_tools() |
| 77 | + async def list_tools(): |
| 78 | + return [ |
| 79 | + Tool( |
| 80 | + name="test_tool", |
| 81 | + description="Test tool", |
| 82 | + inputSchema={"type": "object"}, |
| 83 | + outputSchema=output_schema, |
| 84 | + ) |
| 85 | + ] |
| 86 | + |
| 87 | + @server.call_tool() |
| 88 | + async def call_tool(name: str, arguments: dict): |
| 89 | + # Return unstructured content instead of structured content |
| 90 | + # This will trigger the validation error we want to test |
| 91 | + return "This is unstructured text content" |
| 92 | + |
| 93 | + # Set logging level to capture warnings |
| 94 | + caplog.set_level(logging.WARNING) |
| 95 | + |
| 96 | + # Create client with lenient validation |
| 97 | + validation_options = ValidationOptions(strict_output_validation=False) |
| 98 | + |
| 99 | + with bypass_server_output_validation(): |
| 100 | + async with client_session(server, validation_options=validation_options) as client: |
| 101 | + # Should not raise with lenient validation |
| 102 | + result = await client.call_tool("test_tool", {}) |
| 103 | + |
| 104 | + # Should have logged a warning |
| 105 | + assert "has an output schema but did not return structured content" in caplog.text |
| 106 | + assert "Continuing without structured content validation" in caplog.text |
| 107 | + |
| 108 | + # Result should still be returned |
| 109 | + assert result.isError is False |
| 110 | + assert result.structuredContent is None |
| 111 | + |
| 112 | + @pytest.mark.anyio |
| 113 | + async def test_lenient_validation_invalid_content(self, caplog): |
| 114 | + """Test lenient validation when structured content is invalid.""" |
| 115 | + server = Server("test-server") |
| 116 | + |
| 117 | + output_schema = { |
| 118 | + "type": "object", |
| 119 | + "properties": {"result": {"type": "integer"}}, |
| 120 | + "required": ["result"], |
| 121 | + } |
| 122 | + |
| 123 | + @server.list_tools() |
| 124 | + async def list_tools(): |
| 125 | + return [ |
| 126 | + Tool( |
| 127 | + name="test_tool", |
| 128 | + description="Test tool", |
| 129 | + inputSchema={"type": "object"}, |
| 130 | + outputSchema=output_schema, |
| 131 | + ) |
| 132 | + ] |
| 133 | + |
| 134 | + @server.call_tool() |
| 135 | + async def call_tool(name: str, arguments: dict): |
| 136 | + # Return invalid structured content (string instead of integer) |
| 137 | + return {"result": "not_an_integer"} |
| 138 | + |
| 139 | + # Set logging level to capture warnings |
| 140 | + caplog.set_level(logging.WARNING) |
| 141 | + |
| 142 | + # Create client with lenient validation |
| 143 | + validation_options = ValidationOptions(strict_output_validation=False) |
| 144 | + |
| 145 | + with bypass_server_output_validation(): |
| 146 | + async with client_session(server, validation_options=validation_options) as client: |
| 147 | + # Should not raise with lenient validation |
| 148 | + result = await client.call_tool("test_tool", {}) |
| 149 | + |
| 150 | + # Should have logged a warning |
| 151 | + assert "Invalid structured content returned by tool test_tool" in caplog.text |
| 152 | + assert "Continuing due to lenient validation mode" in caplog.text |
| 153 | + |
| 154 | + # Result should still be returned with the invalid content |
| 155 | + assert result.isError is False |
| 156 | + assert result.structuredContent == {"result": "not_an_integer"} |
| 157 | + |
| 158 | + @pytest.mark.anyio |
| 159 | + async def test_strict_validation_with_valid_content(self): |
| 160 | + """Test that valid content passes with strict validation.""" |
| 161 | + server = Server("test-server") |
| 162 | + |
| 163 | + output_schema = { |
| 164 | + "type": "object", |
| 165 | + "properties": {"result": {"type": "integer"}}, |
| 166 | + "required": ["result"], |
| 167 | + } |
| 168 | + |
| 169 | + @server.list_tools() |
| 170 | + async def list_tools(): |
| 171 | + return [ |
| 172 | + Tool( |
| 173 | + name="test_tool", |
| 174 | + description="Test tool", |
| 175 | + inputSchema={"type": "object"}, |
| 176 | + outputSchema=output_schema, |
| 177 | + ) |
| 178 | + ] |
| 179 | + |
| 180 | + @server.call_tool() |
| 181 | + async def call_tool(name: str, arguments: dict): |
| 182 | + # Return valid structured content |
| 183 | + return {"result": 42} |
| 184 | + |
| 185 | + async with client_session(server) as client: |
| 186 | + # Should succeed with valid content |
| 187 | + result = await client.call_tool("test_tool", {}) |
| 188 | + assert result.isError is False |
| 189 | + assert result.structuredContent == {"result": 42} |
| 190 | + |
| 191 | + @pytest.mark.anyio |
| 192 | + async def test_schema_errors_always_raised(self): |
| 193 | + """Test that schema errors are always raised regardless of validation mode.""" |
| 194 | + server = Server("test-server") |
| 195 | + |
| 196 | + # Invalid schema (missing required 'type' field) |
| 197 | + output_schema = {"properties": {"result": {}}} |
| 198 | + |
| 199 | + @server.list_tools() |
| 200 | + async def list_tools(): |
| 201 | + return [ |
| 202 | + Tool( |
| 203 | + name="test_tool", |
| 204 | + description="Test tool", |
| 205 | + inputSchema={"type": "object"}, |
| 206 | + outputSchema=output_schema, |
| 207 | + ) |
| 208 | + ] |
| 209 | + |
| 210 | + @server.call_tool() |
| 211 | + async def call_tool(name: str, arguments: dict): |
| 212 | + return {"result": 42} |
| 213 | + |
| 214 | + # Test with lenient validation |
| 215 | + validation_options = ValidationOptions(strict_output_validation=False) |
| 216 | + |
| 217 | + with bypass_server_output_validation(): |
| 218 | + async with client_session(server, validation_options=validation_options) as client: |
| 219 | + # Should still raise for schema errors |
| 220 | + with pytest.raises(RuntimeError) as exc_info: |
| 221 | + await client.call_tool("test_tool", {}) |
| 222 | + assert "Invalid schema for tool test_tool" in str(exc_info.value) |
0 commit comments