import {Client} from '@modelcontextprotocol/sdk/client/index.js'; import {StreamableHTTPClientTransport} from '@modelcontextprotocol/sdk/client/streamableHttp.js'; import {randomUUID} from 'node:crypto'; import {fileURLToPath} from 'node:url'; import {resolve} from 'node:path'; export function assertStagingTarget(value){ const url=new URL(value); if(!['http:','https:'].includes(url.protocol)||url.username||url.password||url.pathname!=='/api/mcp'||url.search||url.hash||url.hostname==='mega.dash.apphub.co.za')throw Error('A separate staging MCP URL is required for mutation checks'); return url; } async function main(){ const url=assertStagingTarget(process.env.MEGA_MCP_STAGING_URL||''); if(!process.env.MEGA_MCP_TOKEN||!process.env.MEGA_MCP_STAGING_SITE||!process.env.MEGA_MCP_STAGING_SUPPLIER)throw Error('Set staging token, fixture site and supplier'); const client=new Client({name:'mega-staging-smoke',version:'1'}); await client.connect(new StreamableHTTPClientTransport(url,{requestInit:{headers:{Authorization:`Bearer ${process.env.MEGA_MCP_TOKEN}`}}})); const checks=[]; const call=async(name,args)=>{ const result=await client.callTool({name,arguments:args},undefined,{timeout:240000}); if(result.isError)throw Error(`Staging tool ${name} failed`); const content=result.content.find(c=>c.type==='text');return JSON.parse(content.text); }; try{ const names=(await client.listTools()).tools.map(t=>t.name); if(names.some(n=>/delete|remove|clear_all/.test(n)))throw Error('Destructive tool discovered');checks.push('no destructive tools'); const input={operationId:randomUUID(),site:process.env.MEGA_MCP_STAGING_SITE,supplierId:process.env.MEGA_MCP_STAGING_SUPPLIER,number:`MCP-STAGING-${Date.now()}`,items:[{description:'MCP staging fixture',qty:2,rate:10,vat_percent:15,unit:'each'}]}; const created=await call('create_purchase_order',input);const repeat=await call('create_purchase_order',input); if(created.result.id!==repeat.result.id)throw Error('Operation retry duplicated the order');checks.push('PO create and retry'); const detail=await call('get_purchase_order_detail',{id:created.result.id}); if(detail.po.total_value!==23||detail.items.length!==1)throw Error('PO totals or line count mismatch');checks.push('PO totals'); await call('update_purchase_order_item',{operationId:randomUUID(),id:created.result.id,itemId:created.result.itemIds[0],item:{description:'MCP staging fixture',qty:3,rate:10,vat_percent:15,unit:'each'}});checks.push('PO edit'); await call('set_purchase_order_verification',{operationId:randomUUID(),id:created.result.id,verified:true});checks.push('PO verification'); console.log(JSON.stringify({checkedAt:new Date().toISOString(),origin:url.origin,checks,status:'passed',scope:'PO create/edit/verification; document/matching/posting require staged source fixtures'},null,2)); }finally{await client.close();} } if(process.argv[1]&&resolve(process.argv[1])===fileURLToPath(import.meta.url))main().catch(()=>{console.error('Staging smoke failed; check isolated fixtures and MCP setup');process.exitCode=1;});