Skip to content

Commit c403341

Browse files
committed
ruff and pyright
1 parent cf1b7cc commit c403341

File tree

4 files changed

+30
-32
lines changed

4 files changed

+30
-32
lines changed

src/mcp/server/fastmcp/server.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -652,9 +652,9 @@ async def read_resource(self, uri: str | AnyUrl) -> Iterable[ReadResourceContent
652652
Returns:
653653
The resource content as either text or bytes
654654
"""
655-
assert self._fastmcp is not None, (
656-
"Context is not available outside of a request"
657-
)
655+
assert (
656+
self._fastmcp is not None
657+
), "Context is not available outside of a request"
658658
return await self._fastmcp.read_resource(uri)
659659

660660
async def log(
Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
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
55
base64 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
88
urlsafe_b64encode.
99
After the fix, the test should PASS.
1010
"""
@@ -29,59 +29,61 @@
2929
@pytest.mark.anyio
3030
async 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

tests/shared/test_sse.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,7 @@ def server(server_port: int) -> Generator[None, None, None]:
138138
time.sleep(0.1)
139139
attempt += 1
140140
else:
141-
raise RuntimeError(
142-
f"Server failed to start after {max_attempts} attempts"
143-
)
141+
raise RuntimeError(f"Server failed to start after {max_attempts} attempts")
144142

145143
yield
146144

tests/shared/test_ws.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,7 @@ def server(server_port: int) -> Generator[None, None, None]:
134134
time.sleep(0.1)
135135
attempt += 1
136136
else:
137-
raise RuntimeError(
138-
f"Server failed to start after {max_attempts} attempts"
139-
)
137+
raise RuntimeError(f"Server failed to start after {max_attempts} attempts")
140138

141139
yield
142140

0 commit comments

Comments
 (0)