Chanwoo Noh commited on
Commit
6626a1e
·
1 Parent(s): 05dfb42

fix: update mount_example.py for current fastmcp API

Browse files

- 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

Files changed (1) hide show
  1. examples/mount_example.py +13 -9
examples/mount_example.py CHANGED
@@ -9,6 +9,7 @@ the ToolManager's import_tools functionality. It shows how to:
9
  """
10
 
11
  import asyncio
 
12
 
13
  from fastmcp import FastMCP
14
 
@@ -65,17 +66,17 @@ def check_app_status() -> dict[str, str]:
65
 
66
 
67
  # Mount sub-applications
68
- app.mount("weather", weather_app)
69
 
70
- app.mount("news", news_app)
71
 
72
 
73
  async def get_server_details():
74
  """Print information about mounted resources."""
75
  # Print available tools
76
- tools = app._tool_manager.list_tools()
77
  print(f"\nAvailable tools ({len(tools)}):")
78
- for tool in tools:
79
  print(f" - {tool.name}: {tool.description}")
80
 
81
  # Print available resources
@@ -83,18 +84,21 @@ async def get_server_details():
83
 
84
  # Distinguish between native and imported resources
85
  # Native resources would be those directly in the main app (not prefixed)
 
 
 
86
  native_resources = [
87
  uri
88
- for uri in app._resource_manager._resources
89
- if not (uri.startswith("weather+") or uri.startswith("news+"))
90
  ]
91
 
92
  # Imported resources - categorized by source app
93
  weather_resources = [
94
- uri for uri in app._resource_manager._resources if uri.startswith("weather+")
95
  ]
96
  news_resources = [
97
- uri for uri in app._resource_manager._resources if uri.startswith("news+")
98
  ]
99
 
100
  print(f" - Native app resources: {native_resources}")
@@ -102,7 +106,7 @@ async def get_server_details():
102
  print(f" - Imported from news app: {news_resources}")
103
 
104
  # Let's try to access resources using the prefixed URI
105
- weather_data = await app.read_resource("weather+weather://forecast")
106
  print(f"\nWeather data from prefixed URI: {weather_data}")
107
 
108
 
 
9
  """
10
 
11
  import asyncio
12
+ from urllib.parse import urlparse
13
 
14
  from fastmcp import FastMCP
15
 
 
66
 
67
 
68
  # Mount sub-applications
69
+ app.mount(server=weather_app, prefix="weather")
70
 
71
+ app.mount(server=news_app, prefix="news")
72
 
73
 
74
  async def get_server_details():
75
  """Print information about mounted resources."""
76
  # Print available tools
77
+ tools = await app.get_tools()
78
  print(f"\nAvailable tools ({len(tools)}):")
79
+ for _, tool in tools.items():
80
  print(f" - {tool.name}: {tool.description}")
81
 
82
  # Print available resources
 
84
 
85
  # Distinguish between native and imported resources
86
  # Native resources would be those directly in the main app (not prefixed)
87
+
88
+ resources = await app.get_resources()
89
+
90
  native_resources = [
91
  uri
92
+ for uri, _ in resources.items()
93
+ if urlparse(uri).netloc not in ("weather", "news")
94
  ]
95
 
96
  # Imported resources - categorized by source app
97
  weather_resources = [
98
+ uri for uri, _ in resources.items() if urlparse(uri).netloc == "weather"
99
  ]
100
  news_resources = [
101
+ uri for uri, _ in resources.items() if urlparse(uri).netloc == "news"
102
  ]
103
 
104
  print(f" - Native app resources: {native_resources}")
 
106
  print(f" - Imported from news app: {news_resources}")
107
 
108
  # Let's try to access resources using the prefixed URI
109
+ weather_data = await app._mcp_read_resource(uri="weather://weather/forecast")
110
  print(f"\nWeather data from prefixed URI: {weather_data}")
111
 
112