fix(mcp): type tool error content

This commit is contained in:
Adam 2026-06-14 21:24:54 -05:00
parent 9fdfa23f01
commit 9258e8ca86
No known key found for this signature in database
GPG Key ID: 9CB48779AF150E75

View File

@ -64,12 +64,7 @@ export function convertTool(mcpTool: MCPToolDef, client: Client, timeout?: numbe
},
)
if (result.isError)
throw new Error(
result.content
.flatMap((item) => (item.type === "text" ? [item.text] : []))
.filter((text) => text.trim())
.join("\n\n") || "MCP tool returned an error",
)
throw new Error(formatToolErrorContent(result.content))
if (result.structuredContent === undefined || result.structuredContent === null) return result
return {
...result,
@ -107,6 +102,22 @@ export function fetch<T extends { name: string }>(
export const sanitize = (value: string) => value.replace(/[^a-zA-Z0-9_-]/g, "_")
function formatToolErrorContent(content: unknown) {
if (!Array.isArray(content)) return "MCP tool returned an error"
return (
content
.flatMap((item) => (isTextContent(item) ? [item.text] : []))
.filter((text) => text.trim())
.join("\n\n") || "MCP tool returned an error"
)
}
function isTextContent(value: unknown): value is { type: "text"; text: string } {
if (typeof value !== "object" || value === null) return false
const item = value as Record<string, unknown>
return item.type === "text" && typeof item.text === "string"
}
export function prompts(client: Client, timeout?: number) {
if (!client.getServerCapabilities()?.prompts) return Promise.resolve([])
return paginate(