From 69533ad1128cdb73d5249dd0db5118a8da037eb5 Mon Sep 17 00:00:00 2001
From: Saoud Rizwan <7799382+saoudrizwan@users.noreply.github.com>
Date: Wed, 11 Dec 2024 16:13:21 -0800
Subject: [PATCH] Fixe resource template matching
---
webview-ui/src/components/chat/ChatRow.tsx | 4 ++--
webview-ui/src/components/mcp/McpResourceRow.tsx | 4 ++--
webview-ui/src/utils/mcp.ts | 12 +++++-------
3 files changed, 9 insertions(+), 11 deletions(-)
diff --git a/webview-ui/src/components/chat/ChatRow.tsx b/webview-ui/src/components/chat/ChatRow.tsx
index a4325c2..f0b166e 100644
--- a/webview-ui/src/components/chat/ChatRow.tsx
+++ b/webview-ui/src/components/chat/ChatRow.tsx
@@ -794,8 +794,6 @@ export const ChatRowContent = ({
{useMcpServer.type === "access_mcp_resource" && (
)}
diff --git a/webview-ui/src/components/mcp/McpResourceRow.tsx b/webview-ui/src/components/mcp/McpResourceRow.tsx
index ac23f96..766c78e 100644
--- a/webview-ui/src/components/mcp/McpResourceRow.tsx
+++ b/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 (
{
// 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)