Skip to content

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

@TheTerribleChild

Description

@TheTerribleChild

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions