π The fast, Pythonic way to build MCP servers and clients.
#4084 [Issues] Windows startup crash due to UnicodeDecodeError when l⦠(#4092)
Shaik Mohammed Kaif6b6db33c4a28
diff --git a/src/fastmcp/server/providers/skills/skill_provider.py b/src/fastmcp/server/providers/skills/skill_provider.pyindex 8e8d2cf4b..d404ca3b4 100644--- a/src/fastmcp/server/providers/skills/skill_provider.py+++ b/src/fastmcp/server/providers/skills/skill_provider.py@@ -53,7 +53,7 @@ class SkillResource(Resource):return self._generate_manifest()else:main_file_path = self.skill_info.path / self.skill_info.main_file- return main_file_path.read_text()+ return main_file_path.read_text(encoding="utf-8")def _generate_manifest(self) -> str:"""Generate JSON manifest for the skill."""@@ -94,7 +94,7 @@ class SkillFileTemplate(ResourceTemplate):# Determine if binary or text based on mime typemime_type, _ = mimetypes.guess_type(str(full_path))if mime_type and mime_type.startswith("text/"):- return full_path.read_text()+ return full_path.read_text(encoding="utf-8")else:return full_path.read_bytes()@@ -166,7 +166,7 @@ class SkillFileResource(Resource):mime_type, _ = mimetypes.guess_type(str(full_path))if mime_type and mime_type.startswith("text/"):- return full_path.read_text()+ return full_path.read_text(encoding="utf-8")else:return full_path.read_bytes()@@ -238,7 +238,7 @@ class SkillProvider(Provider):f"Expected {self._main_file_name} in {self._skill_path}")- content = main_file.read_text()+ content = main_file.read_text(encoding="utf-8")frontmatter, body = parse_frontmatter(content)# Get description from frontmatter or first non-empty linediff --git a/tests/server/providers/test_skills_provider.py b/tests/server/providers/test_skills_provider.pyindex 97732f308..8bebaeece 100644--- a/tests/server/providers/test_skills_provider.py+++ b/tests/server/providers/test_skills_provider.py@@ -658,3 +658,55 @@ class TestPathTraversalPrevention:await client.read_resource(AnyUrl("skill://test-skill/../../../secret.txt"))+++async def test_skill_provider_loads_and_serves_utf8_skill_md(+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch+) -> None:+ """SKILL.md and its supporting text files must be read as utf-8.++ Regression test for #4084. On Windows the default encoding is cp1252,+ so a bare ``read_text()`` call fails to decode UTF-8 content with+ ``UnicodeDecodeError: 'charmap' codec can't decode byte ...``. The fix+ passes ``encoding="utf-8"`` explicitly at every read site: skill load,+ main-file resource read, and supporting-file template/resource reads.+ This test simulates the Windows behavior on any platform by failing+ every text read that doesn't pass ``encoding="utf-8"``.+ """+ skill_dir = tmp_path / "test-skill"+ skill_dir.mkdir()+ (skill_dir / "SKILL.md").write_text(+ "---\n"+ "name: test-skill\n"+ "description: Test skill with UTF-8 characters\n"+ "---\n"+ "# Test Skill π―\n"+ "- β Success indicator\n",+ encoding="utf-8",+ )+ (skill_dir / "reference.md").write_text(+ "# Reference\n- β¨ utf-8 supporting file\n", encoding="utf-8"+ )++ original_read_text = Path.read_text++ def strict_read_text(self: Path, *args, **kwargs):+ if kwargs.get("encoding") != "utf-8":+ raise UnicodeDecodeError(+ "charmap", b"\x9d", 0, 1, "simulated cp1252 default"+ )+ return original_read_text(self, *args, **kwargs)++ monkeypatch.setattr(Path, "read_text", strict_read_text)++ mcp = FastMCP("Test")+ mcp.add_provider(SkillProvider(skill_path=skill_dir))++ async with Client(mcp) as client:+ main = await client.read_resource(AnyUrl("skill://test-skill/SKILL.md"))+ assert isinstance(main[0], TextResourceContents)+ assert "π―" in main[0].text++ ref = await client.read_resource(AnyUrl("skill://test-skill/reference.md"))+ assert isinstance(ref[0], TextResourceContents)+ assert "β¨" in ref[0].text