Skip to content

[Bug] Resource templates with Pydantic field aliases register but every read fails #5854

Closed

Description

@BlueX888

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.read receives path/query values keyed by Python parameter names (URI placeholders are normalized to Python identifiers by from_function/match_uri_template).
  • from_function wraps the function with validate_call(...) (~line 823), and Pydantic's call schema names the parameter after its alias (identifier), so passing id=... 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 parameter id is 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

  1. added
    bugSomething isn't working. Reports of errors, unexpected behavior, or broken functionality.
    on Oct 19, 2026
  2. added a commit that references this issue on Nov 10, 2026
    0acc6ee
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working. Reports of errors, unexpected behavior, or broken functionality.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions