fix(mcp): type tool error content
This commit is contained in:
@@ -64,12 +64,7 @@ export function convertTool(mcpTool: MCPToolDef, client: Client, timeout?: numbe
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
if (result.isError)
|
if (result.isError)
|
||||||
throw new Error(
|
throw new Error(formatToolErrorContent(result.content))
|
||||||
result.content
|
|
||||||
.flatMap((item) => (item.type === "text" ? [item.text] : []))
|
|
||||||
.filter((text) => text.trim())
|
|
||||||
.join("\n\n") || "MCP tool returned an error",
|
|
||||||
)
|
|
||||||
if (result.structuredContent === undefined || result.structuredContent === null) return result
|
if (result.structuredContent === undefined || result.structuredContent === null) return result
|
||||||
return {
|
return {
|
||||||
...result,
|
...result,
|
||||||
@@ -107,6 +102,22 @@ export function fetch<T extends { name: string }>(
|
|||||||
|
|
||||||
export const sanitize = (value: string) => value.replace(/[^a-zA-Z0-9_-]/g, "_")
|
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) {
|
export function prompts(client: Client, timeout?: number) {
|
||||||
if (!client.getServerCapabilities()?.prompts) return Promise.resolve([])
|
if (!client.getServerCapabilities()?.prompts) return Promise.resolve([])
|
||||||
return paginate(
|
return paginate(
|
||||||
|
|||||||
Reference in New Issue
Block a user