MCPcopy Create free account
hub / github.com/Zoo-Code-Org/Zoo-Code / findMatchingTemplate

Function findMatchingTemplate

webview-ui/src/utils/mcp.ts:9–25  ·  view source on GitHub ↗
(
	uri: string,
	templates: McpResourceTemplate[] = [],
)

Source from the content-addressed store, hash-verified

7 * @returns The matching template or undefined if no match is found
8 */
9export 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

Callers 1

Calls 2

replaceMethod · 0.65
StringInterface · 0.50

Tested by

no test coverage detected