Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins
TypeScript ai-agentcoding-agentdesktop-appelectronglobali18nlocal-firstmcppipi-agentpi-desktoppluginsreactrusttypescript
fix(agent-runtime): keep MCP embedded resource text in tool results
xpeng5278-webc18f3f6dcff3
diff --git a/docs/spec/03-runtime/02-agent-runtime.md b/docs/spec/03-runtime/02-agent-runtime.mdindex 3c0b979d8..fa0281afa 100644--- a/docs/spec/03-runtime/02-agent-runtime.md+++ b/docs/spec/03-runtime/02-agent-runtime.md@@ -45,6 +45,9 @@ The Node runtime normalizes text and image content from host tool results,including MCP `content` blocks and bare plugin content-block arrays. Well-formedimage blocks are passed to vision-capable models and retained when tool historyis restored; malformed image entries are ignored without failing the tool call.+Embedded MCP text resources contribute their `resource.text`, and MCP resource+links contribute a one-line `[resource link]` reference, to the model-visible+text alongside sibling text blocks; binary resources are not inlined.## 4. Runtime API (package-level)diff --git a/packages/agent-runtime/src/runtime.test.ts b/packages/agent-runtime/src/runtime.test.tsindex afa70951d..e805a324a 100644--- a/packages/agent-runtime/src/runtime.test.ts+++ b/packages/agent-runtime/src/runtime.test.ts@@ -11444,6 +11444,51 @@ describe("toolResultFromUi image restoration (issue #1073)", () => {expect(restored.content).toEqual(expected);await runtime.dispose();});++ it("keeps embedded MCP text resources and resource links next to a status text block", async () => {+ // github-mcp-server `get_file_contents` answers with a status line plus the+ // file body as an embedded resource, and with a resource link for large files.+ const rawContent = {+ content: [+ { type: "text", text: "successfully downloaded text file (SHA: abc123)" },+ {+ type: "resource",+ resource: {+ uri: "repo://acme/app/contents/src/index.ts",+ mimeType: "text/plain",+ text: "export const answer = 42;\n",+ },+ },+ {+ type: "resource_link",+ uri: "https://raw.githubusercontent.com/acme/app/main/big.json",+ name: "big.json",+ },+ ],+ isError: false,+ };+ const host = {+ call: vi.fn().mockImplementation((method: string) =>+ Promise.resolve(+ method === "project.instructions.resolve"+ ? { entries: [] }+ : { ok: true, content: rawContent },+ ),+ ),+ };+ const runtime = createRuntime({ host });+ try {+ const read = (runtime as any).toolCatalog.get("Read");+ const result = await read.execute("resource-result", { path: "src/index.ts" });+ expect(result.content).toHaveLength(1);+ const text = result.content[0].text as string;+ expect(text).toContain("successfully downloaded text file (SHA: abc123)");+ expect(text).toContain("export const answer = 42;");+ expect(text).toContain("https://raw.githubusercontent.com/acme/app/main/big.json");+ } finally {+ await runtime.dispose();+ }+ });});it("does not reuse stale plugin declarations when schema or permission metadata changes", async () => {diff --git a/packages/agent-runtime/src/runtime.ts b/packages/agent-runtime/src/runtime.tsindex e594cb645..ffcb21a31 100644--- a/packages/agent-runtime/src/runtime.ts+++ b/packages/agent-runtime/src/runtime.ts@@ -1502,6 +1502,33 @@ function selectRetainedUserMessages(return selected.reverse();}+/**+ * Model-visible text for one host tool content block. Besides plain `text`+ * blocks, MCP servers return file bodies as embedded text resources and large+ * payloads as resource links (github-mcp-server `get_file_contents` sends a+ * status line plus one of these), so dropping them next to a sibling text+ * block hides the actual result from the model. Binary resources and other+ * block kinds have no text to show and stay out of the text channel.+ */+function hostContentBlockText(block: unknown): string | undefined {+ if (!isRecord(block)) return undefined;+ if (block.type === "text" && typeof block.text === "string") return block.text;+ if (+ block.type === "resource" &&+ isRecord(block.resource) &&+ typeof block.resource.text === "string"+ ) {+ return block.resource.text;+ }+ if (block.type === "resource_link" && typeof block.uri === "string") {+ const name = typeof block.name === "string" && block.name.trim()+ ? `${block.name.trim()}: `+ : "";+ return `[resource link] ${name}${block.uri}`;+ }+ return undefined;+}+/** Rebuild a pi-ai tool result from a persisted tool row. Rows that never* finished (app quit / abort mid-tool) restore as errored results so the* model knows the call produced nothing. Exported for tests. */@@ -3545,9 +3572,8 @@ Do not invent objections or turn speculative risks into blockers. Stop when the// and hand images to the model instead of stringifying (#1360).const textParts: string[] = [];for (const block of rawContent) {- if (isRecord(block) && block.type === "text" && typeof block.text === "string") {- textParts.push(block.text);- }+ const blockText = hostContentBlockText(block);+ if (blockText !== undefined) textParts.push(blockText);}collectImageBlocks(rawContent);text = textParts.join("\n") || JSON.stringify(rawContent, null, 2);@@ -3557,9 +3583,8 @@ Do not invent objections or turn speculative risks into blockers. Stop when theconst contentBlocks = rawContent.content;const textParts: string[] = [];for (const block of contentBlocks) {- if (isRecord(block) && block.type === "text" && typeof block.text === "string") {- textParts.push(block.text);- }+ const blockText = hostContentBlockText(block);+ if (blockText !== undefined) textParts.push(blockText);}collectImageBlocks(contentBlocks);text = textParts.join("\n") || JSON.stringify(rawContent, null, 2);