Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

fix(mcp-host): keep backslashes in an HTML link label readable

marcoteb278d8142a434
diff --git a/mcp-host/src/workflow/__tests__/inlineMarkup.test.ts b/mcp-host/src/workflow/__tests__/inlineMarkup.test.ts
index 9bff8d42e..edf30d91e 100644
--- a/mcp-host/src/workflow/__tests__/inlineMarkup.test.ts
+++ b/mcp-host/src/workflow/__tests__/inlineMarkup.test.ts
@@ -184,6 +184,38 @@ describe('inline markdown', () => {
])
})
+ it('prints backslashes in the label of an HTML link as the text around it prints them', () => {
+ const labels = [
+ 'C:\\Users\\',
+ 'a\\',
+ '\\',
+ 'a \\[1\\] b',
+ '[\\]',
+ 'a\\\\]',
+ '\\*x\\*',
+ 'path\\to',
+ '\[1]',
+ ]
+ for (const label of labels) {
+ const outside = inlineSpans(label)
+ .map(s => s.text)
+ .join('')
+ expect(inlineSpans(`<a href="https://x.com/a">${label}</a> end`), label).toEqual([
+ { text: outside, link: 'https://x.com/a' },
+ { text: ' end' },
+ ])
+ }
+ expect(htmlToMarkdownInline('<a href="https://x.test/a">C:\\dir\\</a>')).toBe(
+ '[C:\\dir\\\\](https://x.test/a)'
+ )
+ expect(htmlToMarkdownInline('<a href="https://x.test/a">a \\[1\\] b</a>')).toBe(
+ '[a \\[1\\] b](https://x.test/a)'
+ )
+ expect(htmlToMarkdownInline('<a href="https://x.test/a">[1] a]b</a>')).toBe(
+ '[\\[1\\] a\\]b](https://x.test/a)'
+ )
+ })
+
it('keeps balanced parentheses in a link target', () => {
expect(inlineSpans('[wiki](https://en.wikipedia.org/wiki/Foo_(bar)) end')).toEqual([
{ text: 'wiki', link: 'https://en.wikipedia.org/wiki/Foo_(bar)' },
diff --git a/mcp-host/src/workflow/inlineMarkup.ts b/mcp-host/src/workflow/inlineMarkup.ts
index 767c9b84d..9951edb52 100644
--- a/mcp-host/src/workflow/inlineMarkup.ts
+++ b/mcp-host/src/workflow/inlineMarkup.ts
@@ -281,6 +281,15 @@ function withPlaceholdersKept(text: string): string {
})
}
+/**
+ * `label` as the text of a markdown link. A bracket, or a backslash that ends
+ * the label, is escaped so it prints instead of ending the label; an escape the
+ * label already holds is kept whole, so it prints as it does outside a link.
+ */
+function linkLabel(label: string): string {
+ return label.replace(/\\[\s\S]?|[[\]]/g, m => (m.length === 2 ? m : `\\${m}`))
+}
+
function htmlSegmentToMarkdown(text: string): string {
const html = withListMarkers(withoutScripts(withoutComments(withPlaceholdersKept(text)))).replace(
LINE_BREAK,
@@ -290,8 +299,7 @@ function htmlSegmentToMarkdown(text: string): string {
.replace(IMG_TAG, imageMarkdown)
.replace(ANCHOR, (_whole: string, tag: string, label: string) => {
const href = HREF.exec(tag)?.[1]
- // Brackets in the label are escaped, so they print instead of ending it.
- return href ? `[${label.replace(/[[\]]/g, '\\$&')}](${href})` : label
+ return href ? `[${linkLabel(label)}](${href})` : label
})
.replace(BOLD_TAG, tag => styleMark('bold', tag))
.replace(ITALIC_TAG, tag => styleMark('italics', tag))