Description
Summary
A resource template whose function parameter uses a Pydantic field alias registers successfully and matches its URI template, but every read that supplies that parameter fails validation:
@mcp.resource("data://items/{id}")
def item(id: str = Field(alias="identifier")) -> str:
return f"item:{id}"read_resource("data://items/7") raises:
ResourceError: 2 validation errors for item
identifier Missing required argument
id Unexpected keyword argument
Query-string templates (data://search{?q} with q: str = Field(alias="query"), read via ?q=hi) fail identically. Reads that only use a default work by accident. This is the resource-template counterpart of the prompt bug fixed in #5836: tools support aliases because they validate against the call schema directly.
Reproduction (current main adf90b6, offline)
from pydantic import Field
@mcp.resource("data://items/{id}")
def item(id: str = Field(alias="identifier")) -> str:
return f"item:{id}"
@mcp.resource("data://search{?q}")
def search(q: str = Field(default="all", alias="query")) -> str:
return f"search:{q}"Observed: data://items/7 and data://search?q=hi raise the validation error above, while data://search returns search:all.
Expected: item:7 and search:hi.
Root cause
fastmcp_slim/fastmcp/resources/template.py:
FunctionResourceTemplate.readreceives path/query values keyed by Python parameter names (URI placeholders are normalized to Python identifiers byfrom_function/match_uri_template).from_functionwraps the function withvalidate_call(...)(~line 823), and Pydantic's call schema names the parameter after its alias (identifier), so passingid=...fails both the missing-alias and unexpected-name checks.from_function(~685-804) also only maps URI names to Python names, so registering{identifier}for the Python parameteridis rejected.
Suggested fix
Mirror the prompt Field-alias mapping: derive an advertised(alias) → Python-name mapping from resolved type hints and Pydantic FieldInfo (validation_alias string, then alias string, else Python name), exclude injected parameters, and translate extracted path/query params to the call-schema name before invocation. Keep unknown/extra argument filtering unchanged. Registering the alias as the URI placeholder remains a separate design decision and can stay rejected.
Tests
A fix with six regression tests is ready (aliased path, aliased query with override and default, validation_alias, unaliased control, injected Context excluded, unknown argument behavior unchanged). I'll open the linked PR with red/green evidence.
Activity
fix(resources): honor Pydantic field aliases in resource templates (P…