PathSegmentTemplateMatcher accepts conflicting captures for repeated variable names

Open Beginner friendly
#1,217 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 3 days

@Christiansada is already working on this.

Since Nov 25, 2026.

  • #1218 by @Christiansada — open

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
64/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
kotlin
Domain
backend

Research direction

Start in PathSegmentTemplateMatcher (io.modelcontextprotocol.kotlin.sdk.utils): find the one-pass match loop that writes each capture into the variables map, and the surrounding percent-decoding and scoring logic. Reproduce with the snippet in the issue, then add unit tests for differing captures, equal captures, decoded equality and independent names, and run the core JVM test suite plus ServerResourceTemplateTest. Done means a repeated name with differing decoded values returns no match while 42/42 still matches and distinct names, scoring and depth limits are unchanged. Note a local candidate fix already exists on the reporter's side, so coordinate before duplicating it.

Written by the indexing model from the issue text.

Description

PathSegmentTemplateMatcher accepts conflicting values for a repeated variable name and retains only the last capture. A resource request can therefore match a template whose repeated variable cannot explain the request consistently.

This was identified by @chrikrah in the review of #1175. This issue tracks the matching behavior separately from #1084's depth-check allocation fix.

Reproduction
import io.modelcontextprotocol.kotlin.sdk.types.ResourceTemplate
import io.modelcontextprotocol.kotlin.sdk.utils.PathSegmentTemplateMatcher

fun main() {
    val matcher = PathSegmentTemplateMatcher(
        ResourceTemplate("test://items/{id}/{id}", "Repeated ID"),
    )
    println(matcher.match("test://items/42/43"))
    check(matcher.match("test://items/42/43") == null)
}

Actual: the URI matches with variables = {id=43}; the first capture is overwritten and the check fails. The same template with 42/42 matches consistently and should keep doing so.

Expected: reject a match when a repeated name captures different values, using the matcher's existing one-pass percent-decoding semantics. Keep consistent repeated captures, distinct names, scoring, URI normalization and depth limits unchanged.

Version check

The same six tests against the latest release tag 0.16.0 (4c55826) have three failures and three passing controls. The inconsistent repeated captures are accepted there too.

Candidate and local validation

A local candidate checks an existing capture before overwriting it. Six tests cover differing captures, equal captures and their score, decoded equality, a differing third occurrence, independent names and one-pass decoding.

On reviewed head 0f6a84c, three of those six cases fail without the candidate. With it, all 278 core JVM tests and nine ServerResourceTemplateTest integration tests pass. Core ktlint, Kotlin ABI and diff checks pass. These are local results on macOS arm64 with Temurin 21; JS, Native and Wasm runtime tests and detekt were not run for this candidate.

The existing #1175 branch remains unchanged. This candidate intentionally rejects some previously accepted URIs, so a separate follow-up keeps that behavior change reviewable without expanding the allocation PR. No production impact or full multiplatform validation is claimed.

Dominant language
Kotlin
Stars
1.5k
Forks
270
Avg merge
3d 7h
Merged PRs (30d)
59

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from modelcontextprotocol/kotlin-sdk

All issues in modelcontextprotocol/kotlin-sdk

Similar issues

More Kotlin issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.