Skip to content

Match resource templates in linear time - #760

Merged
koic merged 1 commit into
modelcontextprotocol:mainfrom
koic:match_resource_templates_in_linear_time
Dec 3, 2026
Merged

koic merged 1 commit into
modelcontextprotocol:mainfrom
koic:match_resource_templates_in_linear_time

Conversation

@koic

@koic koic commented Dec 3, 2026

Copy link
Copy Markdown
Member

Motivation and Context

ResourceTemplate.match_uri matched a URI with a pattern that gives each {var} the group [^/]+. Two or more variables in one path segment, as in file:///{name}.{ext}, leave a backtracking engine many ways to split a long run of the separator, and Ruby before 3.2, which the gem still supports, does not memoize the search. On Ruby 2.7, file:/// followed by 40,000 dots and a / took 5 seconds to fail against that template, and 800 separators took 0.4 seconds against one with three variables, growing with the cube of the length.

match_uri now finds the same match without a search. A literal that holds a / can sit in one place only, since no variable holds one, and inside a run of variables between two such places taking the last occurrence of each separator from the right gives the split the pattern gives. The cost is linear in the length of the URI on every Ruby.

How Has This Been Tested?

test/mcp/resource_template_test.rb, including a comparison with the previous pattern over generated templates and URIs. Checked by hand on Ruby 2.7.8: the two inputs above return at once, and 1,200,000 generated inputs give the result of the pattern.

Breaking Changes

None for a URI that is a valid String. match_uri returns nil for a String that is not valid in its encoding and for a value that is neither a String nor nil, where the pattern raised ArgumentError or TypeError or, for a Symbol, matched it.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Test update (non-breaking change which adds or updates tests)
  • Refactoring (non-breaking change which makes things better)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update
  • CI/build script update (update CI workflow or build scripts)

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • I have added automated tests for my change
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

## Motivation and Context

`ResourceTemplate.match_uri` matched a URI with a pattern that gives each `{var}` the group `[^/]+`. Two or more variables
in one path segment, as in `file:///{name}.{ext}`, leave a backtracking engine many ways to split a long run of the separator,
and Ruby before 3.2, which the gem still supports, does not memoize the search. On Ruby 2.7, `file:///` followed by 40,000 dots and a `/`
took 5 seconds to fail against that template, and 800 separators took 0.4 seconds against one with three variables, growing with the cube
of the length.

`match_uri` now finds the same match without a search. A literal that holds a `/` can sit in one place only, since no variable holds one,
and inside a run of variables between two such places taking the last occurrence of each separator from the right gives the split
the pattern gives. The cost is linear in the length of the URI on every Ruby.

## How Has This Been Tested?

`test/mcp/resource_template_test.rb`, including a comparison with the previous pattern over generated templates and URIs.
Checked by hand on Ruby 2.7.8: the two inputs above return at once, and 1,200,000 generated inputs give the result of the pattern.

## Breaking Changes

None for a URI that is a valid String. `match_uri` returns `nil` for a String that is not valid in its encoding and for a value that is
neither a String nor `nil`, where the pattern raised `ArgumentError` or `TypeError` or, for a Symbol, matched it.
@koic
koic merged commit 3a64334 into modelcontextprotocol:main Dec 3, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant