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 every markdown link form and every invisible character as a viewer does

marcoteb81e7d281545a
diff --git a/mcp-host/src/core/tools/__tests__/generatedArtifactAttachments.test.ts b/mcp-host/src/core/tools/__tests__/generatedArtifactAttachments.test.ts
index 1bb7c60a5..fb7aff9e8 100644
--- a/mcp-host/src/core/tools/__tests__/generatedArtifactAttachments.test.ts
+++ b/mcp-host/src/core/tools/__tests__/generatedArtifactAttachments.test.ts
@@ -735,7 +735,15 @@ describe('generated internal artifact attachments', () => {
['a relative link', 'probe-[redaction](notes.md)-value'],
['an angle-bracket destination', 'probe-[redaction](<notes page.md>)-value'],
['a reference link', 'probe-[redaction][r]-value\n\n[r]: https://a.example'],
+ ['a link titled in single quotes', "probe-[redaction](#a 'x')-value"],
+ ['a link titled in parentheses', 'probe-[redaction](#a (x))-value'],
+ ['a destination after a space', 'probe-[redaction]( #a )-value'],
+ ['a destination holding parentheses', 'probe-[redaction](a(b)c)-value'],
+ ['a link after a reference definition', '[r]: #x\n\nprobe-[redaction](#a)-value'],
['a bidi control', 'probe-red\u202aaction-value'],
+ ['the Arabic letter mark', 'probe-red\u061caction-value'],
+ ['a combining grapheme joiner', 'probe-red\u034faction-value'],
+ ['a tag character', 'probe-red\u{e0041}action-value'],
])(
'does not attach markdown that splits a protected value with %s',
async (_label, content) => {
diff --git a/mcp-host/src/workflow/__tests__/toolText.test.ts b/mcp-host/src/workflow/__tests__/toolText.test.ts
index cf1d009b9..1cb0f7421 100644
--- a/mcp-host/src/workflow/__tests__/toolText.test.ts
+++ b/mcp-host/src/workflow/__tests__/toolText.test.ts
@@ -138,6 +138,15 @@ describe('printedForms', () => {
expect(asKey).toContain('probe-redaction-value')
})
+ it('reads brackets and links that never close in linear time', () => {
+ // A quadratic pattern takes tens of seconds on these.
+ for (const text of ['['.repeat(200_000), '[a '.repeat(70_000), '](a "'.repeat(40_000)]) {
+ const started = Date.now()
+ printedForms({ text })
+ expect(Date.now() - started).toBeLessThan(2000)
+ }
+ })
+
it('reads keys nested absurdly deep without overflowing the stack', () => {
let deep: unknown = { 'k\u0007': 'v' }
for (let i = 0; i < 10_000; i++) deep = { n: deep }
diff --git a/mcp-host/src/workflow/toolText.ts b/mcp-host/src/workflow/toolText.ts
index c60955ed2..e70fb0d2f 100644
--- a/mcp-host/src/workflow/toolText.ts
+++ b/mcp-host/src/workflow/toolText.ts
@@ -65,16 +65,32 @@ export function cleanToolArgs<T>(value: T, depth = 0): T {
return out as T
}
-/** Characters the document renderers draw as nothing, bidi controls among them. */
-const INVISIBLE = /[\u00AD\u200B-\u200F\u202A-\u202E\u2060-\u2064\u2066-\u2069\uFEFF]/g
+/**
+ * Characters a viewer draws as nothing (Unicode's default ignorables): bidi
+ * controls, joiners, variation selectors, tags and the like.
+ */
+const INVISIBLE = /\p{Default_Ignorable_Code_Point}/gu
/**
* Markdown link syntax a viewer does not print: a destination of any kind
- * ("](#notes)", "](notes.md)", "](<a b.md> \"title\")"), a reference ("][r]"),
- * a reference definition line and the brackets around a label.
+ * with its title ("](#notes)", "]( notes.md 'title')", "](<a b.md> (title))"),
+ * a reference ("][r]"), a reference definition and the brackets around a
+ * label. The text it runs on has its lines folded, so a definition is matched
+ * wherever it stands, up to its title. Whitespace sits where only one
+ * alternative can take it, and a label stops at the next bracket, so the
+ * pattern stays linear.
*/
-const LINK_SYNTAX =
- /\]\((?:<[^>\n]*>|[^\s()]*(?:\([^\s()]*\))*)(?:\s+"[^"\n]*")?\)|\]\[[^\]\n]*\]|^[ \t]*\[[^\]\n]+\]:.*$|[[\]]/gm
+const LINK_TITLE = String.raw`(?:"[^"\n]*"|'[^'\n]*'|\([^()\n]*\))`
+const LINK_DESTINATION = String.raw`(?:<[^>\n]*>|(?:[^\s()]|\([^\s()]*\))+)`
+const LINK_SYNTAX = new RegExp(
+ [
+ String.raw`\]\([ \t]*(?:${LINK_DESTINATION}(?:[ \t]+(?:${LINK_TITLE}[ \t]*)?)?)?\)`,
+ String.raw`\]\[[^\]\n]*\]`,
+ String.raw`\[[^[\]\n]+\]:[ \t]*(?:<[^>\n]*>|\S+)(?:[ \t]+${LINK_TITLE})?`,
+ String.raw`[[\]]`,
+ ].join('|'),
+ 'g'
+)
/** A backslash before ASCII punctuation, which markdown prints as that character. */
const MARKDOWN_ESCAPE = /\\([!-/:-@[-`{-~])/g