fix(mcp): return valid string for void tool results (#6703)
This commit is contained in:
1 parent
441b181e79
commit
87cdcf28e0
3 files changed
+28
-1
No files matched your search
@@ -14,6 +14,7 @@ import { db } from "@homarr/db";
|
|||||||
|
|
||||||
import { getPackageVersion } from "~/versions/package-reader";
|
import { getPackageVersion } from "~/versions/package-reader";
|
||||||
import { extractMcpTools } from "../_extract-tools";
|
import { extractMcpTools } from "../_extract-tools";
|
||||||
|
import { serializeMcpToolResult } from "../_serialize-result";
|
||||||
|
|
||||||
const logger = createLogger({ module: "mcpRoute" });
|
const logger = createLogger({ module: "mcpRoute" });
|
||||||
|
|
||||||
@@ -203,7 +204,7 @@ const mcpHandler = createMcpHandler(
|
|||||||
const input = args && Object.keys(args).length > 0 ? args : undefined;
|
const input = args && Object.keys(args).length > 0 ? args : undefined;
|
||||||
const result = await procedure(input);
|
const result = await procedure(input);
|
||||||
return {
|
return {
|
||||||
content: [{ type: "text" as const, text: JSON.stringify(result) }],
|
content: [{ type: "text" as const, text: serializeMcpToolResult(result) }],
|
||||||
};
|
};
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
const message = sanitizeErrorMessage(error);
|
const message = sanitizeErrorMessage(error);
|
||||||
|
|||||||
@@ -0,0 +1,23 @@
|
|||||||
|
import { describe, expect, it } from "vitest";
|
||||||
|
|
||||||
|
import { serializeMcpToolResult } from "./_serialize-result";
|
||||||
|
|
||||||
|
describe("serializeMcpToolResult", () => {
|
||||||
|
it("serializes objects as JSON strings", () => {
|
||||||
|
expect(serializeMcpToolResult({ boardId: "1" })).toBe('{"boardId":"1"}');
|
||||||
|
});
|
||||||
|
|
||||||
|
it("serializes primitive values", () => {
|
||||||
|
expect(serializeMcpToolResult("hello")).toBe('"hello"');
|
||||||
|
expect(serializeMcpToolResult(42)).toBe("42");
|
||||||
|
expect(serializeMcpToolResult(true)).toBe("true");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("serializes null as the string null", () => {
|
||||||
|
expect(serializeMcpToolResult(null)).toBe("null");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("serializes void results as the string null so MCP content text is always a string", () => {
|
||||||
|
expect(serializeMcpToolResult(undefined)).toBe("null");
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
export function serializeMcpToolResult(result: unknown): string {
|
||||||
|
return JSON.stringify(result) ?? "null";
|
||||||
|
}
|
||||||
Reference in new issue
Block a user