Spaces:
Running
Running
File size: 29,737 Bytes
0d61263 7826473 0d61263 7826473 d913408 7826473 177d39a 582f82a 7826473 3acddf3 282875e 177d39a fd0c13f bacf327 7826473 6cdad57 7826473 224caf9 3d3691f 224caf9 7826473 224caf9 7826473 6cdad57 7826473 6cdad57 7826473 6cdad57 7826473 fd0c13f 7826473 fd0c13f 7826473 de8e775 7826473 96618d0 7826473 edc0d82 aa46c09 edc0d82 aa46c09 edc0d82 7b03b73 edc0d82 0d61263 edc0d82 7826473 d6d2b36 81e8514 d6d2b36 3d3691f 6cdad57 957b451 6cdad57 bacf327 3d3691f 7826473 0feb19f 7826473 81e8514 7826473 81e8514 aa46c09 7826473 c6466c2 81e8514 7826473 ed6ec93 aa46c09 ed6ec93 fee3201 ed6ec93 7826473 81e8514 582f82a 7826473 582f82a 7826473 6cdad57 957b451 6cdad57 7826473 0feb19f 7826473 81e8514 d4fceae 7826473 81e8514 7826473 81e8514 d4fceae 7826473 8604c71 81e8514 7826473 ed6ec93 7826473 81e8514 7826473 6cdad57 957b451 6cdad57 7826473 0feb19f 7826473 81e8514 d4fceae 7826473 81e8514 7826473 ed6ec93 7826473 81e8514 7826473 6cdad57 957b451 6cdad57 7826473 81e8514 7826473 81e8514 7826473 81e8514 7826473 81e8514 3becd4d d4fceae d913408 ed6ec93 d913408 582f82a d913408 582f82a d913408 a9a1a98 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 | import inspect
import json
from typing import Any, cast
import pytest
from anyio import create_task_group
from dirty_equals import Contains
from mcp import McpError
from pydantic import AnyUrl
from fastmcp import FastMCP
from fastmcp.client import Client
from fastmcp.client.transports import FastMCPTransport, StreamableHttpTransport
from fastmcp.exceptions import ToolError
from fastmcp.server.proxy import FastMCPProxy, ProxyClient
from fastmcp.tools.tool_transform import (
ToolTransformConfig,
)
USERS = [
{"id": "1", "name": "Alice", "active": True},
{"id": "2", "name": "Bob", "active": True},
{"id": "3", "name": "Charlie", "active": False},
]
@pytest.fixture
def fastmcp_server():
server = FastMCP("TestServer")
# --- Tools ---
@server.tool(tags={"greet"})
def greet(name: str) -> str:
"""Greet someone by name."""
return f"Hello, {name}!"
@server.tool
def tool_without_description() -> str:
return "Hello?"
@server.tool
def add(a: int, b: int) -> int:
"""Add two numbers together."""
return a + b
@server.tool
def error_tool():
"""This tool always raises an error."""
raise ValueError("This is a test error")
# --- Resources ---
@server.resource(uri="resource://wave", tags={"wave"})
def wave() -> str:
return "π"
@server.resource(uri="data://users")
async def get_users() -> list[dict[str, Any]]:
return USERS
@server.resource(uri="data://user/{user_id}", tags={"users"})
async def get_user(user_id: str) -> dict[str, Any] | None:
return next((user for user in USERS if user["id"] == user_id), None)
# --- Prompts ---
@server.prompt(tags={"welcome"})
def welcome(name: str) -> str:
return f"Welcome to FastMCP, {name}!"
return server
@pytest.fixture
async def proxy_server(fastmcp_server):
"""Fixture that creates a FastMCP proxy server."""
return FastMCP.as_proxy(ProxyClient(transport=FastMCPTransport(fastmcp_server)))
async def test_create_proxy(fastmcp_server):
"""Test that the proxy server properly forwards requests to the original server."""
# Create a client
client = ProxyClient(transport=FastMCPTransport(fastmcp_server))
server = FastMCPProxy.as_proxy(client)
assert isinstance(server, FastMCPProxy)
assert isinstance(server, FastMCP)
assert server.name == "FastMCP"
async def test_as_proxy_with_server(fastmcp_server):
"""FastMCP.as_proxy should accept a FastMCP instance."""
proxy = FastMCP.as_proxy(fastmcp_server)
async with Client(proxy) as client:
result = await client.call_tool("greet", {"name": "Test"})
assert result.data == "Hello, Test!"
async def test_as_proxy_with_transport(fastmcp_server):
"""FastMCP.as_proxy should accept a ClientTransport."""
proxy = FastMCP.as_proxy(FastMCPTransport(fastmcp_server))
async with Client(proxy) as client:
result = await client.call_tool("greet", {"name": "Test"})
assert result.data == "Hello, Test!"
def test_as_proxy_with_url():
"""FastMCP.as_proxy should accept a URL without connecting."""
proxy = FastMCP.as_proxy("http://example.com/mcp/")
assert isinstance(proxy, FastMCPProxy)
client = cast(Client, proxy.client_factory())
assert isinstance(client.transport, StreamableHttpTransport)
assert client.transport.url == "http://example.com/mcp/" # type: ignore[attr-defined]
async def test_proxy_with_async_client_factory():
"""FastMCPProxy should accept an async client_factory."""
async def async_factory():
return Client("http://example.com/mcp/")
proxy = FastMCPProxy(client_factory=async_factory)
assert isinstance(proxy, FastMCPProxy)
assert inspect.iscoroutinefunction(proxy.client_factory)
client = await proxy.client_factory()
assert isinstance(client, Client)
assert isinstance(client.transport, StreamableHttpTransport)
assert client.transport.url == "http://example.com/mcp/" # type: ignore[attr-defined]
class TestTools:
async def test_get_tools(self, proxy_server):
tools = await proxy_server.get_tools()
assert "greet" in tools
assert "add" in tools
assert "error_tool" in tools
assert "tool_without_description" in tools
async def test_get_tools_meta(self, proxy_server):
tools = await proxy_server.get_tools()
greet_tool = tools["greet"]
assert greet_tool.meta == {"_fastmcp": {"tags": ["greet"]}}
async def test_get_transformed_tools(
self, fastmcp_server: FastMCP, proxy_server: FastMCPProxy
):
"""An explicit None description should change the tool description to None."""
fastmcp_server.add_tool_transformation(
"add", ToolTransformConfig(name="add_transformed")
)
tools = await proxy_server.get_tools()
assert "add_transformed" in tools
assert "add" not in tools
async def test_call_transformed_tools(
self, fastmcp_server: FastMCP, proxy_server: FastMCPProxy
):
"""An explicit None description should change the tool description to None."""
fastmcp_server.add_tool_transformation(
"add", ToolTransformConfig(name="add_transformed")
)
async with Client(proxy_server) as client:
result = await client.call_tool("add_transformed", {"a": 1, "b": 2})
assert result.data == 3
async def test_tool_without_description(self, proxy_server):
tools = await proxy_server.get_tools()
assert tools["tool_without_description"].description is None
async def test_list_tools_same_as_original(self, fastmcp_server, proxy_server):
assert (
await proxy_server._mcp_list_tools()
== await fastmcp_server._mcp_list_tools()
)
async def test_call_tool_result_same_as_original(
self, fastmcp_server: FastMCP, proxy_server: FastMCPProxy
):
result = await fastmcp_server._mcp_call_tool("greet", {"name": "Alice"})
proxy_result = await proxy_server._mcp_call_tool("greet", {"name": "Alice"})
assert result == proxy_result
async def test_call_tool_calls_tool(self, proxy_server):
async with Client(proxy_server) as client:
proxy_result = await client.call_tool("add", {"a": 1, "b": 2})
assert proxy_result.data == 3
async def test_error_tool_raises_error(self, proxy_server):
with pytest.raises(ToolError, match="This is a test error"):
async with Client(proxy_server) as client:
await client.call_tool("error_tool", {})
async def test_proxy_can_overwrite_proxied_tool(self, proxy_server):
"""
Test that a tool defined on the proxy can overwrite the proxied tool with the same name.
"""
@proxy_server.tool
def greet(name: str, extra: str = "extra") -> str:
return f"Overwritten, {name}! {extra}"
async with Client(proxy_server) as client:
result = await client.call_tool("greet", {"name": "Marvin", "extra": "abc"})
assert result.data == "Overwritten, Marvin! abc"
async def test_proxy_errors_if_overwritten_tool_is_disabled(self, proxy_server):
"""
Test that a tool defined on the proxy is not listed if it is disabled,
and it doesn't fall back to the proxied tool with the same name
"""
@proxy_server.tool(enabled=False)
def greet(name: str, extra: str = "extra") -> str:
return f"Overwritten, {name}! {extra}"
async with Client(proxy_server) as client:
with pytest.raises(ToolError, match="Unknown tool"):
await client.call_tool("greet", {"name": "Marvin", "extra": "abc"})
async def test_proxy_can_list_overwritten_tool(self, proxy_server):
"""
Test that a tool defined on the proxy is listed instead of the proxied tool
"""
@proxy_server.tool
def greet(name: str, extra: str = "extra") -> str:
return f"Overwritten, {name}! {extra}"
async with Client(proxy_server) as client:
tools = await client.list_tools()
greet_tool = next(t for t in tools if t.name == "greet")
assert "extra" in greet_tool.inputSchema["properties"]
async def test_proxy_can_list_overwritten_tool_if_disabled(self, proxy_server):
"""
Test that a tool defined on the proxy is not listed if it is disabled,
and it doesn't fall back to the proxied tool with the same name
"""
@proxy_server.tool(enabled=False)
def greet(name: str, extra: str = "extra") -> str:
return f"Overwritten, {name}! {extra}"
async with Client(proxy_server) as client:
tools = await client.list_tools()
assert not any(t.name == "greet" for t in tools)
class TestResources:
async def test_get_resources(self, proxy_server):
resources = await proxy_server.get_resources()
assert [r.uri for r in resources.values()] == Contains(
AnyUrl("data://users"),
AnyUrl("resource://wave"),
)
assert [r.name for r in resources.values()] == Contains("get_users", "wave")
async def test_get_resources_meta(self, proxy_server):
resources = await proxy_server.get_resources()
wave_resource = resources["resource://wave"]
assert wave_resource.meta == {"_fastmcp": {"tags": ["wave"]}}
async def test_list_resources_same_as_original(self, fastmcp_server, proxy_server):
assert (
await proxy_server._mcp_list_resources()
== await fastmcp_server._mcp_list_resources()
)
async def test_read_resource(self, proxy_server: FastMCPProxy):
async with Client(proxy_server) as client:
result = await client.read_resource("resource://wave")
assert result[0].text == "π" # type: ignore[attr-defined]
async def test_read_resource_same_as_original(self, fastmcp_server, proxy_server):
async with Client(fastmcp_server) as client:
result = await client.read_resource("resource://wave")
async with Client(proxy_server) as client:
proxy_result = await client.read_resource("resource://wave")
assert proxy_result == result
async def test_read_json_resource(self, proxy_server: FastMCPProxy):
async with Client(proxy_server) as client:
result = await client.read_resource("data://users")
assert json.loads(result[0].text) == USERS # type: ignore[attr-defined]
async def test_read_resource_returns_none_if_not_found(self, proxy_server):
with pytest.raises(
McpError, match="Unknown resource: 'resource://nonexistent'"
):
async with Client(proxy_server) as client:
await client.read_resource("resource://nonexistent")
async def test_proxy_can_overwrite_proxied_resource(self, proxy_server):
"""
Test that a resource defined on the proxy can overwrite the proxied resource with the same URI.
"""
@proxy_server.resource(uri="resource://wave")
def overwritten_wave() -> str:
return "Overwritten wave! π"
async with Client(proxy_server) as client:
result = await client.read_resource("resource://wave")
assert result[0].text == "Overwritten wave! π" # type: ignore[attr-defined]
async def test_proxy_errors_if_overwritten_resource_is_disabled(self, proxy_server):
"""
Test that a resource defined on the proxy is not accessible if it is disabled,
and it doesn't fall back to the proxied resource with the same URI
"""
@proxy_server.resource(uri="resource://wave", enabled=False)
def overwritten_wave() -> str:
return "Overwritten wave! π"
async with Client(proxy_server) as client:
with pytest.raises(McpError, match="Unknown resource"):
await client.read_resource("resource://wave")
async def test_proxy_can_list_overwritten_resource(self, proxy_server):
"""
Test that a resource defined on the proxy is listed instead of the proxied resource
"""
@proxy_server.resource(uri="resource://wave", name="overwritten_wave")
def overwritten_wave() -> str:
return "Overwritten wave! π"
async with Client(proxy_server) as client:
resources = await client.list_resources()
wave_resource = next(
r for r in resources if str(r.uri) == "resource://wave"
)
assert wave_resource.name == "overwritten_wave"
async def test_proxy_can_list_overwritten_resource_if_disabled(self, proxy_server):
"""
Test that a resource defined on the proxy is not listed if it is disabled,
and it doesn't fall back to the proxied resource with the same URI
"""
@proxy_server.resource(uri="resource://wave", enabled=False)
def overwritten_wave() -> str:
return "Overwritten wave! π"
async with Client(proxy_server) as client:
resources = await client.list_resources()
wave_resources = [r for r in resources if str(r.uri) == "resource://wave"]
assert len(wave_resources) == 0
class TestResourceTemplates:
async def test_get_resource_templates(self, proxy_server):
templates = await proxy_server.get_resource_templates()
assert [t.name for t in templates.values()] == Contains("get_user")
async def test_get_resource_templates_meta(self, proxy_server):
templates = await proxy_server.get_resource_templates()
get_user_template = templates["data://user/{user_id}"]
assert get_user_template.meta == {"_fastmcp": {"tags": ["users"]}}
async def test_list_resource_templates_same_as_original(
self, fastmcp_server, proxy_server
):
result = await fastmcp_server._mcp_list_resource_templates()
proxy_result = await proxy_server._mcp_list_resource_templates()
assert proxy_result == result
@pytest.mark.parametrize("id", [1, 2, 3])
async def test_read_resource_template(self, proxy_server: FastMCPProxy, id: int):
async with Client(proxy_server) as client:
result = await client.read_resource(f"data://user/{id}")
assert json.loads(result[0].text) == USERS[id - 1] # type: ignore[attr-defined]
async def test_read_resource_template_same_as_original(
self, fastmcp_server, proxy_server
):
async with Client(fastmcp_server) as client:
result = await client.read_resource("data://user/1")
async with Client(proxy_server) as client:
proxy_result = await client.read_resource("data://user/1")
assert proxy_result == result
async def test_proxy_can_overwrite_proxied_resource_template(self, proxy_server):
"""
Test that a resource template defined on the proxy can overwrite the proxied template with the same URI template.
"""
@proxy_server.resource(uri="data://user/{user_id}", name="overwritten_get_user")
def overwritten_get_user(user_id: str) -> dict[str, Any]:
return {
"id": user_id,
"name": "Overwritten User",
"active": True,
"extra": "data",
}
async with Client(proxy_server) as client:
result = await client.read_resource("data://user/1")
user_data = json.loads(result[0].text) # type: ignore[attr-defined]
assert user_data["name"] == "Overwritten User"
assert user_data["extra"] == "data"
async def test_proxy_errors_if_overwritten_resource_template_is_disabled(
self, proxy_server
):
"""
Test that a resource template defined on the proxy is not accessible if it is disabled,
and it doesn't fall back to the proxied template with the same URI template
"""
@proxy_server.resource(uri="data://user/{user_id}", enabled=False)
def overwritten_get_user(user_id: str) -> dict[str, Any]:
return {"id": user_id, "name": "Overwritten User", "active": True}
async with Client(proxy_server) as client:
with pytest.raises(McpError, match="Unknown resource"):
await client.read_resource("data://user/1")
async def test_proxy_can_list_overwritten_resource_template(self, proxy_server):
"""
Test that a resource template defined on the proxy is listed instead of the proxied template
"""
@proxy_server.resource(uri="data://user/{user_id}", name="overwritten_get_user")
def overwritten_get_user(user_id: str) -> dict[str, Any]:
return {"id": user_id, "name": "Overwritten User", "active": True}
async with Client(proxy_server) as client:
templates = await client.list_resource_templates()
user_template = next(
t for t in templates if t.uriTemplate == "data://user/{user_id}"
)
assert user_template.name == "overwritten_get_user"
async def test_proxy_can_list_overwritten_resource_template_if_disabled(
self, proxy_server
):
"""
Test that a resource template defined on the proxy is not listed if it is disabled,
and it doesn't fall back to the proxied template with the same URI template
"""
@proxy_server.resource(uri="data://user/{user_id}", enabled=False)
def overwritten_get_user(user_id: str) -> dict[str, Any]:
return {"id": user_id, "name": "Overwritten User", "active": True}
async with Client(proxy_server) as client:
templates = await client.list_resource_templates()
user_templates = [
t for t in templates if t.uriTemplate == "data://user/{user_id}"
]
assert len(user_templates) == 0
class TestPrompts:
async def test_get_prompts_server_method(self, proxy_server: FastMCPProxy):
prompts = await proxy_server.get_prompts()
assert [p.name for p in prompts.values()] == Contains("welcome")
async def test_get_prompts_meta(self, proxy_server):
prompts = await proxy_server.get_prompts()
welcome_prompt = prompts["welcome"]
assert welcome_prompt.meta == {"_fastmcp": {"tags": ["welcome"]}}
async def test_list_prompts_same_as_original(self, fastmcp_server, proxy_server):
async with Client(fastmcp_server) as client:
result = await client.list_prompts()
async with Client(proxy_server) as client:
proxy_result = await client.list_prompts()
assert proxy_result == result
async def test_render_prompt_same_as_original(
self, fastmcp_server: FastMCP, proxy_server: FastMCPProxy
):
async with Client(fastmcp_server) as client:
result = await client.get_prompt("welcome", {"name": "Alice"})
async with Client(proxy_server) as client:
proxy_result = await client.get_prompt("welcome", {"name": "Alice"})
assert proxy_result == result
async def test_render_prompt_calls_prompt(self, proxy_server):
async with Client(proxy_server) as client:
result = await client.get_prompt("welcome", {"name": "Alice"})
assert result.messages[0].role == "user"
assert result.messages[0].content.text == "Welcome to FastMCP, Alice!" # type: ignore[attr-defined]
async def test_proxy_can_overwrite_proxied_prompt(self, proxy_server):
"""
Test that a prompt defined on the proxy can overwrite the proxied prompt with the same name.
"""
@proxy_server.prompt
def welcome(name: str, extra: str = "friend") -> str:
return f"Overwritten welcome, {name}! You are my {extra}."
async with Client(proxy_server) as client:
result = await client.get_prompt(
"welcome", {"name": "Alice", "extra": "colleague"}
)
assert result.messages[0].role == "user"
assert (
result.messages[0].content.text # type: ignore[attr-defined]
== "Overwritten welcome, Alice! You are my colleague."
)
async def test_proxy_errors_if_overwritten_prompt_is_disabled(self, proxy_server):
"""
Test that a prompt defined on the proxy is not accessible if it is disabled,
and it doesn't fall back to the proxied prompt with the same name
"""
@proxy_server.prompt(enabled=False)
def welcome(name: str, extra: str = "friend") -> str:
return f"Overwritten welcome, {name}! You are my {extra}."
async with Client(proxy_server) as client:
with pytest.raises(McpError, match="Unknown prompt"):
await client.get_prompt("welcome", {"name": "Alice"})
async def test_proxy_can_list_overwritten_prompt(self, proxy_server):
"""
Test that a prompt defined on the proxy is listed instead of the proxied prompt
"""
@proxy_server.prompt
def welcome(name: str, extra: str = "friend") -> str:
return f"Overwritten welcome, {name}! You are my {extra}."
async with Client(proxy_server) as client:
prompts = await client.list_prompts()
welcome_prompt = next(p for p in prompts if p.name == "welcome")
# Check that the overwritten prompt has the additional 'extra' parameter
param_names = [arg.name for arg in welcome_prompt.arguments or []]
assert "extra" in param_names
async def test_proxy_can_list_overwritten_prompt_if_disabled(self, proxy_server):
"""
Test that a prompt defined on the proxy is not listed if it is disabled,
and it doesn't fall back to the proxied prompt with the same name
"""
@proxy_server.prompt(enabled=False)
def welcome(name: str, extra: str = "friend") -> str:
return f"Overwritten welcome, {name}! You are my {extra}."
async with Client(proxy_server) as client:
prompts = await client.list_prompts()
welcome_prompts = [p for p in prompts if p.name == "welcome"]
assert len(welcome_prompts) == 0
async def test_proxy_handles_multiple_concurrent_tasks_correctly(
proxy_server: FastMCPProxy,
):
results = {}
async def get_and_store(name, coro):
results[name] = await coro()
async with create_task_group() as tg:
tg.start_soon(get_and_store, "prompts", proxy_server.get_prompts)
tg.start_soon(get_and_store, "resources", proxy_server.get_resources)
tg.start_soon(get_and_store, "tools", proxy_server.get_tools)
assert list(results) == Contains("resources", "prompts", "tools")
assert list(results["prompts"]) == Contains("welcome")
assert [r.uri for r in results["resources"].values()] == Contains(
AnyUrl("data://users"),
AnyUrl("resource://wave"),
)
assert [r.name for r in results["resources"].values()] == Contains(
"get_users", "wave"
)
assert list(results["tools"]) == Contains(
"greet", "add", "error_tool", "tool_without_description"
)
class TestMirroredComponents:
"""Test mirrored component functionality - components retrieved from proxy servers."""
async def test_mirrored_tool_cannot_be_enabled(self, proxy_server):
"""Test that mirrored tools cannot be enabled directly."""
tools = await proxy_server.get_tools()
mirrored_tool = tools["greet"]
# Verify it's mirrored
assert mirrored_tool._mirrored is True
# Should raise error when trying to enable
with pytest.raises(RuntimeError, match="Cannot enable mirrored component"):
mirrored_tool.enable()
async def test_mirrored_tool_cannot_be_disabled(self, proxy_server):
"""Test that mirrored tools cannot be disabled directly."""
tools = await proxy_server.get_tools()
mirrored_tool = tools["greet"]
# Verify it's mirrored
assert mirrored_tool._mirrored is True
# Should raise error when trying to disable
with pytest.raises(RuntimeError, match="Cannot disable mirrored component"):
mirrored_tool.disable()
async def test_mirrored_resource_cannot_be_enabled(self, proxy_server):
"""Test that mirrored resources cannot be enabled directly."""
resources = await proxy_server.get_resources()
mirrored_resource = resources["resource://wave"]
# Verify it's mirrored
assert mirrored_resource._mirrored is True
# Should raise error when trying to enable
with pytest.raises(RuntimeError, match="Cannot enable mirrored component"):
mirrored_resource.enable()
async def test_mirrored_resource_cannot_be_disabled(self, proxy_server):
"""Test that mirrored resources cannot be disabled directly."""
resources = await proxy_server.get_resources()
mirrored_resource = resources["resource://wave"]
# Verify it's mirrored
assert mirrored_resource._mirrored is True
# Should raise error when trying to disable
with pytest.raises(RuntimeError, match="Cannot disable mirrored component"):
mirrored_resource.disable()
async def test_mirrored_prompt_cannot_be_enabled(self, proxy_server):
"""Test that mirrored prompts cannot be enabled directly."""
prompts = await proxy_server.get_prompts()
mirrored_prompt = prompts["welcome"]
# Verify it's mirrored
assert mirrored_prompt._mirrored is True
# Should raise error when trying to enable
with pytest.raises(RuntimeError, match="Cannot enable mirrored component"):
mirrored_prompt.enable()
async def test_mirrored_prompt_cannot_be_disabled(self, proxy_server):
"""Test that mirrored prompts cannot be disabled directly."""
prompts = await proxy_server.get_prompts()
mirrored_prompt = prompts["welcome"]
# Verify it's mirrored
assert mirrored_prompt._mirrored is True
# Should raise error when trying to disable
with pytest.raises(RuntimeError, match="Cannot disable mirrored component"):
mirrored_prompt.disable()
async def test_copy_creates_non_mirrored_component(self, proxy_server):
"""Test that copy() creates a non-mirrored component that can be modified."""
tools = await proxy_server.get_tools()
mirrored_tool = tools["greet"]
# Create a copy
local_tool = mirrored_tool.copy()
# Copy should not be mirrored
assert local_tool._mirrored is False
# Should be able to enable/disable the copy
local_tool.enable()
assert local_tool.enabled is True
local_tool.disable()
assert local_tool.enabled is False
async def test_local_component_takes_precedence_over_mirrored(self, proxy_server):
"""Test that local components take precedence over mirrored ones."""
# Get the mirrored tool
tools = await proxy_server.get_tools()
mirrored_tool = tools["greet"]
# Create a local copy and add it
local_tool = mirrored_tool.copy()
proxy_server.add_tool(local_tool)
# Disable the local copy
local_tool.disable()
# The local disabled tool should take precedence
updated_tools = await proxy_server.get_tools()
final_tool = updated_tools["greet"]
# Should be the local tool (not mirrored) and disabled
assert final_tool is local_tool
assert final_tool._mirrored is False
assert final_tool.enabled is False
async def test_error_messages_mention_copy_method(self, proxy_server):
"""Test that error messages guide users to use copy() method."""
tools = await proxy_server.get_tools()
mirrored_tool = tools["greet"]
# Check enable error message
with pytest.raises(RuntimeError) as exc_info:
mirrored_tool.enable()
assert "copy()" in str(exc_info.value)
# Check disable error message
with pytest.raises(RuntimeError) as exc_info:
mirrored_tool.disable()
assert "copy()" in str(exc_info.value)
async def test_client_cannot_call_disabled_proxy_tool(self, proxy_server):
"""Test that clients cannot call a tool when local copy is disabled."""
# Get the mirrored tool
tools = await proxy_server.get_tools()
mirrored_tool = tools["greet"]
# Verify the tool works initially
async with Client(proxy_server) as client:
result = await client.call_tool("greet", {"name": "Alice"})
assert result.data == "Hello, Alice!"
# Create a local copy and disable it
local_tool = mirrored_tool.copy()
proxy_server.add_tool(local_tool)
local_tool.disable()
# Client should now get "Unknown tool" error
async with Client(proxy_server) as client:
with pytest.raises(ToolError, match="Unknown tool"):
await client.call_tool("greet", {"name": "Alice"})
# Tool should not appear in tool list either
async with Client(proxy_server) as client:
tools_list = await client.list_tools()
tool_names = [tool.name for tool in tools_list]
assert "greet" not in tool_names
|