Spaces:
Running
Running
Jeremiah Lowin commited on
Commit ·
2d794e9
1
Parent(s): ee7b04a
Use logs instead of warnings
Browse files- src/fastmcp/resources.py +1 -6
- src/fastmcp/tools.py +1 -6
- tests/test_resource_manager.py +9 -8
- tests/test_tool_manager.py +7 -7
src/fastmcp/resources.py
CHANGED
|
@@ -4,7 +4,6 @@ import abc
|
|
| 4 |
import asyncio
|
| 5 |
import json
|
| 6 |
import logging
|
| 7 |
-
import warnings
|
| 8 |
from pathlib import Path
|
| 9 |
from typing import Dict, Optional, Callable, Any
|
| 10 |
from urllib.parse import parse_qs, urlparse
|
|
@@ -226,11 +225,7 @@ class ResourceManager:
|
|
| 226 |
existing = self._resources.get(resource.uri)
|
| 227 |
if existing:
|
| 228 |
if self.warn_on_duplicate_resources:
|
| 229 |
-
|
| 230 |
-
f"Resource already exists: {resource.uri}",
|
| 231 |
-
ResourceWarning,
|
| 232 |
-
stacklevel=2,
|
| 233 |
-
)
|
| 234 |
return existing
|
| 235 |
self._resources[resource.uri] = resource
|
| 236 |
return resource
|
|
|
|
| 4 |
import asyncio
|
| 5 |
import json
|
| 6 |
import logging
|
|
|
|
| 7 |
from pathlib import Path
|
| 8 |
from typing import Dict, Optional, Callable, Any
|
| 9 |
from urllib.parse import parse_qs, urlparse
|
|
|
|
| 225 |
existing = self._resources.get(resource.uri)
|
| 226 |
if existing:
|
| 227 |
if self.warn_on_duplicate_resources:
|
| 228 |
+
logger.warning(f"Resource already exists: {resource.uri}")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 229 |
return existing
|
| 230 |
self._resources[resource.uri] = resource
|
| 231 |
return resource
|
src/fastmcp/tools.py
CHANGED
|
@@ -1,7 +1,6 @@
|
|
| 1 |
"""Tool management for FastMCP."""
|
| 2 |
|
| 3 |
import inspect
|
| 4 |
-
import warnings
|
| 5 |
from typing import Any, Callable, Dict, Optional
|
| 6 |
|
| 7 |
from pydantic import BaseModel, Field, TypeAdapter
|
|
@@ -85,11 +84,7 @@ class ToolManager:
|
|
| 85 |
existing = self._tools.get(tool.name)
|
| 86 |
if existing:
|
| 87 |
if self.warn_on_duplicate_tools:
|
| 88 |
-
|
| 89 |
-
f"Tool already exists: {tool.name}",
|
| 90 |
-
ResourceWarning,
|
| 91 |
-
stacklevel=2,
|
| 92 |
-
)
|
| 93 |
return existing
|
| 94 |
self._tools[tool.name] = tool
|
| 95 |
return tool
|
|
|
|
| 1 |
"""Tool management for FastMCP."""
|
| 2 |
|
| 3 |
import inspect
|
|
|
|
| 4 |
from typing import Any, Callable, Dict, Optional
|
| 5 |
|
| 6 |
from pydantic import BaseModel, Field, TypeAdapter
|
|
|
|
| 84 |
existing = self._tools.get(tool.name)
|
| 85 |
if existing:
|
| 86 |
if self.warn_on_duplicate_tools:
|
| 87 |
+
logging.warning(f"Tool already exists: {tool.name}")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
return existing
|
| 89 |
self._tools[tool.name] = tool
|
| 90 |
return tool
|
tests/test_resource_manager.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
import
|
| 2 |
import pytest
|
| 3 |
from pathlib import Path
|
| 4 |
from tempfile import NamedTemporaryFile, TemporaryDirectory
|
|
@@ -276,8 +276,9 @@ class TestResourceManagerAdd:
|
|
| 276 |
path=Path("test.txt"),
|
| 277 |
)
|
| 278 |
|
| 279 |
-
def test_warn_on_duplicate_resources(self):
|
| 280 |
"""Test warning on duplicate resources."""
|
|
|
|
| 281 |
manager = ResourceManager()
|
| 282 |
resource = FileResource(
|
| 283 |
uri="file:///test.txt",
|
|
@@ -285,11 +286,12 @@ class TestResourceManagerAdd:
|
|
| 285 |
path=Path("/test.txt"),
|
| 286 |
)
|
| 287 |
manager.add_resource(resource)
|
| 288 |
-
|
| 289 |
-
|
| 290 |
|
| 291 |
-
def test_disable_warn_on_duplicate_resources(self):
|
| 292 |
"""Test disabling warning on duplicate resources."""
|
|
|
|
| 293 |
manager = ResourceManager()
|
| 294 |
resource = FileResource(
|
| 295 |
uri="file:///test.txt",
|
|
@@ -298,9 +300,8 @@ class TestResourceManagerAdd:
|
|
| 298 |
)
|
| 299 |
manager.add_resource(resource)
|
| 300 |
manager.warn_on_duplicate_resources = False
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
manager.add_resource(resource)
|
| 304 |
|
| 305 |
|
| 306 |
class TestResourceManagerRead:
|
|
|
|
| 1 |
+
import logging
|
| 2 |
import pytest
|
| 3 |
from pathlib import Path
|
| 4 |
from tempfile import NamedTemporaryFile, TemporaryDirectory
|
|
|
|
| 276 |
path=Path("test.txt"),
|
| 277 |
)
|
| 278 |
|
| 279 |
+
def test_warn_on_duplicate_resources(self, caplog):
|
| 280 |
"""Test warning on duplicate resources."""
|
| 281 |
+
caplog.set_level(logging.WARNING, logger="mcp")
|
| 282 |
manager = ResourceManager()
|
| 283 |
resource = FileResource(
|
| 284 |
uri="file:///test.txt",
|
|
|
|
| 286 |
path=Path("/test.txt"),
|
| 287 |
)
|
| 288 |
manager.add_resource(resource)
|
| 289 |
+
manager.add_resource(resource)
|
| 290 |
+
assert "Resource already exists: file:///test.txt" in caplog.text
|
| 291 |
|
| 292 |
+
def test_disable_warn_on_duplicate_resources(self, caplog):
|
| 293 |
"""Test disabling warning on duplicate resources."""
|
| 294 |
+
caplog.set_level(logging.WARNING, logger="mcp")
|
| 295 |
manager = ResourceManager()
|
| 296 |
resource = FileResource(
|
| 297 |
uri="file:///test.txt",
|
|
|
|
| 300 |
)
|
| 301 |
manager.add_resource(resource)
|
| 302 |
manager.warn_on_duplicate_resources = False
|
| 303 |
+
manager.add_resource(resource)
|
| 304 |
+
assert "Resource already exists: file:///test.txt" not in caplog.text
|
|
|
|
| 305 |
|
| 306 |
|
| 307 |
class TestResourceManagerRead:
|
tests/test_tool_manager.py
CHANGED
|
@@ -1,5 +1,4 @@
|
|
| 1 |
-
import
|
| 2 |
-
|
| 3 |
import pytest
|
| 4 |
from pydantic import BaseModel
|
| 5 |
|
|
@@ -83,7 +82,7 @@ class TestAddTools:
|
|
| 83 |
):
|
| 84 |
manager.add_tool(lambda x: x)
|
| 85 |
|
| 86 |
-
def test_warn_on_duplicate_tools(self):
|
| 87 |
"""Test warning on duplicate tools."""
|
| 88 |
|
| 89 |
def f(x: int) -> int:
|
|
@@ -91,10 +90,11 @@ class TestAddTools:
|
|
| 91 |
|
| 92 |
manager = ToolManager()
|
| 93 |
manager.add_tool(f)
|
| 94 |
-
with
|
| 95 |
manager.add_tool(f)
|
|
|
|
| 96 |
|
| 97 |
-
def test_disable_warn_on_duplicate_tools(self):
|
| 98 |
"""Test disabling warning on duplicate tools."""
|
| 99 |
|
| 100 |
def f(x: int) -> int:
|
|
@@ -103,9 +103,9 @@ class TestAddTools:
|
|
| 103 |
manager = ToolManager()
|
| 104 |
manager.add_tool(f)
|
| 105 |
manager.warn_on_duplicate_tools = False
|
| 106 |
-
with
|
| 107 |
-
warnings.simplefilter("error")
|
| 108 |
manager.add_tool(f)
|
|
|
|
| 109 |
|
| 110 |
|
| 111 |
class TestCallTools:
|
|
|
|
| 1 |
+
import logging
|
|
|
|
| 2 |
import pytest
|
| 3 |
from pydantic import BaseModel
|
| 4 |
|
|
|
|
| 82 |
):
|
| 83 |
manager.add_tool(lambda x: x)
|
| 84 |
|
| 85 |
+
def test_warn_on_duplicate_tools(self, caplog):
|
| 86 |
"""Test warning on duplicate tools."""
|
| 87 |
|
| 88 |
def f(x: int) -> int:
|
|
|
|
| 90 |
|
| 91 |
manager = ToolManager()
|
| 92 |
manager.add_tool(f)
|
| 93 |
+
with caplog.at_level(logging.WARNING):
|
| 94 |
manager.add_tool(f)
|
| 95 |
+
assert "Tool already exists: f" in caplog.text
|
| 96 |
|
| 97 |
+
def test_disable_warn_on_duplicate_tools(self, caplog):
|
| 98 |
"""Test disabling warning on duplicate tools."""
|
| 99 |
|
| 100 |
def f(x: int) -> int:
|
|
|
|
| 103 |
manager = ToolManager()
|
| 104 |
manager.add_tool(f)
|
| 105 |
manager.warn_on_duplicate_tools = False
|
| 106 |
+
with caplog.at_level(logging.WARNING):
|
|
|
|
| 107 |
manager.add_tool(f)
|
| 108 |
+
assert "Tool already exists: f" not in caplog.text
|
| 109 |
|
| 110 |
|
| 111 |
class TestCallTools:
|