|
1 | 1 | import assert from "node:assert/strict"; |
2 | 2 | import { describe, test } from "node:test"; |
| 3 | +import { CallToolResultSchema } from "@modelcontextprotocol/sdk/types.js"; |
3 | 4 | import { |
4 | 5 | applyJmespathFilter, |
5 | 6 | applyJmespathToToolResult, |
@@ -123,6 +124,42 @@ describe("jmespath tool result helpers", () => { |
123 | 124 | assert.deepEqual(applyJmespathToToolResult(original, "starts_with(title, `a`)"), original); |
124 | 125 | }); |
125 | 126 |
|
| 127 | + test("applyJmespathToToolResult keeps structuredContent when the filter returns an object", () => { |
| 128 | + const result = applyJmespathToToolResult( |
| 129 | + { |
| 130 | + content: [{ type: "text", text: JSON.stringify({ items: [{ title: "a" }], total: 1 }) }], |
| 131 | + structuredContent: { items: [{ title: "a" }], total: 1 }, |
| 132 | + }, |
| 133 | + "{total: total}" |
| 134 | + ); |
| 135 | + assert.deepEqual(result.structuredContent, { total: 1 }); |
| 136 | + assert.equal(CallToolResultSchema.safeParse(result).success, true); |
| 137 | + }); |
| 138 | + |
| 139 | + test("applyJmespathToToolResult drops structuredContent the filter turns into a non-object", () => { |
| 140 | + const items = [{ title: "a" }, { title: "b" }]; |
| 141 | + for (const [expression, filtered] of [ |
| 142 | + ["items[].title", ["a", "b"]], |
| 143 | + ["missing", null], |
| 144 | + ["length(items)", 2], |
| 145 | + ] as const) { |
| 146 | + const result = applyJmespathToToolResult( |
| 147 | + { |
| 148 | + content: [{ type: "text", text: JSON.stringify({ items }) }], |
| 149 | + structuredContent: { items }, |
| 150 | + }, |
| 151 | + expression |
| 152 | + ); |
| 153 | + assert.equal("structuredContent" in result, false, expression); |
| 154 | + assert.deepEqual( |
| 155 | + JSON.parse((result.content as { text: string }[])[0].text), |
| 156 | + filtered, |
| 157 | + expression |
| 158 | + ); |
| 159 | + assert.equal(CallToolResultSchema.safeParse(result).success, true, expression); |
| 160 | + } |
| 161 | + }); |
| 162 | + |
126 | 163 | test("getJmespathSyntaxError rejects invalid expressions before execution", () => { |
127 | 164 | assert.match(getJmespathSyntaxError("[[[") ?? "", /Invalid jmespath expression/); |
128 | 165 | assert.equal(getJmespathSyntaxError("[].title"), undefined); |
|
0 commit comments