Files
openclaw/src/agents/agent-bundle-mcp-tools.materialize.test.ts
rain 2df8021cda fix(agents): surface MCP structured content in tool results
Surface inbound bundle-MCP structuredContent as the model-visible result when present so agents can read Codex MCP threadId values and continue with codex-reply. Preserve non-structured content behavior, preserve the empty-result fallback, and keep details.structuredContent for internal consumers.

Also remove an unused secrets path helper that was breaking the latest prod-type gate on main.

Fixes #87511.

Verification:
- node scripts/run-vitest.mjs src/agents/agent-bundle-mcp-tools.materialize.test.ts
- pnpm exec oxfmt --check src/secrets/path-utils.ts src/agents/agent-bundle-mcp-materialize.ts src/agents/agent-bundle-mcp-tools.materialize.test.ts
- pnpm tsgo:prod
- local check-guards shard commands
- live Codex MCP smoke with codex__codex and codex__codex-reply same-thread continuation
- autoreview clean
- CI run 26587222874 green

Co-authored-by: Pluviobyte <Pluviobyte@users.noreply.github.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-28 17:29:29 +01:00

295 lines
8.6 KiB
TypeScript

import type { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
import { validateToolArguments } from "openclaw/plugin-sdk/llm";
import { describe, expect, it } from "vitest";
import { getPluginToolMeta } from "../plugins/tools.js";
import {
createBundleMcpToolRuntime,
materializeBundleMcpToolsForRun,
} from "./agent-bundle-mcp-materialize.js";
import type { McpCatalogTool } from "./agent-bundle-mcp-types.js";
import type { SessionMcpRuntime } from "./agent-bundle-mcp-types.js";
function expectTextContentBlock(block: unknown, text: string) {
const content = block as { type?: string; text?: string } | undefined;
expect(content?.type).toBe("text");
expect(content?.text).toBe(text);
}
function makeToolRuntime(
params: {
tools?: McpCatalogTool[];
serverName?: string;
result?: CallToolResult;
resultText?: string;
} = {},
): SessionMcpRuntime {
const serverName = params.serverName ?? "bundleProbe";
const tools = params.tools ?? [
{
serverName,
safeServerName: serverName,
toolName: "bundle_probe",
description: "Bundle probe",
inputSchema: { type: "object", properties: {} },
fallbackDescription: "Bundle probe",
},
];
return {
sessionId: "session-collision",
workspaceDir: "/tmp",
configFingerprint: "fingerprint",
createdAt: 0,
lastUsedAt: 0,
markUsed: () => {},
getCatalog: async () => ({
version: 1,
generatedAt: 0,
servers: {
[serverName]: {
serverName,
launchSummary: serverName,
toolCount: tools.length,
},
},
tools,
}),
peekCatalog: () => ({
version: 1,
generatedAt: 0,
servers: {
[serverName]: {
serverName,
launchSummary: serverName,
toolCount: tools.length,
},
},
tools,
}),
callTool: async () =>
params.result ?? {
content: [{ type: "text", text: params.resultText ?? "FROM-BUNDLE" }],
isError: false,
},
dispose: async () => {},
};
}
describe("createBundleMcpToolRuntime", () => {
it("materializes bundle MCP tools and executes them", async () => {
const runtime = await materializeBundleMcpToolsForRun({
runtime: makeToolRuntime(),
});
expect(runtime.tools.map((tool) => tool.name)).toEqual(["bundleProbe__bundle_probe"]);
expect(getPluginToolMeta(runtime.tools[0])?.pluginId).toBe("bundle-mcp");
const result = await runtime.tools[0].execute("call-bundle-probe", {}, undefined, undefined);
expectTextContentBlock(result.content[0], "FROM-BUNDLE");
expect(result.details).toEqual({
mcpServer: "bundleProbe",
mcpTool: "bundle_probe",
});
});
it("keeps structuredContent visible when MCP tools also return text content", async () => {
const runtime = await materializeBundleMcpToolsForRun({
runtime: makeToolRuntime({
result: {
content: [{ type: "text", text: "pong" }],
structuredContent: {
threadId: "019e6cdb-8e7f-7cb2-891f-9edb689f6fc7",
content: "pong",
},
isError: false,
},
}),
});
const result = await runtime.tools[0].execute("call-bundle-probe", {}, undefined, undefined);
expectTextContentBlock(
result.content[0],
`structuredContent:\n${JSON.stringify(
{
threadId: "019e6cdb-8e7f-7cb2-891f-9edb689f6fc7",
content: "pong",
},
null,
2,
)}`,
);
expect(result.content).toHaveLength(1);
expect(result.details).toEqual({
mcpServer: "bundleProbe",
mcpTool: "bundle_probe",
structuredContent: {
threadId: "019e6cdb-8e7f-7cb2-891f-9edb689f6fc7",
content: "pong",
},
});
});
it("disambiguates bundle MCP tools that collide with existing tool names", async () => {
const runtime = await materializeBundleMcpToolsForRun({
runtime: makeToolRuntime(),
reservedToolNames: ["bundleProbe__bundle_probe"],
});
expect(runtime.tools.map((tool) => tool.name)).toEqual(["bundleProbe__bundle_probe-2"]);
});
it("materializes configured MCP tools through the session runtime boundary", async () => {
const created: Parameters<
NonNullable<Parameters<typeof createBundleMcpToolRuntime>[0]["createRuntime"]>
>[0][] = [];
const runtime = await createBundleMcpToolRuntime({
workspaceDir: "/workspace",
cfg: {
mcp: {
servers: {
configuredProbe: {
command: "node",
args: ["configured-probe.mjs"],
env: {
BUNDLE_PROBE_TEXT: "FROM-CONFIG",
},
},
},
},
},
createRuntime: (params) => {
created.push(params);
return makeToolRuntime({
serverName: "configuredProbe",
resultText: "FROM-CONFIG",
});
},
});
expect(created).toHaveLength(1);
expect(created[0].sessionId).toMatch(/^bundle-mcp:/);
expect(created[0].workspaceDir).toBe("/workspace");
expect(created[0].cfg?.mcp?.servers?.configuredProbe?.command).toBe("node");
expect(created[0].cfg?.mcp?.servers?.configuredProbe?.args).toEqual(["configured-probe.mjs"]);
expect(runtime.tools.map((tool) => tool.name)).toEqual(["configuredProbe__bundle_probe"]);
const result = await runtime.tools[0].execute(
"call-configured-probe",
{},
undefined,
undefined,
);
expectTextContentBlock(result.content[0], "FROM-CONFIG");
expect(result.details).toEqual({
mcpServer: "configuredProbe",
mcpTool: "bundle_probe",
});
});
it("returns tools sorted alphabetically for stable prompt-cache keys", async () => {
const runtime = await materializeBundleMcpToolsForRun({
runtime: makeToolRuntime({
tools: [
{
serverName: "multi",
safeServerName: "multi",
toolName: "zeta",
description: "z",
inputSchema: { type: "object", properties: {} },
fallbackDescription: "z",
},
{
serverName: "multi",
safeServerName: "multi",
toolName: "alpha",
description: "a",
inputSchema: { type: "object", properties: {} },
fallbackDescription: "a",
},
{
serverName: "multi",
safeServerName: "multi",
toolName: "mu",
description: "m",
inputSchema: { type: "object", properties: {} },
fallbackDescription: "m",
},
],
}),
});
expect(runtime.tools.map((tool) => tool.name)).toEqual([
"multi__alpha",
"multi__mu",
"multi__zeta",
]);
});
it("normalizes local $ref schemas from MCP tools before exposing them", async () => {
const runtime = await materializeBundleMcpToolsForRun({
runtime: makeToolRuntime({
tools: [
{
serverName: "notion",
safeServerName: "notion",
toolName: "API-post-page",
description: "Create a page",
inputSchema: {
type: "object",
required: ["parent"],
properties: {
parent: { $ref: "#/$defs/parentRequest" },
},
$defs: {
parentRequest: {
oneOf: [
{
type: "object",
required: ["page_id"],
properties: { page_id: { type: "string" } },
},
{
type: "object",
required: ["database_id"],
properties: { database_id: { type: "string" } },
},
],
},
},
},
fallbackDescription: "Create a page",
},
],
}),
});
expect(runtime.tools[0]?.parameters).toEqual({
type: "object",
required: ["parent"],
properties: {
parent: {
oneOf: [
{
type: "object",
required: ["page_id"],
properties: { page_id: { type: "string" } },
},
{
type: "object",
required: ["database_id"],
properties: { database_id: { type: "string" } },
},
],
},
},
});
expect(
validateToolArguments(runtime.tools[0], {
type: "toolCall",
id: "call-page",
name: "notion__API-post-page",
arguments: { parent: { page_id: "page-id" } },
}),
).toEqual({ parent: { page_id: "page-id" } });
});
});