File size: 8,304 Bytes
253e783 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 | /** Tests the built-in node-host MCP invocation command. */
import { describe, expect, it, vi } from "vitest";
import type { GatewayClient } from "../gateway/client.js";
import { handleInvoke } from "./invoke.js";
import { NodeHostMcpError, type NodeHostMcpManager } from "./mcp.js";
const MEBIBYTE = 1024 * 1024;
async function invokeMcp(manager: NodeHostMcpManager, params: unknown) {
const request = vi.fn<GatewayClient["request"]>().mockResolvedValue(null);
await handleInvoke(
{
id: "invoke-mcp",
nodeId: "node-1",
command: "mcp.tools.call.v1",
paramsJSON: JSON.stringify(params),
timeoutMs: 321,
},
{ request } as unknown as GatewayClient,
{ current: async () => [] },
manager,
);
return (request.mock.calls[0]?.[1] ?? {}) as {
ok: boolean;
payload?: unknown;
payloadJSON?: string;
error?: { code?: string; message?: string };
};
}
function managerWith(callMcpTool: NodeHostMcpManager["callMcpTool"]): NodeHostMcpManager {
return {
descriptors: [],
callMcpTool,
close: async () => undefined,
};
}
describe("mcp.tools.call.v1", () => {
it("dispatches validated params and preserves raw MCP content for one final projection", async () => {
const callMcpTool = vi.fn<NodeHostMcpManager["callMcpTool"]>().mockResolvedValue({
content: [
{ type: "text", text: "pong" },
{ type: "image", data: "aW1hZ2U=", mimeType: "image/png" },
{
type: "resource_link",
uri: "https://example.com/report",
name: "report",
title: "Report",
},
],
structuredContent: { ok: true },
});
const result = await invokeMcp(managerWith(callMcpTool), {
server: "docs",
tool: "search",
arguments: { query: "x" },
});
expect(callMcpTool).toHaveBeenCalledWith({
server: "docs",
tool: "search",
arguments: { query: "x" },
timeoutMs: 321,
});
expect(result.ok).toBe(true);
expect(result.payload).toEqual({
content: [
{ type: "text", text: "pong" },
{ type: "image", data: "aW1hZ2U=", mimeType: "image/png" },
{
type: "resource_link",
uri: "https://example.com/report",
name: "report",
title: "Report",
},
],
structuredContent: { ok: true },
});
});
it("returns MCP tool errors as results while thrown failures fail the invoke", async () => {
const toolError = await invokeMcp(
managerWith(async () => ({
isError: true,
content: [{ type: "text", text: "bad query" }],
structuredContent: { retryable: true },
})),
{ server: "docs", tool: "search" },
);
expect(toolError).toEqual({
id: "invoke-mcp",
nodeId: "node-1",
ok: true,
payload: {
content: [{ type: "text", text: "bad query" }],
structuredContent: { retryable: true },
isError: true,
},
});
const unavailable = await invokeMcp(
managerWith(async () => {
throw new NodeHostMcpError("MCP_SERVER_UNAVAILABLE", "server unavailable");
}),
{ server: "docs", tool: "search" },
);
expect(unavailable).toMatchObject({
ok: false,
error: { code: "MCP_SERVER_UNAVAILABLE", message: "server unavailable" },
});
const unexpected = await invokeMcp(
managerWith(async () => {
throw new Error("x".repeat(2_000));
}),
{ server: "docs", tool: "search" },
);
expect(unexpected.error?.code).toBe("MCP_TOOL_ERROR");
expect(unexpected.error?.message).toHaveLength(1_024);
});
it("does not publish an MCP result after its invocation is canceled", async () => {
const controller = new AbortController();
let resolveTool:
| ((result: { content: Array<{ type: "text"; text: string }> }) => void)
| undefined;
const callMcpTool = vi.fn<NodeHostMcpManager["callMcpTool"]>(
() =>
new Promise((resolve) => {
resolveTool = resolve;
}),
);
const request = vi.fn<GatewayClient["request"]>().mockResolvedValue(null);
const invoking = handleInvoke(
{
id: "invoke-mcp-canceled",
nodeId: "node-1",
command: "mcp.tools.call.v1",
paramsJSON: JSON.stringify({ server: "docs", tool: "search" }),
timeoutMs: 321,
},
{ request } as unknown as GatewayClient,
{ current: async () => [] },
managerWith(callMcpTool),
{ signal: controller.signal },
);
await vi.waitFor(() => expect(callMcpTool).toHaveBeenCalledOnce());
expect(callMcpTool.mock.calls[0]?.[0].signal).toBe(controller.signal);
controller.abort();
resolveTool?.({ content: [{ type: "text", text: "stale MCP result" }] });
await invoking;
expect(request).not.toHaveBeenCalled();
});
it("caps aggregate MCP text content at one megabyte with a truncation note", async () => {
const result = await invokeMcp(
managerWith(async () => ({
content: [
{ type: "text", text: "a".repeat(MEBIBYTE) },
{ type: "text", text: "overflow" },
],
})),
{ server: "docs", tool: "large" },
);
const payload = result.payload as {
content: Array<{ type: string; text: string }>;
};
const text = payload.content.map((block) => block.text).join("");
expect(Buffer.byteLength(text)).toBeLessThanOrEqual(MEBIBYTE);
expect(text).toContain("truncated: MCP text content exceeded 1 MB");
});
it("drops oversized images and structured content before node.invoke serialization", async () => {
const oversized = "A".repeat(20 * MEBIBYTE);
const result = await invokeMcp(
managerWith(async () => ({
content: [{ type: "image", data: oversized, mimeType: "image/png" }],
structuredContent: { oversized },
})),
{ server: "docs", tool: "large-image" },
);
const payload = result.payload as {
content: Array<{ type: string; text?: string }>;
structuredContent?: Record<string, unknown>;
};
expect(Buffer.byteLength(JSON.stringify(result))).toBeLessThanOrEqual(20 * MEBIBYTE);
expect(payload.content).toEqual([
{ type: "text", text: "[truncated: MCP result exceeded 20 MB]" },
]);
expect(payload.structuredContent).toBeUndefined();
});
it("preserves structured content and recovery guidance when an exact JSON mirror is oversized", async () => {
const structuredContent = {
oversized: "S".repeat(10 * MEBIBYTE),
};
const recovery = "authentication expired; run login";
const result = await invokeMcp(
managerWith(async () => ({
content: [
{ type: "text", text: JSON.stringify(structuredContent, null, 2) },
{
type: "image",
data: "I".repeat(10 * MEBIBYTE),
mimeType: "image/png",
},
{ type: "text", text: recovery },
],
structuredContent,
isError: true,
})),
{ server: "docs", tool: "recover" },
);
const payload = result.payload as {
content: Array<{ type: string; text?: string }>;
structuredContent?: Record<string, unknown>;
isError?: boolean;
};
expect(payload.isError).toBe(true);
expect(payload.structuredContent).toBeDefined();
expect(payload.structuredContent?.oversized).toHaveLength(structuredContent.oversized.length);
expect(payload.content).toEqual([
{ type: "text", text: recovery },
{ type: "text", text: "[truncated: MCP result exceeded 20 MB]" },
]);
expect(Buffer.byteLength(JSON.stringify(result))).toBeLessThanOrEqual(20 * MEBIBYTE);
});
it("sends MCP payloads as structured invoke data without double JSON escaping", async () => {
const escaped = "\\".repeat(8 * 1024 * 1024);
const result = await invokeMcp(
managerWith(async () => ({ content: [], structuredContent: { escaped } })),
{ server: "docs", tool: "escaped" },
);
expect(result.payloadJSON).toBeUndefined();
expect(
(result.payload as { structuredContent: { escaped: string } }).structuredContent.escaped,
).toBe(escaped);
expect(Buffer.byteLength(JSON.stringify(result))).toBeLessThanOrEqual(20 * MEBIBYTE);
});
});
|