Fixe resource template matching

This commit is contained in:
Saoud Rizwan
2024-12-11 16:13:21 -08:00
parent 2dd0590f8a
commit 69533ad112
3 changed files with 9 additions and 11 deletions

View File

@@ -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 || "",
}}
/>
)}

View File

@@ -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

View File

@@ -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)