Skip to content

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

TypeScript

fix(mcp-host): read the label of a link as inline markdown

marcotebc618eef254f9
diff --git a/mcp-host/src/workflow/__tests__/inlineMarkup.test.ts b/mcp-host/src/workflow/__tests__/inlineMarkup.test.ts
index 2e48f87c9..a8428322d 100644
--- a/mcp-host/src/workflow/__tests__/inlineMarkup.test.ts
+++ b/mcp-host/src/workflow/__tests__/inlineMarkup.test.ts
@@ -219,11 +219,9 @@ describe('inline markdown', () => {
'x\\<b>\\</b>',
]
for (const label of labels) {
- const outside = inlineSpans(label)
- .map(s => s.text)
- .join('')
+ const outside = inlineSpans(label).map(s => ({ ...s, link: 'https://x.com/a' }))
expect(inlineSpans(`<a href="https://x.com/a">${label}</a> end`), label).toEqual([
- { text: outside, link: 'https://x.com/a' },
+ ...outside,
{ text: ' end' },
])
}
@@ -253,6 +251,34 @@ describe('inline markdown', () => {
])
})
+ it('reads the label of a link as inline markdown, as CommonMark does', () => {
+ const link = 'https://x.test'
+ expect(inlineSpans(`[**Docs**](${link})`)).toEqual([{ text: 'Docs', bold: true, link }])
+ expect(inlineSpans(`[a *b* \`c\`](${link})`)).toEqual([
+ { text: 'a ', link },
+ { text: 'b', italics: true, link },
+ { text: ' ', link },
+ { text: 'c', code: true, link },
+ ])
+ expect(inlineSpans(`<a href="${link}"><b>Docs</b> page</a>`)).toEqual([
+ { text: 'Docs', bold: true, link },
+ { text: ' page', link },
+ ])
+ })
+
+ it('reads emphasis in a link label with the brackets beside it, as CommonMark does', () => {
+ const link = 'https://x.test'
+ // The closing * is followed by "]", so the rule of three keeps it apart from "**".
+ expect(inlineSpans(`[**\`b*](${link})`)).toEqual([{ text: '**`b*', link }])
+ // Emphasis does not cross the edge of a link.
+ expect(inlineSpans(`[**a](${link})**`)).toEqual([{ text: '**a', link }, { text: '**' }])
+ expect(inlineSpans(`**[a](${link})**`)).toEqual([{ text: 'a', bold: true, link }])
+ })
+
+ it('prints an escaped bracket in a link label as the bracket, as CommonMark does', () => {
+ expect(inlineSpans('[a\\]](https://x.test)')).toEqual([{ text: 'a]', link: 'https://x.test' }])
+ })
+
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/__tests__/internalTools.docx.test.ts b/mcp-host/src/workflow/__tests__/internalTools.docx.test.ts
index ac3e2f9a1..58d28570b 100644
--- a/mcp-host/src/workflow/__tests__/internalTools.docx.test.ts
+++ b/mcp-host/src/workflow/__tests__/internalTools.docx.test.ts
@@ -276,6 +276,13 @@ describe('generate_docx code blocks', () => {
expect(printed.slice(0, 4)).toEqual(['markdown', '```js', 'x', '```'])
expect(printed).toContain('After')
})
+
+ it('keeps a fence line with a language inside the block, as code', async () => {
+ const printed = texts(
+ await documentXml({ body: '```\nExample:\n```js\nlet a = 1\n```\nAfter' })
+ )
+ expect(printed).toEqual(expect.arrayContaining(['```js', 'let a = 1', 'After']))
+ })
})
describe('generate_docx headings', () => {
@@ -295,15 +302,15 @@ describe('generate_docx headings', () => {
})
})
-describe('generate_docx code blocks', () => {
- it('keeps a fence line with a language inside the block, as code', async () => {
- const xml = await documentXml({
- body: '```\nExample:\n```js\nlet a = 1\n```\nAfter',
- })
- const text = paragraphs(xml).map(p =>
- [...p.matchAll(/<w:t[^>]*>([^<]*)</g)].map(m => m[1]).join('')
- )
- expect(text).toEqual(expect.arrayContaining(['```js', 'let a = 1', 'After']))
+describe('generate_docx links', () => {
+ it('keeps the style written inside a link label', async () => {
+ const xml = await documentXml({ body: 'See [**the** `npm ci` docs](https://x.test).' })
+ const link = /<w:hyperlink [^>]*>([\s\S]*?)<\/w:hyperlink>/g
+ const runs = [...xml.matchAll(link)].map(m => m[1]).join('')
+ expect(runs).toMatch(/<w:b\/>[\s\S]*?<w:t[^>]*>the</)
+ expect(runs).toMatch(/w:ascii="Consolas"[\s\S]*?<w:t[^>]*>npm ci</)
+ expect(runs).not.toContain('**')
+ expect(runs).not.toContain('`')
})
})
diff --git a/mcp-host/src/workflow/docxInline.ts b/mcp-host/src/workflow/docxInline.ts
index 3c8193f3f..b210772bf 100644
--- a/mcp-host/src/workflow/docxInline.ts
+++ b/mcp-host/src/workflow/docxInline.ts
@@ -73,6 +73,10 @@ interface RunContext {
codeSize: number
}
+function codeStyle(ctx: RunContext): RunOptions {
+ return { font: { ascii: 'Consolas', hAnsi: 'Consolas' }, size: ctx.codeSize }
+}
+
function appendRuns(markdown: string, base: RunOptions, ctx: RunContext, out: DocxInline[]): void {
for (const span of markdownSpans(markdown)) {
const style: RunOptions = {
@@ -93,21 +97,14 @@ function appendRuns(markdown: string, base: RunOptions, ctx: RunContext, out: Do
link: span.link,
children: textRuns(
span.text,
- { ...style, color: '1D4ED8', underline: {} },
+ { ...style, ...(span.code ? codeStyle(ctx) : {}), color: '1D4ED8', underline: {} },
ctx.eastAsia,
ctx.rtl
),
})
)
} else if (span.code) {
- out.push(
- ...textRuns(
- span.text,
- { ...style, font: { ascii: 'Consolas', hAnsi: 'Consolas' }, size: ctx.codeSize },
- ctx.eastAsia,
- ctx.rtl
- )
- )
+ out.push(...textRuns(span.text, { ...style, ...codeStyle(ctx) }, ctx.eastAsia, ctx.rtl))
} else {
out.push(...textRuns(span.text, style, ctx.eastAsia, ctx.rtl))
}
diff --git a/mcp-host/src/workflow/inlineMarkup.ts b/mcp-host/src/workflow/inlineMarkup.ts
index b99c226ee..07aacdfda 100644
--- a/mcp-host/src/workflow/inlineMarkup.ts
+++ b/mcp-host/src/workflow/inlineMarkup.ts
@@ -737,23 +737,33 @@ function linkLabelEnd(token: string): number {
return at
}
-/** The atom `token` as a span, before the emphasis around it is applied. */
-function atomSpan(token: string): InlineSpan {
+/**
+ * The atom `token` as spans, before the emphasis around it is applied. A link
+ * label is read as inline markdown of its own, so [**Docs**](url) is a bold
+ * link; it holds no unescaped bracket, so no link or image nests inside it.
+ * It is read with its brackets, which then come off: a * beside them opens or
+ * closes as CommonMark decides with them there.
+ */
+function atomSpans(token: string): InlineSpan[] {
if (token.startsWith('![')) {
const split = token.indexOf('](')
- return {
- text: decodeEntities(unescapeMarkdown(withoutMarks(token.slice(2, split)))),
- image: imageTarget(token.slice(split + 2, -1)),
- }
+ return [
+ {
+ text: decodeEntities(unescapeMarkdown(withoutMarks(token.slice(2, split)))),
+ image: imageTarget(token.slice(split + 2, -1)),
+ },
+ ]
}
if (token.startsWith('[')) {
const split = linkLabelEnd(token)
- return {
- text: decodeEntities(unescapeMarkdown(withoutMarks(token.slice(1, split)))),
- link: decodeEntities(token.slice(split + 2, -1)),
- }
+ const link = decodeEntities(token.slice(split + 2, -1))
+ const label = markdownSpans(token.slice(0, split + 1))
+ label[0].text = label[0].text.slice(1)
+ label[label.length - 1].text = label[label.length - 1].text.slice(0, -1)
+ const spans = label.filter(span => span.text).map(span => ({ ...span, link }))
+ return spans.length > 0 ? spans : [{ text: '', link }]
}
- return { text: token.slice(1) }
+ return [{ text: token.slice(1) }]
}
/**
@@ -783,7 +793,7 @@ export function markdownSpans(markdown: string): InlineSpan[] {
at = span.end
} else if (m) {
scanText(markdown, at, m.index, pieces)
- pieces.push({ span: atomSpan(m[0]) })
+ for (const span of atomSpans(m[0])) pieces.push({ span })
at = atoms.lastIndex
} else {
break