Skip to content

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

Fix #5486: Data-backed File names its resource URI after the MIME subtype (#5568)

Nandana Dileepdc06e71ab90e
diff --git a/fastmcp_slim/fastmcp/utilities/types.py b/fastmcp_slim/fastmcp/utilities/types.py
index 6955161a2..bc0445504 100644
--- a/fastmcp_slim/fastmcp/utilities/types.py
+++ b/fastmcp_slim/fastmcp/utilities/types.py
@@ -463,15 +463,20 @@ class File:
uri_str = self.path.resolve().as_uri()
elif self.data is not None:
raw_data = self.data
+ # The URI extension follows the requested format, not the MIME
+ # subtype (format="txt" is text/plain but should stay .txt).
+ extension = (
+ f".{self._format.lower()}"
+ if self._format
+ else f".{self._mime_type.split('/')[1]}"
+ )
if self._name:
- extension = (
- ""
- if Path(self._name).suffix
- else f".{self._mime_type.split('/')[1]}"
- )
- uri_str = f"file:///{quote(self._name + extension)}"
+ if Path(self._name).suffix:
+ uri_str = f"file:///{quote(self._name)}"
+ else:
+ uri_str = f"file:///{quote(self._name + extension)}"
else:
- uri_str = f"file:///resource.{self._mime_type.split('/')[1]}"
+ uri_str = f"file:///resource{extension}"
else:
raise ValueError("No resource data available")
diff --git a/tests/utilities/test_types.py b/tests/utilities/test_types.py
index ab23fd997..b3234921f 100644
--- a/tests/utilities/test_types.py
+++ b/tests/utilities/test_types.py
@@ -534,6 +534,22 @@ class TestFile:
assert resource.resource.mime_type == "application/pdf"
assert str(resource.resource.uri) == "file:///report.pdf"
+ @pytest.mark.parametrize(
+ ("kwargs", "expected_uri"),
+ [
+ (dict(format="txt"), "file:///resource.txt"),
+ (dict(format="md"), "file:///resource.md"),
+ (dict(format="md", name="README"), "file:///README.md"),
+ (dict(format="TXT"), "file:///resource.txt"),
+ ],
+ )
+ def test_to_resource_content_extension_follows_format(self, kwargs, expected_uri):
+ """The URI extension comes from `format`, not the MIME subtype."""
+ file = File(data=b"hello", **kwargs)
+ resource = file.to_resource_content()
+
+ assert str(resource.resource.uri) == expected_uri
+
@pytest.mark.parametrize(
("name", "expected_uri"),
[