Use logs instead of warnings

This commit is contained in:
Jeremiah Lowin 2024-11-29 17:55:41 -05:00
commit b3ca9e2df1
4 changed files with 18 additions and 27 deletions

View file

@ -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

View file

@ -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

View file

@ -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:

View file

@ -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: