π The fast, Pythonic way to build MCP servers and clients.
Use logs instead of warnings
Jeremiah Lowinb3ca9e2df122
diff --git a/src/fastmcp/resources.py b/src/fastmcp/resources.pyindex 330581b73..d8e0ac2bf 100644--- a/src/fastmcp/resources.py+++ b/src/fastmcp/resources.py@@ -4,7 +4,6 @@ import abcimport asyncioimport jsonimport logging-import warningsfrom pathlib import Pathfrom typing import Dict, Optional, Callable, Anyfrom urllib.parse import parse_qs, urlparse@@ -226,11 +225,7 @@ class ResourceManager:existing = self._resources.get(resource.uri)if existing:if self.warn_on_duplicate_resources:- warnings.warn(- f"Resource already exists: {resource.uri}",- ResourceWarning,- stacklevel=2,- )+ logger.warning(f"Resource already exists: {resource.uri}")return existingself._resources[resource.uri] = resourcereturn resourcediff --git a/src/fastmcp/tools.py b/src/fastmcp/tools.pyindex 93b82a5f9..4be52f95e 100644--- a/src/fastmcp/tools.py+++ b/src/fastmcp/tools.py@@ -1,7 +1,6 @@"""Tool management for FastMCP."""import inspect-import warningsfrom typing import Any, Callable, Dict, Optionalfrom pydantic import BaseModel, Field, TypeAdapter@@ -85,11 +84,7 @@ class ToolManager:existing = self._tools.get(tool.name)if existing:if self.warn_on_duplicate_tools:- warnings.warn(- f"Tool already exists: {tool.name}",- ResourceWarning,- stacklevel=2,- )+ logging.warning(f"Tool already exists: {tool.name}")return existingself._tools[tool.name] = toolreturn tooldiff --git a/tests/test_resource_manager.py b/tests/test_resource_manager.pyindex a299a0f5d..4ef673c35 100644--- a/tests/test_resource_manager.py+++ b/tests/test_resource_manager.py@@ -1,4 +1,4 @@-import warnings+import loggingimport pytestfrom pathlib import Pathfrom tempfile import NamedTemporaryFile, TemporaryDirectory@@ -276,8 +276,9 @@ class TestResourceManagerAdd:path=Path("test.txt"),)- def test_warn_on_duplicate_resources(self):+ def test_warn_on_duplicate_resources(self, caplog):"""Test warning on duplicate resources."""+ caplog.set_level(logging.WARNING, logger="mcp")manager = ResourceManager()resource = FileResource(uri="file:///test.txt",@@ -285,11 +286,12 @@ class TestResourceManagerAdd:path=Path("/test.txt"),)manager.add_resource(resource)- with pytest.warns(ResourceWarning):- manager.add_resource(resource)+ manager.add_resource(resource)+ assert "Resource already exists: file:///test.txt" in caplog.text- def test_disable_warn_on_duplicate_resources(self):+ def test_disable_warn_on_duplicate_resources(self, caplog):"""Test disabling warning on duplicate resources."""+ caplog.set_level(logging.WARNING, logger="mcp")manager = ResourceManager()resource = FileResource(uri="file:///test.txt",@@ -298,9 +300,8 @@ class TestResourceManagerAdd:)manager.add_resource(resource)manager.warn_on_duplicate_resources = False- with warnings.catch_warnings():- warnings.simplefilter("error")- manager.add_resource(resource)+ manager.add_resource(resource)+ assert "Resource already exists: file:///test.txt" not in caplog.textclass TestResourceManagerRead:diff --git a/tests/test_tool_manager.py b/tests/test_tool_manager.pyindex ea56a8b19..8818af938 100644--- a/tests/test_tool_manager.py+++ b/tests/test_tool_manager.py@@ -1,5 +1,4 @@-import warnings-+import loggingimport pytestfrom pydantic import BaseModel@@ -83,7 +82,7 @@ class TestAddTools:):manager.add_tool(lambda x: x)- def test_warn_on_duplicate_tools(self):+ def test_warn_on_duplicate_tools(self, caplog):"""Test warning on duplicate tools."""def f(x: int) -> int:@@ -91,10 +90,11 @@ class TestAddTools:manager = ToolManager()manager.add_tool(f)- with pytest.warns(ResourceWarning):+ with caplog.at_level(logging.WARNING):manager.add_tool(f)+ assert "Tool already exists: f" in caplog.text- def test_disable_warn_on_duplicate_tools(self):+ def test_disable_warn_on_duplicate_tools(self, caplog):"""Test disabling warning on duplicate tools."""def f(x: int) -> int:@@ -103,9 +103,9 @@ class TestAddTools:manager = ToolManager()manager.add_tool(f)manager.warn_on_duplicate_tools = False- with warnings.catch_warnings():- warnings.simplefilter("error")+ with caplog.at_level(logging.WARNING):manager.add_tool(f)+ assert "Tool already exists: f" not in caplog.textclass TestCallTools: