Skip to content

[Bug] ResourceTemplate.matches() does not escape regex special chars in URI template literals #2961

Description

@Robin1987China

Bug Description

ResourceTemplate.matches() in src/mcp/server/mcpserver/resources/templates.py:97 converts URI templates to regex patterns using simple string replacement, but does not escape regex-special characters in literal (non-parameter) parts of the template.

# L97
pattern = self.uri_template.replace("{", "(?P<").replace("}", ">[^/]+)")

Reproduction Cases

Template literal char Regex side effect False match
.well-known/{name} . = any char data://Xwell-known/hello incorrectly matches
{id}.json . = any char data://items/123Xjson incorrectly matches
{id}?format=json ? = optional data://items/123format=json matches without ?
{name}+suffix + = one-or-more quantifier Semantic distortion
{name}++suffix ++ = possessive quantifier Behavior change

Fix (proposed)

Use re.escape() on the template first, then replace escaped \{param\} with named capture groups:

import re as _re

_URI_PARAM_PATTERN = _re.compile(r"\\\{([^}]+)\\\}")

def _build_template_pattern(template: str) -> str:
    escaped = _re.escape(template)
    return _URI_PARAM_PATTERN.sub(lambda m: f"(?P<{m.group(1)}>[^/]+)", escaped)

AI Disclosure

Bug discovered with AI assistance (opencode).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions