Skip to content

πŸš€ The fast, Pythonic way to build MCP servers and clients.

Fix File helper extension handling (#4531)

VectorPeak74b8f1bc1c20
diff --git a/fastmcp_slim/fastmcp/utilities/types.py b/fastmcp_slim/fastmcp/utilities/types.py
index fe43c9081..47d132e63 100644
--- a/fastmcp_slim/fastmcp/utilities/types.py
+++ b/fastmcp_slim/fastmcp/utilities/types.py
@@ -421,7 +421,12 @@ class File:
elif self.data is not None:
raw_data = self.data
if self._name:
- uri_str = f"file:///{self._name}.{self._mime_type.split('/')[1]}"
+ extension = (
+ ""
+ if Path(self._name).suffix
+ else f".{self._mime_type.split('/')[1]}"
+ )
+ uri_str = f"file:///{self._name}{extension}"
else:
uri_str = f"file:///resource.{self._mime_type.split('/')[1]}"
else:
diff --git a/tests/utilities/test_types.py b/tests/utilities/test_types.py
index 3d366aa64..85aa8c2be 100644
--- a/tests/utilities/test_types.py
+++ b/tests/utilities/test_types.py
@@ -461,6 +461,22 @@ class TestFile:
if isinstance(resource.resource, BlobResourceContents):
assert resource.resource.blob == base64.b64encode(test_data).decode()
+ def test_to_resource_content_with_data_and_name_without_extension(self):
+ """Test data-backed File URI with a custom name that needs an extension."""
+ file = File(data=b"test file data", format="pdf", name="report")
+ resource = file.to_resource_content()
+
+ assert resource.resource.mime_type == "application/pdf"
+ assert str(resource.resource.uri) == "file:///report.pdf"
+
+ def test_to_resource_content_with_data_preserves_name_extension(self):
+ """Test data-backed File URI preserves a custom name with an extension."""
+ file = File(data=b"test file data", format="pdf", name="report.pdf")
+ resource = file.to_resource_content()
+
+ assert resource.resource.mime_type == "application/pdf"
+ assert str(resource.resource.uri) == "file:///report.pdf"
+
def test_to_resource_content_with_text_data(self):
"""Test conversion to ResourceContent with text data (TextResourceContents)."""
test_data = b"hello world"