1+ import { isRecord } from "@openclaw/normalization-core/record-coerce" ;
12import { uniqueValues } from "@openclaw/normalization-core/string-normalization" ;
23import { logWarn } from "../logger.js" ;
34import { resolveGatewayScopedTools } from "./tool-resolution.js" ;
@@ -10,23 +11,92 @@ export type McpToolSchemaEntry = {
1011 inputSchema : Record < string , unknown > ;
1112} ;
1213
14+ function readLoopbackToolField ( tool : McpLoopbackTool , key : "name" | "description" | "parameters" ) {
15+ try {
16+ return ( tool as unknown as Record < typeof key , unknown > ) [ key ] ;
17+ } catch {
18+ return undefined ;
19+ }
20+ }
21+
22+ export function readMcpLoopbackToolName ( tool : McpLoopbackTool ) : string | undefined {
23+ const value = readLoopbackToolField ( tool , "name" ) ;
24+ if ( typeof value !== "string" ) {
25+ return undefined ;
26+ }
27+ const name = value . trim ( ) ;
28+ return name || undefined ;
29+ }
30+
31+ function readLoopbackToolDescription ( tool : McpLoopbackTool ) : string | undefined {
32+ const value = readLoopbackToolField ( tool , "description" ) ;
33+ return typeof value === "string" ? value : undefined ;
34+ }
35+
36+ function readLoopbackToolParameters ( tool : McpLoopbackTool ) : Record < string , unknown > | undefined {
37+ let value ;
38+ try {
39+ value = ( tool as unknown as { parameters ?: unknown } ) . parameters ;
40+ } catch {
41+ return undefined ;
42+ }
43+ if ( ! isRecord ( value ) ) {
44+ return { } ;
45+ }
46+ try {
47+ return { ...value } ;
48+ } catch {
49+ return undefined ;
50+ }
51+ }
52+
1353function flattenUnionSchema ( raw : Record < string , unknown > ) : Record < string , unknown > {
14- const variants = ( raw . anyOf ?? raw . oneOf ) as Record < string , unknown > [ ] | undefined ;
54+ const variants = ( raw . anyOf ?? raw . oneOf ) as unknown [ ] | undefined ;
1555 if ( ! Array . isArray ( variants ) || variants . length === 0 ) {
1656 return raw ;
1757 }
1858 const mergedProps : Record < string , unknown > = { } ;
1959 const requiredSets : Set < string > [ ] = [ ] ;
2060 for ( const variant of variants ) {
21- const props = variant . properties as Record < string , unknown > | undefined ;
61+ if ( variant === true ) {
62+ requiredSets . push ( new Set ( ) ) ;
63+ continue ;
64+ }
65+ if ( ! isRecord ( variant ) ) {
66+ continue ;
67+ }
68+ const props = isRecord ( variant . properties ) ? variant . properties : undefined ;
2269 if ( props ) {
2370 for ( const [ key , schema ] of Object . entries ( props ) ) {
71+ if ( ! isPropertySchema ( schema ) ) {
72+ logWarn ( `mcp loopback: malformed schema definition for "${ key } ", ignoring that variant` ) ;
73+ continue ;
74+ }
2475 if ( ! ( key in mergedProps ) ) {
2576 mergedProps [ key ] = schema ;
2677 continue ;
2778 }
28- const existing = mergedProps [ key ] as Record < string , unknown > ;
29- const incoming = schema as Record < string , unknown > ;
79+ const existing = mergedProps [ key ] ;
80+ const incoming = schema ;
81+ if ( existing === true || incoming === true ) {
82+ mergedProps [ key ] = true ;
83+ continue ;
84+ }
85+ if ( existing === false ) {
86+ mergedProps [ key ] = incoming ;
87+ continue ;
88+ }
89+ if ( incoming === false ) {
90+ continue ;
91+ }
92+ if ( ! isRecord ( existing ) || ! isRecord ( incoming ) ) {
93+ if ( existing !== incoming ) {
94+ logWarn (
95+ `mcp loopback: conflicting schema definitions for "${ key } ", keeping the first variant` ,
96+ ) ;
97+ }
98+ continue ;
99+ }
30100 if ( Array . isArray ( existing . enum ) && Array . isArray ( incoming . enum ) ) {
31101 mergedProps [ key ] = {
32102 ...existing ,
@@ -54,18 +124,28 @@ function flattenUnionSchema(raw: Record<string, unknown>): Record<string, unknow
54124 }
55125 const required =
56126 requiredSets . length > 0
57- ? [ ...( requiredSets [ 0 ] ?? [ ] ) ] . filter ( ( key ) => requiredSets . every ( ( set ) => set . has ( key ) ) )
127+ ? [ ...( requiredSets [ 0 ] ?? [ ] ) ] . filter (
128+ ( key ) => key in mergedProps && requiredSets . every ( ( set ) => set . has ( key ) ) ,
129+ )
58130 : [ ] ;
59131 const { anyOf : _anyOf , oneOf : _oneOf , ...rest } = raw ;
60132 return { ...rest , type : "object" , properties : mergedProps , required } ;
61133}
62134
135+ function isPropertySchema ( value : unknown ) : value is boolean | Record < string , unknown > {
136+ return typeof value === "boolean" || isRecord ( value ) ;
137+ }
138+
63139export function buildMcpToolSchema ( tools : McpLoopbackTool [ ] ) : McpToolSchemaEntry [ ] {
64- return tools . map ( ( tool ) => {
65- let raw =
66- tool . parameters && typeof tool . parameters === "object"
67- ? { ...( tool . parameters as Record < string , unknown > ) }
68- : { } ;
140+ return tools . flatMap ( ( tool ) => {
141+ const name = readMcpLoopbackToolName ( tool ) ;
142+ if ( ! name ) {
143+ return [ ] ;
144+ }
145+ let raw = readLoopbackToolParameters ( tool ) ;
146+ if ( ! raw ) {
147+ return [ ] ;
148+ }
69149 if ( raw . anyOf || raw . oneOf ) {
70150 raw = flattenUnionSchema ( raw ) ;
71151 }
@@ -76,8 +156,8 @@ export function buildMcpToolSchema(tools: McpLoopbackTool[]): McpToolSchemaEntry
76156 raw . properties = { } ;
77157 }
78158 return {
79- name : tool . name ,
80- description : tool . description ,
159+ name,
160+ description : readLoopbackToolDescription ( tool ) ,
81161 inputSchema : raw ,
82162 } ;
83163 } ) ;
0 commit comments