Skip to content

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

Map File text formats to canonical text MIME types (#5334)

Asem ahmed589eca2bbb2a
diff --git a/fastmcp_slim/fastmcp/utilities/types.py b/fastmcp_slim/fastmcp/utilities/types.py
index 6a6eba3bf..6955161a2 100644
--- a/fastmcp_slim/fastmcp/utilities/types.py
+++ b/fastmcp_slim/fastmcp/utilities/types.py
@@ -429,12 +429,21 @@ class File:
def _get_mime_type(self) -> str:
"""Get MIME type from format or guess from file extension."""
+ # Text formats whose canonical MIME type is text/*, not application/*
+ mapping = {
+ "plain": "text/plain",
+ "txt": "text/plain",
+ "text": "text/plain",
+ "csv": "text/csv",
+ "html": "text/html",
+ "htm": "text/html",
+ "md": "text/markdown",
+ "markdown": "text/markdown",
+ }
+
if self._format:
fmt = self._format.lower()
- # Map common text formats to text/plain
- if fmt in {"plain", "txt", "text"}:
- return "text/plain"
- return f"application/{fmt}"
+ return mapping.get(fmt, f"application/{fmt}")
if self.path:
mime_type, _ = mimetypes.guess_type(self.path)
diff --git a/tests/utilities/test_types.py b/tests/utilities/test_types.py
index 5623d2f8d..ab23fd997 100644
--- a/tests/utilities/test_types.py
+++ b/tests/utilities/test_types.py
@@ -437,6 +437,31 @@ class TestFile:
file = File(data=b"test", format="pdf")
assert file._mime_type == "application/pdf"
+ @pytest.mark.parametrize(
+ ("fmt", "mime_type"),
+ [
+ ("csv", "text/csv"),
+ ("HTML", "text/html"),
+ ("htm", "text/html"),
+ ("md", "text/markdown"),
+ ("markdown", "text/markdown"),
+ ("txt", "text/plain"),
+ ("pdf", "application/pdf"),
+ ],
+ )
+ def test_mime_type_from_format(self, fmt, mime_type):
+ """Text format aliases map to their canonical text/* MIME types."""
+ file = File(data=b"a,b", format=fmt)
+ assert file._mime_type == mime_type
+
+ def test_text_format_is_sent_as_text(self):
+ """A text/* format is embedded as text, not a base64 blob."""
+ file = File(data=b"a,b\n1,2\n", format="csv")
+ resource = file.to_resource_content().resource
+ assert isinstance(resource, TextResourceContents)
+ assert resource.mime_type == "text/csv"
+ assert resource.text == "a,b\n1,2\n"
+
def test_file_initialization_with_name(self):
"""Test file initialization with a custom name."""
file = File(data=b"test", name="custom")