Skip to content

Commit 91af3cd

Browse files
authored
Internal refactor of MCP handlers (#2005)
1 parent 05dc0f4 commit 91af3cd

14 files changed

Lines changed: 247 additions & 192 deletions

examples/mount_example.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ async def get_server_details():
104104
print(f" - Imported from news app: {news_resources}")
105105

106106
# Let's try to access resources using the prefixed URI
107-
weather_data = await app._mcp_read_resource(uri="weather://weather/forecast")
107+
weather_data = await app._read_resource_mcp(uri="weather://weather/forecast")
108108
print(f"\nWeather data from prefixed URI: {weather_data}")
109109

110110

examples/serializer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def get_example_data() -> dict:
2121

2222

2323
async def example_usage():
24-
result = await server._mcp_call_tool("get_example_data", {})
24+
result = await server._call_tool_mcp("get_example_data", {})
2525
print("Tool Result:")
2626
print(result)
2727
print("This is an example of using a custom serializer with FastMCP.")

src/fastmcp/prompts/prompt_manager.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,21 +46,23 @@ def mount(self, server: MountedServer) -> None:
4646
"""Adds a mounted server as a source for prompts."""
4747
self._mounted_servers.append(server)
4848

49-
async def _load_prompts(self, *, via_server: bool = False) -> dict[str, Prompt]:
49+
async def _load_prompts(
50+
self, *, apply_filtering: bool = False
51+
) -> dict[str, Prompt]:
5052
"""
51-
The single, consolidated recursive method for fetching prompts. The 'via_server'
53+
The single, consolidated recursive method for fetching prompts. The 'apply_filtering'
5254
parameter determines the communication path.
5355
54-
- via_server=False: Manager-to-manager path for complete, unfiltered inventory
55-
- via_server=True: Server-to-server path for filtered MCP requests
56+
- apply_filtering=False: Manager-to-manager path for complete, unfiltered inventory
57+
- apply_filtering=True: Server-to-server path for filtered MCP requests
5658
"""
5759
all_prompts: dict[str, Prompt] = {}
5860

5961
for mounted in self._mounted_servers:
6062
try:
61-
if via_server:
63+
if apply_filtering:
6264
# Use the server-to-server filtered path
63-
child_results = await mounted.server._list_prompts()
65+
child_results = await mounted.server._list_prompts_middleware()
6466
else:
6567
# Use the manager-to-manager unfiltered path
6668
child_results = await mounted.server._prompt_manager.list_prompts()
@@ -104,13 +106,13 @@ async def get_prompts(self) -> dict[str, Prompt]:
104106
"""
105107
Gets the complete, unfiltered inventory of all prompts.
106108
"""
107-
return await self._load_prompts(via_server=False)
109+
return await self._load_prompts(apply_filtering=False)
108110

109111
async def list_prompts(self) -> list[Prompt]:
110112
"""
111113
Lists all prompts, applying protocol filtering.
112114
"""
113-
prompts_dict = await self._load_prompts(via_server=True)
115+
prompts_dict = await self._load_prompts(apply_filtering=True)
114116
return list(prompts_dict.values())
115117

116118
def add_prompt_from_fn(
@@ -196,7 +198,9 @@ async def render_prompt(
196198
else:
197199
continue
198200
try:
199-
return await mounted.server._get_prompt(prompt_key, arguments)
201+
return await mounted.server._get_prompt_middleware(
202+
prompt_key, arguments
203+
)
200204
except NotFoundError:
201205
continue
202206

src/fastmcp/resources/resource_manager.py

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -63,27 +63,31 @@ def mount(self, server: MountedServer) -> None:
6363

6464
async def get_resources(self) -> dict[str, Resource]:
6565
"""Get all registered resources, keyed by URI."""
66-
return await self._load_resources(via_server=False)
66+
return await self._load_resources(apply_filtering=False)
6767

6868
async def get_resource_templates(self) -> dict[str, ResourceTemplate]:
6969
"""Get all registered templates, keyed by URI template."""
70-
return await self._load_resource_templates(via_server=False)
70+
return await self._load_resource_templates(apply_filtering=False)
7171

72-
async def _load_resources(self, *, via_server: bool = False) -> dict[str, Resource]:
72+
async def _load_resources(
73+
self, *, apply_filtering: bool = False
74+
) -> dict[str, Resource]:
7375
"""
74-
The single, consolidated recursive method for fetching resources. The 'via_server'
76+
The single, consolidated recursive method for fetching resources. The 'apply_filtering'
7577
parameter determines the communication path.
7678
77-
- via_server=False: Manager-to-manager path for complete, unfiltered inventory
78-
- via_server=True: Server-to-server path for filtered MCP requests
79+
- apply_filtering=False: Manager-to-manager path for complete, unfiltered inventory
80+
- apply_filtering=True: Server-to-server path for filtered MCP requests
7981
"""
8082
all_resources: dict[str, Resource] = {}
8183

8284
for mounted in self._mounted_servers:
8385
try:
84-
if via_server:
86+
if apply_filtering:
8587
# Use the server-to-server filtered path
86-
child_resources_list = await mounted.server._list_resources()
88+
child_resources_list = (
89+
await mounted.server._list_resources_middleware()
90+
)
8791
child_resources = {
8892
resource.key: resource for resource in child_resources_list
8993
}
@@ -123,22 +127,24 @@ async def _load_resources(self, *, via_server: bool = False) -> dict[str, Resour
123127
return all_resources
124128

125129
async def _load_resource_templates(
126-
self, *, via_server: bool = False
130+
self, *, apply_filtering: bool = False
127131
) -> dict[str, ResourceTemplate]:
128132
"""
129-
The single, consolidated recursive method for fetching templates. The 'via_server'
133+
The single, consolidated recursive method for fetching templates. The 'apply_filtering'
130134
parameter determines the communication path.
131135
132-
- via_server=False: Manager-to-manager path for complete, unfiltered inventory
133-
- via_server=True: Server-to-server path for filtered MCP requests
136+
- apply_filtering=False: Manager-to-manager path for complete, unfiltered inventory
137+
- apply_filtering=True: Server-to-server path for filtered MCP requests
134138
"""
135139
all_templates: dict[str, ResourceTemplate] = {}
136140

137141
for mounted in self._mounted_servers:
138142
try:
139-
if via_server:
143+
if apply_filtering:
140144
# Use the server-to-server filtered path
141-
child_templates = await mounted.server._list_resource_templates()
145+
child_templates = (
146+
await mounted.server._list_resource_templates_middleware()
147+
)
142148
else:
143149
# Use the manager-to-manager unfiltered path
144150
child_templates = (
@@ -179,14 +185,14 @@ async def list_resources(self) -> list[Resource]:
179185
"""
180186
Lists all resources, applying protocol filtering.
181187
"""
182-
resources_dict = await self._load_resources(via_server=True)
188+
resources_dict = await self._load_resources(apply_filtering=True)
183189
return list(resources_dict.values())
184190

185191
async def list_resource_templates(self) -> list[ResourceTemplate]:
186192
"""
187193
Lists all templates, applying protocol filtering.
188194
"""
189-
templates_dict = await self._load_resource_templates(via_server=True)
195+
templates_dict = await self._load_resource_templates(apply_filtering=True)
190196
return list(templates_dict.values())
191197

192198
def add_resource_or_template_from_fn(
@@ -492,7 +498,7 @@ async def read_resource(self, uri: AnyUrl | str) -> str | bytes:
492498
continue
493499

494500
try:
495-
result = await mounted.server._read_resource(key)
501+
result = await mounted.server._read_resource_middleware(key)
496502
return result[0].content
497503
except NotFoundError:
498504
continue

src/fastmcp/server/context.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ async def read_resource(self, uri: str | AnyUrl) -> list[ReadResourceContents]:
205205
"""
206206
if self.fastmcp is None:
207207
raise ValueError("Context is not available outside of a request")
208-
return await self.fastmcp._mcp_read_resource(uri)
208+
return await self.fastmcp._read_resource_mcp(uri)
209209

210210
async def log(
211211
self,

0 commit comments

Comments
 (0)