From 7a1e332aae0ef36b5ad26c63dbf67c859ec751d9 Mon Sep 17 00:00:00 2001 From: Chanwoo Noh Date: Thu, 26 Jun 2025 14:44:51 +0900 Subject: [PATCH] fix: update mount_example.py for current fastmcp API - Update mount() calls to use keyword arguments (server, prefix) - Replace private API access with public async methods (get_tools, get_resources) - Fix resource URI handling to use URL parsing instead of string prefixes - Update resource access to use _mcp_read_resource with proper URI format - Add urllib.parse import for URL parsing functionality The mounting API and resource handling changed in recent fastmcp versions, breaking the mount_example.py functionality. This update ensures the example works correctly with fastmcp v2.9.0. Tested on: fastmcp v2.9.0 --- examples/mount_example.py | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/examples/mount_example.py b/examples/mount_example.py index 7720f0eb2..b6061954f 100644 --- a/examples/mount_example.py +++ b/examples/mount_example.py @@ -9,6 +9,7 @@ the ToolManager's import_tools functionality. It shows how to: """ import asyncio +from urllib.parse import urlparse from fastmcp import FastMCP @@ -65,17 +66,17 @@ def check_app_status() -> dict[str, str]: # Mount sub-applications -app.mount("weather", weather_app) +app.mount(server=weather_app, prefix="weather") -app.mount("news", news_app) +app.mount(server=news_app, prefix="news") async def get_server_details(): """Print information about mounted resources.""" # Print available tools - tools = app._tool_manager.list_tools() + tools = await app.get_tools() print(f"\nAvailable tools ({len(tools)}):") - for tool in tools: + for _, tool in tools.items(): print(f" - {tool.name}: {tool.description}") # Print available resources @@ -83,18 +84,21 @@ async def get_server_details(): # Distinguish between native and imported resources # Native resources would be those directly in the main app (not prefixed) + + resources = await app.get_resources() + native_resources = [ uri - for uri in app._resource_manager._resources - if not (uri.startswith("weather+") or uri.startswith("news+")) + for uri, _ in resources.items() + if urlparse(uri).netloc not in ("weather", "news") ] # Imported resources - categorized by source app weather_resources = [ - uri for uri in app._resource_manager._resources if uri.startswith("weather+") + uri for uri, _ in resources.items() if urlparse(uri).netloc == "weather" ] news_resources = [ - uri for uri in app._resource_manager._resources if uri.startswith("news+") + uri for uri, _ in resources.items() if urlparse(uri).netloc == "news" ] print(f" - Native app resources: {native_resources}") @@ -102,7 +106,7 @@ async def get_server_details(): print(f" - Imported from news app: {news_resources}") # Let's try to access resources using the prefixed URI - weather_data = await app.read_resource("weather+weather://forecast") + weather_data = await app._mcp_read_resource(uri="weather://weather/forecast") print(f"\nWeather data from prefixed URI: {weather_data}")