fix(mcp): exclude archived records
This commit is contained in:
@@ -1,16 +1,42 @@
|
||||
import { McpToolResult } from "./types"
|
||||
|
||||
const OMIT_ARCHIVED = Symbol("omit-archived")
|
||||
|
||||
function omitArchivedRecords(value: unknown): unknown | typeof OMIT_ARCHIVED {
|
||||
if (Array.isArray(value)) {
|
||||
return value
|
||||
.map(omitArchivedRecords)
|
||||
.filter((item) => item !== OMIT_ARCHIVED)
|
||||
}
|
||||
|
||||
if (!value || typeof value !== "object") return value
|
||||
|
||||
const prototype = Object.getPrototypeOf(value)
|
||||
if (prototype !== Object.prototype && prototype !== null) return value
|
||||
|
||||
const record = value as Record<string, unknown>
|
||||
if (record.archived === true) return OMIT_ARCHIVED
|
||||
|
||||
return Object.fromEntries(
|
||||
Object.entries(record)
|
||||
.map(([key, item]) => [key, omitArchivedRecords(item)] as const)
|
||||
.filter(([, item]) => item !== OMIT_ARCHIVED),
|
||||
)
|
||||
}
|
||||
|
||||
export function asToolResult(payload: unknown): McpToolResult {
|
||||
const sanitizedPayload = omitArchivedRecords(payload)
|
||||
const resultPayload = sanitizedPayload === OMIT_ARCHIVED ? {} : sanitizedPayload
|
||||
const structuredContent =
|
||||
payload && typeof payload === "object" && !Array.isArray(payload)
|
||||
? payload as Record<string, unknown>
|
||||
: { result: payload }
|
||||
resultPayload && typeof resultPayload === "object" && !Array.isArray(resultPayload)
|
||||
? resultPayload as Record<string, unknown>
|
||||
: { result: resultPayload }
|
||||
|
||||
return {
|
||||
content: [
|
||||
{
|
||||
type: "text",
|
||||
text: JSON.stringify(payload, null, 2),
|
||||
text: JSON.stringify(resultPayload, null, 2),
|
||||
},
|
||||
],
|
||||
structuredContent,
|
||||
@@ -33,4 +59,3 @@ export function asToolError(error: unknown): McpToolResult {
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user