Skip to content

Add a segment-aware prefix matching mode to Template #1564

Closed

Description

@jlouvel

Problem

Template.MODE_STARTS_WITH matches characters, not URI path segments. When routers or applications are mounted under path prefixes on one connector, a route attached to /mcp also matches /mcpx and /mcp-admin/tools:

router.attach("/mcp", mcpRouter);            // Router target => MODE_STARTS_WITH
router.attach("/mcp-admin", adminRouter);
// GET /mcpx            -> mcpRouter, remaining part "x"
// GET /mcp-admin/tools -> mcpRouter (first match), remaining part "-admin/tools"

MODE_BEST_MATCH fixes the /mcp-admin case but not /mcpx. Combining MODE_EQUALS on /mcp with MODE_STARTS_WITH on /mcp/ gets the boundaries right but consumes the leading /, so the mounted router sees tools instead of /tools. Today the only full fix is a TemplateRoute subclass overriding score().

Proposal

Add a third matching mode, Template.MODE_STARTS_WITH_SEGMENT: like MODE_STARTS_WITH, but the match must end on a segment boundary, meaning the end of the string, a following /, ?, ; or #, or a match that itself ends with /. An empty template still matches anything.

router.setRoutingMode(Router.MODE_BEST_MATCH);
router.attach("/mcp", mcpRouter, Template.MODE_STARTS_WITH_SEGMENT);
router.attach("/mcp-admin", adminRouter, Template.MODE_STARTS_WITH_SEGMENT);
router.attach("", rootRouter, Template.MODE_STARTS_WITH_SEGMENT);
// /mcp, /mcp/tools, /mcp?x=1 -> mcpRouter
// /mcp-admin/tools           -> adminRouter
// /mcpx                      -> rootRouter

Existing modes are unchanged, so this is opt-in and non-breaking.

Open question

Should Router.getMatchingMode() later default to this mode for Router and Directory targets (and VirtualHost)? That changes behaviour for existing applications, so it seems better suited to a later major version than to 3.0.

Context

Ikanos (Naftiko) plans to serve its REST, MCP and Skill adapters on one connector, routed by path prefix and by VirtualHost.

Activity

  1. self-assigned this
    on Oct 25, 2026
  2. added theissue type on Oct 25, 2026
  3. jlouvel commented on Nov 5, 2026

    @jlouvel
    CollaboratorAuthor

    Fixed by #1565: Template.MODE_STARTS_WITH_SEGMENT matches a prefix only on a segment boundary (end of string, /, ?, ; or #, or a match ending with /), keeping the leading / in the remaining part, with Template and Router tests.

    🤖 Generated with Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions