|
1 | 1 | // MCP loopback tool schema projection. |
2 | 2 | // Converts gateway-scoped tools into MCP tools/list-compatible schemas. |
| 3 | +import { isDeepStrictEqual } from "node:util"; |
3 | 4 | import { isRecord } from "@openclaw/normalization-core/record-coerce"; |
4 | | -import { uniqueValues } from "@openclaw/normalization-core/string-normalization"; |
5 | 5 | import { logWarn } from "../logger.js"; |
6 | 6 | import { resolveGatewayScopedTools } from "./tool-resolution.js"; |
7 | 7 |
|
@@ -57,6 +57,69 @@ function readLoopbackToolParameters(tool: McpLoopbackTool): Record<string, unkno |
57 | 57 | } |
58 | 58 | } |
59 | 59 |
|
| 60 | +function readLiteralSchemaValues(schema: Record<string, unknown>): unknown[] | undefined { |
| 61 | + const enumValues = Array.isArray(schema.enum) ? schema.enum : undefined; |
| 62 | + if (Object.hasOwn(schema, "const")) { |
| 63 | + if (!enumValues) { |
| 64 | + return [schema.const]; |
| 65 | + } |
| 66 | + return enumValues.some((value) => isDeepStrictEqual(value, schema.const)) ? [schema.const] : []; |
| 67 | + } |
| 68 | + return enumValues; |
| 69 | +} |
| 70 | + |
| 71 | +function uniqueLiteralValues(values: unknown[]): unknown[] { |
| 72 | + return values.filter( |
| 73 | + (value, index) => |
| 74 | + values.findIndex((candidate) => isDeepStrictEqual(candidate, value)) === index, |
| 75 | + ); |
| 76 | +} |
| 77 | + |
| 78 | +const SCHEMA_ANNOTATION_KEYS = new Set([ |
| 79 | + "$comment", |
| 80 | + "default", |
| 81 | + "deprecated", |
| 82 | + "description", |
| 83 | + "example", |
| 84 | + "examples", |
| 85 | + "readOnly", |
| 86 | + "title", |
| 87 | + "writeOnly", |
| 88 | +]); |
| 89 | + |
| 90 | +function readLiteralValidationConstraints( |
| 91 | + schema: Record<string, unknown>, |
| 92 | +): Record<string, unknown> { |
| 93 | + return Object.fromEntries( |
| 94 | + Object.entries(schema).filter( |
| 95 | + ([key]) => key !== "const" && key !== "enum" && !SCHEMA_ANNOTATION_KEYS.has(key), |
| 96 | + ), |
| 97 | + ); |
| 98 | +} |
| 99 | + |
| 100 | +function mergeLiteralSchemas( |
| 101 | + existing: Record<string, unknown>, |
| 102 | + incoming: Record<string, unknown>, |
| 103 | +): Record<string, unknown> | undefined { |
| 104 | + const existingValues = readLiteralSchemaValues(existing); |
| 105 | + const incomingValues = readLiteralSchemaValues(incoming); |
| 106 | + if (existingValues === undefined || incomingValues === undefined) { |
| 107 | + return undefined; |
| 108 | + } |
| 109 | + const existingConstraints = readLiteralValidationConstraints(existing); |
| 110 | + const incomingConstraints = readLiteralValidationConstraints(incoming); |
| 111 | + if (!isDeepStrictEqual(existingConstraints, incomingConstraints)) { |
| 112 | + return undefined; |
| 113 | + } |
| 114 | + const values = uniqueLiteralValues([...existingValues, ...incomingValues]); |
| 115 | + if (values.length === 0) { |
| 116 | + return undefined; |
| 117 | + } |
| 118 | + const merged: Record<string, unknown> = { ...existing, enum: values }; |
| 119 | + delete merged.const; |
| 120 | + return merged; |
| 121 | +} |
| 122 | + |
60 | 123 | function flattenUnionSchema( |
61 | 124 | raw: Record<string, unknown>, |
62 | 125 | toolName: string, |
@@ -111,20 +174,14 @@ function flattenUnionSchema( |
111 | 174 | } |
112 | 175 | continue; |
113 | 176 | } |
114 | | - if (Array.isArray(existing.enum) && Array.isArray(incoming.enum)) { |
115 | | - mergedProps[key] = { |
116 | | - ...existing, |
117 | | - enum: uniqueValues([...(existing.enum as unknown[]), ...(incoming.enum as unknown[])]), |
118 | | - }; |
| 177 | + if (isDeepStrictEqual(existing, incoming)) { |
119 | 178 | continue; |
120 | 179 | } |
121 | | - if ("const" in existing && "const" in incoming && existing.const !== incoming.const) { |
122 | | - const merged: Record<string, unknown> = { |
123 | | - ...existing, |
124 | | - enum: [existing.const, incoming.const], |
125 | | - }; |
126 | | - delete merged.const; |
127 | | - mergedProps[key] = merged; |
| 180 | + // A prior const merge becomes an enum. Treat both as one literal family |
| 181 | + // so later union variants cannot silently disappear based on ordering. |
| 182 | + const mergedLiterals = mergeLiteralSchemas(existing, incoming); |
| 183 | + if (mergedLiterals) { |
| 184 | + mergedProps[key] = mergedLiterals; |
128 | 185 | continue; |
129 | 186 | } |
130 | 187 | warnSchemaOnce( |
|
0 commit comments