11"""Test for base64 encoding issue in MCP server.
22
3- This test demonstrates the issue in server.py where the server uses
4- urlsafe_b64encode but the BlobResourceContents validator expects standard
3+ This test demonstrates the issue in server.py where the server uses
4+ urlsafe_b64encode but the BlobResourceContents validator expects standard
55base64 encoding.
66
7- The test should FAIL before fixing server.py to use b64encode instead of
7+ The test should FAIL before fixing server.py to use b64encode instead of
88urlsafe_b64encode.
99After the fix, the test should PASS.
1010"""
2929@pytest .mark .anyio
3030async def test_server_base64_encoding_issue ():
3131 """Tests that server response can be validated by BlobResourceContents.
32-
32+
3333 This test will:
3434 1. Set up a server that returns binary data
3535 2. Extract the base64-encoded blob from the server's response
3636 3. Verify the encoded data can be properly validated by BlobResourceContents
37-
37+
3838 BEFORE FIX: The test will fail because server uses urlsafe_b64encode
3939 AFTER FIX: The test will pass because server uses standard b64encode
4040 """
4141 server = Server ("test" )
42-
43- # Create binary data that will definitely result in + and / characters
42+
43+ # Create binary data that will definitely result in + and / characters
4444 # when encoded with standard base64
4545 binary_data = bytes ([x for x in range (255 )] * 4 )
46-
46+
4747 # Register a resource handler that returns our test data
4848 @server .read_resource ()
4949 async def read_resource (uri : AnyUrl ) -> list [ReadResourceContents ]:
50- return [ReadResourceContents (content = binary_data ,
51- mime_type = "application/octet-stream" )]
52-
50+ return [
51+ ReadResourceContents (
52+ content = binary_data , mime_type = "application/octet-stream"
53+ )
54+ ]
55+
5356 # Get the handler directly from the server
5457 handler = server .request_handlers [ReadResourceRequest ]
55-
58+
5659 # Create a request
5760 request = ReadResourceRequest (
5861 method = "resources/read" ,
5962 params = ReadResourceRequestParams (uri = AnyUrl ("test://resource" )),
6063 )
61-
64+
6265 # Call the handler to get the response
6366 result : ServerResult = await handler (request )
64-
65- # Type cast and access the contents
66- read_result : ReadResourceResult = cast (ReadResourceResult , result )
67- blob_content = read_result .root .contents [0 ]
68-
69-
67+
68+ # After (fixed code):
69+ read_result : ReadResourceResult = cast (ReadResourceResult , result .root )
70+ blob_content = read_result .contents [0 ]
71+
7072 # First verify our test data actually produces different encodings
7173 urlsafe_b64 = base64 .urlsafe_b64encode (binary_data ).decode ()
7274 standard_b64 = base64 .b64encode (binary_data ).decode ()
7375 assert urlsafe_b64 != standard_b64 , "Test data doesn't demonstrate"
7476 " encoding difference"
75-
77+
7678 # Now validate the server's output with BlobResourceContents.model_validate
7779 # Before the fix: This should fail with "Invalid base64" because server
7880 # uses urlsafe_b64encode
7981 # After the fix: This should pass because server will use standard b64encode
8082 model_dict = blob_content .model_dump ()
81-
83+
8284 # Direct validation - this will fail before fix, pass after fix
8385 blob_model = BlobResourceContents .model_validate (model_dict )
84-
86+
8587 # Verify we can decode the data back correctly
8688 decoded = base64 .b64decode (blob_model .blob )
87- assert decoded == binary_data
89+ assert decoded == binary_data
0 commit comments