Spaces:
Running
Running
fix: proxy mcp server should tolerate METHOD_NOT_FOUND
Browse files- src/fastmcp/server/proxy.py +26 -4
src/fastmcp/server/proxy.py
CHANGED
|
@@ -10,7 +10,9 @@ from mcp.types import (
|
|
| 10 |
ImageContent,
|
| 11 |
TextContent,
|
| 12 |
TextResourceContents,
|
|
|
|
| 13 |
)
|
|
|
|
| 14 |
from pydantic.networks import AnyUrl
|
| 15 |
|
| 16 |
from fastmcp.client import Client
|
|
@@ -173,7 +175,12 @@ class FastMCPProxy(FastMCP):
|
|
| 173 |
tools = await super().get_tools()
|
| 174 |
|
| 175 |
async with self.client:
|
| 176 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 177 |
tool_proxy = await ProxyTool.from_client(self.client, tool)
|
| 178 |
tools[tool_proxy.name] = tool_proxy
|
| 179 |
|
|
@@ -183,7 +190,12 @@ class FastMCPProxy(FastMCP):
|
|
| 183 |
resources = await super().get_resources()
|
| 184 |
|
| 185 |
async with self.client:
|
| 186 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
resource_proxy = await ProxyResource.from_client(self.client, resource)
|
| 188 |
resources[str(resource_proxy.uri)] = resource_proxy
|
| 189 |
|
|
@@ -193,7 +205,12 @@ class FastMCPProxy(FastMCP):
|
|
| 193 |
templates = await super().get_resource_templates()
|
| 194 |
|
| 195 |
async with self.client:
|
| 196 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 197 |
template_proxy = await ProxyTemplate.from_client(self.client, template)
|
| 198 |
templates[template_proxy.uri_template] = template_proxy
|
| 199 |
|
|
@@ -203,7 +220,12 @@ class FastMCPProxy(FastMCP):
|
|
| 203 |
prompts = await super().get_prompts()
|
| 204 |
|
| 205 |
async with self.client:
|
| 206 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
prompt_proxy = await ProxyPrompt.from_client(self.client, prompt)
|
| 208 |
prompts[prompt_proxy.name] = prompt_proxy
|
| 209 |
return prompts
|
|
|
|
| 10 |
ImageContent,
|
| 11 |
TextContent,
|
| 12 |
TextResourceContents,
|
| 13 |
+
METHOD_NOT_FOUND,
|
| 14 |
)
|
| 15 |
+
from mcp.shared.exceptions import McpError
|
| 16 |
from pydantic.networks import AnyUrl
|
| 17 |
|
| 18 |
from fastmcp.client import Client
|
|
|
|
| 175 |
tools = await super().get_tools()
|
| 176 |
|
| 177 |
async with self.client:
|
| 178 |
+
try:
|
| 179 |
+
client_tools =await self.client.list_tools()
|
| 180 |
+
except McpError as e:
|
| 181 |
+
if e.error.code == METHOD_NOT_FOUND:
|
| 182 |
+
client_tools = []
|
| 183 |
+
for tool in client_tools:
|
| 184 |
tool_proxy = await ProxyTool.from_client(self.client, tool)
|
| 185 |
tools[tool_proxy.name] = tool_proxy
|
| 186 |
|
|
|
|
| 190 |
resources = await super().get_resources()
|
| 191 |
|
| 192 |
async with self.client:
|
| 193 |
+
try:
|
| 194 |
+
client_resources = await self.client.list_resources()
|
| 195 |
+
except McpError as e:
|
| 196 |
+
if e.error.code == METHOD_NOT_FOUND:
|
| 197 |
+
client_resources = []
|
| 198 |
+
for resource in client_resources:
|
| 199 |
resource_proxy = await ProxyResource.from_client(self.client, resource)
|
| 200 |
resources[str(resource_proxy.uri)] = resource_proxy
|
| 201 |
|
|
|
|
| 205 |
templates = await super().get_resource_templates()
|
| 206 |
|
| 207 |
async with self.client:
|
| 208 |
+
try:
|
| 209 |
+
client_templates = await self.client.list_resource_templates()
|
| 210 |
+
except McpError as e:
|
| 211 |
+
if e.error.code == METHOD_NOT_FOUND:
|
| 212 |
+
client_templates = []
|
| 213 |
+
for template in client_templates:
|
| 214 |
template_proxy = await ProxyTemplate.from_client(self.client, template)
|
| 215 |
templates[template_proxy.uri_template] = template_proxy
|
| 216 |
|
|
|
|
| 220 |
prompts = await super().get_prompts()
|
| 221 |
|
| 222 |
async with self.client:
|
| 223 |
+
try:
|
| 224 |
+
client_prompts = await self.client.list_prompts()
|
| 225 |
+
except McpError as e:
|
| 226 |
+
if e.error.code == METHOD_NOT_FOUND:
|
| 227 |
+
client_prompts = []
|
| 228 |
+
for prompt in client_prompts:
|
| 229 |
prompt_proxy = await ProxyPrompt.from_client(self.client, prompt)
|
| 230 |
prompts[prompt_proxy.name] = prompt_proxy
|
| 231 |
return prompts
|