Browse Source

Fixe resource template matching

Saoud Rizwan 1 year ago
parent
commit
69533ad112

+ 2 - 2
webview-ui/src/components/chat/ChatRow.tsx

@@ -794,8 +794,6 @@ export const ChatRowContent = ({
 								{useMcpServer.type === "access_mcp_resource" && (
 									<McpResourceRow
 										item={{
-											// Always use the actual URI from the request
-											uri: useMcpServer.uri || "",
 											// Use the matched resource/template details, with fallbacks
 											...(findMatchingResourceOrTemplate(
 												useMcpServer.uri || "",
@@ -806,6 +804,8 @@ export const ChatRowContent = ({
 												mimeType: "",
 												description: "",
 											}),
+											// Always use the actual URI from the request
+											uri: useMcpServer.uri || "",
 										}}
 									/>
 								)}

+ 2 - 2
webview-ui/src/components/mcp/McpResourceRow.tsx

@@ -5,8 +5,8 @@ type McpResourceRowProps = {
 }
 
 const McpResourceRow = ({ item }: McpResourceRowProps) => {
-	const isTemplate = "uriTemplate" in item
-	const uri = isTemplate ? item.uriTemplate : item.uri
+	const hasUri = "uri" in item
+	const uri = hasUri ? item.uri : item.uriTemplate
 
 	return (
 		<div

+ 5 - 7
webview-ui/src/utils/mcp.ts

@@ -12,14 +12,12 @@ export function findMatchingTemplate(
 ): McpResourceTemplate | undefined {
 	return templates.find((template) => {
 		// Convert template to regex pattern
-		const pattern = template.uriTemplate
-			// Replace {param} with ([^/]+) to match any non-slash characters
-			.replace(/\{([^}]+)\}/g, "([^/]+)")
-			// Escape special regex characters except the ones we just added
+		const pattern = String(template.uriTemplate)
+			// First escape special regex characters
 			.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")
-			// Un-escape the capturing groups we added
-			.replace(/\\\(/g, "(")
-			.replace(/\\\)/g, ")")
+			// Then replace {param} with ([^/]+) to match any non-slash characters
+			// We need to use \{ and \} because we just escaped them
+			.replace(/\\\{([^}]+)\\\}/g, "([^/]+)")
 
 		const regex = new RegExp(`^${pattern}$`)
 		return regex.test(uri)