modelcontextprotocol/java-sdk

Resource Template does not handle query(?) in URI correctly

Closed this issue · 1 comments

Bug description
Resource Template does not handle query(?) in URI correctly

Environment
Version: 0.14.0

Steps to reproduce

  1. Create an MCP resource with the URI: "file://name/search?={search}"
  2. Try and read that resource with uri: "file://name/search?=abc"
  3. Notice how it does not match the resource

Expected behavior
Using query(?) in template URI should work.

Cause
The cause for this is that "?" in the URI template is not escaped when it is used to create the Regex for matching a URI.
DefaultMcpUriTemplateManager.java:

	@Override
	public boolean matches(String uri) {
		// If the uriTemplate doesn't contain variables, do a direct comparison
		if (!this.isUriTemplate(this.uriTemplate)) {
			return uri.equals(this.uriTemplate);
		}

		// Convert the pattern to a regex
		String regex = this.uriTemplate.replaceAll("\\{[^/]+?\\}", "([^/]+?)");
		regex = regex.replace("/", "\\/");
		//Should also handle ? here.

		// Check if the URI matches the regex
		return Pattern.compile(regex).matcher(uri).matches();
	}
Image

There may be another issue where capturing of the query string is not done correctly either as only the first character of the query string is captured:
Image

DeafaultMcpUriTemplateManagerFactory is also misspelled.

Resolved by 19a8c00