cursor: write each catalog entry as its CallDynamicTool call; a thinking-only turn is an empty reply (#11)

GLM-5.3 read the catalog's <tool name=...> entries as tools of its own and
called them by bare names, which went nowhere. Each entry is now the
CallDynamicTool call itself (namespace, toolName, arguments) — the change
infinitr0us proposed in #11, thanks! A turn that ends with only thinking,
no text and no tool call, is answered as an empty reply so the client asks
again, as the stream's end already did. 0.1.8.
This commit is contained in:
yetone
2026-10-03 08:39:01 +08:00
parent ef68e7c7c0
commit 93aa1671aa
3 changed files with 49 additions and 7 deletions
+34
View File
@@ -173,3 +173,37 @@ test("usage takes the cache out of input_tokens, and keeps reasoning", async ()
expect(usage.prompt_tokens).toBeGreaterThan(90)
expect(usage.prompt_tokens_details).toEqual({ cached_tokens: 0, cache_write_tokens: 0 })
})
// GLM-5.3 called the catalog's <tool name=...> entries bare, as tools of
// their own (#11): each entry is the CallDynamicTool call itself
test("the tool catalog writes each tool as its CallDynamicTool call", () => {
const tools = [
{ name: "Read", description: "read a file", schemaText: '{"type":"object"}' },
{ name: "Bash", description: "run a command", schemaText: '{"type":"object"}' },
]
const s = _internal.catalog(tools)
expect(s).not.toContain("<tool name=")
for (const t of tools) {
expect(s).toContain(`<call>CallDynamicTool({"namespace":"magpie","toolName":"${t.name}","arguments":{...}})\n${t.description}\narguments schema: ${t.schemaText}\n</call>`)
}
expect(_internal.catalog([])).toBe("")
})
// a turn of thinking only is an empty reply, which the client asks again
test("a turn that ends with thinking only is an empty reply", async () => {
const update = (num, body) => ({ data: pb().bytes(1, pb().bytes(num, body)).done() })
const run = async (...frames) => {
const out = []
for await (const p of _internal.decode(frames, { send() {}, blobs: new Map(), tools: [], estimate: 1 })) out.push(p)
return out
}
const empty = { error: { status: 502, message: "an empty reply" } }
const thinking = update(4, pb().str(1, "hmm").done())
const ended = update(14, pb().varint(2, 3).done())
expect(await run(thinking, ended)).toEqual([{ reasoning: "hmm" }, empty])
// and so at the stream's end
expect(await run(thinking, { end: true, data: Buffer.from("{}") })).toEqual([{ reasoning: "hmm" }, empty])
// with text it finishes
const out = await run(thinking, update(1, pb().str(1, "hi").done()), ended)
expect(out.at(-1).stop).toBe("stop")
})
+14 -6
View File
@@ -981,11 +981,14 @@ function toolsOf(chat) {
// catalog lists the caller's tools for the model. It sees MCP tools only
// through Cursor's GetDynamicTools and CallDynamicTool, and Cursor names
// them in a prompt of its own, which this conversation goes without:
// without the list the model says it has no such tool.
// without the list the model says it has no such tool. Each entry is
// written as the CallDynamicTool call itself: listed as <tool name=...>,
// some models (GLM-5.3) called the names bare, as tools of their own, and
// those calls went nowhere (#11).
function catalog(tools) {
if (!tools.length) return ""
let s = `\n\n<dynamic_tool_catalog>\nThe tools below are available in the MCP namespace "magpie". Call one with \`${CALL}\` (namespace "magpie", toolName, arguments). Their schemas are given here, so there is no need to call \`GetDynamicTools\` first.\n`
for (const t of tools) s += `<tool name="${t.name}">\n${t.description}\ninput schema: ${t.schemaText}\n</tool>\n`
let s = `\n\n<dynamic_tool_catalog>\nThe MCP namespace "magpie" has the tools below. They are reached only through \`${CALL}\`, never as tools of their own. Each entry is the exact call to make; its arguments must match the schema given. The schemas are complete, so there is no need to call \`GetDynamicTools\` first.\n`
for (const t of tools) s += `<call>${CALL}({"namespace":"magpie","toolName":"${t.name}","arguments":{...}})\n${t.description}\narguments schema: ${t.schemaText}\n</call>\n`
return s + "</dynamic_tool_catalog>"
}
@@ -1345,6 +1348,8 @@ async function* decode(stream, { send, blobs, tools, estimate }) {
let calls = 0
let listed = 0
let said = 0
// wrote: the reply's text alone; a turn of thinking only says nothing
let wrote = 0
let usage = { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, reasoning: 0 }
const finish = () => {
if (!usage.output) usage.output = Math.floor((said + 3) / 4)
@@ -1369,7 +1374,7 @@ async function* decode(stream, { send, blobs, tools, estimate }) {
if (f.end) {
const e = failure(200, f.data)
if (e.status < 200 || e.status > 299) yield { error: e }
else if (said === 0 && calls === 0) yield { error: { status: 502, message: "an empty reply" } }
else if (wrote === 0 && calls === 0) yield { error: { status: 502, message: "an empty reply" } }
else yield finish()
return
}
@@ -1384,6 +1389,7 @@ async function* decode(stream, { send, blobs, tools, estimate }) {
const t = pbStr(uf, 1)
if (t) {
said += t.length
wrote += t.length
yield { text: t }
}
break
@@ -1400,7 +1406,9 @@ async function* decode(stream, { send, blobs, tools, estimate }) {
case 14: // turn ended, with what it used
usage = usageOf(uf)
if (calls === 0) {
yield finish()
// nothing written and nothing called: an empty reply,
// which the client asks again, not a finished turn
yield wrote === 0 ? { error: { status: 502, message: "an empty reply" } } : finish()
return
}
break
@@ -1725,4 +1733,4 @@ export async function CursorAuthPlugin() {
}
// for tests
export const _internal = { usable, maxRequired, conversationID, usageOf, sessionOf, SESSION, errorResponse, kept, poolBase, splitID, families, byEffort, offered, modelID, conversation, buildRun, decode, exec, fields, pb, pbValue, pbAny, frame, frames, failure, toolsOf, expiry }
export const _internal = { catalog, usable, maxRequired, conversationID, usageOf, sessionOf, SESSION, errorResponse, kept, poolBase, splitID, families, byEffort, offered, modelID, conversation, buildRun, decode, exec, fields, pb, pbValue, pbAny, frame, frames, failure, toolsOf, expiry }
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@magpie-community/opencode-cursor-auth",
"version": "0.1.7",
"version": "0.1.8",
"description": "Cursor subscriptions (Pro, Pro+, Ultra, Teams) in OpenCode and magpie, through the API cursor-agent talks to",
"type": "module",
"main": "./index.mjs",