Skip to content

πŸš€ 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.py
index 330581b73..d8e0ac2bf 100644
--- a/src/fastmcp/resources.py
+++ b/src/fastmcp/resources.py
@@ -4,7 +4,6 @@ import abc
import asyncio
import json
import logging
-import warnings
from pathlib import Path
from typing import Dict, Optional, Callable, Any
from 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 existing
self._resources[resource.uri] = resource
return resource
diff --git a/src/fastmcp/tools.py b/src/fastmcp/tools.py
index 93b82a5f9..4be52f95e 100644
--- a/src/fastmcp/tools.py
+++ b/src/fastmcp/tools.py
@@ -1,7 +1,6 @@
"""Tool management for FastMCP."""
import inspect
-import warnings
from typing import Any, Callable, Dict, Optional
from 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 existing
self._tools[tool.name] = tool
return tool
diff --git a/tests/test_resource_manager.py b/tests/test_resource_manager.py
index a299a0f5d..4ef673c35 100644
--- a/tests/test_resource_manager.py
+++ b/tests/test_resource_manager.py
@@ -1,4 +1,4 @@
-import warnings
+import logging
import pytest
from pathlib import Path
from 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.text
class TestResourceManagerRead:
diff --git a/tests/test_tool_manager.py b/tests/test_tool_manager.py
index ea56a8b19..8818af938 100644
--- a/tests/test_tool_manager.py
+++ b/tests/test_tool_manager.py
@@ -1,5 +1,4 @@
-import warnings
-
+import logging
import pytest
from 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.text
class TestCallTools: