local json = require('panto').ext.json local M = {} function M.catalog(req, names) local all, by, list = {}, {}, json.array({}) for _,t in ipairs(req.tools) do all[t.name]=t end if names==nil then names={}; for name in pairs(all) do names[#names+1]=name end; table.sort(names) end for i,name in ipairs(names) do local t=assert(all[name], 'Tool not in active rig: '..name); local s=json.decode(t.input_schema_json) assert(s.type=='object','Expected MCP object input schema for '..name) local wire=name:gsub('%.','__'); assert(wire:match('^[%w_-]+$') and #wire<=128 and not by[wire], 'Invalid or colliding MCP tool name: '..name) by[wire]={name=name,schema=s}; list[i]={name=wire,description=t.description,inputSchema=s} end return by,list end -- Advertise the exact extension schema. Reject unsupported assertions on use, -- rather than dropping constraints or preventing unrelated tools from loading. local known={type=true,properties=true,required=true,additionalProperties=true,items=true,minItems=true,maxItems=true, enum=true,minimum=true,maximum=true,description=true,title=true,default=true,examples=true,['$schema']=true} local array_mt=getmetatable(json.array({})) local function matches(s,v) if type(s)=='boolean' then return s end for k in pairs(s) do if not known[k] then return false,'Unsupported schema keyword: '..k end end local kind=type(v) if kind=='table' then kind=getmetatable(v)==array_mt and 'array' or 'object' elseif v==json.null then kind='null' end if s.type and s.type~=kind and not (s.type=='integer' and kind=='number' and v%1==0) then return false end if s.enum then local found=false; for _,x in ipairs(s.enum) do if json.encode(x)==json.encode(v) then found=true; break end end if not found then return false end end if kind=='object' then for _,k in ipairs(s.required or {}) do if v[k]==nil then return false end end for k,x in pairs(v) do local sub=(s.properties or {})[k]; if sub==nil then sub=s.additionalProperties end if sub~=nil then local ok,why=matches(sub,x); if not ok then return false,why end end end elseif kind=='array' then if s.minItems and #vs.maxItems then return false end if s.items~=nil then for _,x in ipairs(v) do local ok,why=matches(s.items,x); if not ok then return false,why end end end elseif kind=='number' and (s.minimum and vs.maximum) then return false end return true end M.arguments=matches return M