( uri: string, templates: McpResourceTemplate[] = [], )
| 7 | * @returns The matching template or undefined if no match is found |
| 8 | */ |
| 9 | export function findMatchingTemplate( |
| 10 | uri: string, |
| 11 | templates: McpResourceTemplate[] = [], |
| 12 | ): McpResourceTemplate | undefined { |
| 13 | return templates.find((template) => { |
| 14 | // Convert template to regex pattern |
| 15 | const pattern = String(template.uriTemplate) |
| 16 | // First escape special regex characters |
| 17 | .replace(/[.*+?^${}()|[\]\\]/g, "\\$&") |
| 18 | // Then replace {param} with ([^/]+) to match any non-slash characters |
| 19 | // We need to use \{ and \} because we just escaped them |
| 20 | .replace(/\\\{([^}]+)\\\}/g, "([^/]+)") |
| 21 | |
| 22 | const regex = new RegExp(`^${pattern}$`) |
| 23 | return regex.test(uri) |
| 24 | }) |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * Finds either an exact resource match or a matching template for a given URI |
no test coverage detected