Spaces:
Running
Running
File size: 21,416 Bytes
72f52ef ac60432 38888f9 72f52ef ac60432 72f52ef 38888f9 72f52ef d4fceae 72f52ef cdb0274 72f52ef cdb0274 72f52ef cdb0274 72f52ef cdb0274 72f52ef cdb0274 d4fceae 72f52ef 428a20f cdb0274 428a20f cdb0274 428a20f 72f52ef 3edb3a3 cdb0274 3edb3a3 cdb0274 3edb3a3 72f52ef d4fceae 72f52ef d4fceae 72f52ef ac60432 d4fceae ac60432 72f52ef d4fceae 72f52ef cdb0274 72f52ef cdb0274 72f52ef cdb0274 d4fceae 72f52ef cdb0274 72f52ef cdb0274 d4fceae cdb0274 72f52ef cdb0274 72f52ef cdb0274 d4fceae cdb0274 72f52ef edc0d82 72f52ef d4fceae 72f52ef edc0d82 72f52ef d4fceae 72f52ef edc0d82 72f52ef cdb0274 d4fceae 72f52ef edc0d82 72f52ef 38888f9 edc0d82 38888f9 edc0d82 38888f9 edc0d82 38888f9 | 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 | import json
import sys
from contextlib import asynccontextmanager
import pytest
from fastmcp import FastMCP
from fastmcp.client import Client
from fastmcp.client.transports import FastMCPTransport, SSETransport
from fastmcp.exceptions import NotFoundError
from fastmcp.server.proxy import FastMCPProxy
class TestBasicMount:
"""Test basic mounting functionality."""
async def test_mount_simple_server(self):
"""Test mounting a simple server and accessing its tool."""
# Create main app and sub-app
main_app = FastMCP("MainApp")
sub_app = FastMCP("SubApp")
# Add a tool to the sub-app
@sub_app.tool()
def sub_tool() -> str:
return "This is from the sub app"
# Mount the sub-app to the main app
main_app.mount("sub", sub_app)
# Get tools from main app, should include sub_app's tools
tools = await main_app.get_tools()
assert "sub_sub_tool" in tools
async with Client(main_app) as client:
result = await client.call_tool("sub_sub_tool", {})
assert result[0].text == "This is from the sub app" # type: ignore[attr-defined]
async def test_mount_with_custom_separator(self):
"""Test mounting with a custom tool separator (deprecated but still supported)."""
main_app = FastMCP("MainApp")
sub_app = FastMCP("SubApp")
@sub_app.tool()
def greet(name: str) -> str:
return f"Hello, {name}!"
# Mount without custom separator - custom separators are deprecated
main_app.mount("sub", sub_app)
# Tool should be accessible with the default separator
tools = await main_app.get_tools()
assert "sub_greet" in tools
# Call the tool
result = await main_app._mcp_call_tool("sub_greet", {"name": "World"})
assert result[0].text == "Hello, World!" # type: ignore[attr-defined]
async def test_mount_invalid_resource_prefix(self):
main_app = FastMCP("MainApp")
api_app = FastMCP("APIApp")
# This test doesn't apply anymore with the new prefix format
# just mount the server to maintain test coverage
main_app.mount("api:sub", api_app)
async def test_mount_invalid_resource_separator(self):
main_app = FastMCP("MainApp")
api_app = FastMCP("APIApp")
# This test doesn't apply anymore with the new prefix format
# Mount without deprecated parameters
main_app.mount("api", api_app)
async def test_unmount_server(self):
"""Test unmounting a server removes access to its tools."""
main_app = FastMCP("MainApp")
sub_app = FastMCP("SubApp")
@sub_app.tool()
def sub_tool() -> str:
return "This is from the sub app"
# Mount the sub-app
main_app.mount("sub", sub_app)
# Verify it was mounted
tools = await main_app.get_tools()
assert "sub_sub_tool" in tools
# Unmount the sub-app
main_app.unmount("sub")
# Verify it was unmounted
tools = await main_app.get_tools()
assert "sub_sub_tool" not in tools
# Calling the tool should fail
with pytest.raises(NotFoundError, match="Unknown tool: sub_sub_tool"):
await main_app._mcp_call_tool("sub_sub_tool", {})
async def test_mount_with_no_prefix(self):
main_app = FastMCP("MainApp")
sub_app = FastMCP("SubApp")
@sub_app.tool()
def sub_tool() -> str:
return "This is from the sub app"
# Mount with empty prefix but without deprecated separators
main_app.mount(prefix="", server=sub_app)
tools = await main_app.get_tools()
# With empty prefix, the format is now "_sub_tool" instead of "sub_tool"
assert "_sub_tool" in tools
class TestMultipleServerMount:
"""Test mounting multiple servers simultaneously."""
async def test_mount_multiple_servers(self):
"""Test mounting multiple servers with different prefixes."""
main_app = FastMCP("MainApp")
weather_app = FastMCP("WeatherApp")
news_app = FastMCP("NewsApp")
@weather_app.tool()
def get_forecast() -> str:
return "Weather forecast"
@news_app.tool()
def get_headlines() -> str:
return "News headlines"
# Mount both apps
main_app.mount("weather", weather_app)
main_app.mount("news", news_app)
# Check both are accessible
tools = await main_app.get_tools()
assert "weather_get_forecast" in tools
assert "news_get_headlines" in tools
# Call tools from both mounted servers
result1 = await main_app._mcp_call_tool("weather_get_forecast", {})
assert result1[0].text == "Weather forecast" # type: ignore[attr-defined]
result2 = await main_app._mcp_call_tool("news_get_headlines", {})
assert result2[0].text == "News headlines" # type: ignore[attr-defined]
async def test_mount_same_prefix(self):
"""Test that mounting with the same prefix replaces the previous mount."""
main_app = FastMCP("MainApp")
first_app = FastMCP("FirstApp")
second_app = FastMCP("SecondApp")
@first_app.tool()
def first_tool() -> str:
return "First app tool"
@second_app.tool()
def second_tool() -> str:
return "Second app tool"
# Mount first app
main_app.mount("api", first_app)
tools = await main_app.get_tools()
assert "api_first_tool" in tools
# Mount second app with same prefix
main_app.mount("api", second_app)
tools = await main_app.get_tools()
# First app's tool should no longer be accessible
assert "api_first_tool" not in tools
# Second app's tool should be accessible
assert "api_second_tool" in tools
@pytest.mark.skipif(
sys.platform == "win32", reason="Windows asyncio networking timeouts."
)
async def test_mount_with_unreachable_proxy_servers(self, caplog):
"""Test graceful handling when multiple mounted servers fail to connect."""
main_app = FastMCP("MainApp")
working_app = FastMCP("WorkingApp")
@working_app.tool()
def working_tool() -> str:
return "Working tool"
@working_app.resource(uri="working://data")
def working_resource():
return "Working resource"
@working_app.prompt()
def working_prompt() -> str:
return "Working prompt"
# Mount the working server
main_app.mount("working", working_app)
# Use an unreachable port
unreachable_client = Client(
transport=SSETransport("http://127.0.0.1:99999/sse")
)
# Create a proxy server that will fail to connect
unreachable_proxy = FastMCP.as_proxy(unreachable_client)
# Mount the unreachable proxy
main_app.mount("unreachable", unreachable_proxy)
# All object types should work from working server despite unreachable proxy
async with Client(main_app) as client:
# Test tools
tools = await client.list_tools()
tool_names = [tool.name for tool in tools]
assert "working_working_tool" in tool_names
# Test calling a tool
result = await client.call_tool("working_working_tool", {})
assert result[0].text == "Working tool" # type: ignore[attr-defined]
# Test resources
resources = await client.list_resources()
resource_uris = [str(resource.uri) for resource in resources]
assert "working://working/data" in resource_uris
# Test prompts
prompts = await client.list_prompts()
prompt_names = [prompt.name for prompt in prompts]
assert "working_working_prompt" in prompt_names
# Verify that warnings were logged for the unreachable server
warning_messages = [
record.message for record in caplog.records if record.levelname == "WARNING"
]
assert any(
"Failed to get tools from mounted server 'unreachable'" in msg
for msg in warning_messages
)
assert any(
"Failed to get resources from mounted server 'unreachable'" in msg
for msg in warning_messages
)
assert any(
"Failed to get prompts from mounted server 'unreachable'" in msg
for msg in warning_messages
)
class TestDynamicChanges:
"""Test that changes to mounted servers are reflected dynamically."""
async def test_adding_tool_after_mounting(self):
"""Test that tools added after mounting are accessible."""
main_app = FastMCP("MainApp")
sub_app = FastMCP("SubApp")
# Mount the sub-app before adding any tools
main_app.mount("sub", sub_app)
# Initially, there should be no tools from sub_app
tools = await main_app.get_tools()
assert not any(key.startswith("sub_") for key in tools)
# Add a tool to the sub-app after mounting
@sub_app.tool()
def dynamic_tool() -> str:
return "Added after mounting"
# The tool should be accessible through the main app
tools = await main_app.get_tools()
assert "sub_dynamic_tool" in tools
# Call the dynamically added tool
result = await main_app._mcp_call_tool("sub_dynamic_tool", {})
assert result[0].text == "Added after mounting" # type: ignore[attr-defined]
async def test_removing_tool_after_mounting(self):
"""Test that tools removed from mounted servers are no longer accessible."""
main_app = FastMCP("MainApp")
sub_app = FastMCP("SubApp")
@sub_app.tool()
def temp_tool() -> str:
return "Temporary tool"
# Mount the sub-app
main_app.mount("sub", sub_app)
# Initially, the tool should be accessible
tools = await main_app.get_tools()
assert "sub_temp_tool" in tools
# Remove the tool from sub_app
sub_app._tool_manager._tools.pop("temp_tool")
# The tool should no longer be accessible
# Refresh the cache by clearing it
main_app._cache.cache.clear()
tools = await main_app.get_tools()
assert "sub_temp_tool" not in tools
class TestResourcesAndTemplates:
"""Test mounting with resources and resource templates."""
async def test_mount_with_resources(self):
"""Test mounting a server with resources."""
main_app = FastMCP("MainApp")
data_app = FastMCP("DataApp")
@data_app.resource(uri="data://users")
async def get_users():
return ["user1", "user2"]
# Mount the data app
main_app.mount("data", data_app)
# Resource should be accessible through main app
resources = await main_app.get_resources()
assert "data://data/users" in resources
# Check that resource can be accessed
async with Client(main_app) as client:
result = await client.read_resource("data://data/users")
assert json.loads(result[0].text) == ["user1", "user2"] # type: ignore[attr-defined]
async def test_mount_with_resource_templates(self):
"""Test mounting a server with resource templates."""
main_app = FastMCP("MainApp")
user_app = FastMCP("UserApp")
@user_app.resource(uri="users://{user_id}/profile")
def get_user_profile(user_id: str) -> dict:
return {"id": user_id, "name": f"User {user_id}"}
# Mount the user app
main_app.mount("api", user_app)
# Template should be accessible through main app
templates = await main_app.get_resource_templates()
assert "users://api/{user_id}/profile" in templates
# Check template instantiation
async with Client(main_app) as client:
result = await client.read_resource("users://api/123/profile")
profile = json.loads(result[0].text) # type: ignore
assert profile["id"] == "123"
assert profile["name"] == "User 123"
async def test_adding_resource_after_mounting(self):
"""Test adding a resource after mounting."""
main_app = FastMCP("MainApp")
data_app = FastMCP("DataApp")
# Mount the data app before adding resources
main_app.mount("data", data_app)
# Add a resource after mounting
@data_app.resource(uri="data://config")
def get_config():
return {"version": "1.0"}
# Resource should be accessible through main app
resources = await main_app.get_resources()
assert "data://data/config" in resources
# Check access to the resource
async with Client(main_app) as client:
result = await client.read_resource("data://data/config")
config = json.loads(result[0].text) # type: ignore[attr-defined]
assert config["version"] == "1.0"
class TestPrompts:
"""Test mounting with prompts."""
async def test_mount_with_prompts(self):
"""Test mounting a server with prompts."""
main_app = FastMCP("MainApp")
assistant_app = FastMCP("AssistantApp")
@assistant_app.prompt()
def greeting(name: str) -> str:
return f"Hello, {name}!"
# Mount the assistant app
main_app.mount("assistant", assistant_app)
# Prompt should be accessible through main app
prompts = await main_app.get_prompts()
assert "assistant_greeting" in prompts
# Render the prompt
result = await main_app._mcp_get_prompt("assistant_greeting", {"name": "World"})
assert result.messages is not None
# The message should contain our greeting text
async def test_adding_prompt_after_mounting(self):
"""Test adding a prompt after mounting."""
main_app = FastMCP("MainApp")
assistant_app = FastMCP("AssistantApp")
# Mount the assistant app before adding prompts
main_app.mount("assistant", assistant_app)
# Add a prompt after mounting
@assistant_app.prompt()
def farewell(name: str) -> str:
return f"Goodbye, {name}!"
# Prompt should be accessible through main app
prompts = await main_app.get_prompts()
assert "assistant_farewell" in prompts
# Render the prompt
result = await main_app._mcp_get_prompt("assistant_farewell", {"name": "World"})
assert result.messages is not None
# The message should contain our farewell text
class TestProxyServer:
"""Test mounting a proxy server."""
async def test_mount_proxy_server(self):
"""Test mounting a proxy server."""
# Create original server
original_server = FastMCP("OriginalServer")
@original_server.tool()
def get_data(query: str) -> str:
return f"Data for {query}"
# Create proxy server
proxy_server = FastMCP.as_proxy(
Client(transport=FastMCPTransport(original_server))
)
# Mount proxy server
main_app = FastMCP("MainApp")
main_app.mount("proxy", proxy_server)
# Tool should be accessible through main app
tools = await main_app.get_tools()
assert "proxy_get_data" in tools
# Call the tool
result = await main_app._mcp_call_tool("proxy_get_data", {"query": "test"})
assert result[0].text == "Data for test" # type: ignore[attr-defined]
async def test_dynamically_adding_to_proxied_server(self):
"""Test that changes to the original server are reflected in the mounted proxy."""
# Create original server
original_server = FastMCP("OriginalServer")
# Create proxy server
proxy_server = FastMCP.as_proxy(
Client(transport=FastMCPTransport(original_server))
)
# Mount proxy server
main_app = FastMCP("MainApp")
main_app.mount("proxy", proxy_server)
# Add a tool to the original server
@original_server.tool()
def dynamic_data() -> str:
return "Dynamic data"
# Tool should be accessible through main app via proxy
tools = await main_app.get_tools()
assert "proxy_dynamic_data" in tools
# Call the tool
result = await main_app._mcp_call_tool("proxy_dynamic_data", {})
assert result[0].text == "Dynamic data" # type: ignore[attr-defined]
async def test_proxy_server_with_resources(self):
"""Test mounting a proxy server with resources."""
# Create original server
original_server = FastMCP("OriginalServer")
@original_server.resource(uri="config://settings")
def get_config():
return {"api_key": "12345"}
# Create proxy server
proxy_server = FastMCP.as_proxy(
Client(transport=FastMCPTransport(original_server))
)
# Mount proxy server
main_app = FastMCP("MainApp")
main_app.mount("proxy", proxy_server)
# Resource should be accessible through main app
result = await main_app._mcp_read_resource("config://proxy/settings")
config = json.loads(result[0].content) # type: ignore[attr-defined]
assert config["api_key"] == "12345"
async def test_proxy_server_with_prompts(self):
"""Test mounting a proxy server with prompts."""
# Create original server
original_server = FastMCP("OriginalServer")
@original_server.prompt()
def welcome(name: str) -> str:
return f"Welcome, {name}!"
# Create proxy server
proxy_server = FastMCP.as_proxy(
Client(transport=FastMCPTransport(original_server))
)
# Mount proxy server
main_app = FastMCP("MainApp")
main_app.mount("proxy", proxy_server)
# Prompt should be accessible through main app
result = await main_app._mcp_get_prompt("proxy_welcome", {"name": "World"})
assert result.messages is not None
# The message should contain our welcome text
class TestAsProxyKwarg:
"""Test the as_proxy kwarg."""
async def test_as_proxy_defaults_false(self):
mcp = FastMCP("Main")
sub = FastMCP("Sub")
mcp.mount("sub", sub)
assert mcp._mounted_servers["sub"].server is sub
async def test_as_proxy_false(self):
mcp = FastMCP("Main")
sub = FastMCP("Sub")
mcp.mount("sub", sub, as_proxy=False)
assert mcp._mounted_servers["sub"].server is sub
async def test_as_proxy_true(self):
mcp = FastMCP("Main")
sub = FastMCP("Sub")
mcp.mount("sub", sub, as_proxy=True)
assert mcp._mounted_servers["sub"].server is not sub
assert isinstance(mcp._mounted_servers["sub"].server, FastMCPProxy)
async def test_as_proxy_defaults_true_if_lifespan(self):
@asynccontextmanager
async def lifespan(mcp: FastMCP):
yield
mcp = FastMCP("Main")
sub = FastMCP("Sub", lifespan=lifespan)
mcp.mount("sub", sub)
assert mcp._mounted_servers["sub"].server is not sub
assert isinstance(mcp._mounted_servers["sub"].server, FastMCPProxy)
async def test_as_proxy_ignored_for_proxy_mounts_default(self):
mcp = FastMCP("Main")
sub = FastMCP("Sub")
sub_proxy = FastMCP.as_proxy(Client(transport=FastMCPTransport(sub)))
mcp.mount("sub", sub_proxy)
assert mcp._mounted_servers["sub"].server is sub_proxy
async def test_as_proxy_ignored_for_proxy_mounts_false(self):
mcp = FastMCP("Main")
sub = FastMCP("Sub")
sub_proxy = FastMCP.as_proxy(Client(transport=FastMCPTransport(sub)))
mcp.mount("sub", sub_proxy, as_proxy=False)
assert mcp._mounted_servers["sub"].server is sub_proxy
async def test_as_proxy_ignored_for_proxy_mounts_true(self):
mcp = FastMCP("Main")
sub = FastMCP("Sub")
sub_proxy = FastMCP.as_proxy(Client(transport=FastMCPTransport(sub)))
mcp.mount("sub", sub_proxy, as_proxy=True)
assert mcp._mounted_servers["sub"].server is sub_proxy
async def test_as_proxy_mounts_still_have_live_link(self):
mcp = FastMCP("Main")
sub = FastMCP("Sub")
mcp.mount("sub", sub, as_proxy=True)
assert len(await mcp.get_tools()) == 0
@sub.tool()
def hello():
return "hi"
assert len(await mcp.get_tools()) == 1
async def test_sub_lifespan_is_executed(self):
lifespan_check = []
@asynccontextmanager
async def lifespan(mcp: FastMCP):
lifespan_check.append("start")
yield
mcp = FastMCP("Main")
sub = FastMCP("Sub", lifespan=lifespan)
@sub.tool()
def hello():
return "hi"
mcp.mount("sub", sub, as_proxy=True)
assert lifespan_check == []
async with Client(mcp) as client:
await client.call_tool("sub_hello", {})
assert lifespan_check == ["start"]
|