Fix MCP error wrapping for DRC payloads

This commit is contained in:
jbjardine
2026-05-23 22:32:15 +02:00
parent ece116d563
commit aac80b57b8
3 changed files with 90 additions and 64 deletions

View File

@@ -7,6 +7,7 @@
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { z } from "zod";
import { logger } from "../logger.js";
import { formatKicadResult } from "./tool-response.js";
// Command function type for KiCAD script calls
type CommandFunction = (command: string, params: Record<string, unknown>) => Promise<any>;
@@ -52,14 +53,7 @@ export function registerDesignRuleTools(server: McpServer, callKicadScript: Comm
logger.debug("Setting design rules");
const result = await callKicadScript("set_design_rules", params);
return {
content: [
{
type: "text",
text: JSON.stringify(result),
},
],
};
return formatKicadResult(result);
},
);
@@ -74,14 +68,7 @@ export function registerDesignRuleTools(server: McpServer, callKicadScript: Comm
logger.debug("Getting design rules");
const result = await callKicadScript("get_design_rules", {});
return {
content: [
{
type: "text",
text: JSON.stringify(result),
},
],
};
return formatKicadResult(result);
},
);
@@ -98,14 +85,7 @@ export function registerDesignRuleTools(server: McpServer, callKicadScript: Comm
logger.debug("Running DRC check");
const result = await callKicadScript("run_drc", { reportPath });
return {
content: [
{
type: "text",
text: JSON.stringify(result),
},
],
};
return formatKicadResult(result);
},
);
@@ -162,14 +142,7 @@ export function registerDesignRuleTools(server: McpServer, callKicadScript: Comm
nets,
});
return {
content: [
{
type: "text",
text: JSON.stringify(result),
},
],
};
return formatKicadResult(result);
},
);
@@ -190,14 +163,7 @@ export function registerDesignRuleTools(server: McpServer, callKicadScript: Comm
netClass,
});
return {
content: [
{
type: "text",
text: JSON.stringify(result),
},
],
};
return formatKicadResult(result);
},
);
@@ -224,14 +190,7 @@ export function registerDesignRuleTools(server: McpServer, callKicadScript: Comm
minViaDrill,
});
return {
content: [
{
type: "text",
text: JSON.stringify(result),
},
],
};
return formatKicadResult(result);
},
);
@@ -284,14 +243,7 @@ export function registerDesignRuleTools(server: McpServer, callKicadScript: Comm
item2,
});
return {
content: [
{
type: "text",
text: JSON.stringify(result),
},
],
};
return formatKicadResult(result);
},
);
@@ -311,14 +263,7 @@ export function registerDesignRuleTools(server: McpServer, callKicadScript: Comm
logger.debug("Getting DRC violations");
const result = await callKicadScript("get_drc_violations", { severity });
return {
content: [
{
type: "text",
text: JSON.stringify(result),
},
],
};
return formatKicadResult(result);
},
);

View File

@@ -0,0 +1,30 @@
export type McpTextResult = {
content: Array<{
type: "text";
text: string;
}>;
isError?: true;
};
function isKicadFailure(result: unknown): boolean {
return (
typeof result === "object" &&
result !== null &&
"success" in result &&
(result as { success?: unknown }).success === false
);
}
export function formatKicadResult(result: unknown): McpTextResult {
const text = JSON.stringify(result) ?? String(result);
return {
content: [
{
type: "text",
text,
},
],
...(isKicadFailure(result) ? { isError: true as const } : {}),
};
}