Skip to content

πŸš€ The fast, Pythonic way to build MCP servers and clients.

fix(resources): honor Pydantic field aliases in resource templates (#6054)

Anh Nguyen (Jerry)0acc6ee4597f
diff --git a/fastmcp_slim/fastmcp/resources/template.py b/fastmcp_slim/fastmcp/resources/template.py
index af1bbc631..d341a4183 100644
--- a/fastmcp_slim/fastmcp/resources/template.py
+++ b/fastmcp_slim/fastmcp/resources/template.py
@@ -13,6 +13,7 @@ from urllib.parse import parse_qs, quote, unquote, unquote_plus
from mcp_types import Annotations, Icon
from mcp_types import ResourceTemplate as SDKResourceTemplate
from pydantic import (
+ ConfigDict,
Field,
PrivateAttr,
field_validator,
@@ -887,8 +888,9 @@ class FunctionResourceTemplate(ResourceTemplate):
parameters = type_adapter.json_schema()
parameters = compress_schema(parameters, prune_titles=True)
- # Use validate_call on wrapper for runtime type coercion
- fn = validate_call(wrapper_fn)
+ # Use validate_call on wrapper for runtime type coercion. URI parameters
+ # carry Python parameter names, so accept them for aliased fields too.
+ fn = validate_call(config=ConfigDict(validate_by_name=True))(wrapper_fn)
# Apply ui:// MIME default, then fall back to text/plain
resolved_mime = resolve_ui_mime_type(uri_template, mime_type)
diff --git a/tests/resources/test_resource_template.py b/tests/resources/test_resource_template.py
index fd264b795..80fac7a05 100644
--- a/tests/resources/test_resource_template.py
+++ b/tests/resources/test_resource_template.py
@@ -1367,6 +1367,66 @@ class TestMalformedURITemplates:
assert match.group("id") == "123"
+class TestResourceTemplateAliases:
+ """Pydantic field aliases on template parameters must work on every read."""
+
+ async def test_field_alias_for_path_parameter(self):
+ mcp = FastMCP()
+
+ @mcp.resource("data://items/{id}")
+ def item(id: str = Field(alias="identifier")) -> str:
+ return f"item:{id}"
+
+ async with Client(mcp) as client:
+ [contents] = await client.read_resource("data://items/7")
+
+ assert isinstance(contents, TextResourceContents)
+ assert contents.text == "item:7"
+
+ async def test_field_alias_for_query_parameter(self):
+ mcp = FastMCP()
+
+ @mcp.resource("data://search{?q}")
+ def search(q: str = Field(default="all", alias="query")) -> str:
+ return f"search:{q}"
+
+ async with Client(mcp) as client:
+ reads = {
+ "data://search?q=hi": "search:hi",
+ "data://search": "search:all",
+ }
+ for uri, expected in reads.items():
+ [contents] = await client.read_resource(uri)
+ assert isinstance(contents, TextResourceContents)
+ assert contents.text == expected
+
+ async def test_validation_alias_preserves_type_coercion(self):
+ mcp = FastMCP()
+
+ @mcp.resource("data://count/{n}")
+ def count(n: int = Field(validation_alias="number")) -> str:
+ return str(n + 1)
+
+ async with Client(mcp) as client:
+ [contents] = await client.read_resource("data://count/41")
+
+ assert isinstance(contents, TextResourceContents)
+ assert contents.text == "42"
+
+ async def test_alias_matching_another_parameter_name(self):
+ mcp = FastMCP()
+
+ @mcp.resource("data://x/{a}")
+ def pair(a: str = Field(alias="b"), b: str = "default-b") -> str:
+ return f"a={a} b={b}"
+
+ async with Client(mcp) as client:
+ [contents] = await client.read_resource("data://x/1")
+
+ assert isinstance(contents, TextResourceContents)
+ assert contents.text == "a=1 b=default-b"
+
+
class TestExpandUriTemplate:
"""Test expand_uri_template β€” the inverse of match_uri_template."""