@@ -47,6 +47,12 @@ export interface Options<TManifest extends AppManifest> {
4747 include ?: Array < keyof ( TManifest [ "actions" ] & TManifest [ "loaders" ] ) > ;
4848 exclude ?: Array < keyof ( TManifest [ "actions" ] & TManifest [ "loaders" ] ) > ;
4949}
50+
51+ interface RootSchema extends JSONSchema7 {
52+ inputSchema ?: string ;
53+ outputSchema ?: string ;
54+ }
55+
5056function registerTools < TManifest extends AppManifest > (
5157 mcp : McpServer ,
5258 deco : Deco < TManifest > ,
@@ -73,7 +79,7 @@ function registerTools<TManifest extends AppManifest>(
7379
7480 const tools = [ ...availableLoaders , ...availableActions ] . map (
7581 ( func ) => {
76- func = func as JSONSchema7 ;
82+ func = func as RootSchema ;
7783 if ( ! func . $ref || func . $ref === RESOLVABLE_DEFINITION ) return ;
7884 const funcDefinition = schemas . definitions [ idFromDefinition ( func . $ref ) ] ;
7985 const resolveType =
@@ -94,9 +100,17 @@ function registerTools<TManifest extends AppManifest>(
94100 )
95101 ) return ;
96102
97- const props = funcDefinition . allOf ?? [ ] ;
98- const propsSchema = props [ 0 ] ;
99- const ref = ( propsSchema as JSONSchema7 ) ?. $ref ;
103+ const getInputSchemaId = ( ) => {
104+ if ( "inputSchema" in func ) {
105+ return func . inputSchema as string ;
106+ }
107+ const props = funcDefinition . allOf ?? [ ] ;
108+ const propsSchema = props [ 0 ] ;
109+ const ref = ( propsSchema as JSONSchema7 ) ?. $ref ;
110+ return ref ;
111+ } ;
112+
113+ const ref = getInputSchemaId ( ) ;
100114 const rawInputSchema = ref
101115 ? schemas . definitions [ idFromDefinition ( ref ) ]
102116 : undefined ;
@@ -109,6 +123,23 @@ function registerTools<TManifest extends AppManifest>(
109123 )
110124 : undefined ;
111125
126+ const outputSchemaId = "outputSchema" in func
127+ ? func . outputSchema as string
128+ : undefined ;
129+
130+ const rawOutputSchema = outputSchemaId
131+ ? schemas . definitions [ idFromDefinition ( outputSchemaId ) ]
132+ : undefined ;
133+
134+ const selfReference = ( rawOutputSchema ?. anyOf ?? [ ] ) [ 0 ] ;
135+
136+ const outputSchema = selfReference
137+ ? dereferenceSchema (
138+ selfReference as JSONSchema7 ,
139+ schemas . definitions ,
140+ )
141+ : undefined ;
142+
112143 // Handle tool name slugification and clashes
113144 let toolName = ( funcDefinition as { name ?: string } ) ?. name ??
114145 ( inputSchema as { name ?: string } ) ?. name ?? slugify ( resolveType ) ;
@@ -122,17 +153,21 @@ function registerTools<TManifest extends AppManifest>(
122153 }
123154 toolNames . set ( toolName , resolveType ) ;
124155
156+ const normalizeSchema = ( schema ?: JSONSchema7 ) => {
157+ return schema && "type" in schema && schema . type === "object"
158+ ? schema
159+ : {
160+ type : "object" ,
161+ additionalProperties : true ,
162+ } ;
163+ } ;
125164 return {
126165 name : toolName ,
166+ resolveType,
127167 description : funcDefinition . description ?? inputSchema ?. description ??
128168 resolveType ,
129- inputSchema : inputSchema && "type" in inputSchema &&
130- inputSchema . type === "object"
131- ? inputSchema
132- : {
133- type : "object" ,
134- properties : { } ,
135- } ,
169+ outputSchema : normalizeSchema ( outputSchema ) ,
170+ inputSchema : normalizeSchema ( inputSchema ) ,
136171 } ;
137172 } ,
138173 ) ;
0 commit comments