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 a bare element name nothing closes as text

marcoteb19b1746c567e
diff --git a/mcp-host/src/workflow/__tests__/inlineMarkup.test.ts b/mcp-host/src/workflow/__tests__/inlineMarkup.test.ts
index df427cf44..96553dbf3 100644
--- a/mcp-host/src/workflow/__tests__/inlineMarkup.test.ts
+++ b/mcp-host/src/workflow/__tests__/inlineMarkup.test.ts
@@ -50,6 +50,25 @@ describe('inline HTML', () => {
expect(inlineSpans('<b >a</b >')).toEqual([{ text: 'a', bold: true }])
})
+ it('keeps a bare element name that nothing closes as a placeholder', () => {
+ expect(htmlToPlainText('kubectl logs --since=<time>')).toBe('kubectl logs --since=<time>')
+ expect(htmlToPlainText('Replace <label> with your name')).toBe('Replace <label> with your name')
+ expect(htmlToPlainText('Options <a> and <b> are shown')).toBe('Options <a> and <b> are shown')
+ const spans = inlineSpans('Options <a> and <b> are shown')
+ expect(spans.map(s => s.text).join('')).toBe('Options <a> and <b> are shown')
+ expect(spans.some(s => s.bold)).toBe(false)
+ })
+
+ it('still reads a closed element, and one HTML lets stand unclosed, as markup', () => {
+ expect(inlineSpans('a <b>bold</b> word')).toContainEqual({
+ text: 'bold',
+ bold: true,
+ })
+ expect(htmlToPlainText('<p>one<p>two')).toBe('one two')
+ expect(htmlToPlainText('<ul><li>a<li>b</ul>')).toMatch(/a.*b/)
+ expect(htmlToPlainText('line<hr>next')).not.toContain('<hr>')
+ })
+
it('keeps a placeholder such as <table name> that no closing tag makes a tag', () => {
const sql = 'SELECT * FROM <table name> WHERE <column name> = 1 at <time of day>'
expect(inlineSpans(sql)).toEqual([{ text: sql }])
diff --git a/mcp-host/src/workflow/inlineMarkup.ts b/mcp-host/src/workflow/inlineMarkup.ts
index 1f42a4b78..d1bf50bd3 100644
--- a/mcp-host/src/workflow/inlineMarkup.ts
+++ b/mcp-host/src/workflow/inlineMarkup.ts
@@ -260,25 +260,39 @@ const CLOSING_TAG = /<\/([a-z][a-z0-9]*)\s*>/gi
const STRICT_ONLY = new RegExp(`^${STRICT_ATTRIBUTES}/?$`)
/** Elements that never close, so no closing tag vouches for them. */
const VOID_ELEMENTS = new Set(['hr', 'img', 'br', 'col', 'wbr'])
+/** Elements HTML lets a writer leave unclosed, so a bare one is markup even then. */
+const OPTIONAL_CLOSE = new Set(['p', 'li', 'dt', 'dd', 'tr', 'td', 'th', 'thead', 'tbody', 'tfoot'])
+const BARE_OPEN_TAG = /<([a-z][a-z0-9]*)\s*>/gi
+const ELEMENT_NAME = new RegExp(
+ `^(?:${[...ELEMENTS, 'b', 'strong', 'i', 'em', 'code', 'kbd', 'tt', 'del', 's', 'strike'].join('|')})$`,
+ 'i'
+)
/**
- * `text` with a would-be tag of a longer element name kept as text when its
- * attributes are not all values or boolean ones and nothing closes its
- * element: "SELECT * FROM <table name>" is a placeholder, while
- * "<table border>…</table>" is a table. Its "<" is written as a reference,
- * which prints as itself once the spans are read.
+ * `text` with a would-be tag kept as text when nothing closes its element and
+ * it reads as a placeholder: a longer element name whose attributes are not
+ * all values or boolean ones ("SELECT * FROM <table name>"), or any element
+ * name on its own ("--since=<time>", "Replace <label>", "options <a> and <b>").
+ * "<table border>…</table>" is still a table, and an element HTML lets stand
+ * unclosed, such as <p> or <li>, is still markup. The kept "<" is written as a
+ * reference, which prints as itself once the spans are read.
*/
function withPlaceholdersKept(text: string): string {
if (!text.includes('<')) return text
const closed = new Set<string>()
for (const m of text.matchAll(CLOSING_TAG)) closed.add(m[1].toLowerCase())
- return text.replace(LONG_OPEN_TAG, (tag: string, name: string, attributes: string) => {
- const element = name.toLowerCase()
- if (closed.has(element) || VOID_ELEMENTS.has(element) || STRICT_ONLY.test(attributes)) {
- return tag
- }
- return `&lt;${tag.slice(1)}`
- })
+ const markup = (element: string) => closed.has(element) || VOID_ELEMENTS.has(element)
+ return text
+ .replace(LONG_OPEN_TAG, (tag: string, name: string, attributes: string) => {
+ const element = name.toLowerCase()
+ return markup(element) || STRICT_ONLY.test(attributes) ? tag : `&lt;${tag.slice(1)}`
+ })
+ .replace(BARE_OPEN_TAG, (tag: string, name: string) => {
+ const element = name.toLowerCase()
+ return !ELEMENT_NAME.test(element) || markup(element) || OPTIONAL_CLOSE.has(element)
+ ? tag
+ : `&lt;${tag.slice(1)}`
+ })
}
/**