π The fast, Pythonic way to build MCP servers and clients.
Ensure quoted URIs work; fix proxy template issue
Jeremiah Lowin67fbb4c3e8c4
diff --git a/src/fastmcp/server/proxy.py b/src/fastmcp/server/proxy.pyindex 414dcba94..887e60efc 100644--- a/src/fastmcp/server/proxy.py+++ b/src/fastmcp/server/proxy.py@@ -1,4 +1,5 @@from typing import Any, cast+from urllib.parse import quoteimport mcp.typesfrom mcp.types import BlobResourceContents, TextResourceContents@@ -104,8 +105,14 @@ class ProxyTemplate(ResourceTemplate):)async def create_resource(self, uri: str, params: dict[str, Any]) -> ProxyResource:+ # dont use the provided uri, because it may not be the same as the+ # uri_template on the remote server.+ # quote params to ensure they are valid for the uri_template+ parameterized_uri = self.uri_template.format(+ **{k: quote(v, safe="") for k, v in params.items()}+ )async with self._client:- result = await self._client.read_resource(uri)+ result = await self._client.read_resource(parameterized_uri)if isinstance(result[0], TextResourceContents):value = result[0].text@@ -116,7 +123,7 @@ class ProxyTemplate(ResourceTemplate):return ProxyResource(client=self._client,- uri=uri,+ uri=parameterized_uri,name=self.name,description=self.description,mime_type=result[0].mimeType,diff --git a/tests/resources/test_resource_template.py b/tests/resources/test_resource_template.pyindex 68b408aab..301a48297 100644--- a/tests/resources/test_resource_template.py+++ b/tests/resources/test_resource_template.py@@ -374,3 +374,11 @@ class TestMatchUriTemplate:uri_template = "prefix+test://{x}/test/{y}"result = match_uri_template(uri=uri, uri_template=uri_template)assert result == expected_params++ def test_quoted_params(self):+ uri_template = "user://{name}/{email}"+ quoted_name = quote("John Doe", safe="")+ quoted_email = quote("[email protected]", safe="")+ uri = f"user://{quoted_name}/{quoted_email}"+ result = match_uri_template(uri=uri, uri_template=uri_template)+ assert result == {"name": "John Doe", "email": "[email protected]"}diff --git a/tests/server/test_mount.py b/tests/server/test_mount.pyindex 5b3c79946..e66e65c1b 100644--- a/tests/server/test_mount.py+++ b/tests/server/test_mount.py@@ -1,6 +1,7 @@import contextlib+import json+from urllib.parse import quote-import pytestfrom mcp.types import TextContentfrom fastmcp.server.server import FastMCP@@ -190,7 +191,6 @@ async def test_mount_multiple_prompts():assert "sql_explain_sql" in main_app._prompt_manager._promptsasync def test_mount_lifespan():"""Test that the lifespan of a mounted app is properly handled."""# Create apps@@ -346,3 +346,83 @@ async def test_mount_with_proxy_tools():result = await main_app.call_tool("api_get_data", {"query": "test"})assert isinstance(result[0], TextContent)assert result[0].text == "Data for query: test"+++async def test_mount_with_proxy_prompts():+ """+ Test mounting with prompts that have custom keys.++ This tests that the prompt's name doesn't change even though the registered+ key does, which is important for correct rendering.+ """+ # Create apps+ main_app = FastMCP("MainApp")+ api_app = FastMCP("APIApp")++ @api_app.prompt()+ def greeting(name: str) -> str:+ return f"Hello, {name} from API!"++ main_app.mount("api", await FastMCP.as_proxy(api_app))++ result = await main_app.get_prompt("api_greeting", {"name": "World"})+ assert len(result) > 0+ assert isinstance(result[0].content, TextContent)+ assert result[0].content.text == "Hello, World from API!"+++async def test_mount_with_proxy_resources():+ """+ Test mounting with resources that have custom keys.++ This tests that the resource's name doesn't change even though the registered+ key does, which is important for correct access.+ """+ # Create apps+ main_app = FastMCP("MainApp")+ api_app = FastMCP("APIApp")++ # Create a resource in the API app+ @api_app.resource(uri="config://settings")+ def get_config():+ return {+ "api_key": "12345",+ "base_url": "https://api.example.com",+ }++ main_app.mount("api", await FastMCP.as_proxy(api_app))++ # Access the resource through the main app with the prefixed key+ resource = await main_app.read_resource("api+config://settings")+ assert resource is not None+ resource = json.loads(resource)+ assert resource["api_key"] == "12345"+ assert resource["base_url"] == "https://api.example.com"+++async def test_mount_with_proxy_resource_templates():+ """+ Test mounting with resource templates that have custom keys.++ This tests that the template's name doesn't change even though the registered+ key does, which is important for correct instantiation.+ """+ # Create apps+ main_app = FastMCP("MainApp")+ api_app = FastMCP("APIApp")++ # Create a resource template in the API app+ @api_app.resource(uri="user://{name}/{email}")+ def create_user(name: str, email: str):+ return {"name": name, "email": email}++ main_app.mount("api", await FastMCP.as_proxy(api_app))++ # Instantiate the template through the main app with the prefixed key+ quoted_name = quote("John Doe", safe="")+ quoted_email = quote("[email protected]", safe="")+ user = await main_app.read_resource(f"api+user://{quoted_name}/{quoted_email}")+ assert user is not None+ user = json.loads(user)+ assert user["name"] == "John Doe"+ assert user["email"] == "[email protected]"